diff options
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 | } |
