From: Rui Guo Date: Mon, 15 Jun 2009 13:46:13 +0000 (+0800) Subject: Clean up of the callback unhook part. X-Git-Url: https://repo.or.cz/w/screen-lua.git/commitdiff_plain/54b9673716885f8972b3a8ff8f8ec61a034c7403 Clean up of the callback unhook part. --- diff --git a/src/lua.c b/src/lua.c index 332c599..d09e280 100644 --- a/src/lua.c +++ b/src/lua.c @@ -98,7 +98,6 @@ struct lua_handler const char *name; int reference; } u; - struct listener *listener; }; typedef struct lua_handler *lua_handler; @@ -319,15 +318,22 @@ struct_register(lua_State *L, const char *name, const luaL_reg fn_methods[], con /** }}} */ /** Callback {{{ */ -PUSH_TYPE(callback, struct lua_handler) +PUSH_TYPE(callback, struct listener) -CHECK_TYPE(callback, struct lua_handler) +CHECK_TYPE(callback, struct listener) static int callback_unhook(lua_State *L) { - struct lua_handler *lh = check_callback(L, 1); - if (!lh->listener) + /*Emulate check_callback() */ + struct listener **ppl; + luaL_checktype(L, 1, LUA_TUSERDATA); + ppl = (struct listener **) luaL_checkudata(L, 1, "callback"); + + if (!ppl) + luaL_typerror(L, 1, "callback"); + + if (!*ppl) { lua_pushboolean(L, 0); lua_pushstring(L, "Callback already unhooked."); @@ -335,17 +341,9 @@ callback_unhook(lua_State *L) } else { - if (lh->type == LUA_HANDLER_TYPE_N) - { - Free(lh->u.name); - } - else - { - LuaHRef(L, lh->u.reference, 0); - lh->u.reference = 0; - } - unregister_listener(lh->listener); - lh->listener = NULL; + LuaFreeHandler(L, &(*ppl)->handler); + unregister_listener(*ppl); + *ppl = 0; lua_pushboolean(L, 1); } return 1; @@ -356,20 +354,7 @@ static const luaL_reg callback_methods[] = { {0, 0} }; -int -callback_gc(lua_State *L) -{ - lua_handler lh = check_callback(L, 1); - /* If the callback is not unhooked, we can not reclaim this memory since - * it's still used by the event dispatcher */ - if (lh->listener) - LuaFreeHandler(L, &lh); - - return 0; -} - static const luaL_reg callback_metamethods[] = { - {"__gc", callback_gc}, {0, 0} }; @@ -1254,9 +1239,8 @@ LuaRegEvent(lua_State *L) else return luaL_error(L, "Handler has already been registered"); } - /* Return the handler for un-register */ - lh->listener = l; - push_callback(L, (lua_handler *)&l->handler); + /* Return the ticket for un-register */ + push_callback(L, &l); } else return luaL_error(L, "Invalid event specified: %s for object %s", event, objname);