`setmetatable' cannot change protected objects

This commit is contained in:
Roberto Ierusalimschy 2002-09-16 16:49:45 -03:00
parent 8dae071f42
commit 32a5d96dfc
3 changed files with 16 additions and 12 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.c,v 1.84 2002/08/30 20:00:59 roberto Exp roberto $ ** $Id: lauxlib.c,v 1.85 2002/09/05 19:45:42 roberto Exp roberto $
** Auxiliary functions for building Lua libraries ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -147,7 +147,7 @@ LUALIB_API lua_Number luaL_opt_number (lua_State *L, int narg, lua_Number def) {
} }
LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
if (!lua_getmetatable(L, obj)) /* no metatable? */ if (!lua_getmetatable(L, obj)) /* no metatable? */
return 0; return 0;
lua_pushstring(L, event); lua_pushstring(L, event);
@ -156,6 +156,13 @@ LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
lua_pop(L, 2); /* remove metatable and metafield */ lua_pop(L, 2); /* remove metatable and metafield */
return 0; return 0;
} }
return 1;
}
LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
return 0;
lua_pushvalue(L, obj); lua_pushvalue(L, obj);
lua_call(L, 1, 1); lua_call(L, 1, 1);
return 1; return 1;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.h,v 1.52 2002/08/08 20:08:41 roberto Exp roberto $ ** $Id: lauxlib.h,v 1.53 2002/08/30 20:00:59 roberto Exp roberto $
** Auxiliary functions for building Lua libraries ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -30,6 +30,7 @@ typedef struct luaL_reg {
LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int nup); LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int nup);
LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname, LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname,
const luaL_reg *l, int nup); const luaL_reg *l, int nup);
LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *e);
LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *e); LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *e);
LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname); LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname);
LUALIB_API int luaL_argerror (lua_State *L, int numarg, const char *extramsg); LUALIB_API int luaL_argerror (lua_State *L, int numarg, const char *extramsg);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lbaselib.c,v 1.97 2002/08/08 20:08:41 roberto Exp roberto $ ** $Id: lbaselib.c,v 1.98 2002/09/05 19:45:42 roberto Exp roberto $
** Basic library ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -109,14 +109,8 @@ static int luaB_getmetatable (lua_State *L) {
lua_pushnil(L); lua_pushnil(L);
return 1; /* no metatable */ return 1; /* no metatable */
} }
else { luaL_getmetafield(L, 1, "__metatable");
lua_pushliteral(L, "__metatable"); return 1; /* returns either __metatable field (if present) or metatable */
lua_rawget(L, -2);
if (lua_isnil(L, -1))
lua_pop(L, 1);
/* otherwise returns metatable.__metatable */
}
return 1;
} }
@ -125,6 +119,8 @@ static int luaB_setmetatable (lua_State *L) {
luaL_check_type(L, 1, LUA_TTABLE); luaL_check_type(L, 1, LUA_TTABLE);
luaL_arg_check(L, t == LUA_TNIL || t == LUA_TTABLE, 2, luaL_arg_check(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
"nil or table expected"); "nil or table expected");
if (luaL_getmetafield(L, 1, "__metatable"))
luaL_error(L, "cannot change a protected metatable");
lua_settop(L, 2); lua_settop(L, 2);
lua_setmetatable(L, 1); lua_setmetatable(L, 1);
return 1; return 1;