| 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 lua_CFunction |
| 14 |
find_method_in_luaL_Reg (luaL_Reg *reg, const gchar *method) |
| 15 |
{ |
| 16 |
if (reg) { |
| 17 |
while (reg->name) { |
| 18 |
if (!g_strcmp0 (method, reg->name)) |
| 19 |
return reg->func; |
| 20 |
reg++; |
| 21 |
} |
| 22 |
} |
| 23 |
return NULL; |
| 24 |
} |
| 25 |
|
| 26 |
static int |
| 27 |
_wplua_gboxed___index (lua_State *L) |
| 28 |
{ |
| 29 |
GValue *obj_v = _wplua_togvalue_userdata_named (L, 1, G_TYPE_BOXED, "GBoxed"); |
| 30 |
luaL_argcheck (L, obj_v != NULL, 1, |
| 31 |
"expected userdata storing GValue<GBoxed>"); |
| 32 |
const gchar *key = luaL_tolstring (L, 2, NULL); |
| 33 |
GType type = G_VALUE_TYPE (obj_v); |
| 34 |
GType boxed_type = type; |
| 35 |
lua_CFunction func = NULL; |
| 36 |
GHashTable *vtables; |
| 37 |
|
| 38 |
lua_pushliteral (L, "wplua_vtables"); |
| 39 |
lua_gettable (L, LUA_REGISTRYINDEX); |
| 40 |
vtables = wplua_toboxed (L, -1); |
| 41 |
lua_pop (L, 1); |
| 42 |
|
| 43 |
/* search in registered vtables */ |
| 44 |
while (!func && type) { |
| 45 |
luaL_Reg *reg = g_hash_table_lookup (vtables, GUINT_TO_POINTER (type)); |
| 46 |
func = find_method_in_luaL_Reg (reg, key); |
| 47 |
type = g_type_parent (type); |
| 48 |
} |
| 49 |
|
| 50 |
wp_trace_boxed (type, g_value_get_boxed (obj_v), |
| 51 |
"indexing GBoxed, looking for '%s', found: %p", key, func); |
| 52 |
|
| 53 |
if (func) { |
| 54 |
lua_pushcfunction (L, func); |
| 55 |
return 1; |
| 56 |
} |
| 57 |
|
| 58 |
/* If WpProperties type, just return the property value for that key */ |
| 59 |
if (boxed_type == WP_TYPE_PROPERTIES) { |
| 60 |
WpProperties * props = g_value_get_boxed (obj_v); |
| 61 |
const gchar *val = wp_properties_get (props, key); |
| 62 |
lua_pushstring (L, val); |
| 63 |
return 1; |
| 64 |
} |
| 65 |
|
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
static int |
| 70 |
_wplua_gboxed___newindex (lua_State *L) |
| 71 |
{ |
| 72 |
GValue *obj_v = _wplua_togvalue_userdata_named (L, 1, G_TYPE_BOXED, "GBoxed"); |
| 73 |
luaL_argcheck (L, obj_v != NULL, 1, |
| 74 |
"expected userdata storing GValue<GBoxed>"); |
| 75 |
const gchar *key = luaL_tolstring (L, 2, NULL); |
| 76 |
GType type = G_VALUE_TYPE (obj_v); |
| 77 |
|
| 78 |
/* Set property value */ |
| 79 |
if (type == WP_TYPE_PROPERTIES) { |
| 80 |
WpProperties * props = g_value_dup_boxed (obj_v); |
| 81 |
g_autofree gchar *val = NULL; |
| 82 |
luaL_checkany (L, 3); |
| 83 |
|
| 84 |
switch (lua_type (L, 3)) { |
| 85 |
case LUA_TNIL: |
| 86 |
break; |
| 87 |
case LUA_TUSERDATA: { |
| 88 |
if (wplua_gvalue_userdata_type (L, 3) != G_TYPE_INVALID) { |
| 89 |
GValue *v = lua_touserdata (L, 3); |
| 90 |
gpointer p = g_value_peek_pointer (v); |
| 91 |
val = g_strdup_printf ("%p", p); |
| 92 |
break; |
| 93 |
} else { |
| 94 |
val = g_strdup (luaL_tolstring (L, 3, NULL)); |
| 95 |
break; |
| 96 |
} |
| 97 |
} |
| 98 |
default: |
| 99 |
val = g_strdup (luaL_tolstring (L, 3, NULL)); |
| 100 |
break; |
| 101 |
} |
| 102 |
|
| 103 |
props = wp_properties_ensure_unique_owner (props); |
| 104 |
wp_properties_set (props, key, val); |
| 105 |
g_value_take_boxed (obj_v, props); |
| 106 |
} else { |
| 107 |
luaL_error (L, "cannot assign property '%s' to boxed type %s", |
| 108 |
key, g_type_name (type)); |
| 109 |
} |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
static int |
| 114 |
properties_iterator_next (lua_State *L) |
| 115 |
{ |
| 116 |
WpIterator *it = wplua_checkboxed (L, 1, WP_TYPE_ITERATOR); |
| 117 |
g_auto (GValue) item = G_VALUE_INIT; |
| 118 |
if (wp_iterator_next (it, &item)) { |
| 119 |
WpPropertiesItem *si = g_value_get_boxed (&item); |
| 120 |
const gchar *k = wp_properties_item_get_key (si); |
| 121 |
const gchar *v = wp_properties_item_get_value (si); |
| 122 |
lua_pushstring (L, k); |
| 123 |
lua_pushstring (L, v); |
| 124 |
return 2; |
| 125 |
} else { |
| 126 |
lua_pushnil (L); |
| 127 |
lua_pushnil (L); |
| 128 |
return 2; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
static int |
| 133 |
push_properties_wpiterator (lua_State *L, WpIterator *it) |
| 134 |
{ |
| 135 |
lua_pushcfunction (L, properties_iterator_next); |
| 136 |
wplua_pushboxed (L, WP_TYPE_ITERATOR, it); |
| 137 |
return 2; |
| 138 |
} |
| 139 |
|
| 140 |
static int |
| 141 |
_wplua_gboxed___pairs (lua_State *L) |
| 142 |
{ |
| 143 |
GValue *obj_v = _wplua_togvalue_userdata_named (L, 1, G_TYPE_BOXED, "GBoxed"); |
| 144 |
luaL_argcheck (L, obj_v != NULL, 1, |
| 145 |
"expected userdata storing GValue<GBoxed>"); |
| 146 |
GType type = G_VALUE_TYPE (obj_v); |
| 147 |
|
| 148 |
if (type == WP_TYPE_PROPERTIES) { |
| 149 |
WpProperties * props = g_value_get_boxed (obj_v); |
| 150 |
WpIterator *it = wp_properties_new_iterator (props); |
| 151 |
return push_properties_wpiterator (L, it); |
| 152 |
} else { |
| 153 |
luaL_error (L, "cannot do pairs of boxed type %s", g_type_name (type)); |
| 154 |
} |
| 155 |
return 0; |
| 156 |
} |
| 157 |
|
| 158 |
static int |
| 159 |
_wplua_gboxed___eq (lua_State *L) |
| 160 |
{ |
| 161 |
return _wplua_gvalue_userdata___eq_impl (L, "GBoxed"); |
| 162 |
} |
| 163 |
|
| 164 |
void |
| 165 |
_wplua_init_gboxed (lua_State *L) |
| 166 |
{ |
| 167 |
static const luaL_Reg gboxed_meta[] = { |
| 168 |
{ "__gc", _wplua_gvalue_userdata___gc }, |
| 169 |
{ "__eq", _wplua_gboxed___eq }, |
| 170 |
{ "__index", _wplua_gboxed___index }, |
| 171 |
{ "__newindex", _wplua_gboxed___newindex }, |
| 172 |
{ "__pairs", _wplua_gboxed___pairs }, |
| 173 |
{ NULL, NULL } |
| 174 |
}; |
| 175 |
|
| 176 |
if (!luaL_newmetatable (L, "GBoxed")) |
| 177 |
g_error ("Metatable with key GBoxed in the registry already exists?"); |
| 178 |
luaL_setfuncs (L, gboxed_meta, 0); |
| 179 |
lua_pop (L, 1); |
| 180 |
} |
| 181 |
|
| 182 |
void |
| 183 |
wplua_pushboxed (lua_State * L, GType type, gpointer object) |
| 184 |
{ |
| 185 |
g_return_if_fail (G_TYPE_FUNDAMENTAL (type) == G_TYPE_BOXED); |
| 186 |
|
| 187 |
GValue *v = _wplua_pushgvalue_userdata (L, type); |
| 188 |
wp_trace_boxed (type, object, "pushing to Lua, v=%p", v); |
| 189 |
g_value_take_boxed (v, object); |
| 190 |
} |
| 191 |
|
| 192 |
gpointer |
| 193 |
wplua_toboxed (lua_State *L, int idx) |
| 194 |
{ |
| 195 |
GValue *v = _wplua_togvalue_userdata_named (L, idx, G_TYPE_BOXED, "GBoxed"); |
| 196 |
|
| 197 |
g_return_val_if_fail (v, NULL); |
| 198 |
return g_value_get_boxed (v); |
| 199 |
} |
| 200 |
|
| 201 |
gpointer |
| 202 |
wplua_checkboxed (lua_State *L, int idx, GType type) |
| 203 |
{ |
| 204 |
GValue *v = _wplua_togvalue_userdata_named (L, idx, type, "GBoxed"); |
| 205 |
if (v == NULL) { |
| 206 |
wp_critical ("expected userdata storing GValue<%s>", g_type_name (type)); |
| 207 |
luaL_argerror (L, idx, "expected userdata storing GValue<GBoxed>"); |
| 208 |
} |
| 209 |
return g_value_get_boxed (v); |
| 210 |
} |
| 211 |
|
| 212 |
gboolean |
| 213 |
wplua_isboxed (lua_State *L, int idx, GType type) |
| 214 |
{ |
| 215 |
return g_type_is_a (type, G_TYPE_BOXED) && |
| 216 |
_wplua_togvalue_userdata_named (L, idx, type, "GBoxed") != NULL; |
| 217 |
} |