Debian Codegraph @

modules/module-lua-scripting/wplua/value.c

raw
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 WpProperties *
14 wplua_table_to_properties (lua_State *L, int idx)
15 {
16 WpProperties *p = wp_properties_new_empty ();
17 int table = lua_absindex (L, idx);
18
19 if (lua_type (L, table) != LUA_TTABLE) {
20 wp_critical ("skipping non-table value");
21 return p;
22 }
23
24 lua_pushnil(L);
25 while (lua_next (L, table) != 0) {
26 const gchar *key = luaL_tolstring (L, -2, NULL);
27 g_autofree gchar *value = NULL;
28
29 /* copy key & value to convert them to string */
30 luaL_checkany (L, -2);
31 switch (lua_type (L, -2)) {
32 case LUA_TNIL:
33 lua_pop (L, 2);
34 break;
35 case LUA_TUSERDATA: {
36 if (wplua_gvalue_userdata_type(L, -2) != G_TYPE_INVALID) {
37 GValue *v = lua_touserdata (L, -2);
38 gpointer p = g_value_peek_pointer (v);
39 value = g_strdup_printf ("%p", p);
40 lua_pop (L, 2);
41 } else {
42 value = g_strdup (luaL_tolstring (L, -2, NULL));
43 lua_pop (L, 3);
44 }
45 break;
46 }
47 default:
48 value = g_strdup (luaL_tolstring (L, -2, NULL));
49 lua_pop (L, 3);
50 break;
51 }
52
53 wp_properties_set (p, key, value);
54 }
55
56 /* sort, because the lua table has a random order and it's too messy to read */
57 wp_properties_sort (p);
58
59 return p;
60 }
61
62 void
63 wplua_properties_to_table (lua_State *L, WpProperties *p)
64 {
65 lua_newtable (L);
66 if (p) {
67 g_autoptr (WpIterator) it = wp_properties_new_iterator (p);
68 GValue v = G_VALUE_INIT;
69 const gchar *key, *value;
70
71 while (wp_iterator_next (it, &v)) {
72 WpPropertiesItem *pi = g_value_get_boxed (&v);
73 key = wp_properties_item_get_key (pi);
74 value = wp_properties_item_get_value (pi);
75 lua_pushstring (L, key);
76 lua_pushstring (L, value);
77 lua_settable (L, -3);
78 g_value_unset (&v);
79 }
80 }
81 }
82
83 GVariant *
84 wplua_lua_to_gvariant (lua_State *L, int idx)
85 {
86 switch (lua_type (L, idx)) {
87 case LUA_TNIL:
88 return g_variant_new ("()");
89 case LUA_TBOOLEAN:
90 return g_variant_new_boolean (lua_toboolean (L, idx));
91 case LUA_TNUMBER:
92 if (lua_isinteger (L, idx))
93 return g_variant_new_int64 (lua_tointeger (L, idx));
94 else
95 return g_variant_new_double (lua_tonumber (L, idx));
96 case LUA_TSTRING:
97 return g_variant_new_string (lua_tostring (L, idx));
98 case LUA_TTABLE: {
99 GVariantBuilder b = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_VARDICT);
100 const gchar *key;
101 int table = lua_absindex (L, idx);
102
103 lua_pushnil (L);
104 while (lua_next (L, table) != 0) {
105 /* copy key to convert it to string */
106 lua_pushvalue (L, -2);
107 key = lua_tostring (L, -1);
108 g_variant_builder_add (&b, "{sv}", key, wplua_lua_to_gvariant (L, -2));
109 lua_pop (L, 2);
110 }
111 return g_variant_builder_end (&b);
112 }
113 default:
114 wp_warning ("skipping bad value (its type cannot be represented in GVariant)");
115 return NULL;
116 }
117 }
118
119 void
120 wplua_gvariant_to_lua (lua_State *L, GVariant *variant)
121 {
122 if (variant == NULL || g_variant_is_of_type (variant, G_VARIANT_TYPE_UNIT)) {
123 lua_pushnil (L);
124 }
125 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_INT16)) {
126 lua_pushinteger (L, g_variant_get_int16 (variant));
127 }
128 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_INT32)) {
129 lua_pushinteger (L, g_variant_get_int32 (variant));
130 }
131 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_INT64)) {
132 lua_pushinteger (L, g_variant_get_int64 (variant));
133 }
134 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT16)) {
135 lua_pushinteger (L, g_variant_get_uint16 (variant));
136 }
137 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT32)) {
138 lua_pushinteger (L, g_variant_get_uint32 (variant));
139 }
140 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT64)) {
141 lua_pushinteger (L, g_variant_get_uint64 (variant));
142 }
143 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_DOUBLE)) {
144 lua_pushnumber (L, g_variant_get_double (variant));
145 }
146 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_BOOLEAN)) {
147 lua_pushboolean (L, g_variant_get_boolean (variant));
148 }
149 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING)) {
150 lua_pushstring (L, g_variant_get_string (variant, NULL));
151 }
152 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_VARIANT)) {
153 g_autoptr (GVariant) v = g_variant_get_variant (variant);
154 wplua_gvariant_to_lua (L, v);
155 }
156 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_DICTIONARY)) {
157 gsize n_children, i;
158 n_children = g_variant_n_children (variant);
159 lua_createtable (L, 0, n_children);
160 for (i = 0; i < n_children; i++) {
161 g_autoptr (GVariant) key, value;
162 g_variant_get_child (variant, i, "{@?@*}", &key, &value);
163 wplua_gvariant_to_lua (L, key);
164 /* if the key is a string convertible to integer, convert it */
165 if (lua_type (L, -1) == LUA_TSTRING) {
166 int isnum = 0;
167 lua_Integer num = lua_tointegerx (L, -1, &isnum);
168 if (isnum) {
169 lua_pop (L, 1);
170 lua_pushinteger (L, num);
171 }
172 }
173 wplua_gvariant_to_lua (L, value);
174 lua_settable (L, -3);
175 }
176 }
177 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_ARRAY)) {
178 gsize n_children, i;
179 n_children = g_variant_n_children (variant);
180 lua_createtable (L, n_children, 0);
181 for (i = 0; i < n_children; i++) {
182 g_autoptr (GVariant) value;
183 value = g_variant_get_child_value (variant, i);
184 wplua_gvariant_to_lua (L, value);
185 lua_seti (L, -2, i + 1);
186 }
187 }
188 else {
189 g_autofree gchar *type_name =
190 g_variant_type_dup_string (g_variant_get_type (variant));
191 wp_warning ("Unhandled GVariant type %s", type_name);
192 lua_pushnil (L);
193 }
194 }
195
196 gint
197 wplua_lua_to_enum (lua_State *L, int idx, GType enum_type)
198 {
199 if (lua_type (L, idx) == LUA_TSTRING) {
200 g_autoptr (GEnumClass) klass = g_type_class_ref (enum_type);
201 GEnumValue *value = g_enum_get_value_by_nick (klass, lua_tostring (L, idx));
202 if (value)
203 return value->value;
204 else
205 luaL_error (L, "Invalid enum value '%s'", lua_tostring (L, idx));
206 }
207 return lua_tointeger (L, idx);
208 }
209
210 void
211 wplua_enum_to_lua (lua_State *L, gint enum_val, GType enum_type)
212 {
213 g_autoptr (GEnumClass) klass = g_type_class_ref (enum_type);
214 GEnumValue *value = g_enum_get_value (klass, enum_val);
215 if (value)
216 lua_pushstring (L, value->value_nick);
217 else
218 lua_pushinteger (L, enum_val);
219 }
220
221 void
222 wplua_lua_to_gvalue (lua_State *L, int idx, GValue *v)
223 {
224 switch (g_type_fundamental (G_VALUE_TYPE (v))) {
225 case G_TYPE_CHAR:
226 if (lua_type (L, idx) == LUA_TSTRING)
227 g_value_set_schar (v, *lua_tostring (L, idx));
228 else
229 g_value_set_schar (v, lua_tointeger (L, idx));
230 break;
231 case G_TYPE_UCHAR:
232 g_value_set_uchar (v, lua_tointeger (L, idx));
233 break;
234 case G_TYPE_INT:
235 g_value_set_int (v, lua_tointeger (L, idx));
236 break;
237 case G_TYPE_UINT:
238 g_value_set_uint (v, lua_tointeger (L, idx));
239 break;
240 case G_TYPE_LONG:
241 g_value_set_long (v, lua_tointeger (L, idx));
242 break;
243 case G_TYPE_ULONG:
244 g_value_set_ulong (v, lua_tointeger (L, idx));
245 break;
246 case G_TYPE_INT64:
247 g_value_set_int64 (v, lua_tointeger (L, idx));
248 break;
249 case G_TYPE_UINT64:
250 g_value_set_uint64 (v, lua_tonumber (L, idx));
251 break;
252 case G_TYPE_FLOAT:
253 g_value_set_float (v, lua_tonumber (L, idx));
254 break;
255 case G_TYPE_DOUBLE:
256 g_value_set_double (v, lua_tonumber (L, idx));
257 break;
258 case G_TYPE_BOOLEAN:
259 g_value_set_boolean (v, lua_toboolean (L, idx));
260 break;
261 case G_TYPE_STRING:
262 g_value_set_string (v, lua_tostring (L, idx));
263 break;
264 case G_TYPE_POINTER:
265 if (lua_type (L, idx) == LUA_TLIGHTUSERDATA)
266 g_value_set_pointer (v, lua_touserdata (L, idx));
267 break;
268 case G_TYPE_BOXED:
269 if (_wplua_isgvalue_userdata (L, idx, G_VALUE_TYPE (v)))
270 g_value_set_boxed (v, wplua_toboxed (L, idx));
271 /* table -> WpProperties */
272 else if (lua_istable (L, idx) && G_VALUE_TYPE (v) == WP_TYPE_PROPERTIES)
273 g_value_take_boxed (v, wplua_table_to_properties (L, idx));
274 break;
275 case G_TYPE_OBJECT:
276 case G_TYPE_INTERFACE:
277 if (_wplua_isgvalue_userdata (L, idx, G_VALUE_TYPE (v)))
278 g_value_set_object (v, wplua_toobject (L, idx));
279 break;
280 case G_TYPE_ENUM:
281 g_value_set_enum (v, wplua_lua_to_enum (L, idx, G_VALUE_TYPE (v)));
282 break;
283 case G_TYPE_FLAGS:
284 g_value_set_flags (v, lua_tointeger (L, idx));
285 break;
286 case G_TYPE_VARIANT:
287 g_value_set_variant (v, wplua_lua_to_gvariant (L, idx));
288 break;
289 default:
290 break;
291 }
292 }
293
294 int
295 wplua_gvalue_to_lua (lua_State *L, const GValue *v)
296 {
297 switch (g_type_fundamental (G_VALUE_TYPE (v))) {
298 case G_TYPE_CHAR:
299 lua_pushinteger (L, g_value_get_schar (v));
300 break;
301 case G_TYPE_UCHAR:
302 lua_pushinteger (L, g_value_get_uchar (v));
303 break;
304 case G_TYPE_INT:
305 lua_pushinteger (L, g_value_get_int (v));
306 break;
307 case G_TYPE_UINT:
308 lua_pushinteger (L, g_value_get_uint (v));
309 break;
310 case G_TYPE_LONG:
311 lua_pushinteger (L, g_value_get_long (v));
312 break;
313 case G_TYPE_ULONG:
314 lua_pushinteger (L, g_value_get_ulong (v));
315 break;
316 case G_TYPE_INT64:
317 lua_pushinteger (L, g_value_get_int64 (v));
318 break;
319 case G_TYPE_UINT64:
320 lua_pushnumber (L, g_value_get_uint64 (v));
321 break;
322 case G_TYPE_FLOAT:
323 lua_pushnumber (L, g_value_get_float (v));
324 break;
325 case G_TYPE_DOUBLE:
326 lua_pushnumber (L, g_value_get_double (v));
327 break;
328 case G_TYPE_BOOLEAN:
329 lua_pushboolean (L, g_value_get_boolean (v));
330 break;
331 case G_TYPE_STRING:
332 lua_pushstring (L, g_value_get_string (v));
333 break;
334 case G_TYPE_POINTER:
335 lua_pushlightuserdata (L, g_value_get_pointer (v));
336 break;
337 case G_TYPE_BOXED:
338 wplua_pushboxed (L, G_VALUE_TYPE (v), g_value_dup_boxed (v));
339 break;
340 case G_TYPE_OBJECT:
341 case G_TYPE_INTERFACE: {
342 GObject *object = g_value_dup_object (v);
343 if (object)
344 wplua_pushobject (L, object);
345 else
346 lua_pushnil (L);
347 break;
348 }
349 case G_TYPE_ENUM:
350 wplua_enum_to_lua (L, g_value_get_enum (v), G_VALUE_TYPE (v));
351 break;
352 case G_TYPE_FLAGS:
353 /* FIXME: push as userdata with methods */
354 lua_pushinteger (L, g_value_get_flags (v));
355 break;
356 case G_TYPE_PARAM: {
357 GParamSpec *pspec = g_value_get_param (v);
358 lua_pushstring (L, pspec->name);
359 break;
360 }
361 case G_TYPE_VARIANT: {
362 GVariant *variant = g_value_get_variant (v);
363 wplua_gvariant_to_lua (L, variant);
364 break;
365 }
366 default:
367 lua_pushnil (L);
368 break;
369 }
370 return 1;
371 }