Skip to content

Commit faa02f9

Browse files
authored
Demangle object name (#2)
* Demangle name of a property when assigning an object into Lua
1 parent 74276b7 commit faa02f9

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

lua.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,13 +457,31 @@ int php_lua_send_zval_to_lua(lua_State *L, zval *val) /* {{{ */ {
457457
lua_newtable(L);
458458

459459
ZEND_HASH_FOREACH_KEY_VAL_IND(ht, longkey, key, v) {
460+
zend_bool key_pushed = 0;
461+
460462
if (key) {
461-
ZVAL_STR(&zkey, key);
463+
if (Z_TYPE_P(val) == IS_OBJECT && ZSTR_VAL(key)[0] == 0) {
464+
/* This is object property and it's name should be demangled*/
465+
const char *prop_name, *class_name;
466+
size_t prop_len;
467+
468+
zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_len);
469+
470+
lua_pushlstring(L, prop_name, prop_len);
471+
key_pushed = 1;
472+
} else {
473+
ZVAL_STR(&zkey, key);
474+
}
462475
} else {
463476
ZVAL_LONG(&zkey, longkey);
464477
}
465-
php_lua_send_zval_to_lua(L, &zkey);
478+
479+
if (!key_pushed) {
480+
php_lua_send_zval_to_lua(L, &zkey);
481+
}
482+
466483
php_lua_send_zval_to_lua(L, v);
484+
467485
lua_settable(L, -3);
468486
} ZEND_HASH_FOREACH_END();
469487

tests/016.phpt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--TEST--
2+
PHP Object to lua
3+
--SKIPIF--
4+
<?php if (!extension_loaded("lua")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
class T {
8+
private $v;
9+
private $s;
10+
11+
public function __construct($v)
12+
{
13+
$this->v = $v;
14+
$this->s = "string = $v";
15+
}
16+
17+
static function create($arg)
18+
{
19+
return new self($arg);
20+
}
21+
}
22+
23+
24+
$l = new lua();
25+
$l->registerCallback('create_object', [T::class, 'create']);
26+
27+
$l->eval(<<<CODE
28+
local t = create_object(2)
29+
30+
local keys = {}
31+
32+
for k, _ in pairs(t) do
33+
table.insert(keys, k)
34+
end
35+
36+
table.sort(keys)
37+
for _,k in ipairs(keys) do
38+
print(k, " -> ", t[k], ",")
39+
end
40+
CODE
41+
);
42+
43+
?>
44+
--EXPECTF--
45+
s -> string = 2,v -> 2,

0 commit comments

Comments
 (0)