fix(php): 修复PHP引用类型处理问题

- 将TYPE_REF常量值从'php::Var'更改为'php::Ref'
- 修改引用参数传递逻辑,使用'&'操作符而不是解引用
- 将post-increment操作符错误处理从parsePostOp改为fatalError
- 修复变量赋值时的引用处理逻辑
- 添加引用操作符测试用例
pull/1/head
韩天峰 7 months ago
parent a529ccf306
commit 58f9cb4c28
  1. 11
      src/Php/CompilerBase.php
  2. 19
      tests/aot/ref.phpt

@ -33,7 +33,7 @@ class CompilerBase extends \PhpAot\Core\Translator
public const string TYPE_OBJECT = 'php::Object';
public const string TYPE_ARRAY = 'php::Array';
public const string TYPE_STR = 'php::Str';
public const string TYPE_REF = 'php::Var';
public const string TYPE_REF = 'php::Ref';
public const string TYPE_VOID = 'void';
public const string VALUE_NAN = 'std::numeric_limits<double>::quiet_NaN()';
@ -1493,8 +1493,7 @@ class CompilerBase extends \PhpAot\Core\Translator
$tmpVar = $this->genTmpVarName();
$this->addLocalVar($tmpVar, self::TYPE_REF);
$this->beforeStmtLines[] = $tmpVar . ' = ' . $this->parseExpr($arg->value) . '.toReference();';
$this->afterStmtLines[] = $name . ' = *' . $tmpVar . ';';
$list_args[] = $tmpVar;
$list_args[] = '&' . $tmpVar;
continue;
}
} elseif ($this->isPropertyFetch($arg->value)) {
@ -1539,7 +1538,7 @@ class CompilerBase extends \PhpAot\Core\Translator
$this->afterStmtLines[] = 'php::setStaticProperty(' . $class . ', ' . $prop . ', ' . $tmpVar . ' ' . $op . ' 1);';
return $tmpVar;
}
$this->parsePostOp($expr, "Post-increment operator is not supported for non-variable expressions");
$this->fatalError($expr, "Post-increment operator is not supported for non-variable expressions");
}
protected function parsePostDec($expr): string
@ -2394,10 +2393,10 @@ class CompilerBase extends \PhpAot\Core\Translator
if ($this->isVarExpr($expr->var)) {
$var = $this->parseIdentifier($expr->var);
if (!$this->hasVar($var)) {
$this->addLocalVar($var, self::TYPE_VAR);
$this->addLocalVar($var, self::TYPE_REF);
}
if ($this->isVarExpr($expr->expr)) {
return $var . ' = &' . $this->parseIdentifier($expr->expr);
return $var . ' = ' . $this->parseIdentifier($expr->expr) . '.toReference()';
} elseif ($expr->expr->getType() === self::EXPR_ARRAY_DIM_FETCH) {
return $var . ' = ' . $this->parseIdentifier($expr->expr);
} elseif ($this->isPropertyFetch($expr->expr)) {

@ -0,0 +1,19 @@
--TEST--
object link operator
--FILE--
<?php
function main()
{
$a = [1, 2, 3];
$b = &$a;
$b[] = 5;
$c = &$b;
$c [] = 6;
assert(count($a) === 5);
assert(count($b) === 5);
assert(count($c) === 5);
}
?>
--EXPECT--
Loading…
Cancel
Save