From 9fbe0690fb0d8a64de9046a9c28d08f93e83af2a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 15 Dec 2009 09:25:36 -0200 Subject: [PATCH] base-level C use global table as its environment --- lapi.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/lapi.c b/lapi.c index 96729070..b26efbc0 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.102 2009/12/07 15:49:47 roberto Exp roberto $ +** $Id: lapi.c,v 2.103 2009/12/08 16:15:43 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -39,6 +39,16 @@ const char lua_ident[] = "invalid index") +static Table *getcurrenv (lua_State *L) { + if (L->ci->previous == NULL) /* no enclosing function? */ + return hvalue(&G(L)->l_gt); /* use global table as environment */ + else { + Closure *func = curr_func(L); + return func->c.env; + } +} + + static TValue *index2addr (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { @@ -54,8 +64,7 @@ static TValue *index2addr (lua_State *L, int idx) { else switch (idx) { /* pseudo-indices */ case LUA_REGISTRYINDEX: return &G(L)->l_registry; case LUA_ENVIRONINDEX: { - Closure *func = curr_func(L); - sethvalue(L, &L->env, func->c.env); + sethvalue(L, &L->env, getcurrenv(L)); return &L->env; } case LUA_GLOBALSINDEX: return &G(L)->l_gt; @@ -71,16 +80,6 @@ static TValue *index2addr (lua_State *L, int idx) { } -static Table *getcurrenv (lua_State *L) { - if (L->ci->previous == NULL) /* no enclosing function? */ - return hvalue(&G(L)->l_gt); /* use global table as environment */ - else { - Closure *func = curr_func(L); - return func->c.env; - } -} - - /* ** to be caled by 'lua_checkstack' in protected mode, to grow stack ** capturing memory errors