From a529ccf306458acd40a0dbb6fe4b1e43aa0f7dde Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 22 Jan 2026 19:40:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(php):=20=E4=BF=AE=E5=A4=8D=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E5=80=BC=E8=A7=A3=E6=9E=90=E5=92=8C=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=A4=84=E7=90=86=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复了对象值解析时未正确解析标识符的问题 - 为 FunctionDef 类的 argInfoList 属性添加了 PHPDoc 注释 - 在示例文件中添加了测试函数来验证参数处理功能 - 实现了对带默认值参数的正确处理逻辑 --- examples/obj.php | 5 +++++ src/Php/CompilerBase.php | 2 +- src/Php/FunctionDef.php | 3 +++ src/Php/Translator.php | 7 ++++++- 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/examples/obj.php b/examples/obj.php index b3890232..4db98ff9 100644 --- a/examples/obj.php +++ b/examples/obj.php @@ -11,6 +11,11 @@ class Test $this->b = $b; } + public function testArg(int $a, int $b = 3, string $s = 'hello'): int + { + return $a + $b + strlen($s); + } + public function test() { return $this->a + $this->b; diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 9926e5f5..df0f3539 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -845,7 +845,7 @@ class CompilerBase extends \PhpAot\Core\Translator } elseif ($this->isFuncCallExpr($right) and $this->isNameExpr($right->name)) { $fn = $this->parseIdentifier($right->name); if (count($right->args) === 2 and $fn === 'objval' and $this->isScalarString($right->args[1]->value)) { - $this->objects[$var] = $right->args[1]->value; + $this->objects[$var] = $this->parseIdentifier($right->args[1]->value); $type = self::TYPE_OBJECT; } } diff --git a/src/Php/FunctionDef.php b/src/Php/FunctionDef.php index bf737207..a36ee74c 100644 --- a/src/Php/FunctionDef.php +++ b/src/Php/FunctionDef.php @@ -6,6 +6,9 @@ class FunctionDef { public string $name; public string $returnType; + /** + * @var array + */ public array $argInfoList = []; public int $argCountRequired = 0; public string $params = ''; diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 24f3d3c2..81c77558 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -600,7 +600,12 @@ class Translator extends Preprocessor $callParams = ''; foreach ($methodDef->functionDef->argInfoList as $k => $argInfo) { - $expr = $this->convertExprFromType($argInfo->type, 'ZEND_CALL_ARG(EG(current_execute_data), ' . ($k + 1) . ')'); + if ($argInfo->default) { + $argExpr = 'php::getCallArg(' . $k . ', ' . $argInfo->default . ')'; + } else { + $argExpr = 'php::getCallArg(' . $k . ')'; + } + $expr = $this->convertExprFromType($argInfo->type, $argExpr); $cppCode .= $this->getIndent() . $argInfo->type . ' arg_' . $argInfo->name . ' = ' . $expr . ';' . PHP_EOL; $callParams .= 'arg_' . $argInfo->name . ','; }