summaryrefslogtreecommitdiff
path: root/degesch.c
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2016-01-04 22:24:05 +0100
committerPřemysl Janouch <p.janouch@gmail.com>2016-01-04 22:24:05 +0100
commita227060383b4c61dd54cffba16ef26f1757e004b (patch)
treeb66a1e407d76a8e84e92ba6f88f8264b48ff87a1 /degesch.c
parent4832a994616bb9e7b8037cc1e58c96fc84da85e5 (diff)
downloadxK-a227060383b4c61dd54cffba16ef26f1757e004b.tar.gz
xK-a227060383b4c61dd54cffba16ef26f1757e004b.tar.xz
xK-a227060383b4c61dd54cffba16ef26f1757e004b.zip
degesch: Lua: use references for hook callbacks
Don't associate the callback with the full userdata object, we'll need this for something else.
Diffstat (limited to 'degesch.c')
-rw-r--r--degesch.c18
1 files 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);