"load*" creates chunk with same global table than caller

This commit is contained in:
Roberto Ierusalimschy 2002-12-06 15:05:15 -02:00
parent 04f95ce879
commit acf62ddfbe
1 changed files with 13 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lbaselib.c,v 1.113 2002/12/04 15:38:25 roberto Exp roberto $ ** $Id: lbaselib.c,v 1.114 2002/12/04 17:38:31 roberto Exp roberto $
** Basic library ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -242,8 +242,16 @@ static int luaB_ipairs (lua_State *L) {
} }
static int passresults (lua_State *L, int status) { static int load_aux (lua_State *L, int status) {
if (status == 0) return 1; if (status == 0) { /* OK? */
lua_Debug ar;
lua_getstack(L, 1, &ar);
lua_getinfo(L, "f", &ar); /* get calling function */
lua_getglobals(L, -1); /* get its global table */
lua_setglobals(L, -3); /* set it as the global table of the new chunk */
lua_pop(L, 1); /* remove calling function */
return 1;
}
else { else {
lua_pushnil(L); lua_pushnil(L);
lua_insert(L, -2); lua_insert(L, -2);
@ -256,13 +264,13 @@ static int luaB_loadstring (lua_State *L) {
size_t l; size_t l;
const char *s = luaL_checklstring(L, 1, &l); const char *s = luaL_checklstring(L, 1, &l);
const char *chunkname = luaL_optstring(L, 2, s); const char *chunkname = luaL_optstring(L, 2, s);
return passresults(L, luaL_loadbuffer(L, s, l, chunkname)); return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));
} }
static int luaB_loadfile (lua_State *L) { static int luaB_loadfile (lua_State *L) {
const char *fname = luaL_optstring(L, 1, NULL); const char *fname = luaL_optstring(L, 1, NULL);
return passresults(L, luaL_loadfile(L, fname)); return load_aux(L, luaL_loadfile(L, fname));
} }