Debian Codegraph @

modules/module-lua-scripting/wplua/userdata.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 static const char *
14 _wplua_get_metatable_name (GType type)
15 {
16 if (g_type_is_a (type, G_TYPE_BOXED))
17 return "GBoxed";
18 else if (g_type_is_a (type, G_TYPE_OBJECT))
19 return "GObject";
20 else
21 return NULL;
22 }
23
24 GValue *
25 _wplua_pushgvalue_userdata (lua_State * L, GType type)
26 {
27 GValue *v;
28 const char *table_name = _wplua_get_metatable_name (type);
29
30 if (table_name == NULL)
31 g_error ("type passed to %s not boxed or object", __func__);
32
33 /* auxillary library can use 4 stack slots, plus 1 for userdata */
34 if (!lua_checkstack (L, 5))
35 g_error ("cannot grow Lua stack in %s", __func__);
36 v = lua_newuserdata (L, sizeof (GValue));
37 memset (v, 0, sizeof (GValue));
38 g_value_init (v, type);
39 luaL_getmetatable (L, table_name);
40 lua_setmetatable (L, -2);
41 return v;
42 }
43
44 GValue *
45 _wplua_togvalue_userdata_named (lua_State *L, int idx, GType type,
46 const char *table_name)
47 {
48 GValue *v;
49
50 /* auxillary library can use 4 stack slots */
51 if (!lua_checkstack (L, 4))
52 g_error ("cannot grow Lua stack in %s", __func__);
53 if (!(v = luaL_testudata (L, idx, table_name)))
54 return NULL;
55 /* if this triggers someone misused the debug library */
56 if (lua_rawlen (L, idx) != sizeof (GValue))
57 g_error ("Wrong length for userdata of type %s", table_name);
58 if (type != G_TYPE_NONE && !g_type_is_a (G_VALUE_TYPE (v), type))
59 return NULL;
60
61 return v;
62 }
63
64 GValue *
65 _wplua_togvalue_userdata (lua_State *L, int idx, GType type)
66 {
67 const char *table_name = _wplua_get_metatable_name (type);
68 return table_name == NULL ? NULL :
69 _wplua_togvalue_userdata_named (L, idx, type, table_name);
70 }
71
72 gboolean
73 _wplua_isgvalue_userdata (lua_State *L, int idx, GType type)
74 {
75 return _wplua_togvalue_userdata (L, idx, type) != NULL;
76 }
77
78 GType
79 wplua_gvalue_userdata_type (lua_State *L, int idx)
80 {
81 GValue *v;
82
83 if (!lua_isuserdata (L, idx))
84 return G_TYPE_INVALID;
85 if (lua_rawlen (L, idx) != sizeof (GValue))
86 return G_TYPE_INVALID;
87 if (!(v = lua_touserdata (L, idx)))
88 return G_TYPE_INVALID;
89
90 return G_VALUE_TYPE (v);
91 }
92
93 int
94 _wplua_gvalue_userdata___gc (lua_State *L)
95 {
96 GValue *v = lua_touserdata (L, 1);
97 wp_trace_boxed (G_VALUE_TYPE (v), g_value_peek_pointer (v),
98 "collected, v=%p", v);
99 g_value_unset (v);
100 return 0;
101 }
102
103 int
104 _wplua_gvalue_userdata___eq_impl (lua_State *L, const char *type)
105 {
106 /*
107 * First argument should always be a userdata.
108 * Second argument can be anything.
109 */
110 GValue *v1 = luaL_checkudata (L, 1, type);
111 GValue *v2 = luaL_testudata (L, 2, type);
112 if (v2 != NULL) {
113 gpointer p1 = g_value_peek_pointer (v1);
114 gpointer p2 = g_value_peek_pointer (v2);
115 lua_pushboolean (L, (p1 == p2));
116 } else {
117 lua_pushboolean (L, FALSE);
118 }
119 return 1;
120 }