From 475e6c5352c3daecdd54fe2346b3c3b07f17a791 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 24 Oct 2011 14:53:05 -0200 Subject: 'lua_setglobal/lua_getglobal' implemented as functions to avoid problems with stack indices (e.g., lua_getglobal(L, lua_tostring(L, -1)) ) --- lapi.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'lapi.c') diff --git a/lapi.c b/lapi.c index ab3e92ff..afa86ff8 100644 --- 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); -- cgit v1.2.3-55-g6feb