Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/laruence/php-lua into ups…
Browse files Browse the repository at this point in the history
…tream

* 'master' of https://github.com/laruence/php-lua:
  added more test php version
  Renamed test to prevent merge conflict
  add some more table magic
  add more tests
  make lua_obj optional
  fix crashes when printing tables
  fix incorrect type alloc that causes memory corruption
  Fixed issue laruence#40
  Make test finally stable
  fixed test
  Fixed test
  Demangle name of a property when assigning an object into Lua
  • Loading branch information
tony2001 committed Jan 9, 2020
2 parents 7a00426 + 5c405f9 commit ed6d579
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ addons:

php:
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4

notifications:
email: false
Expand Down
22 changes: 20 additions & 2 deletions lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,13 +457,31 @@ int php_lua_send_zval_to_lua(lua_State *L, zval *val) /* {{{ */ {
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();

Expand Down
45 changes: 45 additions & 0 deletions tests/016.phpt
Original file line number Diff line number Diff line change
@@ -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,

0 comments on commit ed6d579

Please sign in to comment.