diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-08-06 15:54:18 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-08-06 15:54:18 -0300 |
commit | 4664f2e9270a956f6175481abe590ba4931b7477 (patch) | |
tree | b697cf966fada0f46d7779ad620036d9d27e4127 | |
parent | 2e38c6ae5a53cbf1a607a790460fca45ba3b9ca8 (diff) | |
download | lua-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.c | 37 | ||||
-rw-r--r-- | lbaselib.c | 31 | ||||
-rw-r--r-- | lua.h | 6 |
3 files changed, 38 insertions, 36 deletions
@@ -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 | ||
509 | static LClosure *getfunc (lua_State *L, int level) { | 509 | LUA_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 | |||
522 | LUA_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 | ||
611 | LUA_API int lua_setglobals (lua_State *L, int level) { | 598 | LUA_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 | ||
@@ -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 | ||
126 | static 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 | |||
126 | static int luaB_getglobals (lua_State *L) { | 139 | static 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 | ||
134 | static int luaB_setglobals (lua_State *L) { | 146 | static 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 | ||
@@ -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); | |||
167 | LUA_API void lua_newtable (lua_State *L); | 167 | LUA_API void lua_newtable (lua_State *L); |
168 | LUA_API int lua_getmetatable (lua_State *L, int objindex); | 168 | LUA_API int lua_getmetatable (lua_State *L, int objindex); |
169 | LUA_API const char *lua_getmode (lua_State *L, int index); | 169 | LUA_API const char *lua_getmode (lua_State *L, int index); |
170 | LUA_API void lua_getglobals (lua_State *L, int level); | 170 | LUA_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); | |||
178 | LUA_API void lua_rawseti (lua_State *L, int index, int n); | 178 | LUA_API void lua_rawseti (lua_State *L, int index, int n); |
179 | LUA_API void lua_setmode (lua_State *L, int index, const char *mode); | 179 | LUA_API void lua_setmode (lua_State *L, int index, const char *mode); |
180 | LUA_API int lua_setmetatable (lua_State *L, int objindex); | 180 | LUA_API int lua_setmetatable (lua_State *L, int objindex); |
181 | LUA_API int lua_setglobals (lua_State *L, int level); | 181 | LUA_API int lua_setglobals (lua_State *L, int index); |
182 | 182 | ||
183 | 183 | ||
184 | /* | 184 | /* |