summaryrefslogtreecommitdiff
path: root/libs/liblua/src/lbaselib.c
diff options
context:
space:
mode:
Diffstat (limited to 'libs/liblua/src/lbaselib.c')
-rw-r--r--libs/liblua/src/lbaselib.c139
1 files changed, 84 insertions, 55 deletions
diff --git a/libs/liblua/src/lbaselib.c b/libs/liblua/src/lbaselib.c
index 6460e4f8d4..747fd45a2f 100644
--- a/libs/liblua/src/lbaselib.c
+++ b/libs/liblua/src/lbaselib.c
@@ -1,5 +1,5 @@
/*
-** $Id: lbaselib.c,v 1.314.1.1 2017/04/19 17:39:34 roberto Exp $
+** $Id: lbaselib.c $
** Basic library
** See Copyright Notice in lua.h
*/
@@ -24,18 +24,12 @@
static int luaB_print (lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
int i;
- lua_getglobal(L, "tostring");
- for (i=1; i<=n; i++) {
- const char *s;
+ for (i = 1; i <= n; i++) { /* for each argument */
size_t l;
- lua_pushvalue(L, -1); /* function to be called */
- lua_pushvalue(L, i); /* value to print */
- lua_call(L, 1, 1);
- s = lua_tolstring(L, -1, &l); /* get result */
- if (s == NULL)
- return luaL_error(L, "'tostring' must return a string to 'print'");
- if (i>1) lua_writestring("\t", 1);
- lua_writestring(s, l);
+ const char *s = luaL_tolstring(L, i, &l); /* convert it to string */
+ if (i > 1) /* not the first element? */
+ lua_writestring("\t", 1); /* add a tab before it */
+ lua_writestring(s, l); /* print it */
lua_pop(L, 1); /* pop result */
}
lua_writeline();
@@ -43,13 +37,31 @@ static int luaB_print (lua_State *L) {
}
+/*
+** Creates a warning with all given arguments.
+** Check first for errors; otherwise an error may interrupt
+** the composition of a warning, leaving it unfinished.
+*/
+static int luaB_warn (lua_State *L) {
+ int n = lua_gettop(L); /* number of arguments */
+ int i;
+ luaL_checkstring(L, 1); /* at least one argument */
+ for (i = 2; i <= n; i++)
+ luaL_checkstring(L, i); /* make sure all arguments are strings */
+ for (i = 1; i < n; i++) /* compose warning */
+ lua_warning(L, lua_tostring(L, i), 1);
+ lua_warning(L, lua_tostring(L, n), 0); /* close warning */
+ return 0;
+}
+
+
#define SPACECHARS " \f\n\r\t\v"
static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
lua_Unsigned n = 0;
int neg = 0;
s += strspn(s, SPACECHARS); /* skip initial spaces */
- if (*s == '-') { s++; neg = 1; } /* handle signal */
+ if (*s == '-') { s++; neg = 1; } /* handle sign */
else if (*s == '+') s++;
if (!isalnum((unsigned char)*s)) /* no digit? */
return NULL;
@@ -68,7 +80,6 @@ static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
static int luaB_tonumber (lua_State *L) {
if (lua_isnoneornil(L, 2)) { /* standard conversion? */
- luaL_checkany(L, 1);
if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */
lua_settop(L, 1); /* yes; return it */
return 1;
@@ -79,6 +90,7 @@ static int luaB_tonumber (lua_State *L) {
if (s != NULL && lua_stringtonumber(L, s) == l + 1)
return 1; /* successful conversion to number */
/* else not a number */
+ luaL_checkany(L, 1); /* (but there must be some parameter) */
}
}
else {
@@ -94,7 +106,7 @@ static int luaB_tonumber (lua_State *L) {
return 1;
} /* else not a number */
} /* else not a number */
- lua_pushnil(L); /* not a number */
+ luaL_pushfail(L); /* not a number */
return 1;
}
@@ -125,8 +137,7 @@ static int luaB_getmetatable (lua_State *L) {
static int luaB_setmetatable (lua_State *L) {
int t = lua_type(L, 2);
luaL_checktype(L, 1, LUA_TTABLE);
- luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
- "nil or table expected");
+ luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL)
return luaL_error(L, "cannot change a protected metatable");
lua_settop(L, 2);
@@ -145,8 +156,8 @@ static int luaB_rawequal (lua_State *L) {
static int luaB_rawlen (lua_State *L) {
int t = lua_type(L, 1);
- luaL_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
- "table or string expected");
+ luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
+ "table or string");
lua_pushinteger(L, lua_rawlen(L, 1));
return 1;
}
@@ -170,27 +181,58 @@ static int luaB_rawset (lua_State *L) {
}
+static int pushmode (lua_State *L, int oldmode) {
+ lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" : "generational");
+ return 1;
+}
+
+
static int luaB_collectgarbage (lua_State *L) {
static const char *const opts[] = {"stop", "restart", "collect",
"count", "step", "setpause", "setstepmul",
- "isrunning", NULL};
+ "isrunning", "generational", "incremental", NULL};
static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
- LUA_GCISRUNNING};
+ LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC};
int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
- int ex = (int)luaL_optinteger(L, 2, 0);
- int res = lua_gc(L, o, ex);
switch (o) {
case LUA_GCCOUNT: {
- int b = lua_gc(L, LUA_GCCOUNTB, 0);
- lua_pushnumber(L, (lua_Number)res + ((lua_Number)b/1024));
+ int k = lua_gc(L, o);
+ int b = lua_gc(L, LUA_GCCOUNTB);
+ lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024));
+ return 1;
+ }
+ case LUA_GCSTEP: {
+ int step = (int)luaL_optinteger(L, 2, 0);
+ int res = lua_gc(L, o, step);
+ lua_pushboolean(L, res);
+ return 1;
+ }
+ case LUA_GCSETPAUSE:
+ case LUA_GCSETSTEPMUL: {
+ int p = (int)luaL_optinteger(L, 2, 0);
+ int previous = lua_gc(L, o, p);
+ lua_pushinteger(L, previous);
return 1;
}
- case LUA_GCSTEP: case LUA_GCISRUNNING: {
+ case LUA_GCISRUNNING: {
+ int res = lua_gc(L, o);
lua_pushboolean(L, res);
return 1;
}
+ case LUA_GCGEN: {
+ int minormul = (int)luaL_optinteger(L, 2, 0);
+ int majormul = (int)luaL_optinteger(L, 3, 0);
+ return pushmode(L, lua_gc(L, o, minormul, majormul));
+ }
+ case LUA_GCINC: {
+ int pause = (int)luaL_optinteger(L, 2, 0);
+ int stepmul = (int)luaL_optinteger(L, 3, 0);
+ int stepsize = (int)luaL_optinteger(L, 4, 0);
+ return pushmode(L, lua_gc(L, o, pause, stepmul, stepsize));
+ }
default: {
+ int res = lua_gc(L, o);
lua_pushinteger(L, res);
return 1;
}
@@ -206,23 +248,6 @@ static int luaB_type (lua_State *L) {
}
-static int pairsmeta (lua_State *L, const char *method, int iszero,
- lua_CFunction iter) {
- luaL_checkany(L, 1);
- if (luaL_getmetafield(L, 1, method) == LUA_TNIL) { /* no metamethod? */
- lua_pushcfunction(L, iter); /* will return generator, */
- lua_pushvalue(L, 1); /* state, */
- if (iszero) lua_pushinteger(L, 0); /* and initial value */
- else lua_pushnil(L);
- }
- else {
- lua_pushvalue(L, 1); /* argument 'self' to metamethod */
- lua_call(L, 1, 3); /* get 3 values from metamethod */
- }
- return 3;
-}
-
-
static int luaB_next (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
@@ -236,7 +261,17 @@ static int luaB_next (lua_State *L) {
static int luaB_pairs (lua_State *L) {
- return pairsmeta(L, "__pairs", 0, luaB_next);
+ luaL_checkany(L, 1);
+ if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */
+ lua_pushcfunction(L, luaB_next); /* will return generator, */
+ lua_pushvalue(L, 1); /* state, */
+ lua_pushnil(L); /* and initial value */
+ }
+ else {
+ lua_pushvalue(L, 1); /* argument 'self' to metamethod */
+ lua_call(L, 1, 3); /* get 3 values from metamethod */
+ }
+ return 3;
}
@@ -255,15 +290,11 @@ static int ipairsaux (lua_State *L) {
** (The given "table" may not be a table.)
*/
static int luaB_ipairs (lua_State *L) {
-#if defined(LUA_COMPAT_IPAIRS)
- return pairsmeta(L, "__ipairs", 1, ipairsaux);
-#else
luaL_checkany(L, 1);
lua_pushcfunction(L, ipairsaux); /* iteration function */
lua_pushvalue(L, 1); /* state */
lua_pushinteger(L, 0); /* initial value */
return 3;
-#endif
}
@@ -277,9 +308,9 @@ static int load_aux (lua_State *L, int status, int envidx) {
return 1;
}
else { /* error (message is on top of the stack) */
- lua_pushnil(L);
+ luaL_pushfail(L);
lua_insert(L, -2); /* put before error message */
- return 2; /* return nil plus error message */
+ return 2; /* return fail plus error message */
}
}
@@ -459,13 +490,11 @@ static const luaL_Reg base_funcs[] = {
{"ipairs", luaB_ipairs},
{"loadfile", luaB_loadfile},
{"load", luaB_load},
-#if defined(LUA_COMPAT_LOADSTRING)
- {"loadstring", luaB_load},
-#endif
{"next", luaB_next},
{"pairs", luaB_pairs},
{"pcall", luaB_pcall},
{"print", luaB_print},
+ {"warn", luaB_warn},
{"rawequal", luaB_rawequal},
{"rawlen", luaB_rawlen},
{"rawget", luaB_rawget},
@@ -477,7 +506,7 @@ static const luaL_Reg base_funcs[] = {
{"type", luaB_type},
{"xpcall", luaB_xpcall},
/* placeholders */
- {"_G", NULL},
+ {LUA_GNAME, NULL},
{"_VERSION", NULL},
{NULL, NULL}
};
@@ -489,7 +518,7 @@ LUAMOD_API int luaopen_base (lua_State *L) {
luaL_setfuncs(L, base_funcs, 0);
/* set global _G */
lua_pushvalue(L, -1);
- lua_setfield(L, -2, "_G");
+ lua_setfield(L, -2, LUA_GNAME);
/* set global _VERSION */
lua_pushliteral(L, LUA_VERSION);
lua_setfield(L, -2, "_VERSION");