From be22aa751f2bf9e13955ad2ab4765c21612358e5 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 10 Feb 2026 17:42:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E4=BD=BF=E7=94=A8=E5=B8=B8?= =?UTF-8?q?=E9=87=8F=E6=9B=BF=E6=8D=A2=E7=A1=AC=E7=BC=96=E7=A0=81=E7=9A=84?= =?UTF-8?q?=20php::null=20=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 VALUE_NULL 常量用于表示 php::null - 将 offsetSet 方法中的硬编码 null 替换为常量引用 - 将函数返回语句中的硬编码 null 替换为常量引用 - 将数组赋值中的硬编码 null 替换为常量引用 - 将常量解析中的硬编码 null 替换为常量引用 - 修复函数末尾缺失返回语句时使用 NaN 常量作为默认返回值 --- src/Php/CompilerBase.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 283e610b..3d0de88c 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -48,6 +48,7 @@ class CompilerBase extends \PhpAot\Core\Translator public const string TYPE_VOID = 'void'; public const string VALUE_NAN = 'std::numeric_limits::quiet_NaN()'; public const string VALUE_INF = 'std::numeric_limits::infinity()'; + public const string VALUE_NULL = 'php::null'; public const string LITERAL_STRINGS = '_literal_strings'; public const string CLASS_MAP = 'class_map'; public const string FUNC_MAP = 'func_map'; @@ -995,7 +996,7 @@ class CompilerBase extends \PhpAot\Core\Translator $value = $this->trimBrackets($this->parseExpr($right)); if ($left->dim === null) { - return $code . "{$array}.offsetSet(php::null, {$value})"; + return $code . "{$array}.offsetSet(" . self::VALUE_NULL . ", {$value})"; } $dim = $this->trimBrackets($this->parseIdentifier($left->dim)); @@ -1246,7 +1247,7 @@ class CompilerBase extends \PhpAot\Core\Translator if ($this->functionDef->returnType === self::TYPE_VOID) { return 'return;'; } else { - return 'return php::null;'; + return 'return ' . self::VALUE_NULL . ';'; } } // 实际函数的返回值 @@ -1436,7 +1437,7 @@ class CompilerBase extends \PhpAot\Core\Translator $value = $this->parseIdentifier($item->value); if ($assocArray) { // TODO 混杂模式数组赋值 - $key = $item->key ? $this->parseIdentifier($item->key) : 'php::null'; + $key = $item->key ? $this->parseIdentifier($item->key) : self::VALUE_NULL; if (str_starts_with($key, self::LITERAL_STRINGS)) { $key = "{$key}.toStdString()"; } elseif ($key === '0L') { @@ -2343,7 +2344,7 @@ class CompilerBase extends \PhpAot\Core\Translator return $this->getConstant($name); } if ($name === 'null') { - return 'php::null'; + return self::VALUE_NULL; } if ($name === 'true') { return 'true'; @@ -3507,7 +3508,7 @@ class CompilerBase extends \PhpAot\Core\Translator or $this->functionDef->returnType === self::TYPE_BOOL) { return $this->getIndent() . 'return 0;'; } else { - return $this->getIndent() . 'return php::null;'; + return $this->getIndent() . 'return ' . self::VALUE_NULL . ';'; } } @@ -3549,7 +3550,7 @@ class CompilerBase extends \PhpAot\Core\Translator $fnCode .= $fnBodyCode; if (!$this->isReturnStmtInLastLine($expr->stmts)) { - $fnCode .= $this->genReturnCode(); + $fnCode .= 'return '.self::VALUE_NAN. ';' . PHP_EOL; } $this->indentLevel--;