'table.pack' also returns 'n' + 'deprecated' changed to 'removed'

This commit is contained in:
Roberto Ierusalimschy 2011-07-02 13:01:44 -03:00
parent ad1a54b5c0
commit ee37ee50d6
1 changed files with 17 additions and 15 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltablib.c,v 1.58 2010/11/23 17:21:14 roberto Exp roberto $ ** $Id: ltablib.c,v 1.59 2010/12/17 12:15:34 roberto Exp roberto $
** Library for Table Manipulation ** Library for Table Manipulation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -20,8 +20,8 @@
(luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n)) (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n))
static int deprecatedfunc (lua_State *L) { static int removedfunc (lua_State *L) {
return luaL_error(L, "deprecated function"); return luaL_error(L, "removed function");
} }
@ -41,7 +41,7 @@ static int maxn (lua_State *L) {
return 1; return 1;
} }
#else #else
#define maxn deprecatedfunc #define maxn removedfunc
#endif #endif
@ -124,18 +124,20 @@ static int tconcat (lua_State *L) {
*/ */
static int pack (lua_State *L) { static int pack (lua_State *L) {
int top = lua_gettop(L); int n = lua_gettop(L); /* number of elements to pack */
lua_createtable(L, top, 1); /* create result table */ lua_createtable(L, n, 1); /* create result table */
lua_pushinteger(L, top); /* number of elements */ lua_pushinteger(L, n);
lua_setfield(L, -2, "n"); /* t.n = number of elements */ lua_setfield(L, -2, "n"); /* t.n = number of elements */
if (top > 0) { /* at least one element? */ if (n > 0) { /* at least one element? */
int i;
lua_pushvalue(L, 1); lua_pushvalue(L, 1);
lua_rawseti(L, -2, 1); /* insert first element */ lua_rawseti(L, -2, 1); /* insert first element */
lua_replace(L, 1); /* move table into its position (index 1) */ lua_replace(L, 1); /* move table into index 1 */
for (; top >= 2; top--) /* assign other elements */ for (i = n; i >= 2; i--) /* assign other elements */
lua_rawseti(L, 1, top); lua_rawseti(L, 1, i);
} }
return 1; lua_pushinteger(L, n);
return 2; /* return table and number of elements */
} }
@ -265,9 +267,9 @@ static int sort (lua_State *L) {
static const luaL_Reg tab_funcs[] = { static const luaL_Reg tab_funcs[] = {
{"concat", tconcat}, {"concat", tconcat},
{"foreach", deprecatedfunc}, {"foreach", removedfunc},
{"foreachi", deprecatedfunc}, {"foreachi", removedfunc},
{"getn", deprecatedfunc}, {"getn", removedfunc},
{"maxn", maxn}, {"maxn", maxn},
{"insert", tinsert}, {"insert", tinsert},
{"pack", pack}, {"pack", pack},