浏览代码

'lua_setglobal/lua_getglobal' implemented as functions to avoid
problems with stack indices
(e.g., lua_getglobal(L, lua_tostring(L, -1)) )

Roberto Ierusalimschy 13 年之前
父节点
当前提交
475e6c5352
共有 2 个文件被更改,包括 28 次插入9 次删除
  1. 25 1
      lapi.c
  2. 3 8
      lua.h

+ 25 - 1
lapi.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lapi.c,v 2.153 2011/09/30 12:43:17 roberto Exp roberto $
+** $Id: lapi.c,v 2.154 2011/10/24 14:54:05 roberto Exp roberto $
 ** Lua API
 ** See Copyright Notice in lua.h
 */
@@ -596,6 +596,17 @@ LUA_API int lua_pushthread (lua_State *L) {
 */
 
 
+LUA_API void lua_getglobal (lua_State *L, const char *var) {
+  Table *reg = hvalue(&G(L)->l_registry);
+  const TValue *gt;  /* global table */
+  lua_lock(L);
+  gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
+  setsvalue2s(L, L->top++, luaS_new(L, var));
+  luaV_gettable(L, gt, L->top - 1, L->top - 1);
+  lua_unlock(L);
+}
+
+
 LUA_API void lua_gettable (lua_State *L, int idx) {
   StkId t;
   lua_lock(L);
@@ -714,6 +725,19 @@ LUA_API void lua_getuservalue (lua_State *L, int idx) {
 */
 
 
+LUA_API void lua_setglobal (lua_State *L, const char *var) {
+  Table *reg = hvalue(&G(L)->l_registry);
+  const TValue *gt;  /* global table */
+  lua_lock(L);
+  api_checknelems(L, 1);
+  gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
+  setsvalue2s(L, L->top++, luaS_new(L, var));
+  luaV_settable(L, gt, L->top - 1, L->top - 2);
+  L->top -= 2;  /* pop value and key */
+  lua_unlock(L);
+}
+
+
 LUA_API void lua_settable (lua_State *L, int idx) {
   StkId t;
   lua_lock(L);

+ 3 - 8
lua.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lua.h,v 1.279 2011/08/23 17:24:34 roberto Exp roberto $
+** $Id: lua.h,v 1.280 2011/10/24 14:54:05 roberto Exp roberto $
 ** Lua - A Scripting Language
 ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
 ** See Copyright Notice at the end of this file
@@ -215,6 +215,7 @@ LUA_API int   (lua_pushthread) (lua_State *L);
 /*
 ** get functions (Lua -> stack)
 */
+LUA_API void  (lua_getglobal) (lua_State *L, const char *var);
 LUA_API void  (lua_gettable) (lua_State *L, int idx);
 LUA_API void  (lua_getfield) (lua_State *L, int idx, const char *k);
 LUA_API void  (lua_rawget) (lua_State *L, int idx);
@@ -229,6 +230,7 @@ LUA_API void  (lua_getuservalue) (lua_State *L, int idx);
 /*
 ** set functions (stack -> Lua)
 */
+LUA_API void  (lua_setglobal) (lua_State *L, const char *var);
 LUA_API void  (lua_settable) (lua_State *L, int idx);
 LUA_API void  (lua_setfield) (lua_State *L, int idx, const char *k);
 LUA_API void  (lua_rawset) (lua_State *L, int idx);
@@ -316,13 +318,6 @@ LUA_API void      (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
 
 #define lua_newtable(L)		lua_createtable(L, 0, 0)
 
-#define lua_setglobal(L,s)  \
-	(lua_pushglobaltable(L), lua_pushvalue(L, -2), \
-	 lua_setfield(L, -2, (s)), lua_pop(L, 2))
-
-#define lua_getglobal(L,s) \
-	(lua_pushglobaltable(L), lua_getfield(L, -1, (s)), lua_remove(L, -2))
-
 #define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
 
 #define lua_pushcfunction(L,f)	lua_pushcclosure(L, (f), 0)