`luaV_gettable' returns element position

This commit is contained in:
Roberto Ierusalimschy 2002-06-24 10:08:45 -03:00
parent 3941af53ad
commit e34f282365
4 changed files with 38 additions and 38 deletions

6
lapi.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lapi.c,v 1.200 2002/06/18 15:19:27 roberto Exp roberto $ ** $Id: lapi.c,v 1.201 2002/06/20 20:41:46 roberto Exp roberto $
** Lua API ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -423,9 +423,11 @@ LUA_API void lua_pushudataval (lua_State *L, void *p) {
LUA_API void lua_gettable (lua_State *L, int index) { LUA_API void lua_gettable (lua_State *L, int index) {
StkId t; StkId t;
const TObject *v;
lua_lock(L); lua_lock(L);
t = luaA_index(L, index); t = luaA_index(L, index);
luaV_gettable(L, t, L->top-1, L->top-1); v = luaV_gettable(L, t, L->top-1);
setobj(L->top - 1, v);
lua_unlock(L); lua_unlock(L);
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.h,v 1.135 2002/06/13 13:39:55 roberto Exp roberto $ ** $Id: lobject.h,v 1.136 2002/06/20 20:41:46 roberto Exp roberto $
** Type definitions for Lua objects ** Type definitions for Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -74,7 +74,7 @@ typedef struct lua_TObject {
#define setnilvalue(obj) ((obj)->tt=LUA_TNIL) #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
#define setobj(obj1,obj2) \ #define setobj(obj1,obj2) \
{ TObject *o1=(obj1); const TObject *o2=(obj2); \ { const TObject *o2=(obj2); TObject *o1=(obj1); \
o1->tt=o2->tt; o1->value = o2->value; } o1->tt=o2->tt; o1->value = o2->value; }
#define setttype(obj, tt) (ttype(obj) = (tt)) #define setttype(obj, tt) (ttype(obj) = (tt))

54
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 1.239 2002/06/14 17:21:32 roberto Exp roberto $ ** $Id: lvm.c,v 1.240 2002/06/20 20:41:46 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -84,16 +84,14 @@ static void traceexec (lua_State *L) {
static void callTMres (lua_State *L, const TObject *f, static void callTMres (lua_State *L, const TObject *f,
const TObject *p1, const TObject *p2, TObject *result ) { const TObject *p1, const TObject *p2) {
ptrdiff_t res = savestack(L, result);
setobj(L->top, f); /* push function */ setobj(L->top, f); /* push function */
setobj(L->top+1, p1); /* 1st argument */ setobj(L->top+1, p1); /* 1st argument */
setobj(L->top+2, p2); /* 2nd argument */ setobj(L->top+2, p2); /* 2nd argument */
luaD_checkstack(L, 3); /* cannot check before (could invalidate p1, p2) */ luaD_checkstack(L, 3); /* cannot check before (could invalidate p1, p2) */
L->top += 3; L->top += 3;
luaD_call(L, L->top - 3, 1); luaD_call(L, L->top - 3, 1);
result = restorestack(L, res); /* previous call may change stack */ L->top--; /* result will be in L->top */
setobj(result, --L->top); /* get result */
} }
@ -115,32 +113,29 @@ static void callTM (lua_State *L, const TObject *f,
** Receives the table at `t' and the key at `key'. ** Receives the table at `t' and the key at `key'.
** leaves the result at `res'. ** leaves the result at `res'.
*/ */
void luaV_gettable (lua_State *L, const TObject *t, TObject *key, StkId res) { const TObject *luaV_gettable (lua_State *L, const TObject *t, TObject *key) {
const TObject *tm; const TObject *tm;
int loop = 0; int loop = 0;
do { do {
if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */ if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */
Table *h = hvalue(t); Table *h = hvalue(t);
Table *et = h->metatable;
if ((tm = fasttm(L, et, TM_GETTABLE)) == NULL) { /* no gettable TM? */
const TObject *v = luaH_get(h, key); /* do a primitive get */ const TObject *v = luaH_get(h, key); /* do a primitive get */
if (ttype(v) != LUA_TNIL || /* result is no nil ... */ if (ttype(v) != LUA_TNIL || /* result is no nil? */
(tm = fasttm(L, et, TM_INDEX)) == NULL) { /* ... or no index TM? */ (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
setobj(res, v); /* default get */ return v;
return;
}
} }
/* else will try the tag method */ /* else will try the tag method */
} }
else if (ttype(tm = luaT_gettmbyobj(L, t, TM_GETTABLE)) == LUA_TNIL) else if (ttype(tm = luaT_gettmbyobj(L, t, TM_GETTABLE)) == LUA_TNIL)
luaG_typeerror(L, t, "index"); luaG_typeerror(L, t, "index");
if (ttype(tm) == LUA_TFUNCTION) { if (ttype(tm) == LUA_TFUNCTION) {
callTMres(L, tm, t, key, res); callTMres(L, tm, t, key);
return; return L->top;
} }
t = tm; /* else repeat access with `tm' */ t = tm; /* else repeat access with `tm' */
} while (++loop <= MAXTAGLOOP); } while (++loop <= MAXTAGLOOP);
luaG_runerror(L, "loop in gettable"); luaG_runerror(L, "loop in gettable");
return NULL; /* to avoid warnings */
} }
@ -153,15 +148,12 @@ void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val) {
do { do {
if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */ if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */
Table *h = hvalue(t); Table *h = hvalue(t);
Table *et = h->metatable;
if ((tm = fasttm(L, et, TM_SETTABLE)) == NULL) { /* no settable TM? */
TObject *oldval = luaH_set(L, h, key); /* do a primitive set */ TObject *oldval = luaH_set(L, h, key); /* do a primitive set */
if (ttype(oldval) != LUA_TNIL || /* result is no nil ... */ if (ttype(oldval) != LUA_TNIL || /* result is no nil? */
(tm = fasttm(L, et, TM_NEWINDEX)) == NULL) { /* ... or no TM? */ (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
setobj(oldval, val); setobj(oldval, val);
return; return;
} }
}
/* else will try the tag method */ /* else will try the tag method */
} }
else if (ttype(tm = luaT_gettmbyobj(L, t, TM_SETTABLE)) == LUA_TNIL) else if (ttype(tm = luaT_gettmbyobj(L, t, TM_SETTABLE)) == LUA_TNIL)
@ -178,11 +170,14 @@ void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val) {
static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2, static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2,
TObject *res, TMS event) { TObject *res, TMS event) {
ptrdiff_t result = savestack(L, res);
const TObject *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ const TObject *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
if (ttype(tm) == LUA_TNIL) if (ttype(tm) == LUA_TNIL)
tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
if (ttype(tm) != LUA_TFUNCTION) return 0; if (ttype(tm) != LUA_TFUNCTION) return 0;
callTMres(L, tm, p1, p2, res); callTMres(L, tm, p1, p2);
res = restorestack(L, result); /* previous call may change stack */
setobj(res, L->top);
return 1; return 1;
} }
@ -267,7 +262,7 @@ int luaV_equalval (lua_State *L, const TObject *t1, const TObject *t2) {
return 0; /* no TM */ return 0; /* no TM */
else break; /* will try TM */ else break; /* will try TM */
} }
callTMres(L, tm, t1, t2, L->top); /* call TM */ callTMres(L, tm, t1, t2); /* call TM */
return !l_isfalse(L->top); return !l_isfalse(L->top);
} }
@ -308,14 +303,17 @@ void luaV_concat (lua_State *L, int total, int last) {
static void powOp (lua_State *L, StkId ra, StkId rb, StkId rc) { static void powOp (lua_State *L, StkId ra, StkId rb, StkId rc) {
const TObject *b = rb; const TObject *b = rb;
const TObject *c = rc; const TObject *c = rc;
ptrdiff_t res = savestack(L, ra);
TObject tempb, tempc; TObject tempb, tempc;
if (tonumber(b, &tempb) && tonumber(c, &tempc)) { if (tonumber(b, &tempb) && tonumber(c, &tempc)) {
TObject f, o; TObject f, o;
setsvalue(&o, luaS_newliteral(L, "pow")); setsvalue(&o, luaS_newliteral(L, "pow"));
luaV_gettable(L, gt(L), &o, &f); f = *luaV_gettable(L, gt(L), &o);
if (ttype(&f) != LUA_TFUNCTION) if (ttype(&f) != LUA_TFUNCTION)
luaG_runerror(L, "`pow' (for `^' operator) is not a function"); luaG_runerror(L, "`pow' (for `^' operator) is not a function");
callTMres(L, &f, b, c, ra); callTMres(L, &f, b, c);
ra = restorestack(L, res); /* previous call may change stack */
setobj(ra, L->top);
} }
else else
call_arith(L, rb, rc, ra, TM_POW); call_arith(L, rb, rc, ra, TM_POW);
@ -402,11 +400,11 @@ StkId luaV_execute (lua_State *L) {
} }
case OP_GETGLOBAL: { case OP_GETGLOBAL: {
lua_assert(ttype(KBx(i)) == LUA_TSTRING && ttype(&cl->g) == LUA_TTABLE); lua_assert(ttype(KBx(i)) == LUA_TSTRING && ttype(&cl->g) == LUA_TTABLE);
luaV_gettable(L, &cl->g, KBx(i), ra); setobj(RA(i), luaV_gettable(L, &cl->g, KBx(i)));
break; break;
} }
case OP_GETTABLE: { case OP_GETTABLE: {
luaV_gettable(L, RB(i), RKC(i), ra); setobj(RA(i), luaV_gettable(L, RB(i), RKC(i)));
break; break;
} }
case OP_SETGLOBAL: { case OP_SETGLOBAL: {
@ -433,7 +431,7 @@ StkId luaV_execute (lua_State *L) {
case OP_SELF: { case OP_SELF: {
StkId rb = RB(i); StkId rb = RB(i);
setobj(ra+1, rb); setobj(ra+1, rb);
luaV_gettable(L, rb, RKC(i), ra); setobj(RA(i), luaV_gettable(L, rb, RKC(i)));
break; break;
} }
case OP_ADD: { case OP_ADD: {
@ -608,7 +606,7 @@ StkId luaV_execute (lua_State *L) {
if (ttype(ra) == LUA_TTABLE) { if (ttype(ra) == LUA_TTABLE) {
setobj(ra+1, ra); setobj(ra+1, ra);
setsvalue(ra, luaS_new(L, "next")); setsvalue(ra, luaS_new(L, "next"));
luaV_gettable(L, gt(L), ra, ra); setobj(RA(i), luaV_gettable(L, gt(L), ra));
} }
dojump(pc, GETARG_sBx(i)); dojump(pc, GETARG_sBx(i));
break; break;

4
lvm.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.h,v 1.41 2002/06/12 14:51:31 roberto Exp roberto $ ** $Id: lvm.h,v 1.42 2002/06/13 13:39:55 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -26,7 +26,7 @@ int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r);
int luaV_equalval (lua_State *L, const TObject *t1, const TObject *t2); int luaV_equalval (lua_State *L, const TObject *t1, const TObject *t2);
const TObject *luaV_tonumber (const TObject *obj, TObject *n); const TObject *luaV_tonumber (const TObject *obj, TObject *n);
int luaV_tostring (lua_State *L, TObject *obj); int luaV_tostring (lua_State *L, TObject *obj);
void luaV_gettable (lua_State *L, const TObject *t, TObject *key, StkId res); const TObject *luaV_gettable (lua_State *L, const TObject *t, TObject *key);
void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val); void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val);
StkId luaV_execute (lua_State *L); StkId luaV_execute (lua_State *L);
void luaV_concat (lua_State *L, int total, int last); void luaV_concat (lua_State *L, int total, int last);