From b5eb4f3126b05b25678b080fbc5c99bced4b52c1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Jan 1999 11:14:24 -0200 Subject: [PATCH] small optimization in getglobal --- lvm.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/lvm.c b/lvm.c index 8f64f9f5..a5b71a5a 100644 --- a/lvm.c +++ b/lvm.c @@ -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 ** See Copyright Notice in lua.h */ @@ -186,30 +186,32 @@ void luaV_rawsettable (TObject *t) { } -void luaV_getglobal (TaggedString *ts) -{ - /* WARNING: caller must assure stack space */ +void luaV_getglobal (TaggedString *ts) { + /* only userdata, tables and nil can have getglobal tag methods */ + static char valid_getglobals[] = {1, 0, 0, 1, 0, 0, 1, 0}; /* ORDER LUA_T */ TObject *value = &ts->u.s.globalval; - TObject *im = luaT_getimbyObj(value, IM_GETGLOBAL); - if (ttype(im) == LUA_T_NIL) { /* default behavior */ - *L->stack.top++ = *value; - } - else { - struct Stack *S = &L->stack; - ttype(S->top) = LUA_T_STRING; - tsvalue(S->top) = ts; - S->top++; - *S->top++ = *value; - luaD_callTM(im, 2, 1); + if (valid_getglobals[-ttype(value)]) { + TObject *im = luaT_getimbyObj(value, IM_GETGLOBAL); + if (ttype(im) != LUA_T_NIL) { /* is there a tag method? */ + /* WARNING: caller must assure stack space */ + struct Stack *S = &L->stack; + ttype(S->top) = LUA_T_STRING; + tsvalue(S->top) = ts; + S->top++; + *S->top++ = *value; + 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 *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); else { /* WARNING: caller must assure stack space */