From 59f8e6fb7710e46539fb9bf63131b45012bf86ef Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 30 Dec 1998 15:26:49 -0200 Subject: [PATCH] details ("settable") --- lapi.c | 12 ++++------ lvm.c | 76 ++++++++++++++++++++++++++++++++-------------------------- lvm.h | 5 ++-- 3 files changed, 50 insertions(+), 43 deletions(-) diff --git a/lapi.c b/lapi.c index 65cd28b4..d5c16369 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.28 1998/09/07 18:59:59 roberto Exp roberto $ +** $Id: lapi.c,v 1.29 1998/12/03 15:45:15 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -179,17 +179,15 @@ lua_Object lua_rawgettable (void) } -void lua_settable (void) -{ +void lua_settable (void) { checkCparams(3); - luaV_settable(L->stack.top-3, 1); + luaV_settable(L->stack.top-3, 0); } -void lua_rawsettable (void) -{ +void lua_rawsettable (void) { checkCparams(3); - luaV_settable(L->stack.top-3, 0); + luaV_rawsettable(L->stack.top-3); } diff --git a/lvm.c b/lvm.c index 85e570b4..df78f909 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 1.34 1998/12/27 20:25:20 roberto Exp roberto $ +** $Id: lvm.c,v 1.35 1998/12/30 13:16:50 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -95,10 +95,8 @@ int luaV_tostring (TObject *obj) { void luaV_setn (Hash *t, int val) { TObject index, value; - ttype(&index) = LUA_T_STRING; - tsvalue(&index) = luaS_new("n"); - ttype(&value) = LUA_T_NUMBER; - nvalue(&value) = val; + ttype(&index) = LUA_T_STRING; tsvalue(&index) = luaS_new("n"); + ttype(&value) = LUA_T_NUMBER; nvalue(&value) = val; *(luaH_set(t, &index)) = value; } @@ -121,8 +119,7 @@ void luaV_closure (int nelems) ** Function to index a table. ** Receives the table at top-2 and the index at top-1. */ -void luaV_gettable (void) -{ +void luaV_gettable (void) { struct Stack *S = &L->stack; TObject *im; if (ttype(S->top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */ @@ -147,40 +144,51 @@ void luaV_gettable (void) /* else it has a "gettable" method, go through to next command */ } /* object is not a table, or it has a "gettable" method */ - if (ttype(im) != LUA_T_NIL) - luaD_callTM(im, 2, 1); - else + if (ttype(im) == LUA_T_NIL) lua_error("indexed expression not a table"); + luaD_callTM(im, 2, 1); } /* ** Function to store indexed based on values at the stack.top -** mode = 0: raw store (without tag methods) -** mode = 1: normal store (with tag methods) -** mode = 2: "deep L->stack.stack" store (with tag methods) +** deep = 1: "deep L->stack.stack" store (with tag methods) */ -void luaV_settable (TObject *t, int mode) -{ +void luaV_settable (TObject *t, int deep) { struct Stack *S = &L->stack; - TObject *im = (mode == 0) ? NULL : luaT_getimbyObj(t, IM_SETTABLE); - if (ttype(t) == LUA_T_ARRAY && (im == NULL || ttype(im) == LUA_T_NIL)) { - TObject *h = luaH_set(avalue(t), t+1); - *h = *(S->top-1); - S->top -= (mode == 2) ? 1 : 3; - } - else { /* object is not a table, and/or has a specific "settable" method */ - if (im && ttype(im) != LUA_T_NIL) { - if (mode == 2) { - *(S->top+1) = *(L->stack.top-1); - *(S->top) = *(t+1); - *(S->top-1) = *t; - S->top += 2; /* WARNING: caller must assure stack space */ - } - luaD_callTM(im, 3, 0); + TObject *im; + if (ttype(t) != LUA_T_ARRAY) /* not a table, get "settable" method */ + im = luaT_getimbyObj(t, IM_SETTABLE); + else { /* object is a table... */ + im = luaT_getim(avalue(t)->htag, IM_SETTABLE); + if (ttype(im) == LUA_T_NIL) { /* and does not have a "settable" method */ + *(luaH_set(avalue(t), t+1)) = *(S->top-1); + /* if deep, pop only value; otherwise, pop table, index and value */ + S->top -= (deep) ? 1 : 3; + return; } - else - lua_error("indexed expression not a table"); + /* else it has a "settable" method, go through to next command */ + } + /* object is not a table, or it has a "settable" method */ + if (ttype(im) == LUA_T_NIL) + lua_error("indexed expression not a table"); + if (deep) { /* table and index were not on top; copy them */ + *(S->top+1) = *(L->stack.top-1); + *(S->top) = *(t+1); + *(S->top-1) = *t; + S->top += 2; /* WARNING: caller must assure stack space */ + } + luaD_callTM(im, 3, 0); +} + + +void luaV_rawsettable (TObject *t) { + if (ttype(t) != LUA_T_ARRAY) + lua_error("indexed expression not a table"); + else { + struct Stack *S = &L->stack; + *(luaH_set(avalue(t), t+1)) = *(S->top-1); + S->top -= 3; } } @@ -462,11 +470,11 @@ StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base) break; case SETTABLE0: - luaV_settable(S->top-3, 1); + luaV_settable(S->top-3, 0); break; case SETTABLE: - luaV_settable(S->top-3-(*pc++), 2); + luaV_settable(S->top-3-(*pc++), 1); break; case SETLISTW: diff --git a/lvm.h b/lvm.h index 7c9b5a8c..ee962945 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 1.5 1998/07/12 16:16:43 roberto Exp roberto $ +** $Id: lvm.h,v 1.6 1998/12/30 13:16:50 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -22,7 +22,8 @@ int luaV_tonumber (TObject *obj); int luaV_tostring (TObject *obj); void luaV_setn (Hash *t, int val); void luaV_gettable (void); -void luaV_settable (TObject *t, int mode); +void luaV_settable (TObject *t, int deep); +void luaV_rawsettable (TObject *t); void luaV_getglobal (TaggedString *ts); void luaV_setglobal (TaggedString *ts); StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base);