+++ /dev/null
-/*\r
-** $Id: lbaselib.c,v 1.276.1.1 2013/04/12 18:48:47 roberto Exp $\r
-** Basic library\r
-** See Copyright Notice in lua.h\r
-*/\r
-\r
-\r
-\r
-#include <ctype.h>\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <string.h>\r
-\r
-#define lbaselib_c\r
-#define LUA_LIB\r
-\r
-#include "lua.h"\r
-\r
-#include "lauxlib.h"\r
-#include "lualib.h"\r
-\r
-\r
-static int luaB_print (lua_State *L) {\r
- int n = lua_gettop(L); /* number of arguments */\r
- int i;\r
- lua_getglobal(L, "tostring");\r
- for (i=1; i<=n; i++) {\r
- const char *s;\r
- size_t l;\r
- lua_pushvalue(L, -1); /* function to be called */\r
- lua_pushvalue(L, i); /* value to print */\r
- lua_call(L, 1, 1);\r
- s = lua_tolstring(L, -1, &l); /* get result */\r
- if (s == NULL)\r
- return luaL_error(L,\r
- LUA_QL("tostring") " must return a string to " LUA_QL("print"));\r
- if (i>1) luai_writestring("\t", 1);\r
- luai_writestring(s, l);\r
- lua_pop(L, 1); /* pop result */\r
- }\r
- luai_writeline();\r
- return 0;\r
-}\r
-\r
-\r
-#define SPACECHARS " \f\n\r\t\v"\r
-\r
-static int luaB_tonumber (lua_State *L) {\r
- if (lua_isnoneornil(L, 2)) { /* standard conversion */\r
- int isnum;\r
- lua_Number n = lua_tonumberx(L, 1, &isnum);\r
- if (isnum) {\r
- lua_pushnumber(L, n);\r
- return 1;\r
- } /* else not a number; must be something */\r
- luaL_checkany(L, 1);\r
- }\r
- else {\r
- size_t l;\r
- const char *s = luaL_checklstring(L, 1, &l);\r
- const char *e = s + l; /* end point for 's' */\r
- int base = luaL_checkint(L, 2);\r
- int neg = 0;\r
- luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");\r
- s += strspn(s, SPACECHARS); /* skip initial spaces */\r
- if (*s == '-') { s++; neg = 1; } /* handle signal */\r
- else if (*s == '+') s++;\r
- if (isalnum((unsigned char)*s)) {\r
- lua_Number n = 0;\r
- do {\r
- int digit = (isdigit((unsigned char)*s)) ? *s - '0'\r
- : toupper((unsigned char)*s) - 'A' + 10;\r
- if (digit >= base) break; /* invalid numeral; force a fail */\r
- n = n * (lua_Number)base + (lua_Number)digit;\r
- s++;\r
- } while (isalnum((unsigned char)*s));\r
- s += strspn(s, SPACECHARS); /* skip trailing spaces */\r
- if (s == e) { /* no invalid trailing characters? */\r
- lua_pushnumber(L, (neg) ? -n : n);\r
- return 1;\r
- } /* else not a number */\r
- } /* else not a number */\r
- }\r
- lua_pushnil(L); /* not a number */\r
- return 1;\r
-}\r
-\r
-\r
-static int luaB_error (lua_State *L) {\r
- int level = luaL_optint(L, 2, 1);\r
- lua_settop(L, 1);\r
- if (lua_isstring(L, 1) && level > 0) { /* add extra information? */\r
- luaL_where(L, level);\r
- lua_pushvalue(L, 1);\r
- lua_concat(L, 2);\r
- }\r
- return lua_error(L);\r
-}\r
-\r
-\r
-static int luaB_getmetatable (lua_State *L) {\r
- luaL_checkany(L, 1);\r
- if (!lua_getmetatable(L, 1)) {\r
- lua_pushnil(L);\r
- return 1; /* no metatable */\r
- }\r
- luaL_getmetafield(L, 1, "__metatable");\r
- return 1; /* returns either __metatable field (if present) or metatable */\r
-}\r
-\r
-\r
-static int luaB_setmetatable (lua_State *L) {\r
- int t = lua_type(L, 2);\r
- luaL_checktype(L, 1, LUA_TTABLE);\r
- luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,\r
- "nil or table expected");\r
- if (luaL_getmetafield(L, 1, "__metatable"))\r
- return luaL_error(L, "cannot change a protected metatable");\r
- lua_settop(L, 2);\r
- lua_setmetatable(L, 1);\r
- return 1;\r
-}\r
-\r
-\r
-static int luaB_rawequal (lua_State *L) {\r
- luaL_checkany(L, 1);\r
- luaL_checkany(L, 2);\r
- lua_pushboolean(L, lua_rawequal(L, 1, 2));\r
- return 1;\r
-}\r
-\r
-\r
-static int luaB_rawlen (lua_State *L) {\r
- int t = lua_type(L, 1);\r
- luaL_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,\r
- "table or string expected");\r
- lua_pushinteger(L, lua_rawlen(L, 1));\r
- return 1;\r
-}\r
-\r
-\r
-static int luaB_rawget (lua_State *L) {\r
- luaL_checktype(L, 1, LUA_TTABLE);\r
- luaL_checkany(L, 2);\r
- lua_settop(L, 2);\r
- lua_rawget(L, 1);\r
- return 1;\r
-}\r
-\r
-static int luaB_rawset (lua_State *L) {\r
- luaL_checktype(L, 1, LUA_TTABLE);\r
- luaL_checkany(L, 2);\r
- luaL_checkany(L, 3);\r
- lua_settop(L, 3);\r
- lua_rawset(L, 1);\r
- return 1;\r
-}\r
-\r
-\r
-static int luaB_collectgarbage (lua_State *L) {\r
- static const char *const opts[] = {"stop", "restart", "collect",\r
- "count", "step", "setpause", "setstepmul",\r
- "setmajorinc", "isrunning", "generational", "incremental", NULL};\r
- static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,\r
- LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,\r
- LUA_GCSETMAJORINC, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC};\r
- int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];\r
- int ex = luaL_optint(L, 2, 0);\r
- int res = lua_gc(L, o, ex);\r
- switch (o) {\r
- case LUA_GCCOUNT: {\r
- int b = lua_gc(L, LUA_GCCOUNTB, 0);\r
- lua_pushnumber(L, res + ((lua_Number)b/1024));\r
- lua_pushinteger(L, b);\r
- return 2;\r
- }\r
- case LUA_GCSTEP: case LUA_GCISRUNNING: {\r
- lua_pushboolean(L, res);\r
- return 1;\r
- }\r
- default: {\r
- lua_pushinteger(L, res);\r
- return 1;\r
- }\r
- }\r
-}\r
-\r
-\r
-static int luaB_type (lua_State *L) {\r
- luaL_checkany(L, 1);\r
- lua_pushstring(L, luaL_typename(L, 1));\r
- return 1;\r
-}\r
-\r
-\r
-static int pairsmeta (lua_State *L, const char *method, int iszero,\r
- lua_CFunction iter) {\r
- if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */\r
- luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */\r
- lua_pushcfunction(L, iter); /* will return generator, */\r
- lua_pushvalue(L, 1); /* state, */\r
- if (iszero) lua_pushinteger(L, 0); /* and initial value */\r
- else lua_pushnil(L);\r
- }\r
- else {\r
- lua_pushvalue(L, 1); /* argument 'self' to metamethod */\r
- lua_call(L, 1, 3); /* get 3 values from metamethod */\r
- }\r
- return 3;\r
-}\r
-\r
-\r
-static int luaB_next (lua_State *L) {\r
- luaL_checktype(L, 1, LUA_TTABLE);\r
- lua_settop(L, 2); /* create a 2nd argument if there isn't one */\r
- if (lua_next(L, 1))\r
- return 2;\r
- else {\r
- lua_pushnil(L);\r
- return 1;\r
- }\r
-}\r
-\r
-\r
-static int luaB_pairs (lua_State *L) {\r
- return pairsmeta(L, "__pairs", 0, luaB_next);\r
-}\r
-\r
-\r
-static int ipairsaux (lua_State *L) {\r
- int i = luaL_checkint(L, 2);\r
- luaL_checktype(L, 1, LUA_TTABLE);\r
- i++; /* next value */\r
- lua_pushinteger(L, i);\r
- lua_rawgeti(L, 1, i);\r
- return (lua_isnil(L, -1)) ? 1 : 2;\r
-}\r
-\r
-\r
-static int luaB_ipairs (lua_State *L) {\r
- return pairsmeta(L, "__ipairs", 1, ipairsaux);\r
-}\r
-\r
-\r
-static int load_aux (lua_State *L, int status, int envidx) {\r
- if (status == LUA_OK) {\r
- if (envidx != 0) { /* 'env' parameter? */\r
- lua_pushvalue(L, envidx); /* environment for loaded function */\r
- if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */\r
- lua_pop(L, 1); /* remove 'env' if not used by previous call */\r
- }\r
- return 1;\r
- }\r
- else { /* error (message is on top of the stack) */\r
- lua_pushnil(L);\r
- lua_insert(L, -2); /* put before error message */\r
- return 2; /* return nil plus error message */\r
- }\r
-}\r
-\r
-\r
-static int luaB_loadfile (lua_State *L) {\r
- const char *fname = luaL_optstring(L, 1, NULL);\r
- const char *mode = luaL_optstring(L, 2, NULL);\r
- int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */\r
- int status = luaL_loadfilex(L, fname, mode);\r
- return load_aux(L, status, env);\r
-}\r
-\r
-\r
-/*\r
-** {======================================================\r
-** Generic Read function\r
-** =======================================================\r
-*/\r
-\r
-\r
-/*\r
-** reserved slot, above all arguments, to hold a copy of the returned\r
-** string to avoid it being collected while parsed. 'load' has four\r
-** optional arguments (chunk, source name, mode, and environment).\r
-*/\r
-#define RESERVEDSLOT 5\r
-\r
-\r
-/*\r
-** Reader for generic `load' function: `lua_load' uses the\r
-** stack for internal stuff, so the reader cannot change the\r
-** stack top. Instead, it keeps its resulting string in a\r
-** reserved slot inside the stack.\r
-*/\r
-static const char *generic_reader (lua_State *L, void *ud, size_t *size) {\r
- (void)(ud); /* not used */\r
- luaL_checkstack(L, 2, "too many nested functions");\r
- lua_pushvalue(L, 1); /* get function */\r
- lua_call(L, 0, 1); /* call it */\r
- if (lua_isnil(L, -1)) {\r
- lua_pop(L, 1); /* pop result */\r
- *size = 0;\r
- return NULL;\r
- }\r
- else if (!lua_isstring(L, -1))\r
- luaL_error(L, "reader function must return a string");\r
- lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */\r
- return lua_tolstring(L, RESERVEDSLOT, size);\r
-}\r
-\r
-\r
-static int luaB_load (lua_State *L) {\r
- int status;\r
- size_t l;\r
- const char *s = lua_tolstring(L, 1, &l);\r
- const char *mode = luaL_optstring(L, 3, "bt");\r
- int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */\r
- if (s != NULL) { /* loading a string? */\r
- const char *chunkname = luaL_optstring(L, 2, s);\r
- status = luaL_loadbufferx(L, s, l, chunkname, mode);\r
- }\r
- else { /* loading from a reader function */\r
- const char *chunkname = luaL_optstring(L, 2, "=(load)");\r
- luaL_checktype(L, 1, LUA_TFUNCTION);\r
- lua_settop(L, RESERVEDSLOT); /* create reserved slot */\r
- status = lua_load(L, generic_reader, NULL, chunkname, mode);\r
- }\r
- return load_aux(L, status, env);\r
-}\r
-\r
-/* }====================================================== */\r
-\r
-\r
-static int dofilecont (lua_State *L) {\r
- return lua_gettop(L) - 1;\r
-}\r
-\r
-\r
-static int luaB_dofile (lua_State *L) {\r
- const char *fname = luaL_optstring(L, 1, NULL);\r
- lua_settop(L, 1);\r
- if (luaL_loadfile(L, fname) != LUA_OK)\r
- return lua_error(L);\r
- lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);\r
- return dofilecont(L);\r
-}\r
-\r
-\r
-static int luaB_assert (lua_State *L) {\r
- if (!lua_toboolean(L, 1))\r
- return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));\r
- return lua_gettop(L);\r
-}\r
-\r
-\r
-static int luaB_select (lua_State *L) {\r
- int n = lua_gettop(L);\r
- if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {\r
- lua_pushinteger(L, n-1);\r
- return 1;\r
- }\r
- else {\r
- int i = luaL_checkint(L, 1);\r
- if (i < 0) i = n + i;\r
- else if (i > n) i = n;\r
- luaL_argcheck(L, 1 <= i, 1, "index out of range");\r
- return n - i;\r
- }\r
-}\r
-\r
-\r
-static int finishpcall (lua_State *L, int status) {\r
- if (!lua_checkstack(L, 1)) { /* no space for extra boolean? */\r
- lua_settop(L, 0); /* create space for return values */\r
- lua_pushboolean(L, 0);\r
- lua_pushstring(L, "stack overflow");\r
- return 2; /* return false, msg */\r
- }\r
- lua_pushboolean(L, status); /* first result (status) */\r
- lua_replace(L, 1); /* put first result in first slot */\r
- return lua_gettop(L);\r
-}\r
-\r
-\r
-static int pcallcont (lua_State *L) {\r
- int status = lua_getctx(L, NULL);\r
- return finishpcall(L, (status == LUA_YIELD));\r
-}\r
-\r
-\r
-static int luaB_pcall (lua_State *L) {\r
- int status;\r
- luaL_checkany(L, 1);\r
- lua_pushnil(L);\r
- lua_insert(L, 1); /* create space for status result */\r
- status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, pcallcont);\r
- return finishpcall(L, (status == LUA_OK));\r
-}\r
-\r
-\r
-static int luaB_xpcall (lua_State *L) {\r
- int status;\r
- int n = lua_gettop(L);\r
- luaL_argcheck(L, n >= 2, 2, "value expected");\r
- lua_pushvalue(L, 1); /* exchange function... */\r
- lua_copy(L, 2, 1); /* ...and error handler */\r
- lua_replace(L, 2);\r
- status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 0, pcallcont);\r
- return finishpcall(L, (status == LUA_OK));\r
-}\r
-\r
-\r
-static int luaB_tostring (lua_State *L) {\r
- luaL_checkany(L, 1);\r
- luaL_tolstring(L, 1, NULL);\r
- return 1;\r
-}\r
-\r
-\r
-static const luaL_Reg base_funcs[] = {\r
- {"assert", luaB_assert},\r
- {"collectgarbage", luaB_collectgarbage},\r
- {"dofile", luaB_dofile},\r
- {"error", luaB_error},\r
- {"getmetatable", luaB_getmetatable},\r
- {"ipairs", luaB_ipairs},\r
- {"loadfile", luaB_loadfile},\r
- {"load", luaB_load},\r
-#if defined(LUA_COMPAT_LOADSTRING)\r
- {"loadstring", luaB_load},\r
-#endif\r
- {"next", luaB_next},\r
- {"pairs", luaB_pairs},\r
- {"pcall", luaB_pcall},\r
- {"print", luaB_print},\r
- {"rawequal", luaB_rawequal},\r
- {"rawlen", luaB_rawlen},\r
- {"rawget", luaB_rawget},\r
- {"rawset", luaB_rawset},\r
- {"select", luaB_select},\r
- {"setmetatable", luaB_setmetatable},\r
- {"tonumber", luaB_tonumber},\r
- {"tostring", luaB_tostring},\r
- {"type", luaB_type},\r
- {"xpcall", luaB_xpcall},\r
- {NULL, NULL}\r
-};\r
-\r
-\r
-LUAMOD_API int luaopen_base (lua_State *L) {\r
- /* set global _G */\r
- lua_pushglobaltable(L);\r
- lua_pushglobaltable(L);\r
- lua_setfield(L, -2, "_G");\r
- /* open lib into global table */\r
- luaL_setfuncs(L, base_funcs, 0);\r
- lua_pushliteral(L, LUA_VERSION);\r
- lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */\r
- return 1;\r
-}\r
-\r