From 67bbfb8852df874dc3fa66b6133abddc2b4d752c Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Fri, 3 Apr 2026 16:10:05 +0800 Subject: [PATCH] =?UTF-8?q?fix(php):=20=E4=BF=AE=E5=A4=8DAOT=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=99=A8=E4=B8=ADisset=E6=93=8D=E4=BD=9C=E5=92=8C?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E8=8E=B7=E5=8F=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加变量存在性检查,防止AOT编译器操作未定义变量 - 优化单属性读取逻辑,支持原生属性访问 - 移除不必要的数组反转操作提升性能 - 修复属性映射偏移量计算错误,统一加减1024补偿值 - 更新示例代码中的静态变量初始值 --- examples/micro_bench.php | 2 +- src/Php/CompilerBase.php | 15 +++++++++------ src/template/extension.cc.php | 4 ++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/examples/micro_bench.php b/examples/micro_bench.php index 27c9af67..325b6a1b 100644 --- a/examples/micro_bench.php +++ b/examples/micro_bench.php @@ -23,7 +23,7 @@ function simpleicall($n) } class Foo { - static $a = 0; + static $a = 12; public $b = 0; const TEST = 23; diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 760809cd..6313b5f3 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -3648,15 +3648,20 @@ class CompilerBase extends \PhpAot\Core\Translator protected function parseChainedExpr(NodeAbstract $node, string $op, bool $getValue = false): string { + // AOT 编译器不允许操作未定义的变量,PHP 的 isset($var) 可能 $var 未定义 + $this->checkVarMustExist($node, $this->parseIdentifier($node)); $fn = $this->getChainedFunc($op); $expr = $node; if ($this->isVarExpr($expr)) { - if ($op === self::OP_ISSET) { - $var = $this->parseIdentifier($expr); - return $this->hasVar($var) ? $fn . '(' . $var . ')' : 'false'; - } return $fn . '(' . $this->parseExpr($expr) . ')'; } + // 单属性读取(非链式) + if ($this->isPropertyFetch($expr) and $this->isVarExpr($expr->var) and $this->isIdExpr($expr->name)) { + $prop = $this->parsePropertyFetch($expr); + if ($expr->hasAttribute('nativeProperty')) { + return $fn . '(' . $prop . ')'; + } + } $list = []; while (true) { @@ -3681,8 +3686,6 @@ class CompilerBase extends \PhpAot\Core\Translator $expr = $expr->var; } - $list = array_reverse($list); - if ($getValue) { $result = $this->addTmpVar(self::TYPE_VAR); $node->setAttribute('chainOpResult', $result); diff --git a/src/template/extension.cc.php b/src/template/extension.cc.php index d48b5707..6370ef00 100644 --- a/src/template/extension.cc.php +++ b/src/template/extension.cc.php @@ -58,9 +58,9 @@ zend_function *php_get_method(int func_id, const php::Str &method_name, int clas uint32_t php_get_prop(int prop_id, const php::Str &prop_name, int class_id, const php::Str &class_name) { if (UNEXPECTED([prop_id] == 0)) { - [prop_id] = php::getPropertyOffset(class_name, prop_name); + [prop_id] = php::getPropertyOffset(class_name, prop_name) + 1024; } - return [prop_id]; + return [prop_id] - 1024; } // literal strings