tables of globals accessible through pseudo-index in C API

This commit is contained in:
Roberto Ierusalimschy 2001-10-31 17:58:11 -02:00
parent 46347d768e
commit af59848219
7 changed files with 31 additions and 35 deletions

29
lapi.c
View File

@ -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 ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -44,13 +44,16 @@ static TObject *negindex (lua_State *L, int index) {
if (index > LUA_REGISTRYINDEX) { if (index > LUA_REGISTRYINDEX) {
api_check(L, index != 0 && -index <= L->top - L->ci->base); api_check(L, index != 0 && -index <= L->top - L->ci->base);
return L->top+index; return L->top+index;
} else if (index == LUA_REGISTRYINDEX) /* pseudo-indices */ }
return &G(L)->registry; else switch (index) { /* pseudo-indices */
else { case LUA_REGISTRYINDEX: return &G(L)->registry;
TObject *func = (L->ci->base - 1); case LUA_GLOBALSINDEX: return &L->gt;
index = LUA_REGISTRYINDEX - index; default: {
api_check(L, iscfunction(func) && index <= clvalue(func)->c.nupvalues); TObject *func = (L->ci->base - 1);
return &clvalue(func)->c.upvalue[index-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_API void lua_newtable (lua_State *L) {
lua_lock(L); lua_lock(L);
sethvalue(L->top, luaH_new(L, 0, 0)); sethvalue(L->top, luaH_new(L, 0, 0));
@ -453,7 +448,7 @@ LUA_API void lua_setglobals (lua_State *L) {
api_checknelems(L, 1); api_checknelems(L, 1);
newtable = --L->top; newtable = --L->top;
api_check(L, ttype(newtable) == LUA_TTABLE); api_check(L, ttype(newtable) == LUA_TTABLE);
L->gt = hvalue(newtable); setobj(&L->gt, newtable);
lua_unlock(L); lua_unlock(L);
} }

View File

@ -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 ** Debug Interface
** See Copyright Notice in lua.h ** 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) { static const l_char *travglobals (lua_State *L, const TObject *o) {
Table *g = L->gt; Table *g = hvalue(&L->gt);
int i = sizenode(g); int i = sizenode(g);
while (i--) { while (i--) {
Node *n = node(g, i); Node *n = node(g, i);

4
lgc.c
View File

@ -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 ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -112,7 +112,7 @@ static void markstacks (lua_State *L, GCState *st) {
lua_State *L1 = L; lua_State *L1 = L;
do { /* for each thread */ do { /* for each thread */
StkId o, lim; StkId o, lim;
marktable(st, L1->gt); /* mark table of globals */ markobject(st, &L1->gt); /* mark table of globals */
for (o=L1->stack; o<L1->top; o++) for (o=L1->stack; o<L1->top; o++)
markobject(st, o); markobject(st, o);
lim = (L1->stack_last - L1->ci->base > MAXSTACK) ? L1->ci->base+MAXSTACK lim = (L1->stack_last - L1->ci->base > MAXSTACK) ? L1->ci->base+MAXSTACK

View File

@ -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 ** Global State
** See Copyright Notice in lua.h ** 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)->ntag = 0;
G(L)->nblocks = sizeof(lua_State) + sizeof(global_State); G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
luaD_init(L, so->stacksize); /* init stack */ 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); G(L)->type2tag = luaH_new(L, 0, 3);
sethvalue(&G(L)->registry, luaH_new(L, 0, 0)); sethvalue(&G(L)->registry, luaH_new(L, 0, 0));
luaS_resize(L, 4); /* initial size of string table */ luaS_resize(L, 4); /* initial size of string table */

View File

@ -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 ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -80,7 +80,7 @@ struct lua_State {
StkId top; /* first free slot in the stack */ StkId top; /* first free slot in the stack */
CallInfo *ci; /* call info for current function */ CallInfo *ci; /* call info for current function */
StkId stack_last; /* last free slot in the stack */ StkId stack_last; /* last free slot in the stack */
Table *gt; /* table for globals */ TObject gt; /* table for globals */
global_State *G; global_State *G;
StkId stack; /* stack base */ StkId stack; /* stack base */
int stacksize; int stacksize;

13
lua.h
View File

@ -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 ** Lua - An Extensible Extension Language
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
** e-mail: info@lua.org ** e-mail: info@lua.org
@ -30,11 +30,12 @@
#define LUA_MULTRET (-1) #define LUA_MULTRET (-1)
/* pseudo-index for registry */ /*
** pseudo-indices
*/
#define LUA_REGISTRYINDEX (-10000) #define LUA_REGISTRYINDEX (-10000)
#define LUA_GLOBALSINDEX (-10001)
/* pseudo-indices for upvalues */ #define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i))
#define lua_upvalueindex(i) (LUA_REGISTRYINDEX-(i))
/* error codes for `lua_do*' and the like */ /* 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_gettable (lua_State *L, int index);
LUA_API void lua_rawget (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_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_gettagmethod (lua_State *L, int tag, const lua_char *event);
LUA_API void lua_newtable (lua_State *L); LUA_API void lua_newtable (lua_State *L);
LUA_API void lua_getweakregistry (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) (sizeof(s)/sizeof(lua_char))-1)
#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX); #define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX);
#define lua_getglobals(L) lua_pushvalue(L, LUA_GLOBALSINDEX);

8
lvm.c
View File

@ -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 ** Lua virtual machine
** See Copyright Notice in lua.h ** 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) { 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; Closure *tm;
if (!HAS_TM_GETGLOBAL(L, ttype(value)) || /* is there a tag method? */ if (!HAS_TM_GETGLOBAL(L, ttype(value)) || /* is there a tag method? */
(tm = luaT_gettmbyObj(G(L), value, TM_GETGLOBAL)) == NULL) { (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) { 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; Closure *tm;
if (!HAS_TM_SETGLOBAL(L, ttype(oldvalue)) || /* no tag methods? */ if (!HAS_TM_SETGLOBAL(L, ttype(oldvalue)) || /* no tag methods? */
(tm = luaT_gettmbyObj(G(L), oldvalue, TM_SETGLOBAL)) == NULL) { (tm = luaT_gettmbyObj(G(L), oldvalue, TM_SETGLOBAL)) == NULL) {
if (oldvalue == &luaO_nilobject) if (oldvalue == &luaO_nilobject)
luaH_setstr(L, L->gt, name, val); /* raw set */ luaH_setstr(L, hvalue(&L->gt), name, val); /* raw set */
else else
settableval(oldvalue, val); /* warning: tricky optimization! */ settableval(oldvalue, val); /* warning: tricky optimization! */
} }