Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
75 changes: 52 additions & 23 deletions lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ static zend_object_handlers lua_object_handlers;
/** {{{ ARG_INFO
*
*/
ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_call, 0, 0, 2)
ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_call, 0, 0, 1)
ZEND_ARG_INFO(0, method)
ZEND_ARG_INFO(0, args)
ZEND_ARG_INFO(0, use_self)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_lua___call, 0, 0, 2)
ZEND_ARG_INFO(0, method)
ZEND_ARG_INFO(0, args)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -64,9 +70,15 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_eval, 0, 0, 1)
ZEND_ARG_INFO(0, statements)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_free, 0, 0, 1)
ZEND_ARG_INFO(0, closure)
ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_getVersion, 0, 0, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_getVersionInt, 0, 0, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_lua___construct, 0, 0, 0)
ZEND_END_ARG_INFO()

/* }}} */

/* {{{ lua_module_entry
Expand Down Expand Up @@ -217,46 +229,46 @@ 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 = (Z_LUAVAL_P(object))->L;
zend_string *str_member;
zval *php_lua_read_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv){
lua_State *L = php_lua_obj_from_obj(object)->L;

if (type != BP_VAR_R) {
ZVAL_NULL(rv);
return rv;
}

str_member = zval_get_string(member);
#if (LUA_VERSION_NUM < 502)
lua_getfield(L, LUA_GLOBALSINDEX, ZSTR_VAL(str_member));
lua_getfield(L, LUA_GLOBALSINDEX, ZSTR_VAL(member));
#else
lua_getglobal(L, ZSTR_VAL(str_member));
lua_getglobal(L, ZSTR_VAL(member));
#endif
zend_string_release(str_member);

php_lua_get_zval_from_lua(L, -1, object, rv);
zval lua_zval_object;
ZVAL_OBJ(&lua_zval_object, object);

php_lua_get_zval_from_lua(L, -1, &lua_zval_object, rv);
lua_pop(L, 1);

return rv;
}
/* }}} */

/** {{{ 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 = (Z_LUAVAL_P(object))->L;
zend_string *str_member = zval_get_string(member);
static zval* php_lua_write_property(zend_object *object, zend_string *member, zval *value, void ** key) {
lua_State *L = php_lua_obj_from_obj(object)->L;

#if (LUA_VERSION_NUM < 502)
php_lua_send_zval_to_lua(L, member);
lua_pushlstring(L, ZSTR_VAL(val), ZSTR_LEN(val));
php_lua_send_zval_to_lua(L, value);

lua_settable(L, LUA_GLOBALSINDEX);
#else
php_lua_send_zval_to_lua(L, value);
lua_setglobal(L, Z_STRVAL_P(member));
lua_setglobal(L, ZSTR_VAL(member));
#endif

zend_string_release(str_member);
return value;
}
/* }}} */

Expand Down Expand Up @@ -305,7 +317,7 @@ static int php_lua_call_callback(lua_State *L) {
// and pass it to PHP if Lua didn't handle this error. Let's try with the ideal approach somewhere later
zend_clear_exception();

zend_call_method_with_0_params(&exception, Z_OBJCE_P(&exception), NULL, "getmessage", &tmp);
zend_call_method_with_0_params(Z_OBJ_P(&exception), Z_OBJCE_P(&exception), NULL, "getmessage", &tmp);
if (Z_TYPE(tmp) != IS_STRING) {
zend_error(E_WARNING, "%s::getMessage() must return a string", ZSTR_VAL(Z_OBJCE_P(&exception)->name));
lua_pushfstring(L, "Exception of type %s has been thrown in PHP", ZSTR_VAL(Z_OBJCE_P(&exception)->name));
Expand Down Expand Up @@ -481,7 +493,7 @@ int php_lua_send_zval_to_lua(lua_State *L, zval *val) /* {{{ */ {
add_next_index_zval(callbacks, val);
} else {
zval *v;
ulong longkey;
zend_ulong longkey;
zend_string *key;
zval zkey;

Expand Down Expand Up @@ -815,6 +827,23 @@ PHP_METHOD(lua, call) {
}
/* }}} */

/** {{{ proto Lua::__call(mixed $function, array $args)
*/
PHP_METHOD(lua, __call) {
long u_self = 0;
zval *func;
zval *args = NULL;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "za", &func, &args) == FAILURE) {
return;
}

if (!(php_lua_call_lua_function(getThis(), func, args, 0, return_value))) {
RETURN_FALSE;
}
}
/* }}} */

/** {{{ proto Lua::assign(string $name, mix $value)
*/
PHP_METHOD(lua, assign) {
Expand Down Expand Up @@ -907,16 +936,16 @@ PHP_METHOD(lua, __construct) {
*
*/
zend_function_entry lua_class_methods[] = {
PHP_ME(lua, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(lua, __construct, arginfo_lua___construct,ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(lua, eval, arginfo_lua_eval, ZEND_ACC_PUBLIC)
PHP_ME(lua, include, arginfo_lua_include, ZEND_ACC_PUBLIC)
PHP_ME(lua, loadstring, arginfo_lua_loadstring, ZEND_ACC_PUBLIC)
PHP_ME(lua, call, arginfo_lua_call, ZEND_ACC_PUBLIC)
PHP_ME(lua, __call, arginfo_lua___call, ZEND_ACC_PUBLIC)
PHP_ME(lua, assign, arginfo_lua_assign, ZEND_ACC_PUBLIC)
PHP_ME(lua, getVersion, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
PHP_ME(lua, getVersionInt, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC|ZEND_ACC_STATIC)
PHP_ME(lua, getVersion, arginfo_lua_getVersion, ZEND_ACC_PUBLIC)
PHP_ME(lua, getVersionInt, arginfo_lua_getVersionInt, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(lua, registerCallback, arginfo_lua_register, ZEND_ACC_PUBLIC)
PHP_MALIAS(lua, __call, call, arginfo_lua_call, ZEND_ACC_PUBLIC)
PHP_FE_END
};
/* }}} */
Expand Down
11 changes: 7 additions & 4 deletions lua_closure.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ static zend_object_handlers lua_closure_handlers;
/** {{{ ARG_INFO
*
*/
ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_invoke, 0, 0, 1)
ZEND_ARG_INFO(0, arg)
ZEND_ARG_INFO(0, ...)
ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_invoke, 0, 0, 0)
ZEND_ARG_VARIADIC_INFO(0, arg)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_closure___construct, 0, 0, 0)
ZEND_END_ARG_INFO()

/* }}} */

/** {{{ zval * php_lua_closure_instance(zval *instance, long ref_id, zval *lua_obj)
Expand Down Expand Up @@ -145,7 +148,7 @@ PHP_METHOD(lua_closure, invoke) {
/* {{{ 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, __construct, arginfo_lua_closure___construct, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
PHP_ME(lua_closure, invoke, arginfo_lua_invoke, ZEND_ACC_PUBLIC)
PHP_MALIAS(lua_closure, __invoke, invoke, arginfo_lua_invoke, ZEND_ACC_PUBLIC)
PHP_FE_END
Expand Down
25 changes: 19 additions & 6 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
<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>
<api>stable</api>
</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="/">
Expand Down Expand Up @@ -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>
Expand All @@ -96,8 +111,6 @@
<notes>
- Fixed Hash Recursive Detecting in PHP-7.3
</notes>
</release>

<release>
<date>2017-12-31</date>
<version>
Expand Down
2 changes: 1 addition & 1 deletion php_lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 14 additions & 3 deletions tests/003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ var_dump($l->call(array("math", "cos"), array(pi())));
echo "\n";

try {
$l->notexisting(2423);
$l->call(array("math", "sin", "function"), array(432, 342));
$l->notexisting(2423);
$l->call(array("math", "sin", "function"), array(432, 342));

} catch (LuaException $e) {
echo $e->getMessage();
Expand All @@ -58,8 +58,15 @@ echo "\n";

var_dump($l->__call("multiret", array()));

$l->__call("print");
echo "\nusing Lua->__call(print)\n";
$l->__call("print", []);
$l->__call("print", array("foo"));

echo "\nusing Lua->print()\n";

$l->print(); // same as $l->__call("print", []);
$l->print("foo\n"); // same as $l->__call("print", array("foo"));

?>
--EXPECTF--
Hello world!
Expand Down Expand Up @@ -92,4 +99,8 @@ array(3) {
[2]=>
string(1) "c"
}

using Lua->__call(print)
foo
using Lua->print()
foo
7 changes: 4 additions & 3 deletions tests/013.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ PHP Closures from Lua
<?php

$l = new lua();
$l->eval(<<<CODE
$l->eval(<<<'CODE'
function test(cb1)
local cb2 = cb1("called from lua")
local cb2 = cb1("called from lua\n")
cb2("returned from php")
end
CODE
Expand All @@ -27,4 +27,5 @@ try {
}
?>
--EXPECTF--
called from luareturned from php
called from lua
returned from php