diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-12-06 15:05:15 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-12-06 15:05:15 -0200 |
commit | acf62ddfbe1163f7cc1ab463842fb6a1b6aba8fe (patch) | |
tree | c2e842a7d655432b5dfe2cfe6ea691e41af70c37 | |
parent | 04f95ce879ed3d942099145f5e408181a11f4411 (diff) | |
download | lua-acf62ddfbe1163f7cc1ab463842fb6a1b6aba8fe.tar.gz lua-acf62ddfbe1163f7cc1ab463842fb6a1b6aba8fe.tar.bz2 lua-acf62ddfbe1163f7cc1ab463842fb6a1b6aba8fe.zip |
"load*" creates chunk with same global table than caller
-rw-r--r-- | lbaselib.c | 18 |
1 files changed, 13 insertions, 5 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.113 2002/12/04 15:38:25 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.114 2002/12/04 17:38:31 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 | */ |
@@ -242,8 +242,16 @@ static int luaB_ipairs (lua_State *L) { | |||
242 | } | 242 | } |
243 | 243 | ||
244 | 244 | ||
245 | static int passresults (lua_State *L, int status) { | 245 | static int load_aux (lua_State *L, int status) { |
246 | if (status == 0) return 1; | 246 | if (status == 0) { /* OK? */ |
247 | lua_Debug ar; | ||
248 | lua_getstack(L, 1, &ar); | ||
249 | lua_getinfo(L, "f", &ar); /* get calling function */ | ||
250 | lua_getglobals(L, -1); /* get its global table */ | ||
251 | lua_setglobals(L, -3); /* set it as the global table of the new chunk */ | ||
252 | lua_pop(L, 1); /* remove calling function */ | ||
253 | return 1; | ||
254 | } | ||
247 | else { | 255 | else { |
248 | lua_pushnil(L); | 256 | lua_pushnil(L); |
249 | lua_insert(L, -2); | 257 | lua_insert(L, -2); |
@@ -256,13 +264,13 @@ static int luaB_loadstring (lua_State *L) { | |||
256 | size_t l; | 264 | size_t l; |
257 | const char *s = luaL_checklstring(L, 1, &l); | 265 | const char *s = luaL_checklstring(L, 1, &l); |
258 | const char *chunkname = luaL_optstring(L, 2, s); | 266 | const char *chunkname = luaL_optstring(L, 2, s); |
259 | return passresults(L, luaL_loadbuffer(L, s, l, chunkname)); | 267 | return load_aux(L, luaL_loadbuffer(L, s, l, chunkname)); |
260 | } | 268 | } |
261 | 269 | ||
262 | 270 | ||
263 | static int luaB_loadfile (lua_State *L) { | 271 | static int luaB_loadfile (lua_State *L) { |
264 | const char *fname = luaL_optstring(L, 1, NULL); | 272 | const char *fname = luaL_optstring(L, 1, NULL); |
265 | return passresults(L, luaL_loadfile(L, fname)); | 273 | return load_aux(L, luaL_loadfile(L, fname)); |
266 | } | 274 | } |
267 | 275 | ||
268 | 276 | ||