From e92351b23bf765dc6cc64b8adae9c6b2dbc48099 Mon Sep 17 00:00:00 2001
From: George Kiagiadakis <george.kiagiadakis@collabora.com>
Date: Mon, 15 Feb 2021 18:50:56 +0200
Subject: [PATCH] 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"
---
 lib/wplua/value.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/lib/wplua/value.c b/lib/wplua/value.c
index 85af2ec4..c760d32e 100644
--- a/lib/wplua/value.c
+++ b/lib/wplua/value.c
@@ -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);
     }
-- 
GitLab