From eed45fdb611e02a519a2c592cc57103979d5bf01 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Fri, 13 Mar 2026 17:12:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(closure):=20=E4=BF=AE=E5=A4=8D=E7=AE=AD?= =?UTF-8?q?=E5=A4=B4=E5=87=BD=E6=95=B0=E5=92=8C=E9=97=AD=E5=8C=85=E7=94=9F?= =?UTF-8?q?=E6=88=90=E4=B8=AD=E7=9A=84=E4=BB=A3=E7=A0=81=E9=A1=BA=E5=BA=8F?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在闭包生成器中保存和恢复 beforeStmtLines 和 afterStmtLines 状态 - 移除闭包结束大括号前的重复代码插入 - 在编译基类中正确处理表达式前的代码行 - 添加对静态属性写入的测试用例 - 添加对箭头函数功能的测试用例 --- src/Php/CompilerBase.php | 9 ++++++-- src/Php/Generator/ClosureGenerator.php | 9 ++++++-- tests/aot/arrow-func-2.phpt | 17 ++++++++++++++ tests/aot/static_prop_write.phpt | 32 ++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 tests/aot/arrow-func-2.phpt create mode 100644 tests/aot/static_prop_write.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index ee89eef0..c97b7ef2 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -4039,12 +4039,17 @@ class CompilerBase extends \PhpAot\Core\Translator { $cb = function () use ($expr) { $code = $this->parseExpr($expr->expr); + if ($this->beforeStmtLines) { + $beforeCode = implode(PHP_EOL, $this->beforeStmtLines); + } else { + $beforeCode = ''; + } if ($this->isCallExpr($expr->expr)) { if ($this->lastNativeCall and $this->lastNativeCall->returnType === self::TYPE_VOID) { - return $code . ";\nreturn " . self::VALUE_NULL . ';'; + return $beforeCode . PHP_EOL . $code . ";" . PHP_EOL . "return " . self::VALUE_NULL . ';'; } } - return 'return ' . $code . ';'; + return $beforeCode . PHP_EOL . 'return ' . $code . ';'; }; return $this->genClosure($expr, $expr->params, $cb, [], true); } diff --git a/src/Php/Generator/ClosureGenerator.php b/src/Php/Generator/ClosureGenerator.php index a7dde09c..4faad75d 100644 --- a/src/Php/Generator/ClosureGenerator.php +++ b/src/Php/Generator/ClosureGenerator.php @@ -37,12 +37,16 @@ trait ClosureGenerator $oriCeWrappers = $this->ceWrappers; $oriArgs = $this->arguments; $oriInClosure = $this->inClosure; + $oriBeforeStmtLines = $this->beforeStmtLines; + $oriAfterStmtLines = $this->afterStmtLines; $this->objects = []; $this->objectWrappers = []; $this->ceWrappers = []; $this->arguments = []; $this->inClosure = true; + $this->beforeStmtLines = []; + $this->afterStmtLines = []; $this->indentLevel++; @@ -70,8 +74,6 @@ trait ClosureGenerator $this->indentLevel--; $code .= '};' . PHP_EOL; - $this->beforeStmtLines[] = $code; - $useVars = []; if ($uses) { foreach ($uses as $useItem) { @@ -95,6 +97,9 @@ trait ClosureGenerator $this->ceWrappers = $oriCeWrappers; $this->arguments = $oriArgs; $this->inClosure = $oriInClosure; + $this->beforeStmtLines = $oriBeforeStmtLines; + $this->afterStmtLines = $oriAfterStmtLines; + $this->beforeStmtLines[] = $code; if ($this->methodDef) { return 'php::newClosure(' . $tmpVar . ', { ' . implode(', ', $useVars) . ' }, this_)'; diff --git a/tests/aot/arrow-func-2.phpt b/tests/aot/arrow-func-2.phpt new file mode 100644 index 00000000..56ba5c2d --- /dev/null +++ b/tests/aot/arrow-func-2.phpt @@ -0,0 +1,17 @@ +--TEST-- +arrow function 2 +--FILE-- + var_dump(0, ...$array); + $fn1(); +} +?> +--EXPECT-- +int(0) +int(1) +int(5) +int(9) + diff --git a/tests/aot/static_prop_write.phpt b/tests/aot/static_prop_write.phpt new file mode 100644 index 00000000..7b71c01d --- /dev/null +++ b/tests/aot/static_prop_write.phpt @@ -0,0 +1,32 @@ +--TEST-- +Static Class Property Read/Write Test +--FILE-- +errorHandler = $errorHandler; + ($this->errorHandler)(); + } +} + +class Worker +{ + public static ?stdClass $globalEvent = null; + public static string $eventLoopClass = 'Select'; + + public static function init() { + self::$globalEvent = new static::$eventLoopClass(); + self::$globalEvent->setErrorHandler(function ($exception) { + var_dump(__FUNCTION__); + }); + } +} + +function main() { + Worker::init(); +} +?> +--EXPECT-- +string(4) "init" \ No newline at end of file