Merge pull request #42 from negram/demangle-object-name

Demangle name of a property when assigning an object into Lua
This commit is contained in:
Xinchen Hui
2019-12-30 13:26:32 +08:00
committed by GitHub
2 changed files with 65 additions and 2 deletions

22
lua.c
View File

@@ -457,13 +457,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();

45
tests/016.phpt Normal file
View File

@@ -0,0 +1,45 @@
--TEST--
PHP Object to lua
--SKIPIF--
<?php if (!extension_loaded("lua")) print "skip"; ?>
--FILE--
<?php
class T {
private $v;
private $s;
public function __construct($v)
{
$this->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(<<<CODE
local t = create_object(2)
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--
s -> string = 2,v -> 2,