From c740a24501def78405be1ae7e4a1fcd31b279f0d Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Sat, 4 Jul 2026 14:03:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=A4=84=E7=90=86=E4=B8=8Eexit=E8=AF=AD=E4=B9=89=EF=BC=8C?= =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=8A=A8=E6=80=81=E5=B1=9E=E6=80=A7=E6=93=8D?= =?UTF-8?q?=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- project.yml | 2 +- src/Php/CompilerBase.php | 49 +++++++++++++++---- tests/aot/basic/exit-zero.phpt | 13 +++++ tests/aot/exception/catch-exit.phpt | 16 ++++++ .../object_property/property-array-write.phpt | 34 +++++++++++++ tests/aot/optimizations/int-calculation.phpt | 3 +- tests/aot/var_convert/002.phpt | 8 ++- 7 files changed, 113 insertions(+), 12 deletions(-) create mode 100644 tests/aot/basic/exit-zero.phpt create mode 100644 tests/aot/exception/catch-exit.phpt create mode 100644 tests/aot/object_property/property-array-write.phpt diff --git a/project.yml b/project.yml index 7c8d1a63..1610e6a6 100644 --- a/project.yml +++ b/project.yml @@ -1,4 +1,4 @@ -name: swoole-compiler +name: tpc build-mode: bin version: 0.1.0 cxx-std: c++17 diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index e5835423..19806684 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -4744,9 +4744,12 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont protected function parseExit(Expr\Exit_ $node): string { if (!$node->expr) { - return 'php::exit(0)'; + return 'std::exit(0)'; } - return 'php::exit(' . $this->parseIdentifier($node->expr) . ')'; + $status = $this->parseExprAsValue($node->expr); + return '([&]() -> php::Var { php::Var exit_status = ' . $status . '; ' + . 'if (exit_status.isInt()) { std::exit(exit_status.toInt()); } ' + . 'php::echo(php::toString(exit_status)); std::exit(0); return php::null; })()'; } protected function getFixedObjectPropDefaultValue(PropertyDef $def): ?string @@ -5606,14 +5609,23 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont { $this->assertDynamicPropertyTarget($target); - return $target->getDynamicObjectExpr() . '.appendArrayProperty(' . $target->getDynamicPropertyExpr() . ', ' . $value . ')'; + return $this->emitDynamicPropertyAppendArray( + $target->getDynamicObjectExpr(), + $target->getDynamicPropertyExpr(), + $value + ); } protected function emitDynamicPropertyTargetUpdateArray(PropertyWriteTarget $target, string $dim, string $value): string { $this->assertDynamicPropertyTarget($target); - return $target->getDynamicObjectExpr() . '.updateArrayProperty(' . $target->getDynamicPropertyExpr() . ', ' . $dim . ', ' . $value . ')'; + return $this->emitDynamicPropertyUpdateArray( + $target->getDynamicObjectExpr(), + $target->getDynamicPropertyExpr(), + $dim, + $value + ); } protected function canEmitDynamicPropertyTarget(?PropertyWriteTarget $target): bool @@ -5670,7 +5682,11 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont return $this->emitDynamicPropertyTargetAppendArray($target, $value); } - return $this->parseIdentifier($expr->var) . '.appendArrayProperty(' . $this->identifierToStr($expr->name) . ', ' . $value . ')'; + return $this->emitDynamicPropertyAppendArray( + $this->parseIdentifier($expr->var), + $this->identifierToStr($expr->name, literal: true), + $value + ); } protected function emitDynamicPropertyFetchUpdateArray(Expr\PropertyFetch $expr, string $dim, string $value, ?PropertyWriteTarget $target = null): string @@ -5679,7 +5695,22 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont return $this->emitDynamicPropertyTargetUpdateArray($target, $dim, $value); } - return $this->parseIdentifier($expr->var) . '.updateArrayProperty(' . $this->identifierToStr($expr->name) . ', ' . $dim . ', ' . $value . ')'; + return $this->emitDynamicPropertyUpdateArray( + $this->parseIdentifier($expr->var), + $this->identifierToStr($expr->name, literal: true), + $dim, + $value + ); + } + + protected function emitDynamicPropertyAppendArray(string $object, string $property, string $value): string + { + return "{$object}.attr({$property}, true).newItem() = {$value}"; + } + + protected function emitDynamicPropertyUpdateArray(string $object, string $property, string $dim, string $value): string + { + return "{$object}.attr({$property}, true).item({$dim}, true) = {$value}"; } protected function assertDynamicPropertyTarget(PropertyWriteTarget $target): void @@ -6417,7 +6448,7 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont $finally = $v->finally; $exVar = $this->genTmpVarName(); - $this->addLocalVar($exVar, self::TYPE_OBJECT); + $this->addLocalVar($exVar, self::TYPE_VAR); $code .= 'catch(zend_object *_ex) {' . PHP_EOL; $code .= $this->getIndent() . $exVar . ' = php::catchException();' . PHP_EOL; @@ -6434,7 +6465,7 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont $code .= $this->parseStmts($finally->stmts); $code .= PHP_EOL; } - $code .= 'if (' . $exVar . ') {' . PHP_EOL . $this->getIndent() . 'php::throwException(' . $exVar . ');' . PHP_EOL . $this->getIndent() . '}'; + $code .= 'if (' . $exVar . ') {' . PHP_EOL . $this->getIndent() . 'php::throwException(php::Object(' . $exVar . '));' . PHP_EOL . $this->getIndent() . '}'; return $code; } @@ -6465,8 +6496,8 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont $code .= implode(' || ', $conditions); $code .= ') {' . PHP_EOL; $this->indentLevel++; + $code .= $this->getIndent() . "{$exVar} = php::null;" . PHP_EOL; $code .= $this->parseStmts($catch->stmts); - $code .= $this->getIndent() . "{$exVar}.unset();" . PHP_EOL; $this->indentLevel--; $code .= $this->getIndent() . '}'; diff --git a/tests/aot/basic/exit-zero.phpt b/tests/aot/basic/exit-zero.phpt new file mode 100644 index 00000000..d2fce742 --- /dev/null +++ b/tests/aot/basic/exit-zero.phpt @@ -0,0 +1,13 @@ +--TEST-- +exit(0) should terminate without fatal error +--ENV-- +USE_ZEND_ALLOC=0 +--FILE-- + +--EXPECT-- diff --git a/tests/aot/exception/catch-exit.phpt b/tests/aot/exception/catch-exit.phpt new file mode 100644 index 00000000..3e0a3cf3 --- /dev/null +++ b/tests/aot/exception/catch-exit.phpt @@ -0,0 +1,16 @@ +--TEST-- +exit inside catch should not rethrow graceful exit as Throwable +--ENV-- +USE_ZEND_ALLOC=0 +--FILE-- + +--EXPECT-- diff --git a/tests/aot/object_property/property-array-write.phpt b/tests/aot/object_property/property-array-write.phpt new file mode 100644 index 00000000..c5380792 --- /dev/null +++ b/tests/aot/object_property/property-array-write.phpt @@ -0,0 +1,34 @@ +--TEST-- +object property array append and keyed write use generic property path +--FILE-- +items[] = $value; + } + + public function put(string $key, mixed $value): void + { + $this->items[$key] = $value; + } +} + +function main() { + $box = new PropertyArrayWriteBox(); + $box->append('first'); + $box->put('lang', 'php'); + var_dump($box->items); +} +?> +--EXPECT-- +array(2) { + [0]=> + string(5) "first" + ["lang"]=> + string(3) "php" +} diff --git a/tests/aot/optimizations/int-calculation.phpt b/tests/aot/optimizations/int-calculation.phpt index 3cd72ffa..86854ccc 100644 --- a/tests/aot/optimizations/int-calculation.phpt +++ b/tests/aot/optimizations/int-calculation.phpt @@ -3,6 +3,7 @@ SSA: int --FILE-- --EXPECTF-- 12 --9223372036854775797 \ No newline at end of file +9.2233720368547758E+18 \ No newline at end of file diff --git a/tests/aot/var_convert/002.phpt b/tests/aot/var_convert/002.phpt index e4c75c6a..277fe7d2 100644 --- a/tests/aot/var_convert/002.phpt +++ b/tests/aot/var_convert/002.phpt @@ -1,5 +1,11 @@ --TEST-- var convert chained on array element +--SKIPIF-- +toStream()->write('hello'); var_dump($pair[1]->toStream()->read(5));