diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-10-22 16:41:08 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-10-22 16:41:08 -0300 |
| commit | 92a2ef9dca6ef7a82140e0a4b11491fbdad10d96 (patch) | |
| tree | bd87a26f276b5899c9fc5b0890dc8986ee915b8c | |
| parent | 4fb9110a6e85a49e80605a178c9f926c8e70390e (diff) | |
| download | lua-92a2ef9dca6ef7a82140e0a4b11491fbdad10d96.tar.gz lua-92a2ef9dca6ef7a82140e0a4b11491fbdad10d96.tar.bz2 lua-92a2ef9dca6ef7a82140e0a4b11491fbdad10d96.zip | |
field `__globals' protect global tables
| -rw-r--r-- | lbaselib.c | 17 |
1 files changed, 15 insertions, 2 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbaselib.c,v 1.98 2002/09/05 19:45:42 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.99 2002/09/16 19:49:45 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 | */ |
| @@ -140,9 +140,18 @@ static void getfunc (lua_State *L) { | |||
| 140 | } | 140 | } |
| 141 | 141 | ||
| 142 | 142 | ||
| 143 | static int aux_getglobals (lua_State *L) { | ||
| 144 | lua_getglobals(L, -1); | ||
| 145 | lua_pushliteral(L, "__globals"); | ||
| 146 | lua_rawget(L, -2); | ||
| 147 | return !lua_isnil(L, -1); | ||
| 148 | } | ||
| 149 | |||
| 150 | |||
| 143 | static int luaB_getglobals (lua_State *L) { | 151 | static int luaB_getglobals (lua_State *L) { |
| 144 | getfunc(L); | 152 | getfunc(L); |
| 145 | lua_getglobals(L, -1); | 153 | if (!aux_getglobals(L)) /* __globals not defined? */ |
| 154 | lua_pop(L, 1); /* remove it, to return real globals */ | ||
| 146 | return 1; | 155 | return 1; |
| 147 | } | 156 | } |
| 148 | 157 | ||
| @@ -150,6 +159,10 @@ static int luaB_getglobals (lua_State *L) { | |||
| 150 | static int luaB_setglobals (lua_State *L) { | 159 | static int luaB_setglobals (lua_State *L) { |
| 151 | luaL_check_type(L, 2, LUA_TTABLE); | 160 | luaL_check_type(L, 2, LUA_TTABLE); |
| 152 | getfunc(L); | 161 | getfunc(L); |
| 162 | if (aux_getglobals(L)) /* __globals defined? */ | ||
| 163 | luaL_error(L, "cannot change a protected global table"); | ||
| 164 | else | ||
| 165 | lua_pop(L, 2); /* remove __globals and real global table */ | ||
| 153 | lua_pushvalue(L, 2); | 166 | lua_pushvalue(L, 2); |
| 154 | if (lua_setglobals(L, -2) == 0) | 167 | if (lua_setglobals(L, -2) == 0) |
| 155 | luaL_error(L, "cannot change global table of given function"); | 168 | luaL_error(L, "cannot change global table of given function"); |
