Path: blob/main/sys/contrib/openzfs/module/lua/lbaselib.c
48383 views
// SPDX-License-Identifier: MIT1/*2** $Id: lbaselib.c,v 1.276.1.1 2013/04/12 18:48:47 roberto Exp $3** Basic library4** See Copyright Notice in lua.h5*/67/* The following built-in lua functions have been removed and are not available8* for use in ZFS channel programs:9*10* dofile11* loadfile12* load13* pcall1415* xpcall16*/171819#define lbaselib_c20#define LUA_LIB2122#include <sys/lua/lua.h>2324#include <sys/lua/lauxlib.h>25#include <sys/lua/lualib.h>2627#define SPACECHARS " \f\n\r\t\v"2829static int luaB_tonumber (lua_State *L) {30if (lua_isnoneornil(L, 2)) { /* standard conversion */31int isnum;32lua_Number n = lua_tonumberx(L, 1, &isnum);33if (isnum) {34lua_pushnumber(L, n);35return 1;36} /* else not a number; must be something */37luaL_checkany(L, 1);38}39else {40size_t l;41const char *s = luaL_checklstring(L, 1, &l);42const char *e = s + l; /* end point for 's' */43int base = luaL_checkint(L, 2);44int neg = 0;45luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");46s += strspn(s, SPACECHARS); /* skip initial spaces */47if (*s == '-') { s++; neg = 1; } /* handle signal */48else if (*s == '+') s++;49if (isalnum((unsigned char)*s)) {50lua_Number n = 0;51do {52int digit = (isdigit((unsigned char)*s)) ? *s - '0'53: toupper((unsigned char)*s) - 'A' + 10;54if (digit >= base) break; /* invalid numeral; force a fail */55n = n * (lua_Number)base + (lua_Number)digit;56s++;57} while (isalnum((unsigned char)*s));58s += strspn(s, SPACECHARS); /* skip trailing spaces */59if (s == e) { /* no invalid trailing characters? */60lua_pushnumber(L, (neg) ? -n : n);61return 1;62} /* else not a number */63} /* else not a number */64}65lua_pushnil(L); /* not a number */66return 1;67}686970static int luaB_error (lua_State *L) {71int level = luaL_optint(L, 2, 1);72lua_settop(L, 1);73if (lua_isstring(L, 1) && level > 0) { /* add extra information? */74luaL_where(L, level);75lua_pushvalue(L, 1);76lua_concat(L, 2);77}78return lua_error(L);79}808182static int luaB_getmetatable (lua_State *L) {83luaL_checkany(L, 1);84if (!lua_getmetatable(L, 1)) {85lua_pushnil(L);86return 1; /* no metatable */87}88luaL_getmetafield(L, 1, "__metatable");89return 1; /* returns either __metatable field (if present) or metatable */90}919293static int luaB_setmetatable (lua_State *L) {94int t = lua_type(L, 2);95luaL_checktype(L, 1, LUA_TTABLE);96luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,97"nil or table expected");98if (luaL_getmetafield(L, 1, "__metatable"))99return luaL_error(L, "cannot change a protected metatable");100lua_settop(L, 2);101lua_setmetatable(L, 1);102return 1;103}104105106static int luaB_rawequal (lua_State *L) {107luaL_checkany(L, 1);108luaL_checkany(L, 2);109lua_pushboolean(L, lua_rawequal(L, 1, 2));110return 1;111}112113114static int luaB_rawlen (lua_State *L) {115int t = lua_type(L, 1);116luaL_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,117"table or string expected");118lua_pushinteger(L, lua_rawlen(L, 1));119return 1;120}121122123static int luaB_rawget (lua_State *L) {124luaL_checktype(L, 1, LUA_TTABLE);125luaL_checkany(L, 2);126lua_settop(L, 2);127lua_rawget(L, 1);128return 1;129}130131static int luaB_rawset (lua_State *L) {132luaL_checktype(L, 1, LUA_TTABLE);133luaL_checkany(L, 2);134luaL_checkany(L, 3);135lua_settop(L, 3);136lua_rawset(L, 1);137return 1;138}139140141static int luaB_collectgarbage (lua_State *L) {142static const char *const opts[] = {"stop", "restart", "collect",143"count", "step", "setpause", "setstepmul",144"setmajorinc", "isrunning", "generational", "incremental", NULL};145static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,146LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,147LUA_GCSETMAJORINC, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC};148int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];149int ex = luaL_optint(L, 2, 0);150int res = lua_gc(L, o, ex);151switch (o) {152case LUA_GCCOUNT: {153int b = lua_gc(L, LUA_GCCOUNTB, 0);154lua_pushnumber(L, res + ((lua_Number)b/1024));155lua_pushinteger(L, b);156return 2;157}158case LUA_GCSTEP: case LUA_GCISRUNNING: {159lua_pushboolean(L, res);160return 1;161}162default: {163lua_pushinteger(L, res);164return 1;165}166}167}168169170static int luaB_type (lua_State *L) {171luaL_checkany(L, 1);172lua_pushstring(L, luaL_typename(L, 1));173return 1;174}175176177static int pairsmeta (lua_State *L, const char *method, int iszero,178lua_CFunction iter) {179if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */180luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */181lua_pushcfunction(L, iter); /* will return generator, */182lua_pushvalue(L, 1); /* state, */183if (iszero) lua_pushinteger(L, 0); /* and initial value */184else lua_pushnil(L);185}186else {187lua_pushvalue(L, 1); /* argument 'self' to metamethod */188lua_call(L, 1, 3); /* get 3 values from metamethod */189}190return 3;191}192193194static int luaB_next (lua_State *L) {195luaL_checktype(L, 1, LUA_TTABLE);196lua_settop(L, 2); /* create a 2nd argument if there isn't one */197if (lua_next(L, 1))198return 2;199else {200lua_pushnil(L);201return 1;202}203}204205206static int luaB_pairs (lua_State *L) {207return pairsmeta(L, "__pairs", 0, luaB_next);208}209210211static int ipairsaux (lua_State *L) {212int i = luaL_checkint(L, 2);213luaL_checktype(L, 1, LUA_TTABLE);214i++; /* next value */215lua_pushinteger(L, i);216lua_rawgeti(L, 1, i);217return (lua_isnil(L, -1)) ? 1 : 2;218}219220221static int luaB_ipairs (lua_State *L) {222return pairsmeta(L, "__ipairs", 1, ipairsaux);223}224225226static int luaB_assert (lua_State *L) {227if (!lua_toboolean(L, 1))228return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));229return lua_gettop(L);230}231232233static int luaB_select (lua_State *L) {234int n = lua_gettop(L);235if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {236lua_pushinteger(L, n-1);237return 1;238}239else {240int i = luaL_checkint(L, 1);241if (i < 0) i = n + i;242else if (i > n) i = n;243luaL_argcheck(L, 1 <= i, 1, "index out of range");244return n - i;245}246}247248static int luaB_tostring (lua_State *L) {249luaL_checkany(L, 1);250luaL_tolstring(L, 1, NULL);251return 1;252}253254static const luaL_Reg base_funcs[] = {255{"assert", luaB_assert},256{"collectgarbage", luaB_collectgarbage},257{"error", luaB_error},258{"getmetatable", luaB_getmetatable},259{"ipairs", luaB_ipairs},260#if defined(LUA_COMPAT_LOADSTRING)261{"loadstring", luaB_load},262#endif263{"next", luaB_next},264{"pairs", luaB_pairs},265{"rawequal", luaB_rawequal},266{"rawlen", luaB_rawlen},267{"rawget", luaB_rawget},268{"rawset", luaB_rawset},269{"select", luaB_select},270{"setmetatable", luaB_setmetatable},271{"tonumber", luaB_tonumber},272{"tostring", luaB_tostring},273{"type", luaB_type},274{NULL, NULL}275};276277278LUAMOD_API int luaopen_base (lua_State *L) {279/* set global _G */280lua_pushglobaltable(L);281lua_pushglobaltable(L);282lua_setfield(L, -2, "_G");283/* open lib into global table */284luaL_setfuncs(L, base_funcs, 0);285lua_pushliteral(L, LUA_VERSION);286lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */287return 1;288}289290#if defined(_KERNEL)291292EXPORT_SYMBOL(luaopen_base);293294#endif295296297