'lua_setglobal/lua_getglobal' implemented as functions to avoid

problems with stack indices
(e.g., lua_getglobal(L, lua_tostring(L, -1)) )
This commit is contained in:
Roberto Ierusalimschy
2011-10-24 14:53:05 -02:00
parent af00a0772c
commit 475e6c5352
2 changed files with 28 additions and 9 deletions

26
lapi.c
View File

@@ -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);