From f281b1e29b64e9dc5837add807006d47d2535d8f Mon Sep 17 00:00:00 2001 From: Mikhail Galanin Date: Sun, 27 Oct 2019 20:30:26 +0000 Subject: [PATCH 01/11] Added Lua::loadstring() Currently, just executes C-api lua_loadstring, which is useful if you intend to check syntax. Later on it can be used for more sophisticated thing, i.e. execute the loaded chunk on lazy basis. --- lua.c | 32 ++++++++++++++++++++++++++++++++ tests/014.phpt | 23 +++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 tests/014.phpt diff --git a/lua.c b/lua.c index 7429b8b..b44b53a 100755 --- a/lua.c +++ b/lua.c @@ -54,6 +54,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_include, 0, 0, 1) ZEND_ARG_INFO(0, file) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_loadstring, 0, 0, 2) + ZEND_ARG_INFO(0, statements) + ZEND_ARG_INFO(0, chunk_name) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_lua_eval, 0, 0, 1) ZEND_ARG_INFO(0, statements) ZEND_END_ARG_INFO() @@ -671,6 +676,32 @@ PHP_METHOD(lua, eval) { } /* }}} */ + +/** {{{ proto Lua::loadstring(string $lua_chunk, string $chunk_name) + * Loads a string into Lua state + */ +PHP_METHOD(lua, loadstring) { + lua_State *L; + char *statements; + char *chunk_name; + long bp, len, len2; + int ret; + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &statements, &len, &chunk_name, &len2) == FAILURE) { + return; + } + + L = (Z_LUAVAL_P(getThis()))->L; + + bp = lua_gettop(L); + if ((ret = luaL_loadbuffer(L, statements, len, chunk_name)) != LUA_OK) { + zend_throw_exception_ex(lua_exception_ce, ret, "%s", lua_tostring(L, -1)); + lua_pop(L, 1); + RETURN_FALSE; + } +} +/* }}} */ + /** {{{ proto Lua::include(string $file) * run a lua script file */ @@ -820,6 +851,7 @@ zend_function_entry lua_class_methods[] = { PHP_ME(lua, __construct, NULL, 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, assign, arginfo_lua_assign, ZEND_ACC_PUBLIC) PHP_ME(lua, getVersion, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC) diff --git a/tests/014.phpt b/tests/014.phpt new file mode 100644 index 0000000..7ed9193 --- /dev/null +++ b/tests/014.phpt @@ -0,0 +1,23 @@ +--TEST-- +Loadstring +--SKIPIF-- + +--FILE-- +loadstring(<<getMessage(); +} +?> +--EXPECTF-- +[string "my code"]:4: 'end' expected (to close 'function' at line 1) near From cd9ffc363fee751360a25f843423446e11bb91c5 Mon Sep 17 00:00:00 2001 From: Mikhail Galanin Date: Sat, 2 Nov 2019 16:37:47 +0000 Subject: [PATCH 02/11] Github Actions integration --- .../continuous-integration-workflow.yml | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 .github/workflows/continuous-integration-workflow.yml diff --git a/.github/workflows/continuous-integration-workflow.yml b/.github/workflows/continuous-integration-workflow.yml new file mode 100644 index 0000000..7753b33 --- /dev/null +++ b/.github/workflows/continuous-integration-workflow.yml @@ -0,0 +1,30 @@ +name: Run tests +# This workflow is triggered on pushes to the repository. +on: [push] + +jobs: + build: + name: Build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + with: + fetch-depth: 1 + - name: apt-get update + run: sudo apt-get update + - name: Install PHP and Lua + run: | + sudo apt-get install php-dev lua5.3 liblua5.3-dev php-json + sudo update-alternatives --install /usr/bin/lua lua-interpreter /usr/bin/lua5.3 130 --slave /usr/share/man/man1/lua.1.gz lua-manual /usr/share/man/man1/lua5.3.1.gz + - name: configure + run: | + cd /home/runner/work/php-lua/php-lua/ + phpize + ./configure --with-lua --with-lua-version=5.3 + - name: make + run: make + - name: test + run: make test + + + # PHP 7.3 (PHP 7.3.9-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Sep 2 2019 12:54:04) ( NTS )) From e4e7c1060fc4c157afc9a48edc36bd5033b9ed19 Mon Sep 17 00:00:00 2001 From: Mikhail Galanin Date: Sat, 2 Nov 2019 18:20:53 +0000 Subject: [PATCH 03/11] Run Actions on pull request --- .github/workflows/continuous-integration-workflow.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/continuous-integration-workflow.yml b/.github/workflows/continuous-integration-workflow.yml index 7753b33..b36ccad 100644 --- a/.github/workflows/continuous-integration-workflow.yml +++ b/.github/workflows/continuous-integration-workflow.yml @@ -1,6 +1,6 @@ name: Run tests # This workflow is triggered on pushes to the repository. -on: [push] +on: [push, pull_request] jobs: build: From c3c8760e72cea26b8edd820900aa3afb70c2f476 Mon Sep 17 00:00:00 2001 From: Antony Dovgal Date: Thu, 9 Jan 2020 19:22:51 +0300 Subject: [PATCH 04/11] [CT-4283] bail out with error on exception in PHP callback --- lua.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lua.c b/lua.c index b44b53a..e18ab37 100755 --- a/lua.c +++ b/lua.c @@ -290,6 +290,10 @@ static int php_lua_call_callback(lua_State *L) { } call_user_function(EG(function_table), NULL, func, &retval, arg_num, params); + if (EG(exception)) { + /* bail out on exception */ + return lua_error(L); + } php_lua_send_zval_to_lua(L, &retval); for (i = 0; i Date: Sun, 1 Mar 2020 15:33:39 +0800 Subject: [PATCH 05/11] Fixed ulong undefined --- lua.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua.c b/lua.c index 13bc284..52f44fa 100755 --- a/lua.c +++ b/lua.c @@ -481,7 +481,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; From 9b321fcb06159b312ab88c47ec9cf06b2fb239ef Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Tue, 10 Mar 2020 11:09:26 +0800 Subject: [PATCH 06/11] Preparing 2.0.7 --- package.xml | 25 +++++++++++++++++++------ php_lua.h | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/package.xml b/package.xml index 1a968e3..884d582 100644 --- a/package.xml +++ b/package.xml @@ -23,11 +23,11 @@ msaraujo@php.net yes - 2018-12-21 + 2020-03-10 - 2.0.6 - 2.0.6 + 2.0.7 + 2.0.7 stable @@ -35,7 +35,7 @@ PHP - - Fixed Hash Recursive Detecting in PHP-7.3 + - Fixed windows build for 7.4 @@ -83,6 +83,21 @@ + 2020-03-10 + + 2.0.7 + 2.0.7 + + + stable + stable + + PHP License + + - Fixed windows build for 7.4 + + + 2018-12-21 2.0.6 @@ -96,8 +111,6 @@ - Fixed Hash Recursive Detecting in PHP-7.3 - - 2017-12-31 diff --git a/php_lua.h b/php_lua.h index 40b7a09..2fcac3f 100644 --- a/php_lua.h +++ b/php_lua.h @@ -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.7" struct _php_lua_object { lua_State *L; From 1cf70b1f18f70effa9bec9d5b6f317a6d5be67a3 Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Tue, 10 Mar 2020 11:09:56 +0800 Subject: [PATCH 07/11] Back to dev --- php_lua.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php_lua.h b/php_lua.h index 2fcac3f..5b1cd2b 100644 --- a/php_lua.h +++ b/php_lua.h @@ -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.7" +#define PHP_LUA_VERSION "2.0.8-dev" struct _php_lua_object { lua_State *L; From b14deebe804b45f0d9487ad2b734052f59c8422e Mon Sep 17 00:00:00 2001 From: Eugene Tupikov Date: Tue, 20 Apr 2021 17:54:05 +0300 Subject: [PATCH 08/11] Remove incompatible ZEND_ACC_ALLOW_STATIC --- lua.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua.c b/lua.c index 52f44fa..ee1e75d 100755 --- a/lua.c +++ b/lua.c @@ -913,8 +913,8 @@ zend_function_entry lua_class_methods[] = { PHP_ME(lua, loadstring, arginfo_lua_loadstring, 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, NULL, ZEND_ACC_PUBLIC) + PHP_ME(lua, getVersionInt, NULL, 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 From 5a1d52f73400a72bd71f33885f1c383dcd65120b Mon Sep 17 00:00:00 2001 From: Mikhail Galanin Date: Tue, 24 Aug 2021 08:28:21 +0100 Subject: [PATCH 09/11] php8: added arginfos --- lua.c | 43 ++++++++++++++++++++++++++++++++++++------- lua_closure.c | 11 +++++++---- tests/003.phpt | 17 ++++++++++++++--- tests/013.phpt | 7 ++++--- 4 files changed, 61 insertions(+), 17 deletions(-) diff --git a/lua.c b/lua.c index ee1e75d..edc2868 100755 --- a/lua.c +++ b/lua.c @@ -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() @@ -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 @@ -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) { @@ -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) - PHP_ME(lua, getVersionInt, NULL, ZEND_ACC_PUBLIC|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 }; /* }}} */ diff --git a/lua_closure.c b/lua_closure.c index 50ef039..6b385bf 100644 --- a/lua_closure.c +++ b/lua_closure.c @@ -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) @@ -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 diff --git a/tests/003.phpt b/tests/003.phpt index af50e46..cc5e68c 100644 --- a/tests/003.phpt +++ b/tests/003.phpt @@ -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(); @@ -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! @@ -92,4 +99,8 @@ array(3) { [2]=> string(1) "c" } + +using Lua->__call(print) foo +using Lua->print() +foo \ No newline at end of file diff --git a/tests/013.phpt b/tests/013.phpt index 5da0ed4..4f6a2ac 100644 --- a/tests/013.phpt +++ b/tests/013.phpt @@ -6,9 +6,9 @@ PHP Closures from Lua eval(<<eval(<<<'CODE' function test(cb1) - local cb2 = cb1("called from lua") + local cb2 = cb1("called from lua\n") cb2("returned from php") end CODE @@ -27,4 +27,5 @@ try { } ?> --EXPECTF-- -called from luareturned from php +called from lua +returned from php From de1068d634519abf2461dac9427b5ff24b6603af Mon Sep 17 00:00:00 2001 From: Mikhail Galanin Date: Tue, 24 Aug 2021 08:28:47 +0100 Subject: [PATCH 10/11] php8: Signature of write_property/read_property was changed in https://github.com/php/php-src/commit/91ef4124e56 --- lua.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lua.c b/lua.c index edc2868..32c1471 100755 --- a/lua.c +++ b/lua.c @@ -229,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; } /* }}} */ From a2ef4676ea0b6dea48c81623262138c6b039d6b2 Mon Sep 17 00:00:00 2001 From: Mikhail Galanin Date: Tue, 24 Aug 2021 08:29:20 +0100 Subject: [PATCH 11/11] php8: zend_call_method_with_0_params() now takes zend_object as the 1st arg --- lua.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua.c b/lua.c index 32c1471..de652e9 100755 --- a/lua.c +++ b/lua.c @@ -317,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));