From af59848219abcab589ac174c829102ed8a2adc8d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 31 Oct 2001 17:58:11 -0200 Subject: [PATCH] tables of globals accessible through pseudo-index in C API --- lapi.c | 29 ++++++++++++----------------- ldebug.c | 4 ++-- lgc.c | 4 ++-- lstate.c | 4 ++-- lstate.h | 4 ++-- lua.h | 13 +++++++------ lvm.c | 8 ++++---- 7 files changed, 31 insertions(+), 35 deletions(-) diff --git a/lapi.c b/lapi.c index c0212e79..36b77abe 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.157 2001/10/25 19:14:14 roberto Exp $ +** $Id: lapi.c,v 1.158 2001/10/31 19:40:14 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -44,13 +44,16 @@ static TObject *negindex (lua_State *L, int index) { if (index > LUA_REGISTRYINDEX) { api_check(L, index != 0 && -index <= L->top - L->ci->base); return L->top+index; - } else if (index == LUA_REGISTRYINDEX) /* pseudo-indices */ - return &G(L)->registry; - else { - TObject *func = (L->ci->base - 1); - index = LUA_REGISTRYINDEX - index; - api_check(L, iscfunction(func) && index <= clvalue(func)->c.nupvalues); - return &clvalue(func)->c.upvalue[index-1]; + } + else switch (index) { /* pseudo-indices */ + case LUA_REGISTRYINDEX: return &G(L)->registry; + case LUA_GLOBALSINDEX: return &L->gt; + default: { + TObject *func = (L->ci->base - 1); + index = LUA_GLOBALSINDEX - index; + api_check(L, iscfunction(func) && index <= clvalue(func)->c.nupvalues); + return &clvalue(func)->c.upvalue[index-1]; + } } } @@ -381,14 +384,6 @@ LUA_API void lua_rawgeti (lua_State *L, int index, int n) { } -LUA_API void lua_getglobals (lua_State *L) { - lua_lock(L); - sethvalue(L->top, L->gt); - api_incr_top(L); - lua_unlock(L); -} - - LUA_API void lua_newtable (lua_State *L) { lua_lock(L); sethvalue(L->top, luaH_new(L, 0, 0)); @@ -453,7 +448,7 @@ LUA_API void lua_setglobals (lua_State *L) { api_checknelems(L, 1); newtable = --L->top; api_check(L, ttype(newtable) == LUA_TTABLE); - L->gt = hvalue(newtable); + setobj(&L->gt, newtable); lua_unlock(L); } diff --git a/ldebug.c b/ldebug.c index 5adab2af..7b568e60 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 1.90 2001/10/02 16:45:03 roberto Exp $ +** $Id: ldebug.c,v 1.91 2001/10/25 19:14:14 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -221,7 +221,7 @@ static const l_char *travtagmethods (global_State *G, const TObject *o) { static const l_char *travglobals (lua_State *L, const TObject *o) { - Table *g = L->gt; + Table *g = hvalue(&L->gt); int i = sizenode(g); while (i--) { Node *n = node(g, i); diff --git a/lgc.c b/lgc.c index cf9148e9..abace987 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 1.113 2001/10/17 21:12:57 roberto Exp $ +** $Id: lgc.c,v 1.114 2001/10/25 19:14:14 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -112,7 +112,7 @@ static void markstacks (lua_State *L, GCState *st) { lua_State *L1 = L; do { /* for each thread */ StkId o, lim; - marktable(st, L1->gt); /* mark table of globals */ + markobject(st, &L1->gt); /* mark table of globals */ for (o=L1->stack; otop; o++) markobject(st, o); lim = (L1->stack_last - L1->ci->base > MAXSTACK) ? L1->ci->base+MAXSTACK diff --git a/lstate.c b/lstate.c index 1fe006ae..98190c22 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 1.69 2001/10/17 21:12:57 roberto Exp $ +** $Id: lstate.c,v 1.70 2001/10/25 19:14:14 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -64,7 +64,7 @@ static void f_luaopen (lua_State *L, void *ud) { G(L)->ntag = 0; G(L)->nblocks = sizeof(lua_State) + sizeof(global_State); luaD_init(L, so->stacksize); /* init stack */ - L->gt = luaH_new(L, 0, 4); /* table of globals */ + sethvalue(&L->gt, luaH_new(L, 0, 4)); /* table of globals */ G(L)->type2tag = luaH_new(L, 0, 3); sethvalue(&G(L)->registry, luaH_new(L, 0, 0)); luaS_resize(L, 4); /* initial size of string table */ diff --git a/lstate.h b/lstate.h index 4339d73d..4b4c60e7 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 1.61 2001/10/17 21:12:57 roberto Exp $ +** $Id: lstate.h,v 1.62 2001/10/25 19:12:21 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -80,7 +80,7 @@ struct lua_State { StkId top; /* first free slot in the stack */ CallInfo *ci; /* call info for current function */ StkId stack_last; /* last free slot in the stack */ - Table *gt; /* table for globals */ + TObject gt; /* table for globals */ global_State *G; StkId stack; /* stack base */ int stacksize; diff --git a/lua.h b/lua.h index 1691b07f..9cc3f081 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.105 2001/10/17 21:12:57 roberto Exp $ +** $Id: lua.h,v 1.106 2001/10/31 19:40:14 roberto Exp roberto $ ** Lua - An Extensible Extension Language ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil ** e-mail: info@lua.org @@ -30,11 +30,12 @@ #define LUA_MULTRET (-1) -/* pseudo-index for registry */ +/* +** pseudo-indices +*/ #define LUA_REGISTRYINDEX (-10000) - -/* pseudo-indices for upvalues */ -#define lua_upvalueindex(i) (LUA_REGISTRYINDEX-(i)) +#define LUA_GLOBALSINDEX (-10001) +#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i)) /* error codes for `lua_do*' and the like */ @@ -160,7 +161,6 @@ LUA_API void lua_getglobal (lua_State *L, const lua_char *name); LUA_API void lua_gettable (lua_State *L, int index); LUA_API void lua_rawget (lua_State *L, int index); LUA_API void lua_rawgeti (lua_State *L, int index, int n); -LUA_API void lua_getglobals (lua_State *L); LUA_API void lua_gettagmethod (lua_State *L, int tag, const lua_char *event); LUA_API void lua_newtable (lua_State *L); LUA_API void lua_getweakregistry (lua_State *L); @@ -244,6 +244,7 @@ LUA_API int lua_getweakmode (lua_State *L, int index); (sizeof(s)/sizeof(lua_char))-1) #define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX); +#define lua_getglobals(L) lua_pushvalue(L, LUA_GLOBALSINDEX); diff --git a/lvm.c b/lvm.c index 29877135..6f0d9ab7 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 1.195 2001/10/02 16:45:03 roberto Exp $ +** $Id: lvm.c,v 1.196 2001/10/25 19:14:14 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -176,7 +176,7 @@ void luaV_settable (lua_State *L, StkId t, TObject *key, StkId val) { void luaV_getglobal (lua_State *L, TString *name, StkId res) { - const TObject *value = luaH_getstr(L->gt, name); + const TObject *value = luaH_getstr(hvalue(&L->gt), name); Closure *tm; if (!HAS_TM_GETGLOBAL(L, ttype(value)) || /* is there a tag method? */ (tm = luaT_gettmbyObj(G(L), value, TM_GETGLOBAL)) == NULL) { @@ -188,12 +188,12 @@ void luaV_getglobal (lua_State *L, TString *name, StkId res) { void luaV_setglobal (lua_State *L, TString *name, StkId val) { - const TObject *oldvalue = luaH_getstr(L->gt, name); + const TObject *oldvalue = luaH_getstr(hvalue(&L->gt), name); Closure *tm; if (!HAS_TM_SETGLOBAL(L, ttype(oldvalue)) || /* no tag methods? */ (tm = luaT_gettmbyObj(G(L), oldvalue, TM_SETGLOBAL)) == NULL) { if (oldvalue == &luaO_nilobject) - luaH_setstr(L, L->gt, name, val); /* raw set */ + luaH_setstr(L, hvalue(&L->gt), name, val); /* raw set */ else settableval(oldvalue, val); /* warning: tricky optimization! */ }