back to `__mode' metafield to specify weakness

This commit is contained in:
Roberto Ierusalimschy
2002-11-14 10:01:35 -02:00
parent 43d1a6af12
commit 5c5d9b2703
9 changed files with 49 additions and 74 deletions

View File

@@ -1,5 +1,5 @@
/*
** $Id: lbaselib.c,v 1.104 2002/11/06 19:08:00 roberto Exp roberto $
** $Id: lbaselib.c,v 1.105 2002/11/07 15:39:23 roberto Exp roberto $
** Basic library
** See Copyright Notice in lua.h
*/
@@ -88,21 +88,6 @@ static int luaB_error (lua_State *L) {
}
static int luaB_getmode (lua_State *L) {
luaL_check_type(L, 1, LUA_TTABLE);
lua_pushstring(L, lua_getmode(L, 1));
return 1;
}
static int luaB_setmode (lua_State *L) {
luaL_check_type(L, 1, LUA_TTABLE);
lua_setmode(L, 1, luaL_check_string(L, 2));
lua_settop(L, 1);
return 1;
}
static int luaB_getmetatable (lua_State *L) {
luaL_check_any(L, 1);
if (!lua_getmetatable(L, 1)) {
@@ -524,8 +509,6 @@ static const luaL_reg base_funcs[] = {
{"setmetatable", luaB_setmetatable},
{"getglobals", luaB_getglobals},
{"setglobals", luaB_setglobals},
{"getmode", luaB_getmode},
{"setmode", luaB_setmode},
{"next", luaB_next},
{"ipairs", luaB_ipairs},
{"pairs", luaB_pairs},
@@ -646,7 +629,11 @@ static void base_open (lua_State *L) {
/* `newproxy' needs a weaktable as upvalue */
lua_pushliteral(L, "newproxy");
lua_newtable(L); /* new table `w' */
lua_setmode(L, -1, "k");
lua_pushvalue(L, -1); /* `w' will be its own metatable */
lua_setmetatable(L, -2);
lua_pushliteral(L, "__mode");
lua_pushliteral(L, "k");
lua_rawset(L, -3); /* metatable(w).__mode = "k" */
lua_pushcclosure(L, luaB_newproxy, 1);
lua_rawset(L, -3); /* set global `newproxy' */
lua_rawset(L, -1); /* set global _G */