diff options
Diffstat (limited to 'lbaselib.c')
-rw-r--r-- | lbaselib.c | 31 |
1 files changed, 21 insertions, 10 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.94 2002/08/06 17:06:56 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.95 2002/08/06 18:01:50 roberto Exp roberto $ |
3 | ** Basic library | 3 | ** Basic library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -123,21 +123,32 @@ static int luaB_setmetatable (lua_State *L) { | |||
123 | } | 123 | } |
124 | 124 | ||
125 | 125 | ||
126 | static void getfunc (lua_State *L) { | ||
127 | if (lua_isfunction(L, 1)) lua_pushvalue(L, 1); | ||
128 | else { | ||
129 | lua_Debug ar; | ||
130 | int level = luaL_opt_int(L, 1, 1); | ||
131 | luaL_arg_check(L, level >= 0, 1, "level must be non-negative"); | ||
132 | if (lua_getstack(L, level, &ar) == 0) | ||
133 | luaL_argerror(L, 1, "invalid level"); | ||
134 | lua_getinfo(L, "f", &ar); | ||
135 | } | ||
136 | } | ||
137 | |||
138 | |||
126 | static int luaB_getglobals (lua_State *L) { | 139 | static int luaB_getglobals (lua_State *L) { |
127 | int level = luaL_opt_int(L, 1, 1); | 140 | getfunc(L); |
128 | luaL_arg_check(L, level >= 0, 2, "level must be non-negative"); | 141 | lua_getglobals(L, -1); |
129 | lua_getglobals(L, level); /* value to be returned */ | ||
130 | return 1; | 142 | return 1; |
131 | } | 143 | } |
132 | 144 | ||
133 | 145 | ||
134 | static int luaB_setglobals (lua_State *L) { | 146 | static int luaB_setglobals (lua_State *L) { |
135 | int level = luaL_opt_int(L, 2, 1); | 147 | luaL_check_type(L, 2, LUA_TTABLE); |
136 | luaL_arg_check(L, level >= 1, 2, "level must be positive"); | 148 | getfunc(L); |
137 | luaL_check_type(L, 1, LUA_TTABLE); | 149 | lua_pushvalue(L, 2); |
138 | lua_settop(L, 1); | 150 | if (lua_setglobals(L, -2) == 0) |
139 | if (lua_setglobals(L, level) == 0) | 151 | luaL_error(L, "cannot change global table of given function"); |
140 | luaL_error(L, "cannot change global table at level %d", level); | ||
141 | return 0; | 152 | return 0; |
142 | } | 153 | } |
143 | 154 | ||