test(class-const): 添加类常量测试用例并优化常量生成逻辑

- 添加 class-const-003.phpt 测试文件验证类常量功能
- 修复数组类型类常量的值复制逻辑
- 移除未使用的 Zend 引擎常量表更新代码
- 统一代码风格将冒号语法替换为大括号语法
- 简化类常量更新方法的控制流程
pull/1/head
韩天峰 5 months ago
parent ad3bd659b0
commit 28f2b5ac8a
  1. 19
      src/Php/Translator.php
  2. 31
      tests/aot/class-const-003.phpt

@ -526,21 +526,14 @@ class Translator extends Preprocessor
protected function genUpdateClassConstants(): string
{
$code = '';
foreach ($this->classes as $classDef) :
foreach ($classDef->constants as $constant) :
if ($constant->type === self::TYPE_ARRAY) :
foreach ($this->classes as $classDef) {
foreach ($classDef->constants as $constant) {
if ($constant->type === self::TYPE_ARRAY) {
$constName = self::PREFIX . $this->getNativeName($constant->name, $classDef->namespace, $classDef->name);
$code .= $constName . " = " . $constant->value . ";\n";
if (false) {
$code .= "do {\n";
$code .= "auto c = (zend_class_constant *) zend_hash_str_find_ptr(&" . $this->getClassCe($classDef) . "->constants_table, ZEND_STRL(\"" . $constant->name . "\"));";
$code .= "c->type = (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ARRAY);\n";
$code .= "ZVAL_COPY(&c->value, $constName.ptr());\n";
$code .= " } while (0); \n";
}
endif;
endforeach;
endforeach;
}
}
}
return $code;
}

@ -0,0 +1,31 @@
--TEST--
class const 002
--FILE--
<?php
class Worker
{
public const int V1 = 1999;
public const int V2 = self::V1;
public const array ARR = [
'a' => self::V1,
'b' => self::V2,
];
}
function main()
{
var_dump(Worker::V1);
var_dump(Worker::V2);
var_dump(Worker::ARR);
}
?>
--EXPECT--
int(1999)
int(1999)
array(2) {
["a"]=>
int(1999)
["b"]=>
int(1999)
}
Loading…
Cancel
Save