fix(const): 修复命名空间常量定义和引用问题

- 在常量名称前添加命名空间前缀以支持命名空间常量
- 将常量信息存储结构扩展为包含命名空间和名称字段
- 使用转义后的命名空间名称作为常量查找键值
- 更新模板中常量遍历变量名以避免混淆
- 修复常量定义时的字符串转义处理
- 添加命名空间常量测试用例验证功能
- 实现参数信息存根文件名生成方法
pull/1/head
韩天峰 7 months ago
parent 0c02958589
commit 7e5b6ef045
  1. 2
      bin/gen_stub.php
  2. 13
      src/Php/CompilerBase.php
  3. 7
      src/Php/Translator.php
  4. 14
      src/template/extension.cc.php
  5. 21
      tests/aot/ns-const.phpt

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

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

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

@ -52,9 +52,9 @@ foreach ($this->literalStrings as $str => $index):
// constants
<?php
foreach ($this->nativeConstants as $name => $constant):
foreach ($this->nativeConstants as $name => $const):
?>
<?=$constant->type?> <?=$name?>;
<?=$const->type?> <?=$name?>;
<?php endforeach; ?>
// property offset
@ -109,10 +109,10 @@ foreach ($this->registerSymbols as $registerSymbolFn):
void php_app_init() {
// register constants
<?php
foreach ($this->nativeConstants as $name => $constant):
foreach ($this->nativeConstants as $name => $const):
?>
<?=$name?> = <?= $constant->value ?>;
php::define("<?=$name?>", <?= $name ?>);
<?=$name?> = <?= $const->value ?>;
php::define("<?=$this->escapeString($const->name)?>", <?= $name ?>);
<?php endforeach; ?>
// global vars
@ -142,8 +142,8 @@ foreach ($this->globalVars as $name => $type) :
php::unsetGlobal("<?=$name?>");
<?php endforeach; ?>
<?php
foreach ($this->nativeConstants as $name => $constant):
if ($constant->type !== Translator::TYPE_VAR) {
foreach ($this->nativeConstants as $name => $const):
if ($const->type !== Translator::TYPE_VAR) {
continue;
}
?>

@ -0,0 +1,21 @@
--TEST--
namespace constants
--FILE--
<?php
namespace foo {
const bar = 42;
}
namespace {
const bar = 35;
function main() {
var_dump(bar);
var_dump(foo\bar);
echo "Done\n";
}
}
?>
--EXPECT--
int(35)
int(42)
Done
Loading…
Cancel
Save