From: Rui Guo Date: Mon, 15 Jun 2009 14:37:32 +0000 (+0800) Subject: Do not do refcount any more. X-Git-Url: https://repo.or.cz/w/screen-lua.git/commitdiff_plain/c281b4a5713323ab7cfe0b2b3473f0c33a0ef0a8 Do not do refcount any more. --- diff --git a/src/lua.c b/src/lua.c index 72f7840..d5fb1d9 100644 --- a/src/lua.c +++ b/src/lua.c @@ -102,7 +102,7 @@ struct lua_handler typedef struct lua_handler *lua_handler; void LuaPushHandler(lua_State *L, lua_handler lh); -void LuaHRef(lua_State *L, int key, int reg); +void LuaHUnRef(lua_State *L, int key); lua_handler LuaCheckHandler(lua_State *L, int idx, int ref); lua_handler @@ -134,7 +134,7 @@ LuaFreeHandler(lua_State *L, struct lua_handler **lh) Free((*lh)->u.name) else { - LuaHRef(L, (*lh)->u.reference, 0); + LuaHUnRef(L, (*lh)->u.reference); (*lh)->u.reference = 0; } Free(*lh); @@ -320,8 +320,6 @@ struct_register(lua_State *L, const char *name, const luaL_reg fn_methods[], con /** Callback {{{ */ PUSH_TYPE(callback, struct listener) -CHECK_TYPE(callback, struct listener) - static int callback_unhook(lua_State *L) { @@ -826,16 +824,10 @@ int LuaInit(void) /* To store handler funcs */ luaL_getmetatable(L, "screen"); - /* two kinds of info in this table: - * _funckey[func]->key - * _keyfunc[key]->func */ + /* _keyfunc[key]->func */ lua_pushstring(L, "_funcreg"); lua_newtable(L); lua_rawset(L, -3); - /* _refcnt[key]->cnt */ - lua_pushstring(L, "_refcnt"); - lua_newtable(L); - lua_rawset(L, -3); lua_pop(L, 1); return 0; @@ -1029,40 +1021,6 @@ LuaPushHandler(lua_State *L, lua_handler lh) } } -void -LuaHRef(lua_State *L, int key, int reg) -{ - int refcnt, cnt, sc; - luaL_getmetatable(L, "screen"); - sc = lua_gettop(L); - refcnt = LuaPushHTable(L, sc, "_refcnt"); - - lua_rawgeti(L, refcnt, key); - cnt = lua_tointeger(L, -1);/*cnt = refcnt[key]*/ - if (!reg && cnt <= 1) - { - /*release the last reference*/ - int idx, funcreg = LuaPushHTable(L, sc, "_funcreg"); - lua_rawgeti(L, funcreg, key); - idx = lua_gettop(L); - - luaL_unref(L, funcreg, key); - lua_pushvalue(L, idx); - lua_pushnil(L); - lua_settable(L, funcreg); /*funcreg[func]=nil*/ - lua_pop(L, 5); - return; - } - else - { - lua_pushinteger(L, key); - lua_pushinteger(L, reg? cnt + 1 : cnt - 1); - lua_settable(L, refcnt); /*refcnt[key] = cnt + 1 or cnt - 1*/ - } - lua_pop(L, 3); -} - -/* Ref when asked to. Never unref*/ int LuaFuncKey(lua_State *L, int idx, int ref) { @@ -1072,42 +1030,21 @@ LuaFuncKey(lua_State *L, int idx, int ref) funcreg = LuaPushHTable(L, sc, "_funcreg"); lua_pushvalue(L, idx); - lua_gettable(L, funcreg);/*funcreg[func] ==?*/ - if (lua_isnil(L, -1)) - { - int refcnt; - /* Not found, alloc a new key */ - if (!ref) - return LUA_NOREF; - - /*funcreg[key] = func*/ - lua_pushvalue(L, idx); - key = luaL_ref(L, funcreg); - - /*funcreg[func]=key*/ - lua_pushvalue(L, idx); - lua_pushinteger(L, key); - lua_settable(L, funcreg); - - /*refcnt[key] = func*/ - refcnt = LuaPushHTable(L, sc, "_refcnt"); - lua_pushinteger(L, key); - lua_pushinteger(L, 1); - lua_settable(L, refcnt); - - lua_pop(L, 1); - } - else - { - key = lua_tointeger(L, -1);/*key = funckey[func]*/ - if (ref) - LuaHRef(L, key, 1); - } - - lua_pop(L, 3); + key = luaL_ref(L, funcreg); + lua_pop(L, 2); return key; } +void +LuaHUnRef(lua_State *L, int key) +{ + int funcreg, sc; + luaL_getmetatable(L, "screen"); + sc = lua_gettop(L); + funcreg = LuaPushHTable(L, sc, "_funcreg"); + luaL_unref(L, funcreg, key); +} + lua_handler LuaCheckHandler(lua_State *L, int idx, int ref) {