62 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
Xinchen Hui
84c831e4cb releasing 2.0.6 2018-12-21 11:11:37 +08:00
Xinchen Hui
f588c88528 Fixed hash recursive detecting 2018-12-21 11:09:57 +08:00
Xinchen Hui
ad31d5f7c6 back to dev 2017-12-31 12:34:09 +08:00
Xinchen Hui
15f58c43fb releasing 2.0.5 2017-12-31 12:33:16 +08:00
Xinchen Hui
c4c61f05bf Revert the last merge 2017-10-18 13:05:38 +08:00
Xinchen Hui
9ef044896a Merge pull request #31 from sgolemon/callback.refcount
Explicitly release held references to PHP callbacks
2017-10-18 12:56:28 +08:00
Xinchen Hui
feb39efa86 Merge pull request #32 from sgolemon/gitignore
Add a .gitignore to disregard build artifacts
2017-10-18 12:56:02 +08:00
Xinchen Hui
4438a88e85 Merge pull request #33 from sgolemon/object.alloc
Fix allocation of lua object.
2017-10-18 12:55:49 +08:00
Xinchen Hui
0c6a60d21b Merge pull request #34 from sgolemon/php_printf
Prefer php_printf() over printf()
2017-10-18 12:55:26 +08:00
Xinchen Hui
db95520d2f Merge pull request #35 from sgolemon/non-string.accessor
Fix non-string member access in read/write prop.
2017-10-18 12:55:10 +08:00
Xinchen Hui
8afc555bce Merge pull request #36 from sgolemon/closure.refactor
Refactor LuaClosure
2017-10-18 12:53:50 +08:00
Sara Golemon
486de80ebf Refactor LuaClosure
Use proper internal object storage rather than private props.
Favor NULL clone_obj handler over empty private __clone method.
Favor free_obj handler over __destruct() method.
2017-10-03 10:09:53 -04:00
Sara Golemon
1813b36307 Fix non-string member access in read/write prop.
These both have a null pointer deref waiting to happen
and are coincidentally more complex than they need to be.

zval_get_string() will incref any IS_STRING zval,
or copy/convert any non-string zval.

zend_string_release() will then decref or free as appropriate.
2017-10-03 08:51:02 -04:00
Sara Golemon
a2be2c935b Prefer php_printf() over printf()
On CLI the distinction doesn't matter, but in a web sapi this
will ensure the output goes to the right place.
2017-10-03 08:37:13 -04:00
Sara Golemon
6690ee0c70 Fix allocation of lua object.
Size the malloc using zend's helper.
Use ecalloc over emalloc to make sure uninit'd structs are clear.
No need to check return value as e*alloc() funcs can't fail.
2017-10-03 08:32:51 -04:00
Sara Golemon
f64a8d6d27 Add a .gitignore to disregard build artifacts 2017-10-02 19:01:00 -04:00
Sara Golemon
d6c7850afa Explicitly release held references to PHP callbacks 2017-10-02 18:52:01 -04:00
Xinchen Hui
257d46c1e4 back to dev 2017-07-12 11:42:28 +08:00
Xinchen Hui
e350b425f4 release 2.0.4 2017-07-12 11:42:06 +08:00
Xinchen Hui
527f927ead Fixed bug #73964 (Segmentation fault (11)) 2017-02-12 10:53:36 +08:00
Xinchen Hui
9be57325c6 back to dev 2017-02-10 19:14:10 +08:00
Xinchen Hui
1ff1eb10c9 releaseing 2.0.3 2017-02-10 19:13:44 +08:00
Xinchen Hui
cbe65b9ad6 Fixed issue #20 (PHP 7: Lua::eval(): unsupported type `unknown' for lua) 2016-05-30 11:44:12 +08:00
Xinchen Hui
04f7106631 back to dev 2016-05-26 17:42:59 +08:00
Xinchen Hui
7406b07f2c Prepare for 2.0.2 2016-05-26 17:42:22 +08:00
Xinchen Hui
17cbba8b77 Fixed issue #22 (Boolean FALSE is always TRUE) 2016-05-20 11:54:37 +08:00
Xinchen Hui
7cdb287c33 back to dev 2016-04-11 10:55:22 +08:00
Xinchen Hui
f3d6dca1a0 Prepareing for 2.0.1 2016-04-11 10:54:41 +08:00
Xinchen Hui
f177b21ff1 Added tests for bug #71997 2016-04-11 10:51:15 +08:00
Xinchen Hui
c7d9844cf1 Fixed default searching in centOS 2016-04-11 10:50:28 +08:00
Xinchen Hui
12c53f28eb Fixed nApplyCount checking 2016-04-11 10:37:11 +08:00
Xinchen Hui
86cb62e0b8 Merge pull request #19 from tony2001/php7_fixes
fix send_zval_to_lua()
2016-04-07 19:14:59 +08:00
Antony Dovgal
dc226c345d fix send_zval_to_lua() 2016-03-31 12:56:15 +03:00
Xinchen Hui
2d23b565e5 Merge branch 'master' into php7
Conflicts:
	README.md
2015-10-26 21:11:38 -07:00
Xinchen Hui
6a2bf0e8af Update README 2015-10-26 21:11:12 -07:00
Xinchen Hui
230a8af900 Update README 2015-10-26 21:10:51 -07:00
Xinchen Hui
468203ce7c Merge branch 'master' into php7 2015-10-26 21:05:54 -07:00
Xinchen Hui
aaf49be301 trigger triavis 2015-10-26 21:05:41 -07:00
Xinchen Hui
1bd4adf039 No valgrind is valiable 2015-10-26 20:59:34 -07:00
Xinchen Hui
18e2bb493d enable full test 2015-10-26 20:56:56 -07:00
Xinchen Hui
0b28ef4f55 Merge branch 'master' into php7
Conflicts:
	.travis.yml
	travis/compile.sh
2015-10-26 20:56:19 -07:00
Xinchen Hui
d3323aa9fe enable travis 2015-10-26 20:54:55 -07:00
Xinchen Hui
f6a2f09b19 User hjanuschka doesn't exists? 2015-10-27 11:16:04 +08:00
Xinchen Hui
6c2a37f99c back to dev 2015-10-27 11:14:10 +08:00
Xinchen Hui
50d32564d2 Master is only for PHP5.X now 2015-02-25 22:23:05 +08:00
18 changed files with 549 additions and 192 deletions

38
.gitignore vendored Normal file
View File

@@ -0,0 +1,38 @@
*.o
*.lo
*.la
.*.swp
.deps
.libs/
include/
tests/*.diff
tests/*.exp
tests/*.log
tests/*.php
tests/*.out
tests/*.sh
Makefile
Makefile.fragments
Makefile.global
Makefile.objects
acinclude.m4
aclocal.m4
autom4te.cache/
build/
config.guess
config.h
config.h.in
config.log
config.nice
config.status
config.sub
configure
configure.ac
configure.in
install-sh
libtool
ltmain.sh
missing
mkinstalldirs
modules/
run-tests.php

View File

@@ -1,21 +1,27 @@
language: php
language: php
addons:
apt:
packages:
- liblua5.2-dev
php:
- 5.6
- 5.5
- 5.4
- 5.3
- 5.3.3
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4
before_install:
- sudo apt-get update -qq
- sudo apt-get install -y libjson0-dev libjson0 php5-cli php5-dev liblua5.2-dev
notifications:
email: false
irc: "irc.efnet.org#php.yaf"
env:
- REPORT_EXIT_STATUS=1 NO_INTERACTION=1
#Compile
before_script:
# Compile PHP
- ./travis/compile.sh
script:
- ./travis/build_check.sh
# Run PHPs run-tests.php
script: TEST_PHP_ARGS="--show-diff" make test

View File

@@ -1,2 +1,6 @@
"Lua is a powerful, fast, light-weight, embeddable scripting language."
# PHP Lua
[![Build Status](https://secure.travis-ci.org/laruence/php-lua.png)](https://travis-ci.org/laruence/php-lua)
This extension embeds the lua interpreter and offers an OO-API to lua variables and functions.
"Lua is a powerful, fast, light-weight, embeddable scripting language."

View File

@@ -1,8 +1,9 @@
dnl $Id: config.m4 321796 2012-01-05 17:23:48Z laruence $
PHP_ARG_WITH(lua, for lua support,
[ --with-lua=[DIR] Include php lua support])
PHP_ARG_WITH(lua-version, to specify a custom lua version, [ --with-lua-version=[VERSION]] Use the specified lua version.)
PHP_ARG_WITH(lua-version, to specify a custom lua version, [ --with-lua-version=[VERSION]] Use the specified lua version.)
if test "$PHP_LUA" != "no"; then
if test -r $PHP_LUA/include/lua.h; then
LUA_INCLUDE_DIR=$PHP_LUA/include
@@ -15,6 +16,12 @@ if test "$PHP_LUA" != "no"; then
break
fi
if test -r $i/include/lua.h; then
LUA_INCLUDE_DIR=$i/include
AC_MSG_RESULT(found in $i)
break
fi
if test "$PHP_LUA_VERSION" != "yes"; then
if test -r $i/include/lua$PHP_LUA_VERSION/lua.h; then
LUA_INCLUDE_DIR=$i/include/lua$PHP_LUA_VERSION

189
lua.c
View File

@@ -93,31 +93,31 @@ ZEND_GET_MODULE(lua)
static void php_lua_stack_dump(lua_State* L) {
int i = 1;
int n = lua_gettop(L);
printf("The Length of stack is %d\n", n);
php_printf("The Length of stack is %d\n", n);
for (; i <= n; ++i) {
int t = lua_type(L, i);
printf("%s:", lua_typename(L, t));
php_printf("%s:", lua_typename(L, t));
switch(t) {
case LUA_TNUMBER:
printf("%f", lua_tonumber(L, i));
php_printf("%f", lua_tonumber(L, i));
break;
case LUA_TSTRING:
printf("%s", lua_tostring(L, i));
php_printf("%s", lua_tostring(L, i));
break;
case LUA_TTABLE:
break;
case LUA_TFUNCTION:
break;
case LUA_TNIL:
printf("NULL");
php_printf("NULL");
break;
case LUA_TBOOLEAN:
printf("%s", lua_toboolean(L, i) ? "TRUE" : "FALSE");
php_printf("%s", lua_toboolean(L, i) ? "TRUE" : "FALSE");
break;
default:
break;
}
printf("\n");
php_printf("\n");
}
}
/* }}} */
@@ -174,6 +174,11 @@ static void php_lua_dtor_object(zend_object *object) /* {{{ */ {
php_lua_object *lua_obj = php_lua_obj_from_obj(object);
zend_object_std_dtor(&(lua_obj->obj));
}
/* }}} */
static void php_lua_free_object(zend_object *object) /* {{{ */ {
php_lua_object *lua_obj = php_lua_obj_from_obj(object);
if (lua_obj->L) {
lua_close(lua_obj->L);
@@ -192,12 +197,7 @@ zend_object *php_lua_create_object(zend_class_entry *ce)
lua_atpanic(L, php_lua_atpanic);
intern = emalloc(sizeof(php_lua_object) + sizeof(zval) * (ce->default_properties_count - 1));
if (!intern) {
php_error_docref(NULL, E_ERROR, "alloc memory for lua object failed");
}
intern = ecalloc(1, sizeof(php_lua_object) + zend_object_properties_size(ce));
intern->L = L;
zend_object_std_init(&intern->obj, ce);
@@ -212,27 +212,22 @@ zend_object *php_lua_create_object(zend_class_entry *ce)
/** {{{ static zval * php_lua_read_property(zval *object, zval *member, int type)
*/
zval *php_lua_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv){
lua_State *L = NULL;
zval *tmp_member = NULL;
lua_State *L = (Z_LUAVAL_P(object))->L;
zend_string *str_member;
if (type != BP_VAR_R) {
ZVAL_NULL(rv);
return rv;
}
if (Z_TYPE_P(member) != IS_STRING) {
*tmp_member = *member;
zval_copy_ctor(tmp_member);
convert_to_string(tmp_member);
member = tmp_member;
}
L = (Z_LUAVAL_P(object))->L;
str_member = zval_get_string(member);
#if (LUA_VERSION_NUM < 502)
lua_getfield(L, LUA_GLOBALSINDEX, Z_STRVAL_P(member));
lua_getfield(L, LUA_GLOBALSINDEX, ZSTR_VAL(str_member));
#else
lua_getglobal(L, Z_STRVAL_P(member));
lua_getglobal(L, ZSTR_VAL(str_member));
#endif
zend_string_release(str_member);
php_lua_get_zval_from_lua(L, -1, object, rv);
lua_pop(L, 1);
return rv;
@@ -242,17 +237,8 @@ zval *php_lua_read_property(zval *object, zval *member, int type, void **cache_s
/** {{{ static void php_lua_write_property(zval *object, zval *member, zval *value)
*/
static void php_lua_write_property(zval *object, zval *member, zval *value, void ** key) {
lua_State *L = NULL;
zval *tmp_member = NULL;
if (Z_TYPE_P(member) != IS_STRING) {
*tmp_member = *member;
zval_copy_ctor(tmp_member);
convert_to_string(tmp_member);
member = tmp_member;
}
L = (Z_LUAVAL_P(object))->L;
lua_State *L = (Z_LUAVAL_P(object))->L;
zend_string *str_member = zval_get_string(member);
#if (LUA_VERSION_NUM < 502)
php_lua_send_zval_to_lua(L, member);
@@ -264,9 +250,7 @@ static void php_lua_write_property(zval *object, zval *member, zval *value, void
lua_setglobal(L, Z_STRVAL_P(member));
#endif
if (tmp_member) {
zval_ptr_dtor(tmp_member);
}
zend_string_release(str_member);
}
/* }}} */
@@ -337,21 +321,24 @@ 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;
}
switch(Z_TYPE(key)) {
switch (Z_TYPE(key)) {
case IS_DOUBLE:
case IS_LONG:
add_index_zval(rv, Z_DVAL(key), &val);
@@ -365,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);
@@ -401,11 +384,15 @@ zval *php_lua_get_zval_from_lua(lua_State *L, int index, zval *lua_obj, zval *rv
int php_lua_send_zval_to_lua(lua_State *L, zval *val) /* {{{ */ {
try_again:
switch (Z_TYPE_P(val)) {
case IS_TRUE:
case IS_FALSE:
lua_pushboolean(L, Z_LVAL_P(val));
lua_pushboolean(L, 1);
break;
case IS_FALSE:
lua_pushboolean(L, 0);
break;
case IS_UNDEF:
case IS_NULL:
lua_pushnil(L);
break;
@@ -436,34 +423,92 @@ int php_lua_send_zval_to_lua(lua_State *L, zval *val) /* {{{ */ {
zval_add_ref(val);
add_next_index_zval(callbacks, val);
} else {
zval *v;
zval *v;
zend_ulong longkey;
zend_string *key;
zval zkey;
zval zkey;
HashTable *ht = HASH_OF(val);
if (++ht->u.v.nApplyCount > 1) {
php_error_docref(NULL, E_ERROR, "recursion found");
--ht->u.v.nApplyCount;
break;
if (
#if PHP_VERSION_ID < 70300
ZEND_HASH_APPLY_PROTECTION(ht)
#else
!(GC_FLAGS(ht) & GC_IMMUTABLE)
#endif
) {
if (
#if PHP_VERSION_ID < 70300
ZEND_HASH_GET_APPLY_COUNT(ht)
#else
GC_IS_RECURSIVE(ht)
#endif
) {
php_error_docref(NULL, E_ERROR, "recursion found");
break;
}
#if PHP_VERSION_ID < 70300
ZEND_HASH_INC_APPLY_COUNT(ht);
#else
GC_PROTECT_RECURSION(ht);
#endif
}
lua_newtable(L);
ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, v) {
if (Z_TYPE_P(v) == IS_STRING) {
ZVAL_STR(&zkey, key);
ZEND_HASH_FOREACH_KEY_VAL_IND(ht, longkey, key, v) {
zend_bool key_pushed = 0;
if (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);
}
if (Z_TYPE_P(v) == IS_LONG) {
ZVAL_LONG(&zkey, Z_LVAL_P(v));
if (!key_pushed) {
php_lua_send_zval_to_lua(L, &zkey);
}
php_lua_send_zval_to_lua(L, &zkey);
php_lua_send_zval_to_lua(L, v);
lua_settable(L, -3);
} ZEND_HASH_FOREACH_END();
--ht->u.v.nApplyCount;
if (
#if PHP_VERSION_ID < 70300
ZEND_HASH_APPLY_PROTECTION(ht)
#else
!(GC_FLAGS(ht) & GC_IMMUTABLE)
#endif
) {
#if PHP_VERSION_ID < 70300
ZEND_HASH_DEC_APPLY_COUNT(ht);
#else
GC_UNPROTECT_RECURSION(ht);
#endif
}
}
}
break;
case IS_REFERENCE:
ZVAL_DEREF(val);
goto try_again;
break;
case IS_INDIRECT:
val = Z_INDIRECT_P(val);
goto try_again;
break;
default:
php_error_docref(NULL, E_ERROR, "unsupported type `%s' for lua", zend_zval_type_name(val));
lua_pushnil(L);
@@ -533,13 +578,9 @@ static zval *php_lua_call_lua_function(zval *lua_obj, zval *func, zval *args, in
}
} else if (IS_OBJECT == Z_TYPE_P(func)
&& instanceof_function(Z_OBJCE_P(func), php_lua_get_closure_ce())) {
zval *closure = zend_read_property(php_lua_get_closure_ce(), func, ZEND_STRL("_closure"), 1, &rv);
if (!Z_LVAL_P(closure)) {
zend_throw_exception_ex(lua_exception_ce, 0, "invalid lua closure");
return NULL;
}
lua_closure_object *closure_obj = php_lua_closure_object_from_zend_object(Z_OBJ_P(func));
bp = lua_gettop(L);
lua_rawgeti(L, LUA_REGISTRYINDEX, Z_LVAL_P(closure));
lua_rawgeti(L, LUA_REGISTRYINDEX, closure_obj->closure);
if (LUA_TFUNCTION != lua_type(L, lua_gettop(L))) {
lua_pop(L, -1);
zend_throw_exception_ex(lua_exception_ce, 0, "call to lua closure failed");
@@ -784,7 +825,7 @@ zend_function_entry lua_class_methods[] = {
PHP_ME(lua, getVersion, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
PHP_ME(lua, registerCallback, arginfo_lua_register, ZEND_ACC_PUBLIC)
PHP_MALIAS(lua, __call, call, arginfo_lua_call, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
PHP_FE_END
};
/* }}} */
@@ -813,7 +854,7 @@ PHP_MINIT_FUNCTION(lua) {
memcpy(&lua_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
lua_object_handlers.offset = XtOffsetOf(php_lua_object, obj);
lua_object_handlers.dtor_obj = php_lua_dtor_object;
lua_object_handlers.free_obj = NULL;
lua_object_handlers.free_obj = php_lua_free_object;
lua_object_handlers.clone_obj = NULL;
lua_object_handlers.write_property = php_lua_write_property;
lua_object_handlers.read_property = php_lua_read_property;

View File

@@ -25,6 +25,7 @@
#include "Zend/zend_exceptions.h"
#include "lua.h"
#include "lua_closure.h"
#include "lauxlib.h"
#include "lualib.h"
@@ -33,6 +34,7 @@
static zend_class_entry *lua_closure_ce;
extern zend_class_entry *lua_ce;
extern zend_class_entry *lua_exception_ce;
static zend_object_handlers lua_closure_handlers;
/** {{{ ARG_INFO
*
@@ -45,10 +47,15 @@ ZEND_END_ARG_INFO()
/** {{{ zval * php_lua_closure_instance(zval *instance, long ref_id, zval *lua_obj)
*/
zval * php_lua_closure_instance(zval *instance, long ref_id, zval *lua_obj) {
zval* php_lua_closure_instance(zval *instance, long ref_id, zval *lua_obj) {
lua_closure_object *objval;
object_init_ex(instance, lua_closure_ce);
zend_update_property_long(lua_closure_ce, instance, ZEND_STRL("_closure"), ref_id);
zend_update_property(lua_closure_ce, instance, ZEND_STRL("_lua_object"), lua_obj);
objval = php_lua_closure_object_from_zend_object(Z_OBJ_P(instance));
objval->closure = ref_id;
if (lua_obj) {
ZVAL_ZVAL(&(objval->lua), lua_obj, 1, 0);
}
return instance;
}
@@ -60,39 +67,17 @@ PHP_METHOD(lua_closure, __construct) {
}
/* }}} */
/** {{{ proto LuaClosure::__destruct()
*/
PHP_METHOD(lua_closure, __destruct) {
zval *lua_obj, *closure, rv;
lua_obj = zend_read_property(lua_closure_ce, getThis(), ZEND_STRL("_lua_object"), 1, &rv);
if (ZVAL_IS_NULL(lua_obj)
|| Z_TYPE_P(lua_obj) != IS_OBJECT
|| !instanceof_function(Z_OBJCE_P(lua_obj), lua_ce)) {
RETURN_FALSE;
}
closure = zend_read_property(lua_closure_ce, getThis(), ZEND_STRL("_closure"), 1, &rv);
if (!Z_LVAL_P(closure)) {
RETURN_FALSE;
}
luaL_unref((Z_LUAVAL_P(lua_obj))->L, LUA_REGISTRYINDEX, Z_LVAL_P(closure));
}
/* }}} */
/** {{{ proto LuaClosure::invoke(mxied $args)
*/
PHP_METHOD(lua_closure, invoke) {
lua_closure_object *objval = php_lua_closure_object_from_zend_object(Z_OBJ_P(getThis()));
int bp, sp;
zval *arguments = NULL;
zval *lua_obj = NULL;
lua_State *L = NULL;
zval *closure = NULL;
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");
@@ -100,25 +85,16 @@ PHP_METHOD(lua_closure, invoke) {
}
}
lua_obj = zend_read_property(lua_closure_ce, getThis(), ZEND_STRL("_lua_object"), 1, &rv);
if (ZVAL_IS_NULL(lua_obj)
|| Z_TYPE_P(lua_obj) != IS_OBJECT
|| !instanceof_function(Z_OBJCE_P(lua_obj), lua_ce)) {
if (Z_TYPE(objval->lua) != IS_OBJECT
|| !instanceof_function(Z_OBJCE(objval->lua), lua_ce)) {
zend_throw_exception_ex(NULL, 0, "corrupted Lua object");
return;
}
closure = zend_read_property(lua_closure_ce, getThis(), ZEND_STRL("_closure"), 1, &rv);
if (!Z_LVAL_P(closure)) {
zend_throw_exception_ex(NULL, 0, "invalid lua closure");
return;
}
L = (Z_LUAVAL_P(lua_obj))->L;
L = (Z_LUAVAL(objval->lua))->L;
bp = lua_gettop(L);
lua_rawgeti(L, LUA_REGISTRYINDEX, Z_LVAL_P(closure));
lua_rawgeti(L, LUA_REGISTRYINDEX, objval->closure);
if (LUA_TFUNCTION != lua_type(L, lua_gettop(L))) {
lua_pop(L, -1);
zend_throw_exception_ex(NULL, 0, "call to lua closure failed");
@@ -147,13 +123,13 @@ PHP_METHOD(lua_closure, invoke) {
if (!sp) {
RETURN_NULL();
} else if (sp == 1) {
php_lua_get_zval_from_lua(L, -1, lua_obj, return_value);
php_lua_get_zval_from_lua(L, -1, &(objval->lua), return_value);
} else {
zval rv;
int i = 0;
array_init(return_value);
for (i = -sp; i < 0; i++) {
php_lua_get_zval_from_lua(L, i, lua_obj, &rv);
php_lua_get_zval_from_lua(L, i, &(objval->lua), &rv);
add_next_index_zval(return_value, &rv);
}
}
@@ -166,51 +142,37 @@ PHP_METHOD(lua_closure, invoke) {
}
/* }}} */
/** {{{ proto LuaClosure::__clone()
*/
PHP_METHOD(lua_closure, __clone) {
}
/* }}} */
/* {{{ lua_class_methods[]
*
*/
zend_function_entry lua_closure_methods[] = {
PHP_ME(lua_closure, __construct, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
PHP_ME(lua_closure, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
PHP_ME(lua_closure, __clone, NULL, ZEND_ACC_PRIVATE)
PHP_ME(lua_closure, invoke, arginfo_lua_invoke, ZEND_ACC_PUBLIC)
PHP_MALIAS(lua_closure, __invoke, invoke, arginfo_lua_invoke, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
PHP_FE_END
};
/* }}} */
static void php_lua_closure_dtor_object(void *object, zend_object_handlers handle) /* {{{ */
static void php_lua_closure_free_obj(zend_object *zobj) /* {{{ */
{
zend_object *obj = (zend_object*)object;
lua_closure_object *objval = php_lua_closure_object_from_zend_object(zobj);
zend_object_std_dtor(obj);
efree(obj);
if ((Z_TYPE(objval->lua) == IS_OBJECT) &&
instanceof_function(Z_OBJCE(objval->lua), lua_ce)) {
luaL_unref((Z_LUAVAL(objval->lua))->L, LUA_REGISTRYINDEX, objval->closure);
}
zval_dtor(&(objval->lua));
zend_object_std_dtor(zobj);
} /* }}} */
zend_object *php_lua_closure_create_object(zend_class_entry *ce) /* {{{ */
{
zend_object *intern;
intern = emalloc(sizeof(zend_object)+ sizeof(zval) * (ce->default_properties_count - 1));
lua_closure_object *objval = ecalloc(1, sizeof(lua_closure_object) + zend_object_properties_size(ce));
zend_object *zobj = &(objval->std);
if (!intern) {
php_error_docref(NULL, E_ERROR, "alloc memory for lua object failed");
}
zend_object_std_init(intern, ce);
object_properties_init(intern, ce);
intern->handlers = zend_get_std_object_handlers();
return intern;
zend_object_std_init(zobj, ce);
zobj->handlers = &lua_closure_handlers;
return zobj;
} /* }}} */
void php_lua_closure_register() /* {{{ */
@@ -222,10 +184,10 @@ void php_lua_closure_register() /* {{{ */
lua_closure_ce->create_object = php_lua_closure_create_object;
lua_closure_ce->ce_flags |= ZEND_ACC_FINAL;
zend_declare_property_long(lua_closure_ce, ZEND_STRL("_closure"), 0, ZEND_ACC_PRIVATE);
zend_declare_property_null(lua_closure_ce, ZEND_STRL("_lua_object"), ZEND_ACC_PRIVATE);
memcpy(&lua_closure_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
lua_closure_handlers.offset = XtOffsetOf(lua_closure_object, std);
lua_closure_handlers.clone_obj = NULL;
lua_closure_handlers.free_obj = php_lua_closure_free_obj;
} /* }}} */
zend_class_entry *php_lua_get_closure_ce() /* {{{ */

View File

@@ -19,6 +19,17 @@
#ifndef LUA_CLOSURE_H
#define LUA_CLOSURE_H
typedef struct _lua_closure_object {
long closure;
zval lua;
zend_object std;
} lua_closure_object;
static inline lua_closure_object* php_lua_closure_object_from_zend_object(zend_object *zobj) {
return ((lua_closure_object*)(zobj + 1)) - 1;
}
void php_lua_closure_register();
zend_class_entry *php_lua_get_closure_ce();
zval * php_lua_closure_instance(zval *instance, long ref_id, zval *lua_obj);

View File

@@ -23,25 +23,19 @@
<email>msaraujo@php.net</email>
<active>yes</active>
</developer>
<developer>
<name>Helmut Januschka</name>
<user>hjanuschka</user>
<email>helmut@januschka.com</email>
<active>yes</active>
</developer>
<date>2015-10-27</date>
<time>11:12:27</time>
<date>2020-03-10</date>
<time>11:10:00</time>
<version>
<release>2.0.0</release>
<api>2.0.0</api>
<release>2.0.7</release>
<api>2.0.7</api>
</version>
<stability>
<release>beta</release>
<api>beta</api>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.php.net/license">PHP</license>
<notes>
- Release lua for PHP7
- Fixed windows build for 7.4
</notes>
<contents>
<dir name="/">
@@ -68,7 +62,10 @@
<file name="011.phpt" role="test" />
<file name="012.phpt" role="test" />
<file name="013.phpt" role="test" />
<file name="issue012.phpt" role="test" />
<file name="bug65097.phpt" role="test" />
<file name="bug71997.phpt" role="test" />
<file name="bug73964.phpt" role="test" />
</dir>
</dir>
</contents>
@@ -85,6 +82,113 @@
<providesextension>lua</providesextension>
<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>
<api>2.0.6</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.php.net/license">PHP License</license>
<notes>
- Fixed Hash Recursive Detecting in PHP-7.3
</notes>
<release>
<date>2017-12-31</date>
<version>
<release>2.0.5</release>
<api>2.0.5</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.php.net/license">PHP License</license>
<notes>
- Refactor LuaClosure. (SaraG)
- Fix non-string member access in read/write prop. (SaraG)
- Fix allocation of lua object. (SaraG)
</notes>
</release>
<release>
<date>2017-07-12</date>
<version>
<release>2.0.4</release>
<api>2.0.4</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.php.net/license">PHP License</license>
<notes>
- Fixed bug #73964 (Segmentation fault)
</notes>
</release>
<release>
<date>2017-02-10</date>
<version>
<release>2.0.3</release>
<api>2.0.3</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.php.net/license">PHP License</license>
<notes>
- Fixed issue #20 (PHP 7: Lua::eval(): unsupported type `unknown' for lua)
</notes>
</release>
<release>
<date>2016-05-26</date>
<version>
<release>2.0.2</release>
<api>2.0.2</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.php.net/license">PHP License</license>
<notes>
- Fixed issue #22 (Boolean FALSE is always TRUE)
</notes>
</release>
<release>
<date>2016-04-11</date>
<version>
<release>2.0.1</release>
<api>2.0.1</api>
</version>
<stability>
<release>beta</release>
<api>beta</api>
</stability>
<license uri="http://www.php.net/license">PHP License</license>
<notes>
- Fixed bug #71997 (One-Dimensional arrays cause segmentation faults)
</notes>
</release>
<release>
<date>2015-10-27</date>
<version>
@@ -100,7 +204,6 @@
- Release lua for PHP7
</notes>
</release>
<release>
<date>2012-10-23</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.0"
#define PHP_LUA_VERSION "2.0.8-dev"
struct _php_lua_object {
lua_State *L;
@@ -61,6 +61,7 @@ static inline php_lua_object *php_lua_obj_from_obj(zend_object *obj) {
return (php_lua_object*)((char*)(obj)-XtOffsetOf(php_lua_object, obj));
}
#define Z_LUAVAL(obj) php_lua_obj_from_obj(Z_OBJ((obj)))
#define Z_LUAVAL_P(obj) php_lua_obj_from_obj(Z_OBJ_P((obj)))
zval *php_lua_get_zval_from_lua(lua_State *L, int index, zval *lua_obj, zval *rv);

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,

22
tests/bug71997.phpt Normal file
View File

@@ -0,0 +1,22 @@
--TEST--
Bug #71997 (One-Dimensional arrays cause segmentation faults)
--SKIPIF--
<?php
if (!extension_loaded("lua")) print "skip lua extension missing";
?>
--FILE--
<?php
$mylua = new \lua();
$mylua->eval(<<<CODE
function nicefunction(args)
print(args[1])
return args[1]
end
CODE
);
echo $mylua->call("nicefunction", array(array('hello', 'world')));
?>
done
--EXPECT--
worldworlddone

45
tests/bug73964.phpt Normal file
View File

@@ -0,0 +1,45 @@
--TEST--
Bug #73964 (Segmentation fault (11))
--SKIPIF--
<?php
if (!extension_loaded("lua")) print "skip lua extension missing";
?>
--FILE--
<?php
class LuaTest
{
function __construct()
{
$this->lua = new Lua();
$this->lua->registerCallback("log", array($this, "API_LuaLog"));
}
function API_LuaLog( $entry)
{
echo("from lua: $entry");
}
public function RunTest()
{
$this->lua->eval(<<<CODE
function TestFunc(str)
log(str)
end
CODE
);
$GamePackage = $this->lua->RootTable;
$this->lua->call("TestFunc", array("okey"));
}
}
$a = new LuaTest();
$a->runtest();
?>
--EXPECT--
from lua: okey

15
tests/issue012.phpt Normal file
View File

@@ -0,0 +1,15 @@
--TEST--
ISSUE #022 (Boolean FALSE is always TRUE)
--SKIPIF--
<?php
if (!extension_loaded("lua")) print "skip lua extension missing";
?>
--FILE--
<?php
$lua = new Lua();
$lua->assign('TEST', false);
$result = $lua->eval('return TEST');
var_dump($result)
?>
--EXPECT--
bool(false)

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
)

View File

@@ -1,14 +1,2 @@
phpize
./configure --with-lua-version=5.2
make
EX=$?;
if [ $EX != 0 ];
then
echo "compile failed";
exit $EX;
fi;
exit $EX;
#!/bin/sh
phpize && ./configure --with-lua-version=5.2 && make