From 634344d61fb4bd7ebd033d37b814a0083e55b5a2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 6 Aug 2002 14:06:56 -0300 Subject: new API for weak mode --- lbaselib.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'lbaselib.c') diff --git a/lbaselib.c b/lbaselib.c index b05a1b15..9658a1c6 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.92 2002/08/05 14:46:02 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.93 2002/08/06 15:32:22 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -81,6 +81,21 @@ 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)) { @@ -460,6 +475,8 @@ 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}, @@ -572,11 +589,7 @@ static void base_open (lua_State *L) { /* `newproxy' needs a weaktable as upvalue */ lua_pushliteral(L, "newproxy"); lua_newtable(L); /* new table `w' */ - 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_setmode(L, -1, "k"); lua_pushcclosure(L, luaB_newproxy, 1); lua_rawset(L, -3); /* set global `newproxy' */ lua_rawset(L, -1); /* set global _G */ -- cgit v1.2.3-55-g6feb