aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-03-07 09:43:52 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-03-07 09:43:52 -0300
commit2b2267069bcd2f214fa47786364653f5522d9529 (patch)
tree5140eb2fd6280745d0eee76d4978f8cd7ffaf224
parent72d3d155b0589ef1a133af2dc385f1688b730e98 (diff)
downloadlua-2b2267069bcd2f214fa47786364653f5522d9529.tar.gz
lua-2b2267069bcd2f214fa47786364653f5522d9529.tar.bz2
lua-2b2267069bcd2f214fa47786364653f5522d9529.zip
new functions `pack' and `unpack'
-rw-r--r--lbaselib.c35
1 files changed, 28 insertions, 7 deletions
diff --git a/lbaselib.c b/lbaselib.c
index dceeadfc..20efb03e 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.28 2001/02/23 17:28:12 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.29 2001/03/06 20:09:38 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*/
@@ -19,6 +19,13 @@
19 19
20 20
21 21
22static void aux_setn (lua_State *L, int t, int n) {
23 lua_pushliteral(L, l_s("n"));
24 lua_pushnumber(L, n);
25 lua_settable(L, t);
26}
27
28
22/* 29/*
23** If your system does not support `stderr', redefine this function, or 30** If your system does not support `stderr', redefine this function, or
24** redefine _ERRORMESSAGE so that it won't need _ALERT. 31** redefine _ERRORMESSAGE so that it won't need _ALERT.
@@ -296,6 +303,17 @@ static int luaB_dofile (lua_State *L) {
296} 303}
297 304
298 305
306static int luaB_pack (lua_State *L) {
307 int n = lua_gettop(L);
308 lua_newtable(L);
309 aux_setn(L, -3, n);
310 lua_insert(L, 1);
311 while (n)
312 lua_rawseti(L, 1, n--);
313 return 1;
314}
315
316
299static int aux_unpack (lua_State *L, int arg) { 317static int aux_unpack (lua_State *L, int arg) {
300 int n, i; 318 int n, i;
301 luaL_checktype(L, arg, LUA_TTABLE); 319 luaL_checktype(L, arg, LUA_TTABLE);
@@ -307,6 +325,11 @@ static int aux_unpack (lua_State *L, int arg) {
307} 325}
308 326
309 327
328static int luaB_unpack (lua_State *L) {
329 return aux_unpack(L, 1);
330}
331
332
310static int luaB_call (lua_State *L) { 333static int luaB_call (lua_State *L) {
311 int oldtop; 334 int oldtop;
312 const l_char *options = luaL_opt_string(L, 3, l_s("")); 335 const l_char *options = luaL_opt_string(L, 3, l_s(""));
@@ -436,9 +459,7 @@ static int luaB_tinsert (lua_State *L) {
436 pos = n+1; 459 pos = n+1;
437 else 460 else
438 pos = luaL_check_int(L, 2); /* 2nd argument is the position */ 461 pos = luaL_check_int(L, 2); /* 2nd argument is the position */
439 lua_pushliteral(L, l_s("n")); 462 aux_setn(L, 1, n+1); /* t.n = n+1 */
440 lua_pushnumber(L, n+1);
441 lua_rawset(L, 1); /* t.n = n+1 */
442 for (; n>=pos; n--) { 463 for (; n>=pos; n--) {
443 lua_rawgeti(L, 1, n); 464 lua_rawgeti(L, 1, n);
444 lua_rawseti(L, 1, n+1); /* t[n+1] = t[n] */ 465 lua_rawseti(L, 1, n+1); /* t[n+1] = t[n] */
@@ -455,14 +476,12 @@ static int luaB_tremove (lua_State *L) {
455 n = lua_getn(L, 1); 476 n = lua_getn(L, 1);
456 pos = luaL_opt_int(L, 2, n); 477 pos = luaL_opt_int(L, 2, n);
457 if (n <= 0) return 0; /* table is `empty' */ 478 if (n <= 0) return 0; /* table is `empty' */
479 aux_setn(L, 1, n-1); /* t.n = n-1 */
458 lua_rawgeti(L, 1, pos); /* result = t[pos] */ 480 lua_rawgeti(L, 1, pos); /* result = t[pos] */
459 for ( ;pos<n; pos++) { 481 for ( ;pos<n; pos++) {
460 lua_rawgeti(L, 1, pos+1); 482 lua_rawgeti(L, 1, pos+1);
461 lua_rawseti(L, 1, pos); /* a[pos] = a[pos+1] */ 483 lua_rawseti(L, 1, pos); /* a[pos] = a[pos+1] */
462 } 484 }
463 lua_pushliteral(L, l_s("n"));
464 lua_pushnumber(L, n-1);
465 lua_rawset(L, 1); /* t.n = n-1 */
466 lua_pushnil(L); 485 lua_pushnil(L);
467 lua_rawseti(L, 1, n); /* t[n] = nil */ 486 lua_rawseti(L, 1, n); /* t[n] = nil */
468 return 1; 487 return 1;
@@ -678,6 +697,8 @@ static const luaL_reg base_funcs[] = {
678 {l_s("sort"), luaB_sort}, 697 {l_s("sort"), luaB_sort},
679 {l_s("tinsert"), luaB_tinsert}, 698 {l_s("tinsert"), luaB_tinsert},
680 {l_s("tremove"), luaB_tremove}, 699 {l_s("tremove"), luaB_tremove},
700 {l_s("pack"), luaB_pack},
701 {l_s("unpack"), luaB_unpack},
681 {l_s("xtype"), luaB_xtype}, 702 {l_s("xtype"), luaB_xtype},
682}; 703};
683 704