commit 475e6c5352c3daecdd54fe2346b3c3b07f17a791
parent af00a0772ca1a37f6d8cce5b6c03cc86db0389c3
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Mon, 24 Oct 2011 14:52:41 -0200
'lua_setglobal/lua_getglobal' implemented as functions to avoid
problems with stack indices
(e.g., lua_getglobal(L, lua_tostring(L, -1)) )
Diffstat:
M | lapi.c | | | 26 | +++++++++++++++++++++++++- |
M | lua.h | | | 11 | +++-------- |
2 files changed, 28 insertions(+), 9 deletions(-)
diff --git a/lapi.c b/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);
diff --git a/lua.h b/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)