summaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-12-15 09:25:36 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-12-15 09:25:36 -0200
commit9fbe0690fb0d8a64de9046a9c28d08f93e83af2a (patch)
treebe569bf9898b77457ad6cd15e0bcb50c9e4b459c /lapi.c
parenta25b8ff69c76515563c18dad715ee8daa8b847fd (diff)
downloadlua-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.c25
1 files 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 @@
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
42static 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
42static TValue *index2addr (lua_State *L, int idx) { 52static 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
74static 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