From fe5c41fb8a0e1d4f130437b22667bf699851b17a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 30 Dec 1998 11:16:50 -0200 Subject: [PATCH] new functions "tinsert" and "tremove" --- lbuiltin.c | 35 ++++++++++++++++++++++++++++++++++- lvm.c | 24 +++++++++++++----------- lvm.h | 3 ++- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/lbuiltin.c b/lbuiltin.c index 7fc5a647..7b67b9fc 100644 --- a/lbuiltin.c +++ b/lbuiltin.c @@ -387,6 +387,37 @@ static void luaB_foreachvar (void) { } +static void luaB_tinsert (void) { + Hash *a = gethash(1); + lua_Object v = lua_getparam(3); + int n = (int)getnarg(a); + int pos; + if (v != LUA_NOOBJECT) + pos = luaL_check_int(2); + else { /* called with only 2 arguments */ + v = luaL_nonnullarg(2); + pos = n+1; + } + luaV_setn(a, n+1); /* increment field "n" */ + for ( ;n>=pos; n--) + tablemove(a, n, n+1); + luaH_setint(a, pos, luaA_Address(v)); +} + + +static void luaB_tremove (void) { + Hash *a = gethash(1); + int n = (int)getnarg(a); + int pos = luaL_opt_int(2, n); + TObject v = *luaH_getint(a, pos); + if (n <= 0) return; /* table is "empty" */ + luaV_setn(a, n-1); /* decrement field "n" */ + for ( ;pos 0) { @@ -275,8 +285,7 @@ void luaV_comparison (lua_Type ttype_less, lua_Type ttype_equal, } -void luaV_pack (StkId firstel, int nvararg, TObject *tab) -{ +void luaV_pack (StkId firstel, int nvararg, TObject *tab) { TObject *firstelem = L->stack.stack+firstel; int i; Hash *htab; @@ -285,14 +294,7 @@ void luaV_pack (StkId firstel, int nvararg, TObject *tab) ttype(tab) = LUA_T_ARRAY; for (i=0; i