From d198e049e29651708e7ca6fb5e6b4c9bc3994b56 Mon Sep 17 00:00:00 2001 From: Rui Guo Date: Tue, 16 Jun 2009 13:54:27 +0800 Subject: [PATCH] Separate the weak table, since only the func part is weak. --- src/lua.c | 55 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/lua.c b/src/lua.c index 041249d..52c446b 100644 --- a/src/lua.c +++ b/src/lua.c @@ -821,6 +821,25 @@ static const struct Xet_reg screen_getters[] = { /** Public functions {{{ */ +static void +prepare_weak_table(lua_State *L, const char *name, const char *mode) +{ + luaL_getmetatable(L, "screen"); + + lua_pushstring(L, name); + lua_newtable(L); + + /* prepare a metatable to indicate weak table */ + lua_newtable(L); + lua_pushstring(L, "__mode"); + lua_pushstring(L, mode); + lua_rawset(L, -3); + /* Mark weak table */ + lua_setmetatable(L, -2); + lua_rawset(L, -3); + lua_pop(L, 1); +} + /* FIXME: Think about this: will it affect the registered handlers?*/ static lua_State *L; int LuaInit(void) @@ -839,38 +858,26 @@ int LuaInit(void) REGISTER(callback); /* To store handler funcs */ - luaL_getmetatable(L, "screen"); - /*_two kinds of information in this table: - * funcreg[key]->func - * funcreg[func]->listener - * The table is weak. That means the registered funcs will be collected once - * the func is no longer available (e.g. overwritten by a new instance of - * the script) + /*_two kinds of information are mantained: * - * The listener is the unhook ticket of the hook. which should be collected - * once the func is collected. The gc metamethod will be triggered - * accordingly. + * funcreg[key]->func + * The 'func' part of the tables are weak. That means the registered funcs + * will be collected once the func is no longer available (e.g. overwritten + * by a new instance of the script) * * To make this process happens faster, GC is forced at the end of each * source. - * * TODO: What if some events are triggered with in the sourcing * procedure? * */ - lua_pushstring(L, "_funcreg"); - lua_newtable(L); + prepare_weak_table(L, "_funcreg", "v"); - /* prepare a metatable to indicate weak table */ - lua_newtable(L); - lua_pushstring(L, "__mode"); - lua_pushstring(L, "kv"); - lua_rawset(L, -3); - /* Mark weak table */ - lua_setmetatable(L, -2); - - lua_rawset(L, -3); - lua_pop(L, 1); + /* funcunhook[func]->listener + * The listener is the unhook ticket of the hook. which should be collected + * once the func is collected. The gc metamethod will be triggered + * accordingly. */ + prepare_weak_table(L, "_funcunhook", "k"); return 0; } @@ -1223,7 +1230,7 @@ LuaRegEvent(lua_State *L) int sc, funcreg; luaL_getmetatable(L, "screen"); sc = lua_gettop(L); - funcreg = LuaPushHTable(L, sc, "_funcreg"); + funcreg = LuaPushHTable(L, sc, "_funcunhook"); lua_pushvalue(L, func); lua_pushvalue(L, ticket); lua_rawset(L, funcreg); -- 2.11.4.GIT