aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2011-10-24 14:53:05 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2011-10-24 14:53:05 -0200
commit475e6c5352c3daecdd54fe2346b3c3b07f17a791 (patch)
treeb624f5ccd781ac63a9af2dc51f772340ed93120e
parentaf00a0772ca1a37f6d8cce5b6c03cc86db0389c3 (diff)
downloadlua-475e6c5352c3daecdd54fe2346b3c3b07f17a791.tar.gz
lua-475e6c5352c3daecdd54fe2346b3c3b07f17a791.tar.bz2
lua-475e6c5352c3daecdd54fe2346b3c3b07f17a791.zip
'lua_setglobal/lua_getglobal' implemented as functions to avoid
problems with stack indices (e.g., lua_getglobal(L, lua_tostring(L, -1)) )
-rw-r--r--lapi.c26
-rw-r--r--lua.h11
2 files changed, 28 insertions, 9 deletions
diff --git a/lapi.c b/lapi.c
index ab3e92ff..afa86ff8 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 2.153 2011/09/30 12:43:17 roberto Exp roberto $ 2** $Id: lapi.c,v 2.154 2011/10/24 14:54:05 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -596,6 +596,17 @@ LUA_API int lua_pushthread (lua_State *L) {
596*/ 596*/
597 597
598 598
599LUA_API void lua_getglobal (lua_State *L, const char *var) {
600 Table *reg = hvalue(&G(L)->l_registry);
601 const TValue *gt; /* global table */
602 lua_lock(L);
603 gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
604 setsvalue2s(L, L->top++, luaS_new(L, var));
605 luaV_gettable(L, gt, L->top - 1, L->top - 1);
606 lua_unlock(L);
607}
608
609
599LUA_API void lua_gettable (lua_State *L, int idx) { 610LUA_API void lua_gettable (lua_State *L, int idx) {
600 StkId t; 611 StkId t;
601 lua_lock(L); 612 lua_lock(L);
@@ -714,6 +725,19 @@ LUA_API void lua_getuservalue (lua_State *L, int idx) {
714*/ 725*/
715 726
716 727
728LUA_API void lua_setglobal (lua_State *L, const char *var) {
729 Table *reg = hvalue(&G(L)->l_registry);
730 const TValue *gt; /* global table */
731 lua_lock(L);
732 api_checknelems(L, 1);
733 gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
734 setsvalue2s(L, L->top++, luaS_new(L, var));
735 luaV_settable(L, gt, L->top - 1, L->top - 2);
736 L->top -= 2; /* pop value and key */
737 lua_unlock(L);
738}
739
740
717LUA_API void lua_settable (lua_State *L, int idx) { 741LUA_API void lua_settable (lua_State *L, int idx) {
718 StkId t; 742 StkId t;
719 lua_lock(L); 743 lua_lock(L);
diff --git a/lua.h b/lua.h
index 6e3ff629..db463675 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.279 2011/08/23 17:24:34 roberto Exp roberto $ 2** $Id: lua.h,v 1.280 2011/10/24 14:54:05 roberto Exp roberto $
3** Lua - A Scripting Language 3** Lua - A Scripting Language
4** Lua.org, PUC-Rio, Brazil (http://www.lua.org) 4** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
5** See Copyright Notice at the end of this file 5** See Copyright Notice at the end of this file
@@ -215,6 +215,7 @@ LUA_API int (lua_pushthread) (lua_State *L);
215/* 215/*
216** get functions (Lua -> stack) 216** get functions (Lua -> stack)
217*/ 217*/
218LUA_API void (lua_getglobal) (lua_State *L, const char *var);
218LUA_API void (lua_gettable) (lua_State *L, int idx); 219LUA_API void (lua_gettable) (lua_State *L, int idx);
219LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k); 220LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k);
220LUA_API void (lua_rawget) (lua_State *L, int idx); 221LUA_API void (lua_rawget) (lua_State *L, int idx);
@@ -229,6 +230,7 @@ LUA_API void (lua_getuservalue) (lua_State *L, int idx);
229/* 230/*
230** set functions (stack -> Lua) 231** set functions (stack -> Lua)
231*/ 232*/
233LUA_API void (lua_setglobal) (lua_State *L, const char *var);
232LUA_API void (lua_settable) (lua_State *L, int idx); 234LUA_API void (lua_settable) (lua_State *L, int idx);
233LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); 235LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k);
234LUA_API void (lua_rawset) (lua_State *L, int idx); 236LUA_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);
316 318
317#define lua_newtable(L) lua_createtable(L, 0, 0) 319#define lua_newtable(L) lua_createtable(L, 0, 0)
318 320
319#define lua_setglobal(L,s) \
320 (lua_pushglobaltable(L), lua_pushvalue(L, -2), \
321 lua_setfield(L, -2, (s)), lua_pop(L, 2))
322
323#define lua_getglobal(L,s) \
324 (lua_pushglobaltable(L), lua_getfield(L, -1, (s)), lua_remove(L, -2))
325
326#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) 321#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
327 322
328#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) 323#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0)