diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-10-24 14:53:05 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-10-24 14:53:05 -0200 |
commit | 475e6c5352c3daecdd54fe2346b3c3b07f17a791 (patch) | |
tree | b624f5ccd781ac63a9af2dc51f772340ed93120e /lapi.c | |
parent | af00a0772ca1a37f6d8cce5b6c03cc86db0389c3 (diff) | |
download | lua-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)) )
Diffstat (limited to 'lapi.c')
-rw-r--r-- | lapi.c | 26 |
1 files changed, 25 insertions, 1 deletions
@@ -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 | ||
599 | LUA_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 | |||
599 | LUA_API void lua_gettable (lua_State *L, int idx) { | 610 | LUA_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 | ||
728 | LUA_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 | |||
717 | LUA_API void lua_settable (lua_State *L, int idx) { | 741 | LUA_API void lua_settable (lua_State *L, int idx) { |
718 | StkId t; | 742 | StkId t; |
719 | lua_lock(L); | 743 | lua_lock(L); |