summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/lua/src/lbaselib.c
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/lua/src/lbaselib.c')
-rw-r--r--3rdparty/lua/src/lbaselib.c246
1 files changed, 154 insertions, 92 deletions
diff --git a/3rdparty/lua/src/lbaselib.c b/3rdparty/lua/src/lbaselib.c
index 5255b3cd9b7..a2403952f02 100644
--- a/3rdparty/lua/src/lbaselib.c
+++ b/3rdparty/lua/src/lbaselib.c
@@ -1,9 +1,13 @@
/*
-** $Id: lbaselib.c,v 1.276.1.1 2013/04/12 18:48:47 roberto Exp $
+** $Id: lbaselib.c,v 1.309 2014/12/10 12:26:42 roberto Exp $
** Basic library
** See Copyright Notice in lua.h
*/
+#define lbaselib_c
+#define LUA_LIB
+
+#include "lprefix.h"
#include <ctype.h>
@@ -11,9 +15,6 @@
#include <stdlib.h>
#include <string.h>
-#define lbaselib_c
-#define LUA_LIB
-
#include "lua.h"
#include "lauxlib.h"
@@ -32,62 +33,74 @@ static int luaB_print (lua_State *L) {
lua_call(L, 1, 1);
s = lua_tolstring(L, -1, &l); /* get result */
if (s == NULL)
- return luaL_error(L,
- LUA_QL("tostring") " must return a string to " LUA_QL("print"));
- if (i>1) luai_writestring("\t", 1);
- luai_writestring(s, l);
+ return luaL_error(L, "'tostring' must return a string to 'print'");
+ if (i>1) lua_writestring("\t", 1);
+ lua_writestring(s, l);
lua_pop(L, 1); /* pop result */
}
- luai_writeline();
+ lua_writeline();
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 */
+ else if (*s == '+') s++;
+ if (!isalnum((unsigned char)*s)) /* no digit? */
+ return NULL;
+ do {
+ int digit = (isdigit((unsigned char)*s)) ? *s - '0'
+ : toupper((unsigned char)*s) - 'A' + 10;
+ if (digit >= base) return NULL; /* invalid numeral */
+ n = n * base + digit;
+ s++;
+ } while (isalnum((unsigned char)*s));
+ s += strspn(s, SPACECHARS); /* skip trailing spaces */
+ *pn = (lua_Integer)((neg) ? (0u - n) : n);
+ return s;
+}
+
+
static int luaB_tonumber (lua_State *L) {
- if (lua_isnoneornil(L, 2)) { /* standard conversion */
- int isnum;
- lua_Number n = lua_tonumberx(L, 1, &isnum);
- if (isnum) {
- lua_pushnumber(L, n);
- return 1;
- } /* else not a number; must be something */
+ 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;
+ }
+ else {
+ size_t l;
+ const char *s = lua_tolstring(L, 1, &l);
+ if (s != NULL && lua_stringtonumber(L, s) == l + 1)
+ return 1; /* successful conversion to number */
+ /* else not a number */
+ }
}
else {
size_t l;
- const char *s = luaL_checklstring(L, 1, &l);
- const char *e = s + l; /* end point for 's' */
- int base = luaL_checkint(L, 2);
- int neg = 0;
+ const char *s;
+ lua_Integer n = 0; /* to avoid warnings */
+ lua_Integer base = luaL_checkinteger(L, 2);
+ luaL_checktype(L, 1, LUA_TSTRING); /* before 'luaL_checklstring'! */
+ s = luaL_checklstring(L, 1, &l);
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
- s += strspn(s, SPACECHARS); /* skip initial spaces */
- if (*s == '-') { s++; neg = 1; } /* handle signal */
- else if (*s == '+') s++;
- if (isalnum((unsigned char)*s)) {
- lua_Number n = 0;
- do {
- int digit = (isdigit((unsigned char)*s)) ? *s - '0'
- : toupper((unsigned char)*s) - 'A' + 10;
- if (digit >= base) break; /* invalid numeral; force a fail */
- n = n * (lua_Number)base + (lua_Number)digit;
- s++;
- } while (isalnum((unsigned char)*s));
- s += strspn(s, SPACECHARS); /* skip trailing spaces */
- if (s == e) { /* no invalid trailing characters? */
- lua_pushnumber(L, (neg) ? -n : n);
- return 1;
- } /* else not a number */
+ if (b_str2int(s, (int)base, &n) == s + l) {
+ lua_pushinteger(L, n);
+ return 1;
} /* else not a number */
- }
+ } /* else not a number */
lua_pushnil(L); /* not a number */
return 1;
}
static int luaB_error (lua_State *L) {
- int level = luaL_optint(L, 2, 1);
+ int level = (int)luaL_optinteger(L, 2, 1);
lua_settop(L, 1);
if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
luaL_where(L, level);
@@ -114,7 +127,7 @@ static int luaB_setmetatable (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
"nil or table expected");
- if (luaL_getmetafield(L, 1, "__metatable"))
+ if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL)
return luaL_error(L, "cannot change a protected metatable");
lua_settop(L, 2);
lua_setmetatable(L, 1);
@@ -160,19 +173,18 @@ static int luaB_rawset (lua_State *L) {
static int luaB_collectgarbage (lua_State *L) {
static const char *const opts[] = {"stop", "restart", "collect",
"count", "step", "setpause", "setstepmul",
- "setmajorinc", "isrunning", "generational", "incremental", NULL};
+ "isrunning", NULL};
static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
- LUA_GCSETMAJORINC, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC};
+ LUA_GCISRUNNING};
int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
- int ex = luaL_optint(L, 2, 0);
+ 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, res + ((lua_Number)b/1024));
- lua_pushinteger(L, b);
- return 2;
+ lua_pushnumber(L, (lua_Number)res + ((lua_Number)b/1024));
+ return 1;
}
case LUA_GCSTEP: case LUA_GCISRUNNING: {
lua_pushboolean(L, res);
@@ -186,16 +198,19 @@ static int luaB_collectgarbage (lua_State *L) {
}
+/*
+** This function has all type names as upvalues, to maximize performance.
+*/
static int luaB_type (lua_State *L) {
luaL_checkany(L, 1);
- lua_pushstring(L, luaL_typename(L, 1));
+ lua_pushvalue(L, lua_upvalueindex(lua_type(L, 1) + 1));
return 1;
}
static int pairsmeta (lua_State *L, const char *method, int iszero,
lua_CFunction iter) {
- if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */
+ if (luaL_getmetafield(L, 1, method) == LUA_TNIL) { /* no metamethod? */
luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */
lua_pushcfunction(L, iter); /* will return generator, */
lua_pushvalue(L, 1); /* state, */
@@ -227,18 +242,44 @@ static int luaB_pairs (lua_State *L) {
}
-static int ipairsaux (lua_State *L) {
- int i = luaL_checkint(L, 2);
+/*
+** Traversal function for 'ipairs' for raw tables
+*/
+static int ipairsaux_raw (lua_State *L) {
+ lua_Integer i = luaL_checkinteger(L, 2) + 1;
luaL_checktype(L, 1, LUA_TTABLE);
- i++; /* next value */
lua_pushinteger(L, i);
- lua_rawgeti(L, 1, i);
- return (lua_isnil(L, -1)) ? 1 : 2;
+ return (lua_rawgeti(L, 1, i) == LUA_TNIL) ? 1 : 2;
}
+/*
+** Traversal function for 'ipairs' for tables with metamethods
+*/
+static int ipairsaux (lua_State *L) {
+ lua_Integer i = luaL_checkinteger(L, 2) + 1;
+ lua_pushinteger(L, i);
+ return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2;
+}
+
+
+/*
+** This function will use either 'ipairsaux' or 'ipairsaux_raw' to
+** traverse a table, depending on whether the table has metamethods
+** that can affect the traversal.
+*/
static int luaB_ipairs (lua_State *L) {
- return pairsmeta(L, "__ipairs", 1, ipairsaux);
+ lua_CFunction iter = (luaL_getmetafield(L, 1, "__index") != LUA_TNIL)
+ ? ipairsaux : ipairsaux_raw;
+#if defined(LUA_COMPAT_IPAIRS)
+ return pairsmeta(L, "__ipairs", 1, iter);
+#else
+ luaL_checkany(L, 1);
+ lua_pushcfunction(L, iter); /* iteration function */
+ lua_pushvalue(L, 1); /* state */
+ lua_pushinteger(L, 0); /* initial value */
+ return 3;
+#endif
}
@@ -284,7 +325,7 @@ static int luaB_loadfile (lua_State *L) {
/*
-** Reader for generic `load' function: `lua_load' uses the
+** Reader for generic 'load' function: 'lua_load' uses the
** stack for internal stuff, so the reader cannot change the
** stack top. Instead, it keeps its resulting string in a
** reserved slot inside the stack.
@@ -328,7 +369,8 @@ static int luaB_load (lua_State *L) {
/* }====================================================== */
-static int dofilecont (lua_State *L) {
+static int dofilecont (lua_State *L, int d1, lua_KContext d2) {
+ (void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */
return lua_gettop(L) - 1;
}
@@ -339,14 +381,20 @@ static int luaB_dofile (lua_State *L) {
if (luaL_loadfile(L, fname) != LUA_OK)
return lua_error(L);
lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
- return dofilecont(L);
+ return dofilecont(L, 0, 0);
}
static int luaB_assert (lua_State *L) {
- if (!lua_toboolean(L, 1))
- return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
- return lua_gettop(L);
+ if (lua_toboolean(L, 1)) /* condition is true? */
+ return lua_gettop(L); /* return all arguments */
+ else { /* error */
+ luaL_checkany(L, 1); /* there must be a condition */
+ lua_remove(L, 1); /* remove it */
+ lua_pushliteral(L, "assertion failed!"); /* default message */
+ lua_settop(L, 1); /* leave only message (default if no other one) */
+ return luaB_error(L); /* call 'error' */
+ }
}
@@ -357,53 +405,57 @@ static int luaB_select (lua_State *L) {
return 1;
}
else {
- int i = luaL_checkint(L, 1);
+ lua_Integer i = luaL_checkinteger(L, 1);
if (i < 0) i = n + i;
else if (i > n) i = n;
luaL_argcheck(L, 1 <= i, 1, "index out of range");
- return n - i;
+ return n - (int)i;
}
}
-static int finishpcall (lua_State *L, int status) {
- if (!lua_checkstack(L, 1)) { /* no space for extra boolean? */
- lua_settop(L, 0); /* create space for return values */
- lua_pushboolean(L, 0);
- lua_pushstring(L, "stack overflow");
+/*
+** Continuation function for 'pcall' and 'xpcall'. Both functions
+** already pushed a 'true' before doing the call, so in case of success
+** 'finishpcall' only has to return everything in the stack minus
+** 'extra' values (where 'extra' is exactly the number of items to be
+** ignored).
+*/
+static int finishpcall (lua_State *L, int status, lua_KContext extra) {
+ if (status != LUA_OK && status != LUA_YIELD) { /* error? */
+ lua_pushboolean(L, 0); /* first result (false) */
+ lua_pushvalue(L, -2); /* error message */
return 2; /* return false, msg */
}
- lua_pushboolean(L, status); /* first result (status) */
- lua_replace(L, 1); /* put first result in first slot */
- return lua_gettop(L);
-}
-
-
-static int pcallcont (lua_State *L) {
- int status = lua_getctx(L, NULL);
- return finishpcall(L, (status == LUA_YIELD));
+ else
+ return lua_gettop(L) - (int)extra; /* return all results */
}
static int luaB_pcall (lua_State *L) {
int status;
luaL_checkany(L, 1);
- lua_pushnil(L);
- lua_insert(L, 1); /* create space for status result */
- status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, pcallcont);
- return finishpcall(L, (status == LUA_OK));
+ lua_pushboolean(L, 1); /* first result if no errors */
+ lua_insert(L, 1); /* put it in place */
+ status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall);
+ return finishpcall(L, status, 0);
}
+/*
+** Do a protected call with error handling. After 'lua_rotate', the
+** stack will have <f, err, true, f, [args...]>; so, the function passes
+** 2 to 'finishpcall' to skip the 2 first values when returning results.
+*/
static int luaB_xpcall (lua_State *L) {
int status;
int n = lua_gettop(L);
- luaL_argcheck(L, n >= 2, 2, "value expected");
- lua_pushvalue(L, 1); /* exchange function... */
- lua_copy(L, 2, 1); /* ...and error handler */
- lua_replace(L, 2);
- status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 0, pcallcont);
- return finishpcall(L, (status == LUA_OK));
+ luaL_checktype(L, 2, LUA_TFUNCTION); /* check error function */
+ lua_pushboolean(L, 1); /* first result */
+ lua_pushvalue(L, 1); /* function */
+ lua_rotate(L, 3, 2); /* move them below function's arguments */
+ status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, finishpcall);
+ return finishpcall(L, status, 2);
}
@@ -438,21 +490,31 @@ static const luaL_Reg base_funcs[] = {
{"setmetatable", luaB_setmetatable},
{"tonumber", luaB_tonumber},
{"tostring", luaB_tostring},
- {"type", luaB_type},
{"xpcall", luaB_xpcall},
+ /* placeholders */
+ {"type", NULL},
+ {"_G", NULL},
+ {"_VERSION", NULL},
{NULL, NULL}
};
LUAMOD_API int luaopen_base (lua_State *L) {
- /* set global _G */
- lua_pushglobaltable(L);
- lua_pushglobaltable(L);
- lua_setfield(L, -2, "_G");
+ int i;
/* open lib into global table */
+ lua_pushglobaltable(L);
luaL_setfuncs(L, base_funcs, 0);
+ /* set global _G */
+ lua_pushvalue(L, -1);
+ lua_setfield(L, -2, "_G");
+ /* set global _VERSION */
lua_pushliteral(L, LUA_VERSION);
- lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */
+ lua_setfield(L, -2, "_VERSION");
+ /* set function 'type' with proper upvalues */
+ for (i = 0; i < LUA_NUMTAGS; i++) /* push all type names as upvalues */
+ lua_pushstring(L, lua_typename(L, i));
+ lua_pushcclosure(L, luaB_type, LUA_NUMTAGS);
+ lua_setfield(L, -2, "type");
return 1;
}