diff options
Diffstat (limited to 'src/lbaselib.c')
| -rw-r--r-- | src/lbaselib.c | 48 |
1 files changed, 29 insertions, 19 deletions
diff --git a/src/lbaselib.c b/src/lbaselib.c index fb21da4b..7d8724a5 100644 --- a/src/lbaselib.c +++ b/src/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.182 2005/08/26 17:36:32 roberto Exp $ +** $Id: lbaselib.c,v 1.186 2005/10/21 13:47:42 roberto Exp $ ** Basic library ** See Copyright Notice in lua.h */ @@ -196,9 +196,23 @@ static int luaB_collectgarbage (lua_State *L) { static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL}; int o = luaL_checkoption(L, 1, "collect", opts); - int ex = luaL_optinteger(L, 2, 0); - lua_pushinteger(L, lua_gc(L, optsnum[o], ex)); - return 1; + int ex = luaL_optint(L, 2, 0); + int res = lua_gc(L, optsnum[o], ex); + switch (optsnum[o]) { + case LUA_GCCOUNT: { + int b = lua_gc(L, LUA_GCCOUNTB, 0); + lua_pushnumber(L, ((lua_Number)res*1024 + b)/1000); + return 1; + } + case LUA_GCSTEP: { + lua_pushboolean(L, res); + return 1; + } + default: { + lua_pushnumber(L, res); + return 1; + } + } } @@ -326,12 +340,10 @@ static int luaB_assert (lua_State *L) { static int luaB_unpack (lua_State *L) { - int i = luaL_optint(L, 2, 1); - int e = luaL_optint(L, 3, -1); - int n; + int i, e, n; luaL_checktype(L, 1, LUA_TTABLE); - if (e == -1) - e = luaL_getn(L, 1); + i = luaL_optint(L, 2, 1); + e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1)); n = e - i + 1; /* number of elements */ if (n <= 0) return 0; /* empty range */ luaL_checkstack(L, n, "table too big to unpack"); @@ -349,8 +361,9 @@ static int luaB_select (lua_State *L) { } else { int i = luaL_checkint(L, 1); - if (i <= 0) i = 1; - else if (i >= n) i = n; + 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; } } @@ -560,7 +573,7 @@ static int luaB_costatus (lua_State *L) { lua_pushliteral(L, "dead"); else lua_pushliteral(L, "suspended"); /* initial state */ - break; + break; } default: /* some error occured */ lua_pushliteral(L, "dead"); @@ -601,8 +614,11 @@ static void auxopen (lua_State *L, const char *name, static void base_open (lua_State *L) { + /* set global _G */ lua_pushvalue(L, LUA_GLOBALSINDEX); - luaL_register(L, NULL, base_funcs); /* open lib into global table */ + lua_setglobal(L, "_G"); + /* open lib into global table */ + luaL_register(L, "_G", base_funcs); lua_pushliteral(L, LUA_VERSION); lua_setglobal(L, "_VERSION"); /* set global _VERSION */ /* `ipairs' and `pairs' need auxliliary functions as upvalues */ @@ -616,12 +632,6 @@ static void base_open (lua_State *L) { lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */ lua_pushcclosure(L, luaB_newproxy, 1); lua_setglobal(L, "newproxy"); /* set global `newproxy' */ - /* create register._LOADED to track loaded modules */ - lua_newtable(L); - lua_setfield(L, LUA_REGISTRYINDEX, "_LOADED"); - /* set global _G */ - lua_pushvalue(L, LUA_GLOBALSINDEX); - lua_setglobal(L, "_G"); } |
