| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2022 Collabora Ltd. |
| 4 |
* @author Julian Bouzas <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <wp/wp.h> |
| 10 |
#include <wplua/wplua.h> |
| 11 |
|
| 12 |
#define WP_LOCAL_LOG_TOPIC log_topic_lua_scripting |
| 13 |
WP_LOG_TOPIC_EXTERN (log_topic_lua_scripting) |
| 14 |
|
| 15 |
/* API */ |
| 16 |
|
| 17 |
static int |
| 18 |
spa_json_get_data (lua_State *L) |
| 19 |
{ |
| 20 |
WpSpaJson *json = wplua_checkboxed (L, 1, WP_TYPE_SPA_JSON); |
| 21 |
lua_pushstring (L, wp_spa_json_get_data (json)); |
| 22 |
return 1; |
| 23 |
} |
| 24 |
|
| 25 |
static int |
| 26 |
spa_json_get_size (lua_State *L) |
| 27 |
{ |
| 28 |
WpSpaJson *json = wplua_checkboxed (L, 1, WP_TYPE_SPA_JSON); |
| 29 |
lua_pushinteger (L, wp_spa_json_get_size (json)); |
| 30 |
return 1; |
| 31 |
} |
| 32 |
|
| 33 |
static int |
| 34 |
spa_json_to_string (lua_State *L) |
| 35 |
{ |
| 36 |
WpSpaJson *json = wplua_checkboxed (L, 1, WP_TYPE_SPA_JSON); |
| 37 |
/* Instead of using wp_spa_json_to_string() and lua_pushstring, we can avoid |
| 38 |
* an extra allocation if we use lua_pushlstring with wp_spa_json_get_data() |
| 39 |
* and wp_spa_json_get_size () */ |
| 40 |
lua_pushlstring (L, wp_spa_json_get_data (json), wp_spa_json_get_size (json)); |
| 41 |
return 1; |
| 42 |
} |
| 43 |
|
| 44 |
static int |
| 45 |
spa_json_is_null (lua_State *L) |
| 46 |
{ |
| 47 |
WpSpaJson *json = wplua_checkboxed (L, 1, WP_TYPE_SPA_JSON); |
| 48 |
lua_pushboolean (L, wp_spa_json_is_null (json)); |
| 49 |
return 1; |
| 50 |
} |
| 51 |
|
| 52 |
static int |
| 53 |
spa_json_is_boolean (lua_State *L) |
| 54 |
{ |
| 55 |
WpSpaJson *json = wplua_checkboxed (L, 1, WP_TYPE_SPA_JSON); |
| 56 |
lua_pushboolean (L, wp_spa_json_is_boolean (json)); |
| 57 |
return 1; |
| 58 |
} |
| 59 |
|
| 60 |
static int |
| 61 |
spa_json_is_int (lua_State *L) |
| 62 |
{ |
| 63 |
WpSpaJson *json = wplua_checkboxed (L, 1, WP_TYPE_SPA_JSON); |
| 64 |
lua_pushboolean (L, wp_spa_json_is_int (json)); |
| 65 |
return 1; |
| 66 |
} |
| 67 |
|
| 68 |
static int |
| 69 |
spa_json_is_float (lua_State *L) |
| 70 |
{ |
| 71 |
WpSpaJson *json = wplua_checkboxed (L, 1, WP_TYPE_SPA_JSON); |
| 72 |
lua_pushboolean (L, wp_spa_json_is_float (json)); |
| 73 |
return 1; |
| 74 |
} |
| 75 |
|
| 76 |
static int |
| 77 |
spa_json_is_string (lua_State *L) |
| 78 |
{ |
| 79 |
WpSpaJson *json = wplua_checkboxed (L, 1, WP_TYPE_SPA_JSON); |
| 80 |
lua_pushboolean (L, wp_spa_json_is_string (json)); |
| 81 |
return 1; |
| 82 |
} |
| 83 |
|
| 84 |
static int |
| 85 |
spa_json_is_array (lua_State *L) |
| 86 |
{ |
| 87 |
WpSpaJson *json = wplua_checkboxed (L, 1, WP_TYPE_SPA_JSON); |
| 88 |
lua_pushboolean (L, wp_spa_json_is_array (json)); |
| 89 |
return 1; |
| 90 |
} |
| 91 |
|
| 92 |
static int |
| 93 |
spa_json_is_object (lua_State *L) |
| 94 |
{ |
| 95 |
WpSpaJson *json = wplua_checkboxed (L, 1, WP_TYPE_SPA_JSON); |
| 96 |
lua_pushboolean (L, wp_spa_json_is_object (json)); |
| 97 |
return 1; |
| 98 |
} |
| 99 |
|
| 100 |
void |
| 101 |
push_luajson (lua_State *L, WpSpaJson *json, gint n_recursions) |
| 102 |
{ |
| 103 |
/* Null */ |
| 104 |
if (wp_spa_json_is_null (json)) { |
| 105 |
lua_pushnil (L); |
| 106 |
} |
| 107 |
|
| 108 |
/* Boolean */ |
| 109 |
else if (wp_spa_json_is_boolean (json)) { |
| 110 |
gboolean value = FALSE; |
| 111 |
g_warn_if_fail (wp_spa_json_parse_boolean (json, &value)); |
| 112 |
lua_pushboolean (L, value); |
| 113 |
} |
| 114 |
|
| 115 |
/* Int */ |
| 116 |
else if (wp_spa_json_is_int (json)) { |
| 117 |
gint value = 0; |
| 118 |
g_warn_if_fail (wp_spa_json_parse_int (json, &value)); |
| 119 |
lua_pushinteger (L, value); |
| 120 |
} |
| 121 |
|
| 122 |
/* Float */ |
| 123 |
else if (wp_spa_json_is_float (json)) { |
| 124 |
float value = 0; |
| 125 |
g_warn_if_fail (wp_spa_json_parse_float (json, &value)); |
| 126 |
lua_pushnumber (L, value); |
| 127 |
} |
| 128 |
|
| 129 |
/* Array */ |
| 130 |
else if (wp_spa_json_is_array (json) && n_recursions > 0) { |
| 131 |
g_auto (GValue) item = G_VALUE_INIT; |
| 132 |
g_autoptr (WpIterator) it = wp_spa_json_new_iterator (json); |
| 133 |
guint i = 1; |
| 134 |
lua_newtable (L); |
| 135 |
for (; wp_iterator_next (it, &item); g_value_unset (&item)) { |
| 136 |
WpSpaJson *j = g_value_get_boxed (&item); |
| 137 |
push_luajson (L, j, n_recursions - 1); |
| 138 |
lua_rawseti (L, -2, i++); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
/* Object */ |
| 143 |
else if (wp_spa_json_is_object (json) && n_recursions > 0) { |
| 144 |
g_auto (GValue) item = G_VALUE_INIT; |
| 145 |
g_autoptr (WpIterator) it = wp_spa_json_new_iterator (json); |
| 146 |
lua_newtable (L); |
| 147 |
for (; wp_iterator_next (it, &item); g_value_unset (&item)) { |
| 148 |
WpSpaJson *key = g_value_get_boxed (&item); |
| 149 |
g_autofree gchar *key_str = NULL; |
| 150 |
WpSpaJson *value = NULL; |
| 151 |
key_str = wp_spa_json_parse_string (key); |
| 152 |
g_warn_if_fail (key_str); |
| 153 |
g_value_unset (&item); |
| 154 |
if (!wp_iterator_next (it, &item)) |
| 155 |
break; |
| 156 |
value = g_value_get_boxed (&item); |
| 157 |
push_luajson (L, value, n_recursions - 1); |
| 158 |
lua_setfield (L, -2, key_str); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
/* Otherwise always parse as String to allow parsing strings without quotes */ |
| 163 |
else { |
| 164 |
g_autofree gchar *value = wp_spa_json_parse_string (json); |
| 165 |
g_warn_if_fail (value); |
| 166 |
lua_pushstring (L, value); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
static int |
| 171 |
spa_json_merge (lua_State *L) |
| 172 |
{ |
| 173 |
WpSpaJson *a = wplua_checkboxed (L, 1, WP_TYPE_SPA_JSON); |
| 174 |
WpSpaJson *b = wplua_checkboxed (L, 2, WP_TYPE_SPA_JSON); |
| 175 |
|
| 176 |
WpSpaJson *merge = wp_json_utils_merge_containers(a, b); |
| 177 |
if(!merge) |
| 178 |
luaL_error (L, "only Json container merge supported"); |
| 179 |
|
| 180 |
wplua_pushboxed (L, WP_TYPE_SPA_JSON, merge); |
| 181 |
return 1; |
| 182 |
} |
| 183 |
|
| 184 |
static int |
| 185 |
spa_json_parse (lua_State *L) |
| 186 |
{ |
| 187 |
WpSpaJson *json = wplua_checkboxed (L, 1, WP_TYPE_SPA_JSON); |
| 188 |
gint n_recursions = luaL_opt (L, luaL_checkinteger, 2, INT_MAX); |
| 189 |
push_luajson (L, json, n_recursions); |
| 190 |
return 1; |
| 191 |
} |
| 192 |
|
| 193 |
/* Raw */ |
| 194 |
|
| 195 |
static int |
| 196 |
spa_json_raw_new (lua_State *L) |
| 197 |
{ |
| 198 |
const gchar *value = lua_tostring (L, 1); |
| 199 |
wplua_pushboxed (L, WP_TYPE_SPA_JSON, wp_spa_json_new_from_string (value)); |
| 200 |
return 1; |
| 201 |
} |
| 202 |
|
| 203 |
/* None */ |
| 204 |
|
| 205 |
static int |
| 206 |
spa_json_null_new (lua_State *L) |
| 207 |
{ |
| 208 |
wplua_pushboxed (L, WP_TYPE_SPA_JSON, wp_spa_json_new_null ()); |
| 209 |
return 1; |
| 210 |
} |
| 211 |
|
| 212 |
/* Boolean */ |
| 213 |
|
| 214 |
static int |
| 215 |
spa_json_boolean_new (lua_State *L) |
| 216 |
{ |
| 217 |
gboolean value = lua_toboolean (L, 1); |
| 218 |
wplua_pushboxed (L, WP_TYPE_SPA_JSON, wp_spa_json_new_boolean (value)); |
| 219 |
return 1; |
| 220 |
} |
| 221 |
|
| 222 |
/* Int */ |
| 223 |
|
| 224 |
static int |
| 225 |
spa_json_int_new (lua_State *L) |
| 226 |
{ |
| 227 |
gint64 value = lua_tointeger (L, 1); |
| 228 |
wplua_pushboxed (L, WP_TYPE_SPA_JSON, wp_spa_json_new_int (value)); |
| 229 |
return 1; |
| 230 |
} |
| 231 |
|
| 232 |
/* Float */ |
| 233 |
|
| 234 |
static int |
| 235 |
spa_json_float_new (lua_State *L) |
| 236 |
{ |
| 237 |
float value = lua_tonumber (L, 1); |
| 238 |
wplua_pushboxed (L, WP_TYPE_SPA_JSON, wp_spa_json_new_float (value)); |
| 239 |
return 1; |
| 240 |
} |
| 241 |
|
| 242 |
/* String */ |
| 243 |
|
| 244 |
static int |
| 245 |
spa_json_string_new (lua_State *L) |
| 246 |
{ |
| 247 |
const gchar *value = lua_tostring (L, 1); |
| 248 |
wplua_pushboxed (L, WP_TYPE_SPA_JSON, wp_spa_json_new_string (value)); |
| 249 |
return 1; |
| 250 |
} |
| 251 |
|
| 252 |
/* Array */ |
| 253 |
|
| 254 |
static int |
| 255 |
spa_json_array_new (lua_State *L) |
| 256 |
{ |
| 257 |
g_autoptr (WpSpaJsonBuilder) builder = wp_spa_json_builder_new_array (); |
| 258 |
|
| 259 |
luaL_checktype (L, 1, LUA_TTABLE); |
| 260 |
|
| 261 |
lua_pushnil (L); |
| 262 |
while (lua_next (L, -2)) { |
| 263 |
/* We only add table values with integer keys */ |
| 264 |
if (lua_isinteger (L, -2)) { |
| 265 |
switch (lua_type (L, -1)) { |
| 266 |
case LUA_TBOOLEAN: |
| 267 |
wp_spa_json_builder_add_boolean (builder, lua_toboolean (L, -1)); |
| 268 |
break; |
| 269 |
case LUA_TNUMBER: |
| 270 |
if (lua_isinteger (L, -1)) |
| 271 |
wp_spa_json_builder_add_int (builder, lua_tointeger (L, -1)); |
| 272 |
else |
| 273 |
wp_spa_json_builder_add_float (builder, lua_tonumber (L, -1)); |
| 274 |
break; |
| 275 |
case LUA_TSTRING: |
| 276 |
wp_spa_json_builder_add_string (builder, lua_tostring (L, -1)); |
| 277 |
break; |
| 278 |
case LUA_TUSERDATA: { |
| 279 |
WpSpaJson *json = wplua_checkboxed (L, -1, WP_TYPE_SPA_JSON); |
| 280 |
wp_spa_json_builder_add_json (builder, json); |
| 281 |
break; |
| 282 |
} |
| 283 |
default: |
| 284 |
luaL_error (L, "Json does not support lua type %s", |
| 285 |
lua_typename(L, lua_type(L, -1))); |
| 286 |
break; |
| 287 |
} |
| 288 |
} |
| 289 |
lua_pop (L, 1); |
| 290 |
} |
| 291 |
|
| 292 |
wplua_pushboxed (L, WP_TYPE_SPA_JSON, wp_spa_json_builder_end (builder)); |
| 293 |
return 1; |
| 294 |
} |
| 295 |
|
| 296 |
/* Object */ |
| 297 |
|
| 298 |
static int |
| 299 |
spa_json_object_new (lua_State *L) |
| 300 |
{ |
| 301 |
g_autoptr (WpSpaJsonBuilder) builder = wp_spa_json_builder_new_object (); |
| 302 |
|
| 303 |
if (lua_istable (L, 1)) { |
| 304 |
luaL_checktype (L, 1, LUA_TTABLE); |
| 305 |
|
| 306 |
lua_pushnil (L); |
| 307 |
while (lua_next (L, -2)) { |
| 308 |
/* We only add table values with string keys */ |
| 309 |
if (lua_type (L, -2) == LUA_TSTRING) { |
| 310 |
wp_spa_json_builder_add_property (builder, lua_tostring (L, -2)); |
| 311 |
|
| 312 |
switch (lua_type (L, -1)) { |
| 313 |
case LUA_TBOOLEAN: |
| 314 |
wp_spa_json_builder_add_boolean (builder, lua_toboolean (L, -1)); |
| 315 |
break; |
| 316 |
case LUA_TNUMBER: |
| 317 |
if (lua_isinteger (L, -1)) |
| 318 |
wp_spa_json_builder_add_int (builder, lua_tointeger (L, -1)); |
| 319 |
else |
| 320 |
wp_spa_json_builder_add_float (builder, lua_tonumber (L, -1)); |
| 321 |
break; |
| 322 |
case LUA_TSTRING: |
| 323 |
wp_spa_json_builder_add_string (builder, lua_tostring (L, -1)); |
| 324 |
break; |
| 325 |
case LUA_TUSERDATA: { |
| 326 |
WpSpaJson *json = wplua_checkboxed (L, -1, WP_TYPE_SPA_JSON); |
| 327 |
wp_spa_json_builder_add_json (builder, json); |
| 328 |
break; |
| 329 |
} |
| 330 |
default: |
| 331 |
luaL_error (L, "Json does not support lua type %s", |
| 332 |
lua_typename(L, lua_type(L, -1))); |
| 333 |
break; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
lua_pop (L, 1); |
| 338 |
} |
| 339 |
} else { |
| 340 |
WpProperties *props = wplua_checkboxed (L, 1, WP_TYPE_PROPERTIES); |
| 341 |
g_autoptr (WpIterator) it = NULL; |
| 342 |
g_auto (GValue) item = G_VALUE_INIT; |
| 343 |
for (it = wp_properties_new_iterator (props); wp_iterator_next (it, &item); |
| 344 |
g_value_unset (&item)) { |
| 345 |
WpPropertiesItem *pi = g_value_get_boxed (&item); |
| 346 |
const gchar *key = wp_properties_item_get_key (pi); |
| 347 |
const gchar *value = wp_properties_item_get_value (pi); |
| 348 |
wp_spa_json_builder_add_property (builder, key); |
| 349 |
wp_spa_json_builder_add_string (builder, value); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
wplua_pushboxed (L, WP_TYPE_SPA_JSON, wp_spa_json_builder_end (builder)); |
| 354 |
return 1; |
| 355 |
} |
| 356 |
|
| 357 |
/* Init */ |
| 358 |
|
| 359 |
static const luaL_Reg spa_json_methods[] = { |
| 360 |
{ "get_data", spa_json_get_data }, |
| 361 |
{ "get_size", spa_json_get_size }, |
| 362 |
{ "to_string", spa_json_to_string }, |
| 363 |
{ "is_null", spa_json_is_null }, |
| 364 |
{ "is_boolean", spa_json_is_boolean }, |
| 365 |
{ "is_int", spa_json_is_int }, |
| 366 |
{ "is_float", spa_json_is_float }, |
| 367 |
{ "is_string", spa_json_is_string }, |
| 368 |
{ "is_array", spa_json_is_array }, |
| 369 |
{ "is_object", spa_json_is_object }, |
| 370 |
{ "parse", spa_json_parse }, |
| 371 |
{ "merge", spa_json_merge }, |
| 372 |
{ NULL, NULL } |
| 373 |
}; |
| 374 |
|
| 375 |
static const luaL_Reg spa_json_constructors[] = { |
| 376 |
{ "Raw", spa_json_raw_new }, |
| 377 |
{ "Null", spa_json_null_new }, |
| 378 |
{ "Boolean", spa_json_boolean_new }, |
| 379 |
{ "Int", spa_json_int_new }, |
| 380 |
{ "Float", spa_json_float_new }, |
| 381 |
{ "String", spa_json_string_new }, |
| 382 |
{ "Array", spa_json_array_new }, |
| 383 |
{ "Object", spa_json_object_new }, |
| 384 |
{ NULL, NULL } |
| 385 |
}; |
| 386 |
|
| 387 |
void |
| 388 |
wp_lua_scripting_json_init (lua_State *L) |
| 389 |
{ |
| 390 |
luaL_newlib (L, spa_json_constructors); |
| 391 |
lua_setglobal (L, "WpSpaJson"); |
| 392 |
|
| 393 |
wplua_register_type_methods (L, WP_TYPE_SPA_JSON, NULL, spa_json_methods); |
| 394 |
} |