'unpack' moved to table library (and therefore "renamed" to

'table.unpack'.
This commit is contained in:
Roberto Ierusalimschy 2009-12-28 14:30:31 -02:00
parent cc1cbd19a0
commit 0dc09cb42e
3 changed files with 33 additions and 21 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lbaselib.c,v 1.233 2009/12/17 16:20:01 roberto Exp roberto $ ** $Id: lbaselib.c,v 1.234 2009/12/22 15:32:50 roberto Exp roberto $
** Basic library ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -412,22 +412,6 @@ static int luaB_assert (lua_State *L) {
} }
static int luaB_unpack (lua_State *L) {
int i, e, n;
luaL_checktype(L, 1, LUA_TTABLE);
i = luaL_optint(L, 2, 1);
e = luaL_opt(L, luaL_checkint, 3, (int)lua_rawlen(L, 1));
if (i > e) return 0; /* empty range */
n = e - i + 1; /* number of elements */
if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
return luaL_error(L, "too many results to unpack");
lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */
while (i++ < e) /* push arg[i + 1...e] */
lua_rawgeti(L, 1, i);
return n;
}
static int luaB_select (lua_State *L) { static int luaB_select (lua_State *L) {
int n = lua_gettop(L); int n = lua_gettop(L);
if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') { if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
@ -542,7 +526,6 @@ static const luaL_Reg base_funcs[] = {
{"tonumber", luaB_tonumber}, {"tonumber", luaB_tonumber},
{"tostring", luaB_tostring}, {"tostring", luaB_tostring},
{"type", luaB_type}, {"type", luaB_type},
{"unpack", luaB_unpack},
{"xpcall", luaB_xpcall}, {"xpcall", luaB_xpcall},
{NULL, NULL} {NULL, NULL}
}; };

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltablib.c,v 1.51 2009/12/17 16:20:01 roberto Exp roberto $ ** $Id: ltablib.c,v 1.52 2009/12/18 16:53:12 roberto Exp roberto $
** Library for Table Manipulation ** Library for Table Manipulation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -164,7 +164,7 @@ static int tconcat (lua_State *L) {
/* /*
** {====================================================== ** {======================================================
** Pack ** Pack/unpack
** ======================================================= ** =======================================================
*/ */
@ -181,6 +181,22 @@ static int pack (lua_State *L) {
return 1; return 1;
} }
static int unpack (lua_State *L) {
int i, e, n;
luaL_checktype(L, 1, LUA_TTABLE);
i = luaL_optint(L, 2, 1);
e = luaL_opt(L, luaL_checkint, 3, (int)lua_rawlen(L, 1));
if (i > e) return 0; /* empty range */
n = e - i + 1; /* number of elements */
if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
return luaL_error(L, "too many results to unpack");
lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */
while (i++ < e) /* push arg[i + 1...e] */
lua_rawgeti(L, 1, i);
return n;
}
/* }====================================================== */ /* }====================================================== */
@ -298,6 +314,7 @@ static const luaL_Reg tab_funcs[] = {
{"maxn", maxn}, {"maxn", maxn},
{"insert", tinsert}, {"insert", tinsert},
{"pack", pack}, {"pack", pack},
{"unpack", unpack},
{"remove", tremove}, {"remove", tremove},
{"sort", sort}, {"sort", sort},
{NULL, NULL} {NULL, NULL}
@ -306,6 +323,11 @@ static const luaL_Reg tab_funcs[] = {
LUAMOD_API int luaopen_table (lua_State *L) { LUAMOD_API int luaopen_table (lua_State *L) {
luaL_register(L, LUA_TABLIBNAME, tab_funcs); luaL_register(L, LUA_TABLIBNAME, tab_funcs);
#if defined(LUA_COMPAT_UNPACK)
/* _G.unpack = table.unpack */
lua_getfield(L, -1, "unpack");
lua_setfield(L, LUA_ENVIRONINDEX, "unpack");
#endif
return 1; return 1;
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: luaconf.h,v 1.124 2009/12/17 13:08:51 roberto Exp roberto $ ** $Id: luaconf.h,v 1.125 2009/12/22 16:47:00 roberto Exp roberto $
** Configuration file for Lua ** Configuration file for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -205,6 +205,13 @@
** =================================================================== ** ===================================================================
*/ */
/*
@@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'.
** CHANGE it (define it) if you have not replaced its uses with
** 'table.unpack'.
*/
/* #define LUA_COMPAT_UNPACK */
/* /*
@@ LUA_COMPAT_CPCALL controls the presence of function 'lua_cpcall'. @@ LUA_COMPAT_CPCALL controls the presence of function 'lua_cpcall'.
** CHANGE it (define it) if you need this function. (You can replace ** CHANGE it (define it) if you need this function. (You can replace