wplua: gvariant_to_lua: convert dictionary keys to integers if possible

When we convert from a lua table to a GVariant dictionary, it is not
possible to maintain the hybrid string & integer keys approach that Lua
has for tables, so we convert all keys to strings and a table becomes a{sv}

When we convert back from a{sv} to a table, it is desirable to get back
the integer keys wherever possible.

The use case is to pass "arrays" (i.e. tables with integer keys) from
the configuration files to the lua scripts, without losing the properties
of the "array"
This commit is contained in:
George Kiagiadakis 2021-02-15 18:50:56 +02:00
parent 73a07d0097
commit e92351b23b

View file

@ -130,6 +130,15 @@ wplua_gvariant_to_lua (lua_State *L, GVariant *variant)
g_autoptr (GVariant) key, value;
g_variant_get_child (variant, i, "{@?@*}", &key, &value);
wplua_gvariant_to_lua (L, key);
/* if the key is a string convertible to integer, convert it */
if (lua_type (L, -1) == LUA_TSTRING) {
int isnum = 0;
lua_Integer num = lua_tointegerx (L, -1, &isnum);
if (isnum) {
lua_pop (L, 1);
lua_pushinteger (L, num);
}
}
wplua_gvariant_to_lua (L, value);
lua_settable (L, -3);
}