Browse Source

any Lua closure has a table of globals (not only active functions)

Roberto Ierusalimschy 23 years ago
parent
commit
4664f2e927
3 changed files with 38 additions and 36 deletions
  1. 14 23
      lapi.c
  2. 21 10
      lbaselib.c
  3. 3 3
      lua.h

+ 14 - 23
lapi.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lapi.c,v 1.207 2002/08/06 15:32:22 roberto Exp roberto $
+** $Id: lapi.c,v 1.208 2002/08/06 17:06:56 roberto Exp roberto $
 ** Lua API
 ** Lua API
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -506,24 +506,11 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) {
 }
 }
 
 
 
 
-static LClosure *getfunc (lua_State *L, int level) {
-  CallInfo *ci;
-  TObject *f;
-  if (L->ci - L->base_ci < level) ci = L->base_ci;
-  else ci = L->ci - level;
-  f = ci->base - 1;
-  if (isLfunction(f))
-    return &clvalue(f)->l;
-  else
-    return NULL;
-}
-
-
-LUA_API void lua_getglobals (lua_State *L, int level) {
-  LClosure *f;
+LUA_API void lua_getglobals (lua_State *L, int index) {
+  StkId o;
   lua_lock(L);
   lua_lock(L);
-  f = getfunc(L, level);
-  setobj(L->top, (f ? &f->g : gt(L)));
+  o = luaA_index(L, index);
+  setobj(L->top, isLfunction(o) ? &clvalue(o)->l.g : gt(L));
   api_incr_top(L);
   api_incr_top(L);
   lua_unlock(L);
   lua_unlock(L);
 }
 }
@@ -608,16 +595,20 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
 }
 }
 
 
 
 
-LUA_API int lua_setglobals (lua_State *L, int level) {
-  LClosure *f;
+LUA_API int lua_setglobals (lua_State *L, int index) {
+  StkId o;
+  int res = 0;
   lua_lock(L);
   lua_lock(L);
   api_checknelems(L, 1);
   api_checknelems(L, 1);
-  f = getfunc(L, level);
+  o = luaA_index(L, index);
   L->top--;
   L->top--;
   api_check(L, ttistable(L->top));
   api_check(L, ttistable(L->top));
-  if (f) f->g = *(L->top);
+  if (isLfunction(o)) {
+    res = 1;
+    clvalue(o)->l.g = *(L->top);
+  }
   lua_unlock(L);
   lua_unlock(L);
-  return (f != NULL);
+  return res;
 }
 }
 
 
 
 

+ 21 - 10
lbaselib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lbaselib.c,v 1.94 2002/08/06 17:06:56 roberto Exp roberto $
+** $Id: lbaselib.c,v 1.95 2002/08/06 18:01:50 roberto Exp roberto $
 ** Basic library
 ** Basic library
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -123,21 +123,32 @@ static int luaB_setmetatable (lua_State *L) {
 }
 }
 
 
 
 
+static void getfunc (lua_State *L) {
+  if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
+  else {
+    lua_Debug ar;
+    int level = luaL_opt_int(L, 1, 1);
+    luaL_arg_check(L, level >= 0, 1, "level must be non-negative");
+    if (lua_getstack(L, level, &ar) == 0)
+      luaL_argerror(L, 1, "invalid level");
+    lua_getinfo(L, "f", &ar);
+  }
+}
+
+
 static int luaB_getglobals (lua_State *L) {
 static int luaB_getglobals (lua_State *L) {
-  int level = luaL_opt_int(L, 1, 1);
-  luaL_arg_check(L, level >= 0, 2, "level must be non-negative");
-  lua_getglobals(L, level);  /* value to be returned */
+  getfunc(L);
+  lua_getglobals(L, -1);
   return 1;
   return 1;
 }
 }
 
 
 
 
 static int luaB_setglobals (lua_State *L) {
 static int luaB_setglobals (lua_State *L) {
-  int level = luaL_opt_int(L, 2, 1);
-  luaL_arg_check(L, level >= 1, 2, "level must be positive");
-  luaL_check_type(L, 1, LUA_TTABLE);
-  lua_settop(L, 1);
-  if (lua_setglobals(L, level) == 0)
-    luaL_error(L, "cannot change global table at level %d", level);
+  luaL_check_type(L, 2, LUA_TTABLE);
+  getfunc(L);
+  lua_pushvalue(L, 2);
+  if (lua_setglobals(L, -2) == 0)
+    luaL_error(L, "cannot change global table of given function");
   return 0;
   return 0;
 }
 }
 
 

+ 3 - 3
lua.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lua.h,v 1.151 2002/08/06 17:26:45 roberto Exp roberto $
+** $Id: lua.h,v 1.152 2002/08/06 18:01:50 roberto Exp roberto $
 ** Lua - An Extensible Extension Language
 ** Lua - An Extensible Extension Language
 ** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
 ** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
 ** http://www.lua.org	mailto:[email protected]
 ** http://www.lua.org	mailto:[email protected]
@@ -167,7 +167,7 @@ LUA_API void  lua_rawgeti (lua_State *L, int index, int n);
 LUA_API void  lua_newtable (lua_State *L);
 LUA_API void  lua_newtable (lua_State *L);
 LUA_API int   lua_getmetatable (lua_State *L, int objindex);
 LUA_API int   lua_getmetatable (lua_State *L, int objindex);
 LUA_API const char *lua_getmode (lua_State *L, int index);
 LUA_API const char *lua_getmode (lua_State *L, int index);
-LUA_API void  lua_getglobals (lua_State *L, int level);
+LUA_API void  lua_getglobals (lua_State *L, int index);
 
 
 
 
 /*
 /*
@@ -178,7 +178,7 @@ LUA_API void  lua_rawset (lua_State *L, int index);
 LUA_API void  lua_rawseti (lua_State *L, int index, int n);
 LUA_API void  lua_rawseti (lua_State *L, int index, int n);
 LUA_API void  lua_setmode (lua_State *L, int index, const char *mode);
 LUA_API void  lua_setmode (lua_State *L, int index, const char *mode);
 LUA_API int   lua_setmetatable (lua_State *L, int objindex);
 LUA_API int   lua_setmetatable (lua_State *L, int objindex);
-LUA_API int   lua_setglobals (lua_State *L, int level);
+LUA_API int   lua_setglobals (lua_State *L, int index);
 
 
 
 
 /*
 /*