1*eda14cbcSMatt Macy /* BEGIN CSTYLED */ 2*eda14cbcSMatt Macy /* 3*eda14cbcSMatt Macy ** $Id: lbaselib.c,v 1.276.1.1 2013/04/12 18:48:47 roberto Exp $ 4*eda14cbcSMatt Macy ** Basic library 5*eda14cbcSMatt Macy ** See Copyright Notice in lua.h 6*eda14cbcSMatt Macy */ 7*eda14cbcSMatt Macy 8*eda14cbcSMatt Macy /* The following built-in lua functions have been removed and are not available 9*eda14cbcSMatt Macy * for use in ZFS channel programs: 10*eda14cbcSMatt Macy * 11*eda14cbcSMatt Macy * dofile 12*eda14cbcSMatt Macy * loadfile 13*eda14cbcSMatt Macy * load 14*eda14cbcSMatt Macy * pcall 15*eda14cbcSMatt Macy * print 16*eda14cbcSMatt Macy * xpcall 17*eda14cbcSMatt Macy */ 18*eda14cbcSMatt Macy 19*eda14cbcSMatt Macy 20*eda14cbcSMatt Macy #define lbaselib_c 21*eda14cbcSMatt Macy #define LUA_LIB 22*eda14cbcSMatt Macy 23*eda14cbcSMatt Macy #include <sys/lua/lua.h> 24*eda14cbcSMatt Macy 25*eda14cbcSMatt Macy #include <sys/lua/lauxlib.h> 26*eda14cbcSMatt Macy #include <sys/lua/lualib.h> 27*eda14cbcSMatt Macy 28*eda14cbcSMatt Macy #define SPACECHARS " \f\n\r\t\v" 29*eda14cbcSMatt Macy 30*eda14cbcSMatt Macy static int luaB_tonumber (lua_State *L) { 31*eda14cbcSMatt Macy if (lua_isnoneornil(L, 2)) { /* standard conversion */ 32*eda14cbcSMatt Macy int isnum; 33*eda14cbcSMatt Macy lua_Number n = lua_tonumberx(L, 1, &isnum); 34*eda14cbcSMatt Macy if (isnum) { 35*eda14cbcSMatt Macy lua_pushnumber(L, n); 36*eda14cbcSMatt Macy return 1; 37*eda14cbcSMatt Macy } /* else not a number; must be something */ 38*eda14cbcSMatt Macy luaL_checkany(L, 1); 39*eda14cbcSMatt Macy } 40*eda14cbcSMatt Macy else { 41*eda14cbcSMatt Macy size_t l; 42*eda14cbcSMatt Macy const char *s = luaL_checklstring(L, 1, &l); 43*eda14cbcSMatt Macy const char *e = s + l; /* end point for 's' */ 44*eda14cbcSMatt Macy int base = luaL_checkint(L, 2); 45*eda14cbcSMatt Macy int neg = 0; 46*eda14cbcSMatt Macy luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); 47*eda14cbcSMatt Macy s += strspn(s, SPACECHARS); /* skip initial spaces */ 48*eda14cbcSMatt Macy if (*s == '-') { s++; neg = 1; } /* handle signal */ 49*eda14cbcSMatt Macy else if (*s == '+') s++; 50*eda14cbcSMatt Macy if (isalnum((unsigned char)*s)) { 51*eda14cbcSMatt Macy lua_Number n = 0; 52*eda14cbcSMatt Macy do { 53*eda14cbcSMatt Macy int digit = (isdigit((unsigned char)*s)) ? *s - '0' 54*eda14cbcSMatt Macy : toupper((unsigned char)*s) - 'A' + 10; 55*eda14cbcSMatt Macy if (digit >= base) break; /* invalid numeral; force a fail */ 56*eda14cbcSMatt Macy n = n * (lua_Number)base + (lua_Number)digit; 57*eda14cbcSMatt Macy s++; 58*eda14cbcSMatt Macy } while (isalnum((unsigned char)*s)); 59*eda14cbcSMatt Macy s += strspn(s, SPACECHARS); /* skip trailing spaces */ 60*eda14cbcSMatt Macy if (s == e) { /* no invalid trailing characters? */ 61*eda14cbcSMatt Macy lua_pushnumber(L, (neg) ? -n : n); 62*eda14cbcSMatt Macy return 1; 63*eda14cbcSMatt Macy } /* else not a number */ 64*eda14cbcSMatt Macy } /* else not a number */ 65*eda14cbcSMatt Macy } 66*eda14cbcSMatt Macy lua_pushnil(L); /* not a number */ 67*eda14cbcSMatt Macy return 1; 68*eda14cbcSMatt Macy } 69*eda14cbcSMatt Macy 70*eda14cbcSMatt Macy 71*eda14cbcSMatt Macy static int luaB_error (lua_State *L) { 72*eda14cbcSMatt Macy int level = luaL_optint(L, 2, 1); 73*eda14cbcSMatt Macy lua_settop(L, 1); 74*eda14cbcSMatt Macy if (lua_isstring(L, 1) && level > 0) { /* add extra information? */ 75*eda14cbcSMatt Macy luaL_where(L, level); 76*eda14cbcSMatt Macy lua_pushvalue(L, 1); 77*eda14cbcSMatt Macy lua_concat(L, 2); 78*eda14cbcSMatt Macy } 79*eda14cbcSMatt Macy return lua_error(L); 80*eda14cbcSMatt Macy } 81*eda14cbcSMatt Macy 82*eda14cbcSMatt Macy 83*eda14cbcSMatt Macy static int luaB_getmetatable (lua_State *L) { 84*eda14cbcSMatt Macy luaL_checkany(L, 1); 85*eda14cbcSMatt Macy if (!lua_getmetatable(L, 1)) { 86*eda14cbcSMatt Macy lua_pushnil(L); 87*eda14cbcSMatt Macy return 1; /* no metatable */ 88*eda14cbcSMatt Macy } 89*eda14cbcSMatt Macy luaL_getmetafield(L, 1, "__metatable"); 90*eda14cbcSMatt Macy return 1; /* returns either __metatable field (if present) or metatable */ 91*eda14cbcSMatt Macy } 92*eda14cbcSMatt Macy 93*eda14cbcSMatt Macy 94*eda14cbcSMatt Macy static int luaB_setmetatable (lua_State *L) { 95*eda14cbcSMatt Macy int t = lua_type(L, 2); 96*eda14cbcSMatt Macy luaL_checktype(L, 1, LUA_TTABLE); 97*eda14cbcSMatt Macy luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, 98*eda14cbcSMatt Macy "nil or table expected"); 99*eda14cbcSMatt Macy if (luaL_getmetafield(L, 1, "__metatable")) 100*eda14cbcSMatt Macy return luaL_error(L, "cannot change a protected metatable"); 101*eda14cbcSMatt Macy lua_settop(L, 2); 102*eda14cbcSMatt Macy lua_setmetatable(L, 1); 103*eda14cbcSMatt Macy return 1; 104*eda14cbcSMatt Macy } 105*eda14cbcSMatt Macy 106*eda14cbcSMatt Macy 107*eda14cbcSMatt Macy static int luaB_rawequal (lua_State *L) { 108*eda14cbcSMatt Macy luaL_checkany(L, 1); 109*eda14cbcSMatt Macy luaL_checkany(L, 2); 110*eda14cbcSMatt Macy lua_pushboolean(L, lua_rawequal(L, 1, 2)); 111*eda14cbcSMatt Macy return 1; 112*eda14cbcSMatt Macy } 113*eda14cbcSMatt Macy 114*eda14cbcSMatt Macy 115*eda14cbcSMatt Macy static int luaB_rawlen (lua_State *L) { 116*eda14cbcSMatt Macy int t = lua_type(L, 1); 117*eda14cbcSMatt Macy luaL_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1, 118*eda14cbcSMatt Macy "table or string expected"); 119*eda14cbcSMatt Macy lua_pushinteger(L, lua_rawlen(L, 1)); 120*eda14cbcSMatt Macy return 1; 121*eda14cbcSMatt Macy } 122*eda14cbcSMatt Macy 123*eda14cbcSMatt Macy 124*eda14cbcSMatt Macy static int luaB_rawget (lua_State *L) { 125*eda14cbcSMatt Macy luaL_checktype(L, 1, LUA_TTABLE); 126*eda14cbcSMatt Macy luaL_checkany(L, 2); 127*eda14cbcSMatt Macy lua_settop(L, 2); 128*eda14cbcSMatt Macy lua_rawget(L, 1); 129*eda14cbcSMatt Macy return 1; 130*eda14cbcSMatt Macy } 131*eda14cbcSMatt Macy 132*eda14cbcSMatt Macy static int luaB_rawset (lua_State *L) { 133*eda14cbcSMatt Macy luaL_checktype(L, 1, LUA_TTABLE); 134*eda14cbcSMatt Macy luaL_checkany(L, 2); 135*eda14cbcSMatt Macy luaL_checkany(L, 3); 136*eda14cbcSMatt Macy lua_settop(L, 3); 137*eda14cbcSMatt Macy lua_rawset(L, 1); 138*eda14cbcSMatt Macy return 1; 139*eda14cbcSMatt Macy } 140*eda14cbcSMatt Macy 141*eda14cbcSMatt Macy 142*eda14cbcSMatt Macy static int luaB_collectgarbage (lua_State *L) { 143*eda14cbcSMatt Macy static const char *const opts[] = {"stop", "restart", "collect", 144*eda14cbcSMatt Macy "count", "step", "setpause", "setstepmul", 145*eda14cbcSMatt Macy "setmajorinc", "isrunning", "generational", "incremental", NULL}; 146*eda14cbcSMatt Macy static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, 147*eda14cbcSMatt Macy LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL, 148*eda14cbcSMatt Macy LUA_GCSETMAJORINC, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC}; 149*eda14cbcSMatt Macy int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; 150*eda14cbcSMatt Macy int ex = luaL_optint(L, 2, 0); 151*eda14cbcSMatt Macy int res = lua_gc(L, o, ex); 152*eda14cbcSMatt Macy switch (o) { 153*eda14cbcSMatt Macy case LUA_GCCOUNT: { 154*eda14cbcSMatt Macy int b = lua_gc(L, LUA_GCCOUNTB, 0); 155*eda14cbcSMatt Macy lua_pushnumber(L, res + ((lua_Number)b/1024)); 156*eda14cbcSMatt Macy lua_pushinteger(L, b); 157*eda14cbcSMatt Macy return 2; 158*eda14cbcSMatt Macy } 159*eda14cbcSMatt Macy case LUA_GCSTEP: case LUA_GCISRUNNING: { 160*eda14cbcSMatt Macy lua_pushboolean(L, res); 161*eda14cbcSMatt Macy return 1; 162*eda14cbcSMatt Macy } 163*eda14cbcSMatt Macy default: { 164*eda14cbcSMatt Macy lua_pushinteger(L, res); 165*eda14cbcSMatt Macy return 1; 166*eda14cbcSMatt Macy } 167*eda14cbcSMatt Macy } 168*eda14cbcSMatt Macy } 169*eda14cbcSMatt Macy 170*eda14cbcSMatt Macy 171*eda14cbcSMatt Macy static int luaB_type (lua_State *L) { 172*eda14cbcSMatt Macy luaL_checkany(L, 1); 173*eda14cbcSMatt Macy lua_pushstring(L, luaL_typename(L, 1)); 174*eda14cbcSMatt Macy return 1; 175*eda14cbcSMatt Macy } 176*eda14cbcSMatt Macy 177*eda14cbcSMatt Macy 178*eda14cbcSMatt Macy static int pairsmeta (lua_State *L, const char *method, int iszero, 179*eda14cbcSMatt Macy lua_CFunction iter) { 180*eda14cbcSMatt Macy if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */ 181*eda14cbcSMatt Macy luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */ 182*eda14cbcSMatt Macy lua_pushcfunction(L, iter); /* will return generator, */ 183*eda14cbcSMatt Macy lua_pushvalue(L, 1); /* state, */ 184*eda14cbcSMatt Macy if (iszero) lua_pushinteger(L, 0); /* and initial value */ 185*eda14cbcSMatt Macy else lua_pushnil(L); 186*eda14cbcSMatt Macy } 187*eda14cbcSMatt Macy else { 188*eda14cbcSMatt Macy lua_pushvalue(L, 1); /* argument 'self' to metamethod */ 189*eda14cbcSMatt Macy lua_call(L, 1, 3); /* get 3 values from metamethod */ 190*eda14cbcSMatt Macy } 191*eda14cbcSMatt Macy return 3; 192*eda14cbcSMatt Macy } 193*eda14cbcSMatt Macy 194*eda14cbcSMatt Macy 195*eda14cbcSMatt Macy static int luaB_next (lua_State *L) { 196*eda14cbcSMatt Macy luaL_checktype(L, 1, LUA_TTABLE); 197*eda14cbcSMatt Macy lua_settop(L, 2); /* create a 2nd argument if there isn't one */ 198*eda14cbcSMatt Macy if (lua_next(L, 1)) 199*eda14cbcSMatt Macy return 2; 200*eda14cbcSMatt Macy else { 201*eda14cbcSMatt Macy lua_pushnil(L); 202*eda14cbcSMatt Macy return 1; 203*eda14cbcSMatt Macy } 204*eda14cbcSMatt Macy } 205*eda14cbcSMatt Macy 206*eda14cbcSMatt Macy 207*eda14cbcSMatt Macy static int luaB_pairs (lua_State *L) { 208*eda14cbcSMatt Macy return pairsmeta(L, "__pairs", 0, luaB_next); 209*eda14cbcSMatt Macy } 210*eda14cbcSMatt Macy 211*eda14cbcSMatt Macy 212*eda14cbcSMatt Macy static int ipairsaux (lua_State *L) { 213*eda14cbcSMatt Macy int i = luaL_checkint(L, 2); 214*eda14cbcSMatt Macy luaL_checktype(L, 1, LUA_TTABLE); 215*eda14cbcSMatt Macy i++; /* next value */ 216*eda14cbcSMatt Macy lua_pushinteger(L, i); 217*eda14cbcSMatt Macy lua_rawgeti(L, 1, i); 218*eda14cbcSMatt Macy return (lua_isnil(L, -1)) ? 1 : 2; 219*eda14cbcSMatt Macy } 220*eda14cbcSMatt Macy 221*eda14cbcSMatt Macy 222*eda14cbcSMatt Macy static int luaB_ipairs (lua_State *L) { 223*eda14cbcSMatt Macy return pairsmeta(L, "__ipairs", 1, ipairsaux); 224*eda14cbcSMatt Macy } 225*eda14cbcSMatt Macy 226*eda14cbcSMatt Macy 227*eda14cbcSMatt Macy static int luaB_assert (lua_State *L) { 228*eda14cbcSMatt Macy if (!lua_toboolean(L, 1)) 229*eda14cbcSMatt Macy return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!")); 230*eda14cbcSMatt Macy return lua_gettop(L); 231*eda14cbcSMatt Macy } 232*eda14cbcSMatt Macy 233*eda14cbcSMatt Macy 234*eda14cbcSMatt Macy static int luaB_select (lua_State *L) { 235*eda14cbcSMatt Macy int n = lua_gettop(L); 236*eda14cbcSMatt Macy if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') { 237*eda14cbcSMatt Macy lua_pushinteger(L, n-1); 238*eda14cbcSMatt Macy return 1; 239*eda14cbcSMatt Macy } 240*eda14cbcSMatt Macy else { 241*eda14cbcSMatt Macy int i = luaL_checkint(L, 1); 242*eda14cbcSMatt Macy if (i < 0) i = n + i; 243*eda14cbcSMatt Macy else if (i > n) i = n; 244*eda14cbcSMatt Macy luaL_argcheck(L, 1 <= i, 1, "index out of range"); 245*eda14cbcSMatt Macy return n - i; 246*eda14cbcSMatt Macy } 247*eda14cbcSMatt Macy } 248*eda14cbcSMatt Macy 249*eda14cbcSMatt Macy static int luaB_tostring (lua_State *L) { 250*eda14cbcSMatt Macy luaL_checkany(L, 1); 251*eda14cbcSMatt Macy luaL_tolstring(L, 1, NULL); 252*eda14cbcSMatt Macy return 1; 253*eda14cbcSMatt Macy } 254*eda14cbcSMatt Macy 255*eda14cbcSMatt Macy static const luaL_Reg base_funcs[] = { 256*eda14cbcSMatt Macy {"assert", luaB_assert}, 257*eda14cbcSMatt Macy {"collectgarbage", luaB_collectgarbage}, 258*eda14cbcSMatt Macy {"error", luaB_error}, 259*eda14cbcSMatt Macy {"getmetatable", luaB_getmetatable}, 260*eda14cbcSMatt Macy {"ipairs", luaB_ipairs}, 261*eda14cbcSMatt Macy #if defined(LUA_COMPAT_LOADSTRING) 262*eda14cbcSMatt Macy {"loadstring", luaB_load}, 263*eda14cbcSMatt Macy #endif 264*eda14cbcSMatt Macy {"next", luaB_next}, 265*eda14cbcSMatt Macy {"pairs", luaB_pairs}, 266*eda14cbcSMatt Macy {"rawequal", luaB_rawequal}, 267*eda14cbcSMatt Macy {"rawlen", luaB_rawlen}, 268*eda14cbcSMatt Macy {"rawget", luaB_rawget}, 269*eda14cbcSMatt Macy {"rawset", luaB_rawset}, 270*eda14cbcSMatt Macy {"select", luaB_select}, 271*eda14cbcSMatt Macy {"setmetatable", luaB_setmetatable}, 272*eda14cbcSMatt Macy {"tonumber", luaB_tonumber}, 273*eda14cbcSMatt Macy {"tostring", luaB_tostring}, 274*eda14cbcSMatt Macy {"type", luaB_type}, 275*eda14cbcSMatt Macy {NULL, NULL} 276*eda14cbcSMatt Macy }; 277*eda14cbcSMatt Macy 278*eda14cbcSMatt Macy 279*eda14cbcSMatt Macy LUAMOD_API int luaopen_base (lua_State *L) { 280*eda14cbcSMatt Macy /* set global _G */ 281*eda14cbcSMatt Macy lua_pushglobaltable(L); 282*eda14cbcSMatt Macy lua_pushglobaltable(L); 283*eda14cbcSMatt Macy lua_setfield(L, -2, "_G"); 284*eda14cbcSMatt Macy /* open lib into global table */ 285*eda14cbcSMatt Macy luaL_setfuncs(L, base_funcs, 0); 286*eda14cbcSMatt Macy lua_pushliteral(L, LUA_VERSION); 287*eda14cbcSMatt Macy lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */ 288*eda14cbcSMatt Macy return 1; 289*eda14cbcSMatt Macy } 290*eda14cbcSMatt Macy 291*eda14cbcSMatt Macy #if defined(_KERNEL) 292*eda14cbcSMatt Macy 293*eda14cbcSMatt Macy EXPORT_SYMBOL(luaopen_base); 294*eda14cbcSMatt Macy 295*eda14cbcSMatt Macy #endif 296*eda14cbcSMatt Macy /* END CSTYLED */ 297