aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-07-07 12:48:29 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-07-07 12:48:29 -0300
commitbbf2cea9a1fe81739b707bc658ac58366cbfd49c (patch)
treeb9442df5c798da786e91fa22a23a435d9c20314b
parentb3f7a43019f08ec47b90236f1da97d23d0d8e5bf (diff)
downloadlua-bbf2cea9a1fe81739b707bc658ac58366cbfd49c.tar.gz
lua-bbf2cea9a1fe81739b707bc658ac58366cbfd49c.tar.bz2
lua-bbf2cea9a1fe81739b707bc658ac58366cbfd49c.zip
more "polite" way to change a thread's environment
-rw-r--r--lbaselib.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/lbaselib.c b/lbaselib.c
index 0eee03dd..3be733e0 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.177 2005/05/20 15:53:42 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.178 2005/05/25 13:21:26 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*/
@@ -133,7 +133,7 @@ static void getfunc (lua_State *L) {
133static int luaB_getfenv (lua_State *L) { 133static int luaB_getfenv (lua_State *L) {
134 getfunc(L); 134 getfunc(L);
135 if (lua_iscfunction(L, -1)) /* is a C function? */ 135 if (lua_iscfunction(L, -1)) /* is a C function? */
136 lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the global env. */ 136 lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the thread's global env. */
137 else 137 else
138 lua_getfenv(L, -1); 138 lua_getfenv(L, -1);
139 return 1; 139 return 1;
@@ -145,7 +145,10 @@ static int luaB_setfenv (lua_State *L) {
145 getfunc(L); 145 getfunc(L);
146 lua_pushvalue(L, 2); 146 lua_pushvalue(L, 2);
147 if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) { 147 if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
148 lua_replace(L, LUA_GLOBALSINDEX); 148 /* change environment of current thread */
149 lua_pushthread(L);
150 lua_insert(L, -2);
151 lua_setfenv(L, -2);
149 return 0; 152 return 0;
150 } 153 }
151 else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0) 154 else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0)