aboutsummaryrefslogtreecommitdiff
path: root/ltablib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-12-28 14:30:31 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-12-28 14:30:31 -0200
commit0dc09cb42e558aabf9ffca397b5588e6ee2fecfa (patch)
tree2c2b12f87ae8edbe510ff42ce052195fc11e1b4e /ltablib.c
parentcc1cbd19a0730744a5db45086a0f37137e4f7bef (diff)
downloadlua-0dc09cb42e558aabf9ffca397b5588e6ee2fecfa.tar.gz
lua-0dc09cb42e558aabf9ffca397b5588e6ee2fecfa.tar.bz2
lua-0dc09cb42e558aabf9ffca397b5588e6ee2fecfa.zip
'unpack' moved to table library (and therefore "renamed" to
'table.unpack'.
Diffstat (limited to 'ltablib.c')
-rw-r--r--ltablib.c26
1 files changed, 24 insertions, 2 deletions
diff --git a/ltablib.c b/ltablib.c
index 9a089932..a288633a 100644
--- a/ltablib.c
+++ b/ltablib.c
@@ -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
185static 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
307LUAMOD_API int luaopen_table (lua_State *L) { 324LUAMOD_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