From 2b2267069bcd2f214fa47786364653f5522d9529 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Mar 2001 09:43:52 -0300 Subject: [PATCH] new functions `pack' and `unpack' --- lbaselib.c | 35 ++++++++++++++++++++++++++++------- 1 file 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 @@ /* -** $Id: lbaselib.c,v 1.28 2001/02/23 17:28:12 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.29 2001/03/06 20:09:38 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -19,6 +19,13 @@ +static void aux_setn (lua_State *L, int t, int n) { + lua_pushliteral(L, l_s("n")); + lua_pushnumber(L, n); + lua_settable(L, t); +} + + /* ** If your system does not support `stderr', redefine this function, or ** redefine _ERRORMESSAGE so that it won't need _ALERT. @@ -296,6 +303,17 @@ static int luaB_dofile (lua_State *L) { } +static int luaB_pack (lua_State *L) { + int n = lua_gettop(L); + lua_newtable(L); + aux_setn(L, -3, n); + lua_insert(L, 1); + while (n) + lua_rawseti(L, 1, n--); + return 1; +} + + static int aux_unpack (lua_State *L, int arg) { int n, i; luaL_checktype(L, arg, LUA_TTABLE); @@ -307,6 +325,11 @@ static int aux_unpack (lua_State *L, int arg) { } +static int luaB_unpack (lua_State *L) { + return aux_unpack(L, 1); +} + + static int luaB_call (lua_State *L) { int oldtop; const l_char *options = luaL_opt_string(L, 3, l_s("")); @@ -436,9 +459,7 @@ static int luaB_tinsert (lua_State *L) { pos = n+1; else pos = luaL_check_int(L, 2); /* 2nd argument is the position */ - lua_pushliteral(L, l_s("n")); - lua_pushnumber(L, n+1); - lua_rawset(L, 1); /* t.n = n+1 */ + aux_setn(L, 1, n+1); /* t.n = n+1 */ for (; n>=pos; n--) { lua_rawgeti(L, 1, n); lua_rawseti(L, 1, n+1); /* t[n+1] = t[n] */ @@ -455,14 +476,12 @@ static int luaB_tremove (lua_State *L) { n = lua_getn(L, 1); pos = luaL_opt_int(L, 2, n); if (n <= 0) return 0; /* table is `empty' */ + aux_setn(L, 1, n-1); /* t.n = n-1 */ lua_rawgeti(L, 1, pos); /* result = t[pos] */ for ( ;pos