From 7c1fc1c567b3f6efea407b34ac460f9c0037bd36 Mon Sep 17 00:00:00 2001
From: George Kiagiadakis <george.kiagiadakis@collabora.com>
Date: Tue, 16 Feb 2021 15:54:20 +0200
Subject: [PATCH] wplua: table_to_properties: use luaL_tolstring to do string
 conversions

The advantage is that luaL_tolstring behaves the same as the native lua
tostring() function, while lua_tostring() does not. In particular, boolean
values are converted properly to "true" and "false" now, while they were
not converted with lua_tostring()

Add a unit test too
---
 lib/wplua/value.c   |  6 ++----
 tests/wplua/wplua.c | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/lib/wplua/value.c b/lib/wplua/value.c
index c760d32e..d3cb0b5d 100644
--- a/lib/wplua/value.c
+++ b/lib/wplua/value.c
@@ -20,10 +20,8 @@ wplua_table_to_properties (lua_State *L, int idx)
   lua_pushnil(L);
   while (lua_next (L, table) != 0) {
     /* copy key & value to convert them to string */
-    lua_pushvalue (L, -2);
-    key = lua_tostring (L, -1);
-    lua_pushvalue (L, -2);
-    value = lua_tostring (L, -1);
+    key = luaL_tolstring (L, -2, NULL);
+    value = luaL_tolstring (L, -2, NULL);
     wp_properties_set (p, key, value);
     lua_pop (L, 3);
   }
diff --git a/tests/wplua/wplua.c b/tests/wplua/wplua.c
index e83586a3..f6aae54a 100644
--- a/tests/wplua/wplua.c
+++ b/tests/wplua/wplua.c
@@ -609,6 +609,44 @@ test_wplua_convert_gvariant_array ()
 
   wplua_free (L);
 }
+static void
+test_wplua_convert_wp_properties ()
+{
+  g_autoptr (GError) error = NULL;
+  lua_State *L = wplua_new ();
+
+  const gchar code[] =
+    "props = { "
+    "  ['test-int'] = 42, "
+    "  ['test-double'] = 3.14, "
+    "  ['test-string'] = 'foobar', "
+    "  ['test-boolean'] = false, "
+    "}\n";
+  wplua_load_buffer (L, code, sizeof (code) - 1, 0, 0, &error);
+  g_assert_no_error (error);
+
+  lua_getglobal (L, "props");
+  g_autoptr (WpProperties) fromlua = wplua_table_to_properties (L, -1);
+
+  g_assert_cmpstr (wp_properties_get (fromlua, "test-int"), ==, "42");
+  g_assert_cmpstr (wp_properties_get (fromlua, "test-double"), ==, "3.14");
+  g_assert_cmpstr (wp_properties_get (fromlua, "test-string"), ==, "foobar");
+  g_assert_cmpstr (wp_properties_get (fromlua, "test-boolean"), ==, "false");
+
+  lua_pop(L, 1);
+  wplua_properties_to_table (L, fromlua);
+  lua_setglobal (L, "fromc");
+
+  const gchar code2[] =
+    "assert (fromc['test-string'] == 'foobar')\n"
+    "assert (fromc['test-int'] == '42')\n"
+    "assert (fromc['test-double'] == '3.14')\n"
+    "assert (fromc['test-boolean'] == 'false')\n";
+  wplua_load_buffer (L, code2, sizeof (code2) - 1, 0, 0, &error);
+  g_assert_no_error (error);
+
+  wplua_free (L);
+}
 
 static void
 test_wplua_script_arguments ()
@@ -660,6 +698,8 @@ main (gint argc, gchar *argv[])
   g_test_add_func ("/wplua/convert/asv", test_wplua_convert_asv);
   g_test_add_func ("/wplua/convert/gvariant_array",
       test_wplua_convert_gvariant_array);
+  g_test_add_func ("/wplua/convert/wp_properties",
+      test_wplua_convert_wp_properties);
   g_test_add_func ("/wplua/script_arguments", test_wplua_script_arguments);
 
   return g_test_run ();
-- 
GitLab