aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
Diffstat (limited to 'lapi.c')
-rw-r--r--lapi.c25
1 files changed, 17 insertions, 8 deletions
diff --git a/lapi.c b/lapi.c
index c824da27..9fffcc16 100644
--- a/lapi.c
+++ b/lapi.c
@@ -629,11 +629,21 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
629} 629}
630 630
631 631
632/*
633** Get the global table in the registry. Since all predefined
634** indices in the registry were inserted right when the registry
635** was created and never removed, they must always be in the array
636** part of the registry.
637*/
638#define getGtable(L) \
639 (&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1])
640
641
632LUA_API int lua_getglobal (lua_State *L, const char *name) { 642LUA_API int lua_getglobal (lua_State *L, const char *name) {
633 Table *reg; 643 const TValue *G;
634 lua_lock(L); 644 lua_lock(L);
635 reg = hvalue(&G(L)->l_registry); 645 G = getGtable(L);
636 return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); 646 return auxgetstr(L, G, name);
637} 647}
638 648
639 649
@@ -811,10 +821,10 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
811 821
812 822
813LUA_API void lua_setglobal (lua_State *L, const char *name) { 823LUA_API void lua_setglobal (lua_State *L, const char *name) {
814 Table *reg; 824 const TValue *G;
815 lua_lock(L); /* unlock done in 'auxsetstr' */ 825 lua_lock(L); /* unlock done in 'auxsetstr' */
816 reg = hvalue(&G(L)->l_registry); 826 G = getGtable(L);
817 auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); 827 auxsetstr(L, G, name);
818} 828}
819 829
820 830
@@ -1063,8 +1073,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
1063 LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */ 1073 LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */
1064 if (f->nupvalues >= 1) { /* does it have an upvalue? */ 1074 if (f->nupvalues >= 1) { /* does it have an upvalue? */
1065 /* get global table from registry */ 1075 /* get global table from registry */
1066 Table *reg = hvalue(&G(L)->l_registry); 1076 const TValue *gt = getGtable(L);
1067 const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
1068 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ 1077 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
1069 setobj(L, f->upvals[0]->v, gt); 1078 setobj(L, f->upvals[0]->v, gt);
1070 luaC_barrier(L, f->upvals[0], gt); 1079 luaC_barrier(L, f->upvals[0], gt);