From 489253d7530e372da8807f1ad083928c3545f772 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 13 Mar 2010 00:57:46 -0300 Subject: better definitions for lua_[gs]etglobal + less uses of ENVIRONINDEX --- ltablib.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'ltablib.c') diff --git a/ltablib.c b/ltablib.c index e2da6081..533f9ac9 100644 --- a/ltablib.c +++ b/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.53 2009/12/28 16:30:31 roberto Exp roberto $ +** $Id: ltablib.c,v 1.54 2010/01/13 19:59:10 roberto Exp roberto $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ @@ -171,15 +171,15 @@ static int tconcat (lua_State *L) { static int pack (lua_State *L) { int top = lua_gettop(L); lua_createtable(L, top, 1); /* create result table */ - /* use function environment as a temporary place to keep new table */ - lua_replace(L, LUA_ENVIRONINDEX); lua_pushinteger(L, top); /* number of elements */ - lua_setfield(L, LUA_ENVIRONINDEX, "n"); /* t.n = number of elements */ - for (; top >= 1; top--) /* assign elements */ - lua_rawseti(L, LUA_ENVIRONINDEX, top); - lua_pushvalue(L, LUA_ENVIRONINDEX); /* return new table */ - /* remove new table from environment to allow its later collection */ - lua_copy(L, LUA_REGISTRYINDEX, LUA_ENVIRONINDEX); + lua_setfield(L, -2, "n"); /* t.n = number of elements */ + if (top > 0) { /* at least one element? */ + lua_pushvalue(L, 1); + lua_rawseti(L, -2, 1); /* insert first element */ + lua_replace(L, 1); /* move table into its position (index 1) */ + for (; top >= 2; top--) /* assign other elements */ + lua_rawseti(L, 1, top); + } return 1; } @@ -328,7 +328,7 @@ LUAMOD_API int luaopen_table (lua_State *L) { #if defined(LUA_COMPAT_UNPACK) /* _G.unpack = table.unpack */ lua_getfield(L, -1, "unpack"); - lua_setfield(L, LUA_ENVIRONINDEX, "unpack"); + lua_setglobal(L, "unpack"); #endif return 1; } -- cgit v1.2.3-55-g6feb