From fa4b4c11009a227769499ea44bcaea295c03505f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 11 Mar 2003 15:17:43 -0300 Subject: loadstring/loadfile use global environment + setfenv(0) changes global environment --- lbaselib.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/lbaselib.c b/lbaselib.c index c4192670..ffc8ec1e 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.126 2003/03/11 12:08:13 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.127 2003/03/11 12:24:34 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -154,7 +154,9 @@ static int luaB_setfenv (lua_State *L) { else lua_pop(L, 2); /* remove __globals and real environment table */ lua_pushvalue(L, 2); - if (lua_setfenv(L, -2) == 0) + if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) + lua_replace(L, LUA_GLOBALSINDEX); + else if (lua_setfenv(L, -2) == 0) luaL_error(L, "cannot change environment of given function"); return 0; } @@ -246,19 +248,12 @@ static int luaB_ipairs (lua_State *L) { static int load_aux (lua_State *L, int status) { - if (status == 0) { /* OK? */ - lua_Debug ar; - lua_getstack(L, 1, &ar); - lua_getinfo(L, "f", &ar); /* get calling function */ - lua_getfenv(L, -1); /* get its environment */ - lua_setfenv(L, -3); /* set it as the environment of the new chunk */ - lua_pop(L, 1); /* remove calling function */ + if (status == 0) /* OK? */ return 1; - } else { lua_pushnil(L); - lua_insert(L, -2); - return 2; + lua_insert(L, -2); /* put before error message */ + return 2; /* return nil plus error message */ } } -- cgit v1.2.3-55-g6feb