| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2020 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "wplua.h" |
| 10 |
#include "private.h" |
| 11 |
#include <wp/wp.h> |
| 12 |
|
| 13 |
/* This structure is added to a lua global and it's only referenced from there; |
| 14 |
When the lua_State closes, it is unrefed and its finalize function below |
| 15 |
invalidates all the closures so that nothing attempts to call into a lua |
| 16 |
function after the state is closed */ |
| 17 |
typedef struct _WpLuaClosureStore WpLuaClosureStore; |
| 18 |
struct _WpLuaClosureStore |
| 19 |
{ |
| 20 |
GPtrArray *closures; |
| 21 |
}; |
| 22 |
|
| 23 |
static WpLuaClosureStore * |
| 24 |
_wplua_closure_store_new (void) |
| 25 |
{ |
| 26 |
WpLuaClosureStore *self = g_rc_box_new (WpLuaClosureStore); |
| 27 |
self->closures = g_ptr_array_new (); |
| 28 |
return self; |
| 29 |
} |
| 30 |
|
| 31 |
static void |
| 32 |
_wplua_closure_store_finalize (WpLuaClosureStore * self) |
| 33 |
{ |
| 34 |
for (guint i = self->closures->len; i > 0; i--) { |
| 35 |
GClosure *c = g_ptr_array_index (self->closures, i-1); |
| 36 |
g_closure_ref (c); |
| 37 |
g_closure_invalidate (c); |
| 38 |
g_ptr_array_remove_index_fast (self->closures, i-1); |
| 39 |
g_closure_unref (c); |
| 40 |
} |
| 41 |
g_ptr_array_unref (self->closures); |
| 42 |
} |
| 43 |
|
| 44 |
static WpLuaClosureStore * |
| 45 |
_wplua_closure_store_ref (WpLuaClosureStore * self) |
| 46 |
{ |
| 47 |
return g_rc_box_acquire (self); |
| 48 |
} |
| 49 |
|
| 50 |
static void |
| 51 |
_wplua_closure_store_unref (WpLuaClosureStore * self) |
| 52 |
{ |
| 53 |
g_rc_box_release_full (self, (GDestroyNotify) _wplua_closure_store_finalize); |
| 54 |
} |
| 55 |
|
| 56 |
G_DEFINE_BOXED_TYPE(WpLuaClosureStore, _wplua_closure_store, |
| 57 |
_wplua_closure_store_ref, _wplua_closure_store_unref) |
| 58 |
|
| 59 |
|
| 60 |
typedef struct _WpLuaClosure WpLuaClosure; |
| 61 |
struct _WpLuaClosure |
| 62 |
{ |
| 63 |
GClosure closure; |
| 64 |
int func_ref; |
| 65 |
GPtrArray *closures; |
| 66 |
}; |
| 67 |
|
| 68 |
static void |
| 69 |
_wplua_closure_marshal (GClosure *closure, GValue *return_value, |
| 70 |
guint n_param_values, const GValue *param_values, |
| 71 |
gpointer invocation_hint, gpointer marshal_data) |
| 72 |
{ |
| 73 |
static int reentrant = 0; |
| 74 |
lua_State *L = closure->data; |
| 75 |
int func_ref = ((WpLuaClosure *) closure)->func_ref; |
| 76 |
|
| 77 |
/* invalid closure, skip it */ |
| 78 |
if (func_ref == LUA_NOREF || func_ref == LUA_REFNIL) |
| 79 |
return; |
| 80 |
|
| 81 |
/* stop the garbage collector */ |
| 82 |
if (reentrant == 0) |
| 83 |
lua_gc (L, LUA_GCSTOP, 0); |
| 84 |
|
| 85 |
/* push the function */ |
| 86 |
lua_rawgeti (L, LUA_REGISTRYINDEX, func_ref); |
| 87 |
|
| 88 |
/* push arguments */ |
| 89 |
for (guint i = 0; i < n_param_values; i++) |
| 90 |
wplua_gvalue_to_lua (L, ¶m_values[i]); |
| 91 |
|
| 92 |
/* call in protected mode */ |
| 93 |
reentrant++; |
| 94 |
int res = _wplua_pcall (L, n_param_values, return_value ? 1 : 0); |
| 95 |
reentrant--; |
| 96 |
|
| 97 |
/* handle the result */ |
| 98 |
if (res == LUA_OK && return_value) { |
| 99 |
wplua_lua_to_gvalue (L, -1, return_value); |
| 100 |
lua_pop (L, 1); |
| 101 |
} |
| 102 |
|
| 103 |
/* clean up */ |
| 104 |
lua_gc (L, LUA_GCCOLLECT, 0); |
| 105 |
if (reentrant == 0) |
| 106 |
lua_gc (L, LUA_GCRESTART, 0); |
| 107 |
} |
| 108 |
|
| 109 |
static void |
| 110 |
_wplua_closure_invalidate (lua_State *L, WpLuaClosure *c) |
| 111 |
{ |
| 112 |
wp_trace_boxed (G_TYPE_CLOSURE, c, "invalidated"); |
| 113 |
luaL_unref (L, LUA_REGISTRYINDEX, c->func_ref); |
| 114 |
c->func_ref = LUA_NOREF; |
| 115 |
} |
| 116 |
|
| 117 |
static void |
| 118 |
_wplua_closure_finalize (lua_State *L, WpLuaClosure *c) |
| 119 |
{ |
| 120 |
g_ptr_array_remove_fast (c->closures, c); |
| 121 |
g_ptr_array_unref (c->closures); |
| 122 |
} |
| 123 |
|
| 124 |
GClosure * |
| 125 |
wplua_checkclosure (lua_State *L, int idx) |
| 126 |
{ |
| 127 |
luaL_checktype (L, idx, LUA_TFUNCTION); |
| 128 |
return wplua_function_to_closure (L, idx); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* wplua_function_to_closure: |
| 133 |
* |
| 134 |
* Make a GClosure out of a Lua function at index @em idx |
| 135 |
* |
| 136 |
* Returns: (transfer floating): the new closure |
| 137 |
*/ |
| 138 |
GClosure * |
| 139 |
wplua_function_to_closure (lua_State *L, int idx) |
| 140 |
{ |
| 141 |
g_return_val_if_fail (lua_isfunction(L, idx), NULL); |
| 142 |
|
| 143 |
GClosure *c = g_closure_new_simple (sizeof (WpLuaClosure), L); |
| 144 |
WpLuaClosure *wlc = (WpLuaClosure *) c; |
| 145 |
WpLuaClosureStore *store; |
| 146 |
|
| 147 |
lua_pushvalue (L, idx); |
| 148 |
wlc->func_ref = luaL_ref (L, LUA_REGISTRYINDEX); |
| 149 |
|
| 150 |
wp_trace_boxed (G_TYPE_CLOSURE, c, "created, func_ref = %d", wlc->func_ref); |
| 151 |
|
| 152 |
g_closure_set_marshal (c, _wplua_closure_marshal); |
| 153 |
g_closure_add_invalidate_notifier (c, L, |
| 154 |
(GClosureNotify) _wplua_closure_invalidate); |
| 155 |
g_closure_add_finalize_notifier (c, L, |
| 156 |
(GClosureNotify) _wplua_closure_finalize); |
| 157 |
|
| 158 |
/* keep a weak ref of the closure in the store's array, |
| 159 |
so that we can invalidate the closure when lua_State closes; |
| 160 |
keep a strong ref of the array in the closure so that |
| 161 |
_wplua_closure_finalize() works even after the state is closed */ |
| 162 |
lua_pushliteral (L, "wplua_closures"); |
| 163 |
lua_gettable (L, LUA_REGISTRYINDEX); |
| 164 |
store = wplua_toboxed (L, -1); |
| 165 |
lua_pop (L, 1); |
| 166 |
|
| 167 |
g_ptr_array_add (store->closures, c); |
| 168 |
wlc->closures = g_ptr_array_ref (store->closures); |
| 169 |
|
| 170 |
return c; |
| 171 |
} |
| 172 |
|
| 173 |
void |
| 174 |
_wplua_init_closure (lua_State *L) |
| 175 |
{ |
| 176 |
lua_pushliteral (L, "wplua_closures"); |
| 177 |
wplua_pushboxed (L, |
| 178 |
_wplua_closure_store_get_type (), |
| 179 |
_wplua_closure_store_new ()); |
| 180 |
lua_settable (L, LUA_REGISTRYINDEX); |
| 181 |
} |