degesch: Lua: use references for hook callbacks

Don't associate the callback with the full userdata object,
we'll need this for something else.
This commit is contained in:
Přemysl Eric Janouch 2016-01-04 22:24:05 +01:00
parent 4832a99461
commit a227060383

View File

@ -7549,6 +7549,7 @@ struct lua_hook
{ {
struct lua_plugin *plugin; ///< The plugin we belong to struct lua_plugin *plugin; ///< The plugin we belong to
enum lua_hook_type type; ///< Type of the hook enum lua_hook_type type; ///< Type of the hook
int ref_callback; ///< Reference to the callback
union union
{ {
struct hook hook; ///< Hook base structure struct hook hook; ///< Hook base structure
@ -7581,6 +7582,9 @@ lua_hook_unhook (lua_State *L)
break; break;
} }
luaL_unref (L, LUA_REGISTRYINDEX, hook->ref_callback);
hook->ref_callback = LUA_REFNIL;
// The hook no longer has to stay alive // The hook no longer has to stay alive
hook->type = XLUA_HOOK_DEFUNCT; hook->type = XLUA_HOOK_DEFUNCT;
lua_cache_invalidate (L, hook); lua_cache_invalidate (L, hook);
@ -7668,10 +7672,8 @@ lua_input_hook_filter (struct input_hook *self, struct buffer *buffer,
lua_pushcfunction (L, lua_plugin_error_handler); lua_pushcfunction (L, lua_plugin_error_handler);
lua_rawgeti (L, LUA_REGISTRYINDEX, hook->ref_callback);
lua_rawgetp (L, LUA_REGISTRYINDEX, hook); // 1: hook lua_rawgetp (L, LUA_REGISTRYINDEX, hook); // 1: hook
lua_getuservalue (L, -1); // Retrieve function
lua_insert (L, -2); // Swap with the hook
lua_plugin_push_buffer (plugin, buffer); // 2: buffer lua_plugin_push_buffer (plugin, buffer); // 2: buffer
lua_pushstring (L, input); // 3: input lua_pushstring (L, input); // 3: input
@ -7706,10 +7708,8 @@ lua_irc_hook_filter (struct irc_hook *self, struct server *s, char *message)
lua_pushcfunction (L, lua_plugin_error_handler); lua_pushcfunction (L, lua_plugin_error_handler);
lua_rawgeti (L, LUA_REGISTRYINDEX, hook->ref_callback);
lua_rawgetp (L, LUA_REGISTRYINDEX, hook); // 1: hook lua_rawgetp (L, LUA_REGISTRYINDEX, hook); // 1: hook
lua_getuservalue (L, -1); // Retrieve function
lua_insert (L, -2); // Swap with the hook
lua_plugin_push_server (plugin, s); // 2: server lua_plugin_push_server (plugin, s); // 2: server
lua_pushstring (L, message); // 3: message lua_pushstring (L, message); // 3: message
@ -7743,9 +7743,8 @@ lua_timer_hook_dispatch (void *user_data)
lua_pushcfunction (L, lua_plugin_error_handler); lua_pushcfunction (L, lua_plugin_error_handler);
lua_rawgeti (L, LUA_REGISTRYINDEX, hook->ref_callback);
lua_rawgetp (L, LUA_REGISTRYINDEX, hook); // 1: hook lua_rawgetp (L, LUA_REGISTRYINDEX, hook); // 1: hook
lua_getuservalue (L, -1); // Retrieve function
lua_insert (L, -2); // Swap with the hook
if (lua_pcall (L, 1, 0, -3)) if (lua_pcall (L, 1, 0, -3))
{ {
@ -7778,9 +7777,8 @@ lua_plugin_push_hook (struct lua_plugin *plugin, int callback_index,
hook->type = type; hook->type = type;
hook->plugin = plugin; hook->plugin = plugin;
// Associate the callback with the hook
lua_pushvalue (L, callback_index); lua_pushvalue (L, callback_index);
lua_setuservalue (L, -2); hook->ref_callback = luaL_ref (L, LUA_REGISTRYINDEX);
// Make sure the hook doesn't get garbage collected and return it // Make sure the hook doesn't get garbage collected and return it
lua_cache_store (L, hook, -1); lua_cache_store (L, hook, -1);