diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-07-07 12:48:29 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-07-07 12:48:29 -0300 |
commit | bbf2cea9a1fe81739b707bc658ac58366cbfd49c (patch) | |
tree | b9442df5c798da786e91fa22a23a435d9c20314b | |
parent | b3f7a43019f08ec47b90236f1da97d23d0d8e5bf (diff) | |
download | lua-bbf2cea9a1fe81739b707bc658ac58366cbfd49c.tar.gz lua-bbf2cea9a1fe81739b707bc658ac58366cbfd49c.tar.bz2 lua-bbf2cea9a1fe81739b707bc658ac58366cbfd49c.zip |
more "polite" way to change a thread's environment
-rw-r--r-- | lbaselib.c | 9 |
1 files changed, 6 insertions, 3 deletions
@@ -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) { | |||
133 | static int luaB_getfenv (lua_State *L) { | 133 | static 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) |