diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-03-13 00:57:46 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-03-13 00:57:46 -0300 |
commit | 489253d7530e372da8807f1ad083928c3545f772 (patch) | |
tree | 27af73f0d2809bef0b2366fee6e26cbbbe748887 /ltablib.c | |
parent | 25c557ec6367870c127e879cce8ed8fa21f34398 (diff) | |
download | lua-489253d7530e372da8807f1ad083928c3545f772.tar.gz lua-489253d7530e372da8807f1ad083928c3545f772.tar.bz2 lua-489253d7530e372da8807f1ad083928c3545f772.zip |
better definitions for lua_[gs]etglobal + less uses of ENVIRONINDEX
Diffstat (limited to 'ltablib.c')
-rw-r--r-- | ltablib.c | 20 |
1 files changed, 10 insertions, 10 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltablib.c,v 1.53 2009/12/28 16:30:31 roberto Exp roberto $ | 2 | ** $Id: ltablib.c,v 1.54 2010/01/13 19:59:10 roberto Exp roberto $ |
3 | ** Library for Table Manipulation | 3 | ** Library for Table Manipulation |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -171,15 +171,15 @@ static int tconcat (lua_State *L) { | |||
171 | static int pack (lua_State *L) { | 171 | static int pack (lua_State *L) { |
172 | int top = lua_gettop(L); | 172 | int top = lua_gettop(L); |
173 | lua_createtable(L, top, 1); /* create result table */ | 173 | lua_createtable(L, top, 1); /* create result table */ |
174 | /* use function environment as a temporary place to keep new table */ | ||
175 | lua_replace(L, LUA_ENVIRONINDEX); | ||
176 | lua_pushinteger(L, top); /* number of elements */ | 174 | lua_pushinteger(L, top); /* number of elements */ |
177 | lua_setfield(L, LUA_ENVIRONINDEX, "n"); /* t.n = number of elements */ | 175 | lua_setfield(L, -2, "n"); /* t.n = number of elements */ |
178 | for (; top >= 1; top--) /* assign elements */ | 176 | if (top > 0) { /* at least one element? */ |
179 | lua_rawseti(L, LUA_ENVIRONINDEX, top); | 177 | lua_pushvalue(L, 1); |
180 | lua_pushvalue(L, LUA_ENVIRONINDEX); /* return new table */ | 178 | lua_rawseti(L, -2, 1); /* insert first element */ |
181 | /* remove new table from environment to allow its later collection */ | 179 | lua_replace(L, 1); /* move table into its position (index 1) */ |
182 | lua_copy(L, LUA_REGISTRYINDEX, LUA_ENVIRONINDEX); | 180 | for (; top >= 2; top--) /* assign other elements */ |
181 | lua_rawseti(L, 1, top); | ||
182 | } | ||
183 | return 1; | 183 | return 1; |
184 | } | 184 | } |
185 | 185 | ||
@@ -328,7 +328,7 @@ LUAMOD_API int luaopen_table (lua_State *L) { | |||
328 | #if defined(LUA_COMPAT_UNPACK) | 328 | #if defined(LUA_COMPAT_UNPACK) |
329 | /* _G.unpack = table.unpack */ | 329 | /* _G.unpack = table.unpack */ |
330 | lua_getfield(L, -1, "unpack"); | 330 | lua_getfield(L, -1, "unpack"); |
331 | lua_setfield(L, LUA_ENVIRONINDEX, "unpack"); | 331 | lua_setglobal(L, "unpack"); |
332 | #endif | 332 | #endif |
333 | return 1; | 333 | return 1; |
334 | } | 334 | } |