diff options
| -rw-r--r-- | lbaselib.c | 19 | ||||
| -rw-r--r-- | ltablib.c | 26 | ||||
| -rw-r--r-- | luaconf.h | 9 |
3 files changed, 33 insertions, 21 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbaselib.c,v 1.233 2009/12/17 16:20:01 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.234 2009/12/22 15:32:50 roberto Exp roberto $ |
| 3 | ** Basic library | 3 | ** Basic library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -412,22 +412,6 @@ static int luaB_assert (lua_State *L) { | |||
| 412 | } | 412 | } |
| 413 | 413 | ||
| 414 | 414 | ||
| 415 | static int luaB_unpack (lua_State *L) { | ||
| 416 | int i, e, n; | ||
| 417 | luaL_checktype(L, 1, LUA_TTABLE); | ||
| 418 | i = luaL_optint(L, 2, 1); | ||
| 419 | e = luaL_opt(L, luaL_checkint, 3, (int)lua_rawlen(L, 1)); | ||
| 420 | if (i > e) return 0; /* empty range */ | ||
| 421 | n = e - i + 1; /* number of elements */ | ||
| 422 | if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ | ||
| 423 | return luaL_error(L, "too many results to unpack"); | ||
| 424 | lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ | ||
| 425 | while (i++ < e) /* push arg[i + 1...e] */ | ||
| 426 | lua_rawgeti(L, 1, i); | ||
| 427 | return n; | ||
| 428 | } | ||
| 429 | |||
| 430 | |||
| 431 | static int luaB_select (lua_State *L) { | 415 | static int luaB_select (lua_State *L) { |
| 432 | int n = lua_gettop(L); | 416 | int n = lua_gettop(L); |
| 433 | if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') { | 417 | if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') { |
| @@ -542,7 +526,6 @@ static const luaL_Reg base_funcs[] = { | |||
| 542 | {"tonumber", luaB_tonumber}, | 526 | {"tonumber", luaB_tonumber}, |
| 543 | {"tostring", luaB_tostring}, | 527 | {"tostring", luaB_tostring}, |
| 544 | {"type", luaB_type}, | 528 | {"type", luaB_type}, |
| 545 | {"unpack", luaB_unpack}, | ||
| 546 | {"xpcall", luaB_xpcall}, | 529 | {"xpcall", luaB_xpcall}, |
| 547 | {NULL, NULL} | 530 | {NULL, NULL} |
| 548 | }; | 531 | }; |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltablib.c,v 1.51 2009/12/17 16:20:01 roberto Exp roberto $ | 2 | ** $Id: ltablib.c,v 1.52 2009/12/18 16:53:12 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 | */ |
| @@ -164,7 +164,7 @@ static int tconcat (lua_State *L) { | |||
| 164 | 164 | ||
| 165 | /* | 165 | /* |
| 166 | ** {====================================================== | 166 | ** {====================================================== |
| 167 | ** Pack | 167 | ** Pack/unpack |
| 168 | ** ======================================================= | 168 | ** ======================================================= |
| 169 | */ | 169 | */ |
| 170 | 170 | ||
| @@ -181,6 +181,22 @@ static int pack (lua_State *L) { | |||
| 181 | return 1; | 181 | return 1; |
| 182 | } | 182 | } |
| 183 | 183 | ||
| 184 | |||
| 185 | static int unpack (lua_State *L) { | ||
| 186 | int i, e, n; | ||
| 187 | luaL_checktype(L, 1, LUA_TTABLE); | ||
| 188 | i = luaL_optint(L, 2, 1); | ||
| 189 | e = luaL_opt(L, luaL_checkint, 3, (int)lua_rawlen(L, 1)); | ||
| 190 | if (i > e) return 0; /* empty range */ | ||
| 191 | n = e - i + 1; /* number of elements */ | ||
| 192 | if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ | ||
| 193 | return luaL_error(L, "too many results to unpack"); | ||
| 194 | lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ | ||
| 195 | while (i++ < e) /* push arg[i + 1...e] */ | ||
| 196 | lua_rawgeti(L, 1, i); | ||
| 197 | return n; | ||
| 198 | } | ||
| 199 | |||
| 184 | /* }====================================================== */ | 200 | /* }====================================================== */ |
| 185 | 201 | ||
| 186 | 202 | ||
| @@ -298,6 +314,7 @@ static const luaL_Reg tab_funcs[] = { | |||
| 298 | {"maxn", maxn}, | 314 | {"maxn", maxn}, |
| 299 | {"insert", tinsert}, | 315 | {"insert", tinsert}, |
| 300 | {"pack", pack}, | 316 | {"pack", pack}, |
| 317 | {"unpack", unpack}, | ||
| 301 | {"remove", tremove}, | 318 | {"remove", tremove}, |
| 302 | {"sort", sort}, | 319 | {"sort", sort}, |
| 303 | {NULL, NULL} | 320 | {NULL, NULL} |
| @@ -306,6 +323,11 @@ static const luaL_Reg tab_funcs[] = { | |||
| 306 | 323 | ||
| 307 | LUAMOD_API int luaopen_table (lua_State *L) { | 324 | LUAMOD_API int luaopen_table (lua_State *L) { |
| 308 | luaL_register(L, LUA_TABLIBNAME, tab_funcs); | 325 | luaL_register(L, LUA_TABLIBNAME, tab_funcs); |
| 326 | #if defined(LUA_COMPAT_UNPACK) | ||
| 327 | /* _G.unpack = table.unpack */ | ||
| 328 | lua_getfield(L, -1, "unpack"); | ||
| 329 | lua_setfield(L, LUA_ENVIRONINDEX, "unpack"); | ||
| 330 | #endif | ||
| 309 | return 1; | 331 | return 1; |
| 310 | } | 332 | } |
| 311 | 333 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: luaconf.h,v 1.124 2009/12/17 13:08:51 roberto Exp roberto $ | 2 | ** $Id: luaconf.h,v 1.125 2009/12/22 16:47:00 roberto Exp roberto $ |
| 3 | ** Configuration file for Lua | 3 | ** Configuration file for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -206,6 +206,13 @@ | |||
| 206 | */ | 206 | */ |
| 207 | 207 | ||
| 208 | /* | 208 | /* |
| 209 | @@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'. | ||
| 210 | ** CHANGE it (define it) if you have not replaced its uses with | ||
| 211 | ** 'table.unpack'. | ||
| 212 | */ | ||
| 213 | /* #define LUA_COMPAT_UNPACK */ | ||
| 214 | |||
| 215 | /* | ||
| 209 | @@ LUA_COMPAT_CPCALL controls the presence of function 'lua_cpcall'. | 216 | @@ LUA_COMPAT_CPCALL controls the presence of function 'lua_cpcall'. |
| 210 | ** CHANGE it (define it) if you need this function. (You can replace | 217 | ** CHANGE it (define it) if you need this function. (You can replace |
| 211 | ** it with the preregistered function cpcall.) | 218 | ** it with the preregistered function cpcall.) |
