lua/pod: simplify spa_pod_object_new()

This commit is contained in:
George Kiagiadakis
2021-02-05 19:49:25 +02:00
parent 38039bdfe1
commit e916ff6b76

View File

@@ -514,36 +514,35 @@ spa_pod_object_new (lua_State *L)
{
g_autoptr (WpSpaPodBuilder) builder = NULL;
const gchar *fields[2] = { NULL, NULL }; // type_name, name_id
guint field_counter = 0;
WpSpaType object_type = WP_SPA_TYPE_INVALID;
WpSpaIdTable table = NULL;
luaL_checktype (L, 1, LUA_TTABLE);
lua_geti (L, 1, 1);
fields[0] = lua_tostring (L, -1);
lua_geti (L, 1, 2);
fields[1] = lua_tostring (L, -1);
object_type = wp_spa_type_from_name (fields[0]);
if (object_type == WP_SPA_TYPE_INVALID)
luaL_error (L, "Invalid object type '%s'", fields[0]);
table = wp_spa_type_get_values_table (object_type);
if (!table)
luaL_error (L, "Object type '%s' has incomplete type information",
fields[0]);
builder = wp_spa_pod_builder_new_object (fields[0], fields[1]);
if (!builder)
luaL_error (L, "Could not create pod object");
lua_pop (L, 2);
lua_pushnil(L);
while (lua_next (L, -2)) {
/* First 2 fileds are always the object type and id */
if (field_counter < 2) {
if (lua_type (L, -1) == LUA_TSTRING) {
fields[field_counter] = lua_tostring (L, -1);
field_counter++;
}
if (fields[0] && fields[1]) {
object_type = wp_spa_type_from_name (fields[0]);
if (object_type == WP_SPA_TYPE_INVALID)
luaL_error (L, "Invalid object type '%s'", fields[0]);
table = wp_spa_type_get_values_table (object_type);
if (!table)
luaL_error (L, "Object type '%s' has incomplete type information",
fields[0]);
builder = wp_spa_pod_builder_new_object (fields[0], fields[1]);
}
}
/* Remaining fields are the object properties */
else {
/* Remaining fields with string keys are the object properties */
if (lua_type (L, -2) == LUA_TSTRING) {
const gchar *key = lua_tostring (L, -2);
if (!object_add_property (builder, table, key, L, -1))
luaL_error (L, "Property '%s' could not be added", key);
@@ -552,11 +551,6 @@ spa_pod_object_new (lua_State *L)
lua_pop (L, 1);
}
if (!builder) {
luaL_error (L, "Could not create pod object");
return 0;
}
wplua_pushboxed (L, WP_TYPE_SPA_POD, wp_spa_pod_builder_end (builder));
return 1;
}