From ccf6f431bbd96f04369875d72a61976bb6609a69 Mon Sep 17 00:00:00 2001 From: "William D. Jones" Date: Wed, 16 Feb 2022 10:24:40 -0500 Subject: [PATCH] Return userdata from pluginprefs __pairs metamethod to avoid immediate GC. --- plugins/lua/lua.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/plugins/lua/lua.c b/plugins/lua/lua.c index d1370eaf..f8f051f4 100644 --- a/plugins/lua/lua.c +++ b/plugins/lua/lua.c @@ -957,16 +957,21 @@ static int api_hexchat_pluginprefs_meta_pairs(lua_State *L) hexchat_plugin *h; if(!script->name) + return luaL_error(L, "cannot use hexchat.pluginprefs before registering with hexchat.register"); dest = lua_newuserdata(L, 4096); + h = script->handle; if(!hexchat_pluginpref_list(h, dest)) strcpy(dest, ""); lua_pushlightuserdata(L, dest); lua_pushlightuserdata(L, dest); lua_pushcclosure(L, api_hexchat_pluginprefs_meta_pairs_closure, 2); - return 1; + lua_insert(L, -2); // Return the userdata (second return value from pairs), + // even though it's not used by the closure (first return + // value from pairs), so that Lua knows not to GC it. + return 2; } static int api_attrs_meta_index(lua_State *L) @@ -1764,4 +1769,3 @@ G_MODULE_EXPORT int hexchat_plugin_deinit(hexchat_plugin *plugin_handle) g_clear_pointer(&expand_buffer, g_free); return 1; } -