diff --git a/modules/module-lua-scripting/module.c b/modules/module-lua-scripting/module.c index 03afa5eb..96eee063 100644 --- a/modules/module-lua-scripting/module.c +++ b/modules/module-lua-scripting/module.c @@ -178,7 +178,7 @@ wp_lua_scripting_plugin_disable (WpPlugin * plugin) { WpLuaScriptingPlugin * self = WP_LUA_SCRIPTING_PLUGIN (plugin); - g_clear_pointer (&self->L, wplua_free); + g_clear_pointer (&self->L, wplua_unref); g_clear_object (&self->export_core); } diff --git a/modules/module-lua-scripting/wplua/wplua.c b/modules/module-lua-scripting/wplua/wplua.c index ec197196..18f942f1 100644 --- a/modules/module-lua-scripting/wplua/wplua.c +++ b/modules/module-lua-scripting/wplua/wplua.c @@ -101,14 +101,39 @@ wplua_new (void) lua_settable (L, LUA_REGISTRYINDEX); } + /* refcount */ + lua_pushinteger (L, 1); + lua_rawsetp (L, LUA_REGISTRYINDEX, L); + + return L; +} + +lua_State * +wplua_ref (lua_State *L) +{ + lua_Integer refcount; + lua_rawgetp (L, LUA_REGISTRYINDEX, L); + refcount = lua_tointeger (L, -1); + lua_pushinteger (L, refcount + 1); + lua_rawsetp (L, LUA_REGISTRYINDEX, L); + lua_pop (L, 1); return L; } void -wplua_free (lua_State * L) +wplua_unref (lua_State * L) { - wp_debug ("closing lua_State %p", L); - lua_close (L); + lua_Integer refcount; + lua_rawgetp (L, LUA_REGISTRYINDEX, L); + refcount = lua_tointeger (L, -1); + if (refcount > 1) { + lua_pushinteger (L, refcount - 1); + lua_rawsetp (L, LUA_REGISTRYINDEX, L); + lua_pop (L, 1); + } else { + wp_debug ("closing lua_State %p", L); + lua_close (L); + } } void diff --git a/modules/module-lua-scripting/wplua/wplua.h b/modules/module-lua-scripting/wplua/wplua.h index 03783f0b..cb34d155 100644 --- a/modules/module-lua-scripting/wplua/wplua.h +++ b/modules/module-lua-scripting/wplua/wplua.h @@ -49,7 +49,8 @@ typedef enum { } WpLuaSandboxFlags; lua_State * wplua_new (void); -void wplua_free (lua_State * L); +lua_State * wplua_ref (lua_State *L); +void wplua_unref (lua_State * L); void wplua_enable_sandbox (lua_State * L, WpLuaSandboxFlags flags); int wplua_push_sandbox (lua_State * L); @@ -94,7 +95,7 @@ gboolean wplua_load_path (lua_State * L, const gchar *path, GError **error); gboolean wplua_pcall (lua_State * L, int nargs, int nres, GError **error); -G_DEFINE_AUTOPTR_CLEANUP_FUNC(lua_State, wplua_free) +G_DEFINE_AUTOPTR_CLEANUP_FUNC(lua_State, wplua_unref) G_END_DECLS diff --git a/tests/wplua/wplua.c b/tests/wplua/wplua.c index 7e8b91e5..dea8f9fc 100644 --- a/tests/wplua/wplua.c +++ b/tests/wplua/wplua.c @@ -262,7 +262,7 @@ static void test_wplua_basic () { lua_State *L = wplua_new (); - wplua_free (L); + wplua_unref (L); } static void @@ -288,7 +288,7 @@ test_wplua_construct () g_object_ref (obj); g_assert_cmpint (obj->ref_count, ==, 2); - wplua_free (L); + wplua_unref (L); g_assert_cmpint (obj->ref_count, ==, 1); } @@ -341,7 +341,7 @@ test_wplua_properties () test_load_and_call (L, code2, sizeof (code2) - 1, 0, 0, &error); g_assert_no_error (error); - wplua_free (L); + wplua_unref (L); } static void @@ -381,7 +381,7 @@ test_wplua_closure () g_assert_true (lua_isboolean (L, -1)); g_assert_true (lua_toboolean (L, -1)); - wplua_free (L); + wplua_unref (L); g_assert_true (closure->is_invalid); g_closure_unref (closure); @@ -420,7 +420,7 @@ test_wplua_signals () "assert(o['test-boolean'] == false)\n"; test_load_and_call (L, code, sizeof (code) - 1, 0, 0, &error); g_assert_no_error (error); - wplua_free (L); + wplua_unref (L); } static void @@ -481,7 +481,7 @@ test_wplua_sandbox_script () g_assert_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_RUNTIME); g_clear_error (&error); - wplua_free (L); + wplua_unref (L); } static void @@ -519,7 +519,7 @@ test_wplua_sandbox_config () test_load_and_call (L, code7, sizeof (code7) - 1, 0, 0, &error); g_assert_no_error (error); - wplua_free (L); + wplua_unref (L); } static void @@ -575,7 +575,7 @@ test_wplua_convert_asv () g_assert_true (g_variant_lookup (nested, "string", "&s", &test_str)); g_assert_cmpstr (test_str, ==, "baz"); - wplua_free (L); + wplua_unref (L); } static void @@ -627,7 +627,7 @@ test_wplua_convert_gvariant_array () g_assert_true (g_variant_lookup (nested, "string", "&s", &test_str)); g_assert_cmpstr (test_str, ==, "baz"); - wplua_free (L); + wplua_unref (L); } static void test_wplua_convert_wp_properties () @@ -665,7 +665,7 @@ test_wplua_convert_wp_properties () test_load_and_call (L, code2, sizeof (code2) - 1, 0, 0, &error); g_assert_no_error (error); - wplua_free (L); + wplua_unref (L); } static void @@ -699,7 +699,7 @@ test_wplua_script_arguments () test_load_and_call (L, code2, sizeof (code2) - 1, 1, 0, &error); g_assert_no_error (error); - wplua_free (L); + wplua_unref (L); } gint