diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-03-11 15:17:43 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-03-11 15:17:43 -0300 |
commit | fa4b4c11009a227769499ea44bcaea295c03505f (patch) | |
tree | 7d5ecb5d06ba87e257cbb7a6bd3c65445ac72aa3 | |
parent | 4039bf457db730caa9f3b3ceba391fa41b516def (diff) | |
download | lua-fa4b4c11009a227769499ea44bcaea295c03505f.tar.gz lua-fa4b4c11009a227769499ea44bcaea295c03505f.tar.bz2 lua-fa4b4c11009a227769499ea44bcaea295c03505f.zip |
loadstring/loadfile use global environment + setfenv(0) changes
global environment
-rw-r--r-- | lbaselib.c | 19 |
1 files changed, 7 insertions, 12 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.126 2003/03/11 12:08:13 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.127 2003/03/11 12:24:34 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 | */ |
@@ -154,7 +154,9 @@ static int luaB_setfenv (lua_State *L) { | |||
154 | else | 154 | else |
155 | lua_pop(L, 2); /* remove __globals and real environment table */ | 155 | lua_pop(L, 2); /* remove __globals and real environment table */ |
156 | lua_pushvalue(L, 2); | 156 | lua_pushvalue(L, 2); |
157 | if (lua_setfenv(L, -2) == 0) | 157 | if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) |
158 | lua_replace(L, LUA_GLOBALSINDEX); | ||
159 | else if (lua_setfenv(L, -2) == 0) | ||
158 | luaL_error(L, "cannot change environment of given function"); | 160 | luaL_error(L, "cannot change environment of given function"); |
159 | return 0; | 161 | return 0; |
160 | } | 162 | } |
@@ -246,19 +248,12 @@ static int luaB_ipairs (lua_State *L) { | |||
246 | 248 | ||
247 | 249 | ||
248 | static int load_aux (lua_State *L, int status) { | 250 | static int load_aux (lua_State *L, int status) { |
249 | if (status == 0) { /* OK? */ | 251 | if (status == 0) /* OK? */ |
250 | lua_Debug ar; | ||
251 | lua_getstack(L, 1, &ar); | ||
252 | lua_getinfo(L, "f", &ar); /* get calling function */ | ||
253 | lua_getfenv(L, -1); /* get its environment */ | ||
254 | lua_setfenv(L, -3); /* set it as the environment of the new chunk */ | ||
255 | lua_pop(L, 1); /* remove calling function */ | ||
256 | return 1; | 252 | return 1; |
257 | } | ||
258 | else { | 253 | else { |
259 | lua_pushnil(L); | 254 | lua_pushnil(L); |
260 | lua_insert(L, -2); | 255 | lua_insert(L, -2); /* put before error message */ |
261 | return 2; | 256 | return 2; /* return nil plus error message */ |
262 | } | 257 | } |
263 | } | 258 | } |
264 | 259 | ||