aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-08-06 15:54:18 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-08-06 15:54:18 -0300
commit4664f2e9270a956f6175481abe590ba4931b7477 (patch)
treeb697cf966fada0f46d7779ad620036d9d27e4127
parent2e38c6ae5a53cbf1a607a790460fca45ba3b9ca8 (diff)
downloadlua-4664f2e9270a956f6175481abe590ba4931b7477.tar.gz
lua-4664f2e9270a956f6175481abe590ba4931b7477.tar.bz2
lua-4664f2e9270a956f6175481abe590ba4931b7477.zip
any Lua closure has a table of globals (not only active functions)
-rw-r--r--lapi.c37
-rw-r--r--lbaselib.c31
-rw-r--r--lua.h6
3 files changed, 38 insertions, 36 deletions
diff --git a/lapi.c b/lapi.c
index 49ee0864..f66ec097 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.207 2002/08/06 15:32:22 roberto Exp roberto $ 2** $Id: lapi.c,v 1.208 2002/08/06 17:06:56 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*/
@@ -506,24 +506,11 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) {
506} 506}
507 507
508 508
509static LClosure *getfunc (lua_State *L, int level) { 509LUA_API void lua_getglobals (lua_State *L, int index) {
510 CallInfo *ci; 510 StkId o;
511 TObject *f;
512 if (L->ci - L->base_ci < level) ci = L->base_ci;
513 else ci = L->ci - level;
514 f = ci->base - 1;
515 if (isLfunction(f))
516 return &clvalue(f)->l;
517 else
518 return NULL;
519}
520
521
522LUA_API void lua_getglobals (lua_State *L, int level) {
523 LClosure *f;
524 lua_lock(L); 511 lua_lock(L);
525 f = getfunc(L, level); 512 o = luaA_index(L, index);
526 setobj(L->top, (f ? &f->g : gt(L))); 513 setobj(L->top, isLfunction(o) ? &clvalue(o)->l.g : gt(L));
527 api_incr_top(L); 514 api_incr_top(L);
528 lua_unlock(L); 515 lua_unlock(L);
529} 516}
@@ -608,16 +595,20 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
608} 595}
609 596
610 597
611LUA_API int lua_setglobals (lua_State *L, int level) { 598LUA_API int lua_setglobals (lua_State *L, int index) {
612 LClosure *f; 599 StkId o;
600 int res = 0;
613 lua_lock(L); 601 lua_lock(L);
614 api_checknelems(L, 1); 602 api_checknelems(L, 1);
615 f = getfunc(L, level); 603 o = luaA_index(L, index);
616 L->top--; 604 L->top--;
617 api_check(L, ttistable(L->top)); 605 api_check(L, ttistable(L->top));
618 if (f) f->g = *(L->top); 606 if (isLfunction(o)) {
607 res = 1;
608 clvalue(o)->l.g = *(L->top);
609 }
619 lua_unlock(L); 610 lua_unlock(L);
620 return (f != NULL); 611 return res;
621} 612}
622 613
623 614
diff --git a/lbaselib.c b/lbaselib.c
index 4ae7ff40..01091602 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.94 2002/08/06 17:06:56 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.95 2002/08/06 18:01:50 roberto Exp roberto $
3** Basic library 3** Basic library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -123,21 +123,32 @@ static int luaB_setmetatable (lua_State *L) {
123} 123}
124 124
125 125
126static void getfunc (lua_State *L) {
127 if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
128 else {
129 lua_Debug ar;
130 int level = luaL_opt_int(L, 1, 1);
131 luaL_arg_check(L, level >= 0, 1, "level must be non-negative");
132 if (lua_getstack(L, level, &ar) == 0)
133 luaL_argerror(L, 1, "invalid level");
134 lua_getinfo(L, "f", &ar);
135 }
136}
137
138
126static int luaB_getglobals (lua_State *L) { 139static int luaB_getglobals (lua_State *L) {
127 int level = luaL_opt_int(L, 1, 1); 140 getfunc(L);
128 luaL_arg_check(L, level >= 0, 2, "level must be non-negative"); 141 lua_getglobals(L, -1);
129 lua_getglobals(L, level); /* value to be returned */
130 return 1; 142 return 1;
131} 143}
132 144
133 145
134static int luaB_setglobals (lua_State *L) { 146static int luaB_setglobals (lua_State *L) {
135 int level = luaL_opt_int(L, 2, 1); 147 luaL_check_type(L, 2, LUA_TTABLE);
136 luaL_arg_check(L, level >= 1, 2, "level must be positive"); 148 getfunc(L);
137 luaL_check_type(L, 1, LUA_TTABLE); 149 lua_pushvalue(L, 2);
138 lua_settop(L, 1); 150 if (lua_setglobals(L, -2) == 0)
139 if (lua_setglobals(L, level) == 0) 151 luaL_error(L, "cannot change global table of given function");
140 luaL_error(L, "cannot change global table at level %d", level);
141 return 0; 152 return 0;
142} 153}
143 154
diff --git a/lua.h b/lua.h
index 955b052a..2eef5439 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.151 2002/08/06 17:26:45 roberto Exp roberto $ 2** $Id: lua.h,v 1.152 2002/08/06 18:01:50 roberto Exp roberto $
3** Lua - An Extensible Extension Language 3** Lua - An Extensible Extension Language
4** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil 4** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
5** http://www.lua.org mailto:info@lua.org 5** http://www.lua.org mailto:info@lua.org
@@ -167,7 +167,7 @@ LUA_API void lua_rawgeti (lua_State *L, int index, int n);
167LUA_API void lua_newtable (lua_State *L); 167LUA_API void lua_newtable (lua_State *L);
168LUA_API int lua_getmetatable (lua_State *L, int objindex); 168LUA_API int lua_getmetatable (lua_State *L, int objindex);
169LUA_API const char *lua_getmode (lua_State *L, int index); 169LUA_API const char *lua_getmode (lua_State *L, int index);
170LUA_API void lua_getglobals (lua_State *L, int level); 170LUA_API void lua_getglobals (lua_State *L, int index);
171 171
172 172
173/* 173/*
@@ -178,7 +178,7 @@ LUA_API void lua_rawset (lua_State *L, int index);
178LUA_API void lua_rawseti (lua_State *L, int index, int n); 178LUA_API void lua_rawseti (lua_State *L, int index, int n);
179LUA_API void lua_setmode (lua_State *L, int index, const char *mode); 179LUA_API void lua_setmode (lua_State *L, int index, const char *mode);
180LUA_API int lua_setmetatable (lua_State *L, int objindex); 180LUA_API int lua_setmetatable (lua_State *L, int objindex);
181LUA_API int lua_setglobals (lua_State *L, int level); 181LUA_API int lua_setglobals (lua_State *L, int index);
182 182
183 183
184/* 184/*