From 7e5b6ef04507a2d5191e3cbab58ccf9ff437cd0e Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 9 Feb 2026 15:04:55 +0800 Subject: [PATCH] =?UTF-8?q?fix(const):=20=E4=BF=AE=E5=A4=8D=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E7=A9=BA=E9=97=B4=E5=B8=B8=E9=87=8F=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E5=92=8C=E5=BC=95=E7=94=A8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在常量名称前添加命名空间前缀以支持命名空间常量 - 将常量信息存储结构扩展为包含命名空间和名称字段 - 使用转义后的命名空间名称作为常量查找键值 - 更新模板中常量遍历变量名以避免混淆 - 修复常量定义时的字符串转义处理 - 添加命名空间常量测试用例验证功能 - 实现参数信息存根文件名生成方法 --- bin/gen_stub.php | 2 +- src/Php/CompilerBase.php | 13 +++++++++---- src/Php/Translator.php | 7 +++++++ src/template/extension.cc.php | 14 +++++++------- tests/aot/ns-const.phpt | 21 +++++++++++++++++++++ 5 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 tests/aot/ns-const.phpt diff --git a/bin/gen_stub.php b/bin/gen_stub.php index 813e8349..2933841a 100755 --- a/bin/gen_stub.php +++ b/bin/gen_stub.php @@ -79,7 +79,7 @@ function processStubFile(string $stubFile, Context $context, bool $includeOnly = if (!$includeOnly) { global $translator; - $stubFilenameWithoutExtension = str_replace([".stub.php", '.php'], "", $stubFile); + $stubFilenameWithoutExtension = $translator->getArgInfoStubFilename($stubFile); $arginfoFile = $translator->getArgInfoHeaderFile($stubFilenameWithoutExtension); $legacyFile = "{$stubFilenameWithoutExtension}_legacy_arginfo.h"; diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 88b271d9..97ec5f7a 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -3186,6 +3186,9 @@ class CompilerBase extends \PhpAot\Core\Translator foreach ($v2->consts as $const) { $name = $this->parseIdentifier($const->name); $value = $this->parseIdentifier($const->value); + if ($this->namespace) { + $name = $this->namespace . '\\' . $name; + } $this->addConstant($name, $value); } @@ -3197,22 +3200,24 @@ class CompilerBase extends \PhpAot\Core\Translator $constInfo = new \stdClass(); $constInfo->value = $value; $constInfo->type = $this->detectStrValueType($value); - $this->nativeConstants[$name] = $constInfo; + $constInfo->namespace = $this->namespace; + $constInfo->name = $name; + $this->nativeConstants[$this->escapeNamespace($name)] = $constInfo; } protected function hasConstant(string $name): bool { - return isset($this->nativeConstants[$name]); + return isset($this->nativeConstants[$this->escapeNamespace($name)]); } protected function getConstant(string $name): string { - return $this->nativeConstants[$name]->value; + return $this->escapeNamespace($name); } protected function getConstantType(string $name): string { - return $this->nativeConstants[$name]->type; + return $this->nativeConstants[$this->escapeNamespace($name)]->type; } protected function detectStrValueType(mixed $constant): string diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 73925cc9..843152ef 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -323,6 +323,13 @@ class Translator extends Preprocessor return $this->buildMode; } + public function getArgInfoStubFilename(string $stubFile): string + { + $rs = str_replace([".stub.php", '.php'], "", $stubFile); + $rs = str_replace('-', '_', $rs); + return $rs; + } + public function getArgInfoHeaderFile(string $stubFilenameWithoutExtension, bool $relative = false): string { $basename = self::PREFIX . basename($stubFilenameWithoutExtension); diff --git a/src/template/extension.cc.php b/src/template/extension.cc.php index 136f15c9..ada04348 100644 --- a/src/template/extension.cc.php +++ b/src/template/extension.cc.php @@ -52,9 +52,9 @@ foreach ($this->literalStrings as $str => $index): // constants nativeConstants as $name => $constant): +foreach ($this->nativeConstants as $name => $const): ?> -type?> ; +type?> ; // property offset @@ -109,10 +109,10 @@ foreach ($this->registerSymbols as $registerSymbolFn): void php_app_init() { // register constants nativeConstants as $name => $constant): +foreach ($this->nativeConstants as $name => $const): ?> - = value ?>; - php::define("", ); + = value ?>; + php::define("escapeString($const->name)?>", ); // global vars @@ -142,8 +142,8 @@ foreach ($this->globalVars as $name => $type) : php::unsetGlobal(""); nativeConstants as $name => $constant): - if ($constant->type !== Translator::TYPE_VAR) { +foreach ($this->nativeConstants as $name => $const): + if ($const->type !== Translator::TYPE_VAR) { continue; } ?> diff --git a/tests/aot/ns-const.phpt b/tests/aot/ns-const.phpt new file mode 100644 index 00000000..ede3cc54 --- /dev/null +++ b/tests/aot/ns-const.phpt @@ -0,0 +1,21 @@ +--TEST-- +namespace constants +--FILE-- + +--EXPECT-- +int(35) +int(42) +Done