small optimization in getglobal

This commit is contained in:
Roberto Ierusalimschy 1999-01-15 11:14:24 -02:00
parent 3fecf187ff
commit b5eb4f3126
1 changed files with 20 additions and 18 deletions

38
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 1.37 1999/01/12 18:38:35 roberto Exp roberto $ ** $Id: lvm.c,v 1.38 1999/01/13 19:09:04 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -186,30 +186,32 @@ void luaV_rawsettable (TObject *t) {
} }
void luaV_getglobal (TaggedString *ts) void luaV_getglobal (TaggedString *ts) {
{ /* only userdata, tables and nil can have getglobal tag methods */
/* WARNING: caller must assure stack space */ static char valid_getglobals[] = {1, 0, 0, 1, 0, 0, 1, 0}; /* ORDER LUA_T */
TObject *value = &ts->u.s.globalval; TObject *value = &ts->u.s.globalval;
TObject *im = luaT_getimbyObj(value, IM_GETGLOBAL); if (valid_getglobals[-ttype(value)]) {
if (ttype(im) == LUA_T_NIL) { /* default behavior */ TObject *im = luaT_getimbyObj(value, IM_GETGLOBAL);
*L->stack.top++ = *value; if (ttype(im) != LUA_T_NIL) { /* is there a tag method? */
} /* WARNING: caller must assure stack space */
else { struct Stack *S = &L->stack;
struct Stack *S = &L->stack; ttype(S->top) = LUA_T_STRING;
ttype(S->top) = LUA_T_STRING; tsvalue(S->top) = ts;
tsvalue(S->top) = ts; S->top++;
S->top++; *S->top++ = *value;
*S->top++ = *value; luaD_callTM(im, 2, 1);
luaD_callTM(im, 2, 1); return;
}
/* else no tag method: go through to default behavior */
} }
*L->stack.top++ = *value; /* default behavior */
} }
void luaV_setglobal (TaggedString *ts) void luaV_setglobal (TaggedString *ts) {
{
TObject *oldvalue = &ts->u.s.globalval; TObject *oldvalue = &ts->u.s.globalval;
TObject *im = luaT_getimbyObj(oldvalue, IM_SETGLOBAL); TObject *im = luaT_getimbyObj(oldvalue, IM_SETGLOBAL);
if (ttype(im) == LUA_T_NIL) /* default behavior */ if (ttype(im) == LUA_T_NIL) /* is there a tag method? */
luaS_rawsetglobal(ts, --L->stack.top); luaS_rawsetglobal(ts, --L->stack.top);
else { else {
/* WARNING: caller must assure stack space */ /* WARNING: caller must assure stack space */