diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-12-15 09:25:36 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-12-15 09:25:36 -0200 |
commit | 9fbe0690fb0d8a64de9046a9c28d08f93e83af2a (patch) | |
tree | be569bf9898b77457ad6cd15e0bcb50c9e4b459c /lapi.c | |
parent | a25b8ff69c76515563c18dad715ee8daa8b847fd (diff) | |
download | lua-9fbe0690fb0d8a64de9046a9c28d08f93e83af2a.tar.gz lua-9fbe0690fb0d8a64de9046a9c28d08f93e83af2a.tar.bz2 lua-9fbe0690fb0d8a64de9046a9c28d08f93e83af2a.zip |
base-level C use global table as its environment
Diffstat (limited to 'lapi.c')
-rw-r--r-- | lapi.c | 25 |
1 files changed, 12 insertions, 13 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 2.102 2009/12/07 15:49:47 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.103 2009/12/08 16:15:43 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -39,6 +39,16 @@ const char lua_ident[] = | |||
39 | "invalid index") | 39 | "invalid index") |
40 | 40 | ||
41 | 41 | ||
42 | static Table *getcurrenv (lua_State *L) { | ||
43 | if (L->ci->previous == NULL) /* no enclosing function? */ | ||
44 | return hvalue(&G(L)->l_gt); /* use global table as environment */ | ||
45 | else { | ||
46 | Closure *func = curr_func(L); | ||
47 | return func->c.env; | ||
48 | } | ||
49 | } | ||
50 | |||
51 | |||
42 | static TValue *index2addr (lua_State *L, int idx) { | 52 | static TValue *index2addr (lua_State *L, int idx) { |
43 | CallInfo *ci = L->ci; | 53 | CallInfo *ci = L->ci; |
44 | if (idx > 0) { | 54 | if (idx > 0) { |
@@ -54,8 +64,7 @@ static TValue *index2addr (lua_State *L, int idx) { | |||
54 | else switch (idx) { /* pseudo-indices */ | 64 | else switch (idx) { /* pseudo-indices */ |
55 | case LUA_REGISTRYINDEX: return &G(L)->l_registry; | 65 | case LUA_REGISTRYINDEX: return &G(L)->l_registry; |
56 | case LUA_ENVIRONINDEX: { | 66 | case LUA_ENVIRONINDEX: { |
57 | Closure *func = curr_func(L); | 67 | sethvalue(L, &L->env, getcurrenv(L)); |
58 | sethvalue(L, &L->env, func->c.env); | ||
59 | return &L->env; | 68 | return &L->env; |
60 | } | 69 | } |
61 | case LUA_GLOBALSINDEX: return &G(L)->l_gt; | 70 | case LUA_GLOBALSINDEX: return &G(L)->l_gt; |
@@ -71,16 +80,6 @@ static TValue *index2addr (lua_State *L, int idx) { | |||
71 | } | 80 | } |
72 | 81 | ||
73 | 82 | ||
74 | static Table *getcurrenv (lua_State *L) { | ||
75 | if (L->ci->previous == NULL) /* no enclosing function? */ | ||
76 | return hvalue(&G(L)->l_gt); /* use global table as environment */ | ||
77 | else { | ||
78 | Closure *func = curr_func(L); | ||
79 | return func->c.env; | ||
80 | } | ||
81 | } | ||
82 | |||
83 | |||
84 | /* | 83 | /* |
85 | ** to be caled by 'lua_checkstack' in protected mode, to grow stack | 84 | ** to be caled by 'lua_checkstack' in protected mode, to grow stack |
86 | ** capturing memory errors | 85 | ** capturing memory errors |