diff --git a/bin/gen_stub.php b/bin/gen_stub.php index 8c271375..48602d9b 100755 --- a/bin/gen_stub.php +++ b/bin/gen_stub.php @@ -4502,7 +4502,7 @@ class FileInfo { } } - if ($stmt instanceof Stmt\Use_) { + if ($stmt instanceof Stmt\Use_ or $stmt instanceof Stmt\GroupUse or $stmt instanceof Stmt\Declare_) { continue; } diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 67bfff10..0fcd122d 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -398,6 +398,8 @@ class CompilerBase extends \PhpAot\Core\Translator return $this->parseShellExec($expr); case 'Expr_Closure': return $this->parseClosure($expr); + case 'Expr_ArrowFunction': + return $this->parseArrowFunction($expr); case 'Name_FullyQualified': return $expr->name; case 'Scalar_Int': @@ -3760,6 +3762,50 @@ class CompilerBase extends \PhpAot\Core\Translator return $this->printer->prettyPrint([$stmt]); } + protected function parseArrowFunction(Node\Expr\ArrowFunction $expr): string + { + $tmpVar = $this->genTmpVarName(); + + $fnCode = $this->getIndent() . + 'php::ClosureFn ' . $tmpVar . ' = [&](' + . 'INTERNAL_FUNCTION_PARAMETERS, ' + . self::TYPE_OBJECT . ' &this_, ' + . self::TYPE_ARGS . ' &vars_) ' . + '-> ' . self::TYPE_VAR . ' {' . PHP_EOL; + + $oriArgs = $this->arguments; + $this->arguments = []; + $oriInClosure = $this->inClosure; + $this->inClosure = true; + + $this->indentLevel++; + + foreach ($expr->params as $i => $param) { + $var = $this->parseIdentifier($param->var); + $fnCode .= 'auto ' . $var . ' = php::getCallArg(' . $i . ');' . PHP_EOL; + $this->addArgument($var, self::TYPE_VAR); + } + + if ($this->methodDef) { + $this->addArgument('this_', self::TYPE_OBJECT); + } + + $fnCode .= 'return ' . $this->parseExpr($expr->expr) . ';'; + + $this->indentLevel--; + $fnCode .= '};' . PHP_EOL; + + $this->beforeStmtLines[] = $fnCode; + $this->arguments = $oriArgs; + $this->inClosure = $oriInClosure; + + if ($this->methodDef) { + return 'php::newClosure(' . $tmpVar . ', { }, this_)'; + } else { + return 'php::newClosure(' . $tmpVar . ', { })'; + } + } + protected function parseClosure(Node\Expr\Closure $expr): string { $tmpVar = $this->genTmpVarName(); diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index d5f5d592..7c01894a 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -144,6 +144,7 @@ class Preprocessor extends CompilerBase break; case 'Stmt_Use': case 'Stmt_Const': + case 'Stmt_Interface': break; case 'Stmt_Expression': $this->foundStrayCode($v2); diff --git a/tests/aot/arrow-func.phpt b/tests/aot/arrow-func.phpt new file mode 100644 index 00000000..3114802b --- /dev/null +++ b/tests/aot/arrow-func.phpt @@ -0,0 +1,13 @@ +--TEST-- +arrow function +--FILE-- + $x + $y; + var_export($fn1(3)); +} +?> +--EXPECT-- +4