mirror of https://github.com/rusefi/lua.git
field `__globals' protect global tables
This commit is contained in:
parent
4fb9110a6e
commit
92a2ef9dca
17
lbaselib.c
17
lbaselib.c
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
** $Id: lbaselib.c,v 1.98 2002/09/05 19:45:42 roberto Exp roberto $
|
** $Id: lbaselib.c,v 1.99 2002/09/16 19:49:45 roberto Exp roberto $
|
||||||
** Basic library
|
** Basic library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
@ -140,9 +140,18 @@ static void getfunc (lua_State *L) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int aux_getglobals (lua_State *L) {
|
||||||
|
lua_getglobals(L, -1);
|
||||||
|
lua_pushliteral(L, "__globals");
|
||||||
|
lua_rawget(L, -2);
|
||||||
|
return !lua_isnil(L, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int luaB_getglobals (lua_State *L) {
|
static int luaB_getglobals (lua_State *L) {
|
||||||
getfunc(L);
|
getfunc(L);
|
||||||
lua_getglobals(L, -1);
|
if (!aux_getglobals(L)) /* __globals not defined? */
|
||||||
|
lua_pop(L, 1); /* remove it, to return real globals */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,6 +159,10 @@ static int luaB_getglobals (lua_State *L) {
|
||||||
static int luaB_setglobals (lua_State *L) {
|
static int luaB_setglobals (lua_State *L) {
|
||||||
luaL_check_type(L, 2, LUA_TTABLE);
|
luaL_check_type(L, 2, LUA_TTABLE);
|
||||||
getfunc(L);
|
getfunc(L);
|
||||||
|
if (aux_getglobals(L)) /* __globals defined? */
|
||||||
|
luaL_error(L, "cannot change a protected global table");
|
||||||
|
else
|
||||||
|
lua_pop(L, 2); /* remove __globals and real global table */
|
||||||
lua_pushvalue(L, 2);
|
lua_pushvalue(L, 2);
|
||||||
if (lua_setglobals(L, -2) == 0)
|
if (lua_setglobals(L, -2) == 0)
|
||||||
luaL_error(L, "cannot change global table of given function");
|
luaL_error(L, "cannot change global table of given function");
|
||||||
|
|
Loading…
Reference in New Issue