From a227060383b4c61dd54cffba16ef26f1757e004b Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Mon, 4 Jan 2016 22:24:05 +0100
Subject: degesch: Lua: use references for hook callbacks
Don't associate the callback with the full userdata object,
we'll need this for something else.
---
degesch.c | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
diff --git a/degesch.c b/degesch.c
index 0a8d4b5..b66114b 100644
--- a/degesch.c
+++ b/degesch.c
@@ -7549,6 +7549,7 @@ struct lua_hook
{
struct lua_plugin *plugin; ///< The plugin we belong to
enum lua_hook_type type; ///< Type of the hook
+ int ref_callback; ///< Reference to the callback
union
{
struct hook hook; ///< Hook base structure
@@ -7581,6 +7582,9 @@ lua_hook_unhook (lua_State *L)
break;
}
+ luaL_unref (L, LUA_REGISTRYINDEX, hook->ref_callback);
+ hook->ref_callback = LUA_REFNIL;
+
// The hook no longer has to stay alive
hook->type = XLUA_HOOK_DEFUNCT;
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_rawgeti (L, LUA_REGISTRYINDEX, hook->ref_callback);
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_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_rawgeti (L, LUA_REGISTRYINDEX, hook->ref_callback);
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_pushstring (L, message); // 3: message
@@ -7743,9 +7743,8 @@ lua_timer_hook_dispatch (void *user_data)
lua_pushcfunction (L, lua_plugin_error_handler);
+ lua_rawgeti (L, LUA_REGISTRYINDEX, hook->ref_callback);
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))
{
@@ -7778,9 +7777,8 @@ lua_plugin_push_hook (struct lua_plugin *plugin, int callback_index,
hook->type = type;
hook->plugin = plugin;
- // Associate the callback with the hook
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
lua_cache_store (L, hook, -1);
--
cgit v1.2.3-70-g09d2