From 199a846d7aed1fde9878a89b1a90b14f138f7fdf Mon Sep 17 00:00:00 2001 From: Mikhail Galanin Date: Sun, 19 May 2019 19:55:43 +0100 Subject: [PATCH 1/5] Demangle name of a property when assigning an object into Lua --- lua.c | 22 ++++++++++++++++++++-- tests/014.phpt | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 tests/014.phpt diff --git a/lua.c b/lua.c index c532e52..4afb4b5 100755 --- a/lua.c +++ b/lua.c @@ -458,13 +458,31 @@ try_again: lua_newtable(L); ZEND_HASH_FOREACH_KEY_VAL_IND(ht, longkey, key, v) { + zend_bool key_pushed = 0; + if (key) { - ZVAL_STR(&zkey, key); + if (Z_TYPE_P(val) == IS_OBJECT && ZSTR_VAL(key)[0] == 0) { + /* This is object property and it's name should be demangled*/ + const char *prop_name, *class_name; + size_t prop_len; + + zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_len); + + lua_pushlstring(L, prop_name, prop_len); + key_pushed = 1; + } else { + ZVAL_STR(&zkey, key); + } } else { ZVAL_LONG(&zkey, longkey); } - php_lua_send_zval_to_lua(L, &zkey); + + if (!key_pushed) { + php_lua_send_zval_to_lua(L, &zkey); + } + php_lua_send_zval_to_lua(L, v); + lua_settable(L, -3); } ZEND_HASH_FOREACH_END(); diff --git a/tests/014.phpt b/tests/014.phpt new file mode 100644 index 0000000..3654409 --- /dev/null +++ b/tests/014.phpt @@ -0,0 +1,38 @@ +--TEST-- +PHP Object to lua +--SKIPIF-- + +--FILE-- +v = $v; + $this->s = "string = $v"; + } + + static function create($arg) + { + return new self($arg); + } +} + + +$l = new lua(); +$l->registerCallback('create_object', [T::class, 'create']); + +$l->eval(<< ", k, "\\n") +end +CODE +); + +?> +--EXPECTF-- +v -> 2 +s -> string = 2 From aa1dd4f5268a4574a29da6e084a8391f6192019a Mon Sep 17 00:00:00 2001 From: Mikhail Galanin Date: Sun, 19 May 2019 20:02:32 +0100 Subject: [PATCH 2/5] Fixed test --- tests/014.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/014.phpt b/tests/014.phpt index 3654409..301af8c 100644 --- a/tests/014.phpt +++ b/tests/014.phpt @@ -35,4 +35,4 @@ CODE ?> --EXPECTF-- v -> 2 -s -> string = 2 +s -> string = 2 \ No newline at end of file From 0d73a2fdaafcd5bc1e7033f7aa68a7af76f00529 Mon Sep 17 00:00:00 2001 From: Mikhail Galanin Date: Sun, 19 May 2019 20:23:55 +0100 Subject: [PATCH 3/5] fixed test --- tests/014.phpt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/014.phpt b/tests/014.phpt index 301af8c..f7c0d2f 100644 --- a/tests/014.phpt +++ b/tests/014.phpt @@ -26,13 +26,13 @@ $l->registerCallback('create_object', [T::class, 'create']); $l->eval(<< b end ) for i,k in pairs(t) do - print(i, " -> ", k, "\\n") + print(i, " -> ", k, ",") end CODE ); ?> --EXPECTF-- -v -> 2 -s -> string = 2 \ No newline at end of file +v -> 2,s -> string = 2, \ No newline at end of file From a780d5623de63c54770f56b20060660f8d8b5b38 Mon Sep 17 00:00:00 2001 From: Mikhail Galanin Date: Tue, 21 May 2019 08:46:23 +0100 Subject: [PATCH 4/5] Make test finally stable --- tests/014.phpt | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/014.phpt b/tests/014.phpt index f7c0d2f..0f094e5 100644 --- a/tests/014.phpt +++ b/tests/014.phpt @@ -26,13 +26,20 @@ $l->registerCallback('create_object', [T::class, 'create']); $l->eval(<< b end ) -for i,k in pairs(t) do - print(i, " -> ", k, ",") + +local keys = {} + +for k, _ in pairs(t) do + table.insert(keys, k) +end + +table.sort(keys) +for _,k in ipairs(keys) do + print(k, " -> ", t[k], ",") end CODE ); ?> --EXPECTF-- -v -> 2,s -> string = 2, \ No newline at end of file +s -> string = 2,v -> 2, \ No newline at end of file From 5663931ce7d2994f97b30fe3ac9d690c7c0a3ad7 Mon Sep 17 00:00:00 2001 From: Mikhail Galanin Date: Sat, 2 Nov 2019 13:07:12 +0000 Subject: [PATCH 5/5] Renamed test to prevent merge conflict --- tests/{014.phpt => 016.phpt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{014.phpt => 016.phpt} (100%) diff --git a/tests/014.phpt b/tests/016.phpt similarity index 100% rename from tests/014.phpt rename to tests/016.phpt