fix(php): 修复AOT编译器中isset操作和属性获取问题

- 添加变量存在性检查,防止AOT编译器操作未定义变量
- 优化单属性读取逻辑,支持原生属性访问
- 移除不必要的数组反转操作提升性能
- 修复属性映射偏移量计算错误,统一加减1024补偿值
- 更新示例代码中的静态变量初始值
pull/1/head
韩天峰 5 months ago
parent c53e7afdb5
commit 67bbfb8852
  1. 2
      examples/micro_bench.php
  2. 15
      src/Php/CompilerBase.php
  3. 4
      src/template/extension.cc.php

@ -23,7 +23,7 @@ function simpleicall($n)
}
class Foo {
static $a = 0;
static $a = 12;
public $b = 0;
const TEST = 23;

@ -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);

@ -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(<?= Translator::PREFIX . Translator::PROP_MAP ?>[prop_id] == 0)) {
<?= Translator::PREFIX . Translator::PROP_MAP ?>[prop_id] = php::getPropertyOffset(class_name, prop_name);
<?= Translator::PREFIX . Translator::PROP_MAP ?>[prop_id] = php::getPropertyOffset(class_name, prop_name) + 1024;
}
return <?= Translator::PREFIX . Translator::PROP_MAP ?>[prop_id];
return <?= Translator::PREFIX . Translator::PROP_MAP ?>[prop_id] - 1024;
}
// literal strings

Loading…
Cancel
Save