diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-07-02 13:01:44 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-07-02 13:01:44 -0300 |
commit | ee37ee50d6560dae4123396f47c505856f33a334 (patch) | |
tree | 105e3459974e1738d45f9e550a1d7a82ebc040b6 /ltablib.c | |
parent | ad1a54b5c0b045365326923ee4f3574773abefff (diff) | |
download | lua-ee37ee50d6560dae4123396f47c505856f33a334.tar.gz lua-ee37ee50d6560dae4123396f47c505856f33a334.tar.bz2 lua-ee37ee50d6560dae4123396f47c505856f33a334.zip |
'table.pack' also returns 'n' + 'deprecated' changed to 'removed'
Diffstat (limited to 'ltablib.c')
-rw-r--r-- | ltablib.c | 32 |
1 files changed, 17 insertions, 15 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltablib.c,v 1.58 2010/11/23 17:21:14 roberto Exp roberto $ | 2 | ** $Id: ltablib.c,v 1.59 2010/12/17 12:15:34 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 | */ |
@@ -20,8 +20,8 @@ | |||
20 | (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n)) | 20 | (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n)) |
21 | 21 | ||
22 | 22 | ||
23 | static int deprecatedfunc (lua_State *L) { | 23 | static int removedfunc (lua_State *L) { |
24 | return luaL_error(L, "deprecated function"); | 24 | return luaL_error(L, "removed function"); |
25 | } | 25 | } |
26 | 26 | ||
27 | 27 | ||
@@ -41,7 +41,7 @@ static int maxn (lua_State *L) { | |||
41 | return 1; | 41 | return 1; |
42 | } | 42 | } |
43 | #else | 43 | #else |
44 | #define maxn deprecatedfunc | 44 | #define maxn removedfunc |
45 | #endif | 45 | #endif |
46 | 46 | ||
47 | 47 | ||
@@ -124,18 +124,20 @@ static int tconcat (lua_State *L) { | |||
124 | */ | 124 | */ |
125 | 125 | ||
126 | static int pack (lua_State *L) { | 126 | static int pack (lua_State *L) { |
127 | int top = lua_gettop(L); | 127 | int n = lua_gettop(L); /* number of elements to pack */ |
128 | lua_createtable(L, top, 1); /* create result table */ | 128 | lua_createtable(L, n, 1); /* create result table */ |
129 | lua_pushinteger(L, top); /* number of elements */ | 129 | lua_pushinteger(L, n); |
130 | lua_setfield(L, -2, "n"); /* t.n = number of elements */ | 130 | lua_setfield(L, -2, "n"); /* t.n = number of elements */ |
131 | if (top > 0) { /* at least one element? */ | 131 | if (n > 0) { /* at least one element? */ |
132 | int i; | ||
132 | lua_pushvalue(L, 1); | 133 | lua_pushvalue(L, 1); |
133 | lua_rawseti(L, -2, 1); /* insert first element */ | 134 | lua_rawseti(L, -2, 1); /* insert first element */ |
134 | lua_replace(L, 1); /* move table into its position (index 1) */ | 135 | lua_replace(L, 1); /* move table into index 1 */ |
135 | for (; top >= 2; top--) /* assign other elements */ | 136 | for (i = n; i >= 2; i--) /* assign other elements */ |
136 | lua_rawseti(L, 1, top); | 137 | lua_rawseti(L, 1, i); |
137 | } | 138 | } |
138 | return 1; | 139 | lua_pushinteger(L, n); |
140 | return 2; /* return table and number of elements */ | ||
139 | } | 141 | } |
140 | 142 | ||
141 | 143 | ||
@@ -265,9 +267,9 @@ static int sort (lua_State *L) { | |||
265 | 267 | ||
266 | static const luaL_Reg tab_funcs[] = { | 268 | static const luaL_Reg tab_funcs[] = { |
267 | {"concat", tconcat}, | 269 | {"concat", tconcat}, |
268 | {"foreach", deprecatedfunc}, | 270 | {"foreach", removedfunc}, |
269 | {"foreachi", deprecatedfunc}, | 271 | {"foreachi", removedfunc}, |
270 | {"getn", deprecatedfunc}, | 272 | {"getn", removedfunc}, |
271 | {"maxn", maxn}, | 273 | {"maxn", maxn}, |
272 | {"insert", tinsert}, | 274 | {"insert", tinsert}, |
273 | {"pack", pack}, | 275 | {"pack", pack}, |