17 Commits

Author SHA1 Message Date
Xinchen Hui
a6c5162a7c Back to dev 2020-03-10 11:09:56 +08:00
Xinchen Hui
0875f92328 Preparing 2.0.7 2020-03-10 11:09:26 +08:00
Xinchen Hui
445e886030 Fixed ulong undefined 2020-03-01 15:33:39 +08:00
Xinchen Hui
5c405f9746 Merge pull request #42 from negram/demangle-object-name
Demangle name of a property when assigning an object into Lua
2019-12-30 13:26:32 +08:00
Xinchen Hui
9b25ea36a5 Merge pull request #44 from tony2001/multiple_fixes
Multiple fixes
2019-12-30 13:25:46 +08:00
Xinchen Hui
5b6ac82491 added more test php version 2019-12-24 12:54:21 +08:00
Mikhail Galanin
5663931ce7 Renamed test to prevent merge conflict 2019-11-02 13:07:12 +00:00
Antony Dovgal
a5a8995b2e add some more table magic
this commit seems to fix all remaining issue with iterating tables
2019-07-04 18:48:58 +03:00
Antony Dovgal
ffae3062a4 add more tests 2019-05-24 12:42:01 +03:00
Antony Dovgal
13e26a383a make lua_obj optional
so that printing a function would not cause crash
2019-05-24 12:41:55 +03:00
Antony Dovgal
ef70d19a33 fix crashes when printing tables 2019-05-24 12:41:42 +03:00
Antony Dovgal
d763f167d8 fix incorrect type alloc that causes memory corruption 2019-05-24 12:41:29 +03:00
Mikhail Galanin
24fd1baf19 Fixed issue #40
Using absolute index could be error-prone, updated code to use relative indices as it commonly done with Lua
2019-05-24 12:41:07 +03:00
Mikhail Galanin
a780d5623d Make test finally stable 2019-05-21 08:46:23 +01:00
Mikhail Galanin
0d73a2fdaa fixed test 2019-05-19 20:23:55 +01:00
Mikhail Galanin
aa1dd4f526 Fixed test 2019-05-19 20:02:32 +01:00
Mikhail Galanin
199a846d7a Demangle name of a property when assigning an object into Lua 2019-05-19 19:55:43 +01:00
11 changed files with 174 additions and 23 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
*.o
*.lo
*.la
.*.swp

View File

@@ -7,6 +7,10 @@ addons:
php:
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4
notifications:
email: false

45
lua.c
View File

@@ -321,15 +321,18 @@ zval *php_lua_get_zval_from_lua(lua_State *L, int index, zval *lua_obj, zval *rv
break;
case LUA_TTABLE:
array_init(rv);
lua_pushnil(L); /* first key */
while (lua_next(L, index-1) != 0) {
lua_pushvalue(L, index); // table
lua_pushnil(L); // first key
while (lua_next(L, -2) != 0) {
zval key, val;
/* uses 'key' (at index -2) and 'value' (at index -1) */
if (!php_lua_get_zval_from_lua(L, -2, lua_obj, &key)) {
lua_pushvalue(L, -2);
/* uses 'key' (at index -1) and 'value' (at index -2) */
if (!php_lua_get_zval_from_lua(L, -1, lua_obj, &key)) {
break;
}
if (!php_lua_get_zval_from_lua(L, -1, lua_obj, &val)) {
if (!php_lua_get_zval_from_lua(L, -2, lua_obj, &val)) {
zval_ptr_dtor(&key);
/* there is a warning already in php_lua_get_zval_from_lua */
break;
@@ -349,17 +352,13 @@ zval *php_lua_get_zval_from_lua(lua_State *L, int index, zval *lua_obj, zval *rv
default:
break;
}
lua_pop(L, 1);
lua_pop(L, 2);
}
lua_pop(L, 1);
break;
case LUA_TFUNCTION:
{
long ref_id = 0;
if (!lua_obj) {
php_error_docref(NULL, E_WARNING, "corrupted Lua object");
break;
}
lua_pushvalue(L, index);
ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
@@ -425,7 +424,7 @@ try_again:
add_next_index_zval(callbacks, val);
} else {
zval *v;
ulong longkey;
zend_ulong longkey;
zend_string *key;
zval zkey;
@@ -458,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();

View File

@@ -53,7 +53,9 @@ zval* php_lua_closure_instance(zval *instance, long ref_id, zval *lua_obj) {
object_init_ex(instance, lua_closure_ce);
objval = php_lua_closure_object_from_zend_object(Z_OBJ_P(instance));
objval->closure = ref_id;
ZVAL_ZVAL(&(objval->lua), lua_obj, 1, 0);
if (lua_obj) {
ZVAL_ZVAL(&(objval->lua), lua_obj, 1, 0);
}
return instance;
}
@@ -75,7 +77,7 @@ PHP_METHOD(lua_closure, invoke) {
zval rv;
if (ZEND_NUM_ARGS()) {
arguments = emalloc(sizeof(zval*) * ZEND_NUM_ARGS());
arguments = emalloc(sizeof(zval) * ZEND_NUM_ARGS());
if (zend_get_parameters_array_ex(ZEND_NUM_ARGS(), arguments) == FAILURE) {
efree(arguments);
zend_throw_exception_ex(NULL, 0, "cannot get arguments for calling closure");

View File

@@ -23,11 +23,11 @@
<email>msaraujo@php.net</email>
<active>yes</active>
</developer>
<date>2018-12-21</date>
<date>2020-03-10</date>
<time>11:10:00</time>
<version>
<release>2.0.6</release>
<api>2.0.6</api>
<release>2.0.7</release>
<api>2.0.7</api>
</version>
<stability>
<release>stable</release>
@@ -35,7 +35,7 @@
</stability>
<license uri="http://www.php.net/license">PHP</license>
<notes>
- Fixed Hash Recursive Detecting in PHP-7.3
- Fixed windows build for 7.4
</notes>
<contents>
<dir name="/">
@@ -83,6 +83,21 @@
<extsrcrelease />
<changelog>
<release>
<date>2020-03-10</date>
<version>
<release>2.0.7</release>
<api>2.0.7</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.php.net/license">PHP License</license>
<notes>
- Fixed windows build for 7.4
</notes>
</release>
<date>2018-12-21</date>
<version>
<release>2.0.6</release>
@@ -96,8 +111,6 @@
<notes>
- Fixed Hash Recursive Detecting in PHP-7.3
</notes>
</release>
<release>
<date>2017-12-31</date>
<version>

View File

@@ -48,7 +48,7 @@ extern zend_module_entry lua_module_entry;
#define LUA_G(v) (lua_globals.v)
#endif
#define PHP_LUA_VERSION "2.0.6"
#define PHP_LUA_VERSION "2.0.8-dev"
struct _php_lua_object {
lua_State *L;

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,

19
tests/issue040.phpt Normal file
View File

@@ -0,0 +1,19 @@
--TEST--
ISSUE #040 (segmentation fault)
--SKIPIF--
<?php
if (!extension_loaded("lua")) print "skip lua extension missing";
?>
--FILE--
<?php
$lua = new Lua();
$lua->eval(<<<CODE
local a = {}
print(a)
CODE
);
?>
--EXPECT--
Array
(
)

BIN
tests/print.phpt Normal file

Binary file not shown.

28
tests/print_tables-1.phpt Normal file
View File

@@ -0,0 +1,28 @@
--TEST--
print with tables - 1
--SKIPIF--
<?php
if (!extension_loaded("lua")) print "skip lua extension missing";
?>
--FILE--
<?php
$lua = new Lua();
$lua->eval(<<<CODE
local people = {
{
phone = "123456"
},
}
print(people)
CODE
);
?>
--EXPECT--
Array
(
[1] => Array
(
[phone] => 123456
)
)

22
tests/print_tables-2.phpt Normal file
View File

@@ -0,0 +1,22 @@
--TEST--
print with tables - 2
--SKIPIF--
<?php
if (!extension_loaded("lua")) print "skip lua extension missing";
?>
--FILE--
<?php
$lua = new Lua();
$lua->eval(<<<CODE
local a = {
LEFT = 1,
}
print(a)
CODE
);
?>
--EXPECT--
Array
(
[LEFT] => 1
)