diff --git a/lapi.c b/lapi.c index 84b3fe72..67fe9ddc 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.202 2002/06/24 13:08:45 roberto Exp roberto $ +** $Id: lapi.c,v 1.203 2002/06/25 19:15:41 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -697,43 +697,6 @@ LUA_API int lua_next (lua_State *L, int index) { } -LUA_API int lua_getn (lua_State *L, int index) { - StkId t; - const TObject *value; - int n; - lua_lock(L); - t = luaA_index(L, index); - api_check(L, ttype(t) == LUA_TTABLE); - value = luaH_getstr(hvalue(t), luaS_newliteral(L, "n")); /* = t.n */ - if (ttype(value) == LUA_TNUMBER) - lua_number2int(n, nvalue(value)); - else { - Node *nd; - Table *a = hvalue(t); - lua_Number max = 0; - int i; - i = sizearray(a); - while (i--) { - if (ttype(&a->array[i]) != LUA_TNIL) - break; - } - max = i+1; - i = sizenode(a); - nd = a->node; - while (i--) { - if (ttype(key(nd)) == LUA_TNUMBER && - ttype(val(nd)) != LUA_TNIL && - nvalue(key(nd)) > max) - max = nvalue(key(nd)); - nd++; - } - lua_number2int(n, max); - } - lua_unlock(L); - return n; -} - - LUA_API void lua_concat (lua_State *L, int n) { lua_lock(L); api_checknelems(L, n); diff --git a/lauxlib.c b/lauxlib.c index b090e9c5..26a81287 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.75 2002/06/18 15:19:27 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.76 2002/06/25 19:15:21 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -272,17 +272,20 @@ LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { LUALIB_API int luaL_ref (lua_State *L, int t) { int ref; lua_rawgeti(L, t, 0); /* get first free element */ - ref = (int)lua_tonumber(L, -1); + ref = (int)lua_tonumber(L, -1); /* ref = t[0] */ lua_pop(L, 1); /* remove it from stack */ if (ref != 0) { /* any free element? */ lua_rawgeti(L, t, ref); /* remove it from list */ - lua_rawseti(L, t, 0); + lua_rawseti(L, t, 0); /* (that is, t[0] = t[ref]) */ } else { /* no free elements */ - ref = lua_getn(L, t) + 1; /* use next `n' */ lua_pushliteral(L, "n"); + lua_pushvalue(L, -1); + lua_rawget(L, t); /* get t.n */ + ref = (int)lua_tonumber(L, -1) + 1; /* ref = t.n + 1 */ + lua_pop(L, 1); /* pop t.n */ lua_pushnumber(L, ref); - lua_rawset(L, t); /* n = n+1 */ + lua_rawset(L, t); /* t.n = t.n + 1 */ } lua_rawseti(L, t, ref); return ref; @@ -292,9 +295,9 @@ LUALIB_API int luaL_ref (lua_State *L, int t) { LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { if (ref >= 0) { lua_rawgeti(L, t, 0); + lua_rawseti(L, t, ref); /* t[ref] = t[0] */ lua_pushnumber(L, ref); - lua_rawseti(L, t, 0); - lua_rawseti(L, t, ref); + lua_rawseti(L, t, 0); /* t[0] = ref */ } } diff --git a/lbaselib.c b/lbaselib.c index aeb213b9..40567969 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.85 2002/06/25 19:19:33 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.86 2002/06/26 16:37:13 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -257,11 +257,20 @@ static int luaB_assert (lua_State *L) { static int luaB_unpack (lua_State *L) { int n, i; luaL_check_type(L, 1, LUA_TTABLE); - n = lua_getn(L, 1); - luaL_check_stack(L, n+LUA_MINSTACK, "table too big to unpack"); - for (i=1; i<=n; i++) /* push arg[1...n] */ - lua_rawgeti(L, 1, i); - return n; + lua_pushliteral(L, "n"); + lua_rawget(L, 1); + n = (lua_isnumber(L, -1)) ? (int)lua_tonumber(L, -1) : -1; + for (i=0; i