| 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 |
static int |
| 14 |
_wplua_gobject_call (lua_State *L) |
| 15 |
{ |
| 16 |
GObject *obj = wplua_checkobject (L, 1, G_TYPE_OBJECT); |
| 17 |
const char *sig_name = lua_tostring (L, 2); |
| 18 |
guint n_params = lua_gettop (L) - 2; |
| 19 |
GSignalQuery query; |
| 20 |
guint sig_id = 0; |
| 21 |
GQuark detail = 0; |
| 22 |
|
| 23 |
if (G_UNLIKELY (!g_signal_parse_name (sig_name, G_TYPE_FROM_INSTANCE (obj), |
| 24 |
&sig_id, &detail, FALSE))) |
| 25 |
luaL_error (L, "unknown signal '%s::%s'", G_OBJECT_TYPE_NAME (obj), |
| 26 |
sig_name); |
| 27 |
|
| 28 |
g_signal_query (sig_id, &query); |
| 29 |
|
| 30 |
if (G_UNLIKELY (!(query.signal_flags & G_SIGNAL_ACTION))) |
| 31 |
luaL_error (L, "lua code is not allowed to emit non-action signal '%s::%s'", |
| 32 |
G_OBJECT_TYPE_NAME (obj), sig_name); |
| 33 |
|
| 34 |
if (G_UNLIKELY (query.n_params > n_params)) |
| 35 |
luaL_error (L, "not enough arguments for '%s::%s': expected %d, got %d", |
| 36 |
G_OBJECT_TYPE_NAME (obj), sig_name, query.n_params, n_params); |
| 37 |
|
| 38 |
GValue ret = G_VALUE_INIT; |
| 39 |
GValue *vals = g_newa (GValue, n_params + 1); |
| 40 |
memset (vals, 0, sizeof (GValue) * (n_params + 1)); |
| 41 |
|
| 42 |
if (query.return_type != G_TYPE_NONE) |
| 43 |
g_value_init (&ret, query.return_type); |
| 44 |
|
| 45 |
g_value_init_from_instance (&vals[0], obj); |
| 46 |
for (guint i = 0; i < n_params; i++) { |
| 47 |
g_value_init (&vals[i+1], query.param_types[i]); |
| 48 |
wplua_lua_to_gvalue (L, i+3, &vals[i+1]); |
| 49 |
} |
| 50 |
|
| 51 |
g_signal_emitv (vals, sig_id, detail, &ret); |
| 52 |
|
| 53 |
for (guint i = 0; i < n_params + 1; i++) { |
| 54 |
g_value_unset (&vals[i]); |
| 55 |
} |
| 56 |
|
| 57 |
int n_ret = 0; |
| 58 |
if (query.return_type != G_TYPE_NONE) |
| 59 |
n_ret = wplua_gvalue_to_lua (L, &ret); |
| 60 |
|
| 61 |
g_value_unset (&ret); |
| 62 |
return n_ret; |
| 63 |
} |
| 64 |
|
| 65 |
static int |
| 66 |
_wplua_gobject_connect (lua_State *L) |
| 67 |
{ |
| 68 |
GObject *obj = wplua_checkobject (L, 1, G_TYPE_OBJECT); |
| 69 |
const char *sig_name = luaL_checkstring (L, 2); |
| 70 |
luaL_checktype (L, 3, LUA_TFUNCTION); |
| 71 |
|
| 72 |
guint sig_id = 0; |
| 73 |
GQuark detail = 0; |
| 74 |
|
| 75 |
if (G_UNLIKELY (!g_signal_parse_name (sig_name, G_TYPE_FROM_INSTANCE (obj), |
| 76 |
&sig_id, &detail, FALSE))) |
| 77 |
luaL_error (L, "unknown signal '%s::%s'", G_OBJECT_TYPE_NAME (obj), |
| 78 |
sig_name); |
| 79 |
|
| 80 |
GClosure *closure = wplua_function_to_closure (L, 3); |
| 81 |
gulong handler = |
| 82 |
g_signal_connect_closure_by_id (obj, sig_id, detail, closure, FALSE); |
| 83 |
|
| 84 |
lua_pushinteger (L, handler); |
| 85 |
return 1; |
| 86 |
} |
| 87 |
|
| 88 |
static lua_CFunction |
| 89 |
find_method_in_luaL_Reg (luaL_Reg *reg, const gchar *method) |
| 90 |
{ |
| 91 |
if (reg) { |
| 92 |
while (reg->name) { |
| 93 |
if (!g_strcmp0 (method, reg->name)) |
| 94 |
return reg->func; |
| 95 |
reg++; |
| 96 |
} |
| 97 |
} |
| 98 |
return NULL; |
| 99 |
} |
| 100 |
|
| 101 |
static int |
| 102 |
_wplua_gobject___index (lua_State *L) |
| 103 |
{ |
| 104 |
GObject *obj = wplua_checkobject (L, 1, G_TYPE_OBJECT); |
| 105 |
const gchar *key = luaL_checkstring (L, 2); |
| 106 |
lua_CFunction func = NULL; |
| 107 |
GHashTable *vtables; |
| 108 |
|
| 109 |
lua_pushliteral (L, "wplua_vtables"); |
| 110 |
lua_gettable (L, LUA_REGISTRYINDEX); |
| 111 |
vtables = wplua_toboxed (L, -1); |
| 112 |
lua_pop (L, 1); |
| 113 |
|
| 114 |
if (!g_strcmp0 (key, "call")) |
| 115 |
func = _wplua_gobject_call; |
| 116 |
else if (!g_strcmp0 (key, "connect")) |
| 117 |
func = _wplua_gobject_connect; |
| 118 |
|
| 119 |
/* search in registered vtables */ |
| 120 |
if (!func) { |
| 121 |
GType type = G_TYPE_FROM_INSTANCE (obj); |
| 122 |
while (!func && type) { |
| 123 |
luaL_Reg *reg = g_hash_table_lookup (vtables, GUINT_TO_POINTER (type)); |
| 124 |
func = find_method_in_luaL_Reg (reg, key); |
| 125 |
type = g_type_parent (type); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/* search in registered vtables of interfaces */ |
| 130 |
if (!func) { |
| 131 |
g_autofree GType *interfaces = |
| 132 |
g_type_interfaces (G_TYPE_FROM_INSTANCE (obj), NULL); |
| 133 |
GType *type = interfaces; |
| 134 |
while (!func && *type) { |
| 135 |
luaL_Reg *reg = g_hash_table_lookup (vtables, GUINT_TO_POINTER (*type)); |
| 136 |
func = find_method_in_luaL_Reg (reg, key); |
| 137 |
type++; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
if (func) { |
| 142 |
lua_pushcfunction (L, func); |
| 143 |
return 1; |
| 144 |
} |
| 145 |
else { |
| 146 |
/* search in properties */ |
| 147 |
GObjectClass *klass = G_OBJECT_GET_CLASS (obj); |
| 148 |
GParamSpec *pspec = g_object_class_find_property (klass, key); |
| 149 |
if (pspec && (pspec->flags & G_PARAM_READABLE)) { |
| 150 |
g_auto (GValue) v = G_VALUE_INIT; |
| 151 |
g_value_init (&v, pspec->value_type); |
| 152 |
g_object_get_property (obj, key, &v); |
| 153 |
return wplua_gvalue_to_lua (L, &v); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
static int |
| 161 |
_wplua_gobject___newindex (lua_State *L) |
| 162 |
{ |
| 163 |
GObject *obj = wplua_checkobject (L, 1, G_TYPE_OBJECT); |
| 164 |
const gchar *key = luaL_checkstring (L, 2); |
| 165 |
|
| 166 |
/* search in properties */ |
| 167 |
GObjectClass *klass = G_OBJECT_GET_CLASS (obj); |
| 168 |
GParamSpec *pspec = g_object_class_find_property (klass, key); |
| 169 |
if (pspec && (pspec->flags & G_PARAM_WRITABLE)) { |
| 170 |
g_auto (GValue) v = G_VALUE_INIT; |
| 171 |
g_value_init (&v, pspec->value_type); |
| 172 |
wplua_lua_to_gvalue (L, 3, &v); |
| 173 |
g_object_set_property (obj, key, &v); |
| 174 |
} else { |
| 175 |
luaL_error (L, "attempted to assign unknown or non-writable property '%s'", |
| 176 |
key); |
| 177 |
} |
| 178 |
return 0; |
| 179 |
} |
| 180 |
|
| 181 |
static int |
| 182 |
_wplua_gobject__tostring (lua_State *L) |
| 183 |
{ |
| 184 |
GObject *obj; |
| 185 |
gchar *str; |
| 186 |
|
| 187 |
obj = wplua_checkobject (L, 1, G_TYPE_OBJECT); |
| 188 |
str = g_strdup_printf (WP_OBJECT_FORMAT, WP_OBJECT_ARGS (obj)); |
| 189 |
lua_pushstring (L, str); |
| 190 |
g_free (str); |
| 191 |
return 1; |
| 192 |
} |
| 193 |
|
| 194 |
static int |
| 195 |
_wplua_gobject___eq (lua_State *L) |
| 196 |
{ |
| 197 |
return _wplua_gvalue_userdata___eq_impl (L, "GObject"); |
| 198 |
} |
| 199 |
|
| 200 |
void |
| 201 |
_wplua_init_gobject (lua_State *L) |
| 202 |
{ |
| 203 |
static const luaL_Reg gobject_meta[] = { |
| 204 |
{ "__gc", _wplua_gvalue_userdata___gc }, |
| 205 |
{ "__eq", _wplua_gobject___eq }, |
| 206 |
{ "__index", _wplua_gobject___index }, |
| 207 |
{ "__newindex", _wplua_gobject___newindex }, |
| 208 |
{ "__tostring", _wplua_gobject__tostring }, |
| 209 |
{ NULL, NULL } |
| 210 |
}; |
| 211 |
|
| 212 |
if (!luaL_newmetatable (L, "GObject")) |
| 213 |
g_error ("Metatable with key GObject in the registry already exists?"); |
| 214 |
luaL_setfuncs (L, gobject_meta, 0); |
| 215 |
lua_pop (L, 1); |
| 216 |
} |
| 217 |
|
| 218 |
void |
| 219 |
wplua_pushobject (lua_State * L, gpointer object) |
| 220 |
{ |
| 221 |
g_return_if_fail (G_IS_OBJECT (object)); |
| 222 |
|
| 223 |
GValue *v = _wplua_pushgvalue_userdata (L, G_TYPE_FROM_INSTANCE (object)); |
| 224 |
wp_trace_object (object, "pushing to Lua, v=%p", v); |
| 225 |
g_value_take_object (v, object); |
| 226 |
} |
| 227 |
|
| 228 |
gpointer |
| 229 |
wplua_toobject (lua_State *L, int idx) |
| 230 |
{ |
| 231 |
GValue *v = _wplua_togvalue_userdata_named (L, idx, G_TYPE_OBJECT, "GObject"); |
| 232 |
|
| 233 |
g_return_val_if_fail (v, NULL); |
| 234 |
return g_value_get_object (v); |
| 235 |
} |
| 236 |
|
| 237 |
gpointer |
| 238 |
wplua_checkobject (lua_State *L, int idx, GType type) |
| 239 |
{ |
| 240 |
GValue *v = _wplua_togvalue_userdata_named (L, idx, type, "GObject"); |
| 241 |
if (v == NULL) { |
| 242 |
wp_critical ("expected userdata storing GValue<%s>", g_type_name (type)); |
| 243 |
luaL_argerror (L, idx, "expected userdata storing GValue<GObject>"); |
| 244 |
} |
| 245 |
return g_value_get_object (v); |
| 246 |
} |
| 247 |
|
| 248 |
gboolean |
| 249 |
wplua_isobject (lua_State *L, int idx, GType type) |
| 250 |
{ |
| 251 |
return g_type_is_a (type, G_TYPE_OBJECT) && |
| 252 |
_wplua_togvalue_userdata_named (L, idx, type, "GObject") != NULL; |
| 253 |
} |