From daa5379e468b355df3a38c062c95483f7855325c Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 7 May 2026 11:49:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(std-array):=20=E6=B7=BB=E5=8A=A0=E6=A0=87?= =?UTF-8?q?=E5=87=86=E6=95=B0=E7=BB=84=E7=B1=BB=E5=9E=8B=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加了 TYPE_STD_ARRAY 常量定义 - 在全局头文件列表中加入 phpx_std_array.h - 修复了嵌套数组索引解析逻辑,使用 convertIntExpr 方法转换整数表达式 - 优化了 std::array 函数参数解析,直接获取数值而非解析标量值 - 使用 TYPE_STD_ARRAY 常量替换硬编码的 std::array 字符串 - 修改返回值为注释格式的声明字符串以便调试 --- examples/array-loop/std-array.php | 3 +-- src/Php/CompilerBase.php | 3 ++- src/Php/Parser/StdArrayParser.php | 8 ++++---- tests/aot/std-array/001.phpt | 27 +++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 tests/aot/std-array/001.phpt diff --git a/examples/array-loop/std-array.php b/examples/array-loop/std-array.php index a3084560..26492600 100644 --- a/examples/array-loop/std-array.php +++ b/examples/array-loop/std-array.php @@ -8,8 +8,7 @@ function main() $index = 9; $index2 = 5; $array[$index2][$index][0] = 2026; -// $array = std::array(native_types::type_int, 100); -// $array[99] = 2026; + var_dump($array[$index2][$index][0]); var_dump($array[$index2][$index]); } \ No newline at end of file diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 98ccf039..a814a6d8 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -63,11 +63,11 @@ class CompilerBase extends \PhpAot\Core\Translator public const string TYPE_FLOAT = 'php::Float'; public const string TYPE_OBJECT = 'php::Object'; public const string TYPE_ARRAY = 'php::Array'; + public const string TYPE_STD_ARRAY = 'php::StdArray'; public const string TYPE_ARGS = 'php::Args'; public const string TYPE_STR = 'php::Str'; public const string TYPE_REF = 'php::Ref'; public const string TYPE_VOID = 'void'; - public const string TYPE_STD_ARRAY = 'std::array'; public const int DECL_TYPE_OF_RETURN = 1; public const int DECL_TYPE_OF_PROPERTY = 2; public const int DECL_TYPE_OF_CONST = 3; @@ -134,6 +134,7 @@ class CompilerBase extends \PhpAot\Core\Translator protected array $globalHeaders = [ 'phpx.h', 'phpx_helper.h', + 'phpx_std_array.h', 'phpx_func.h', 'php_func_decl.h', 'php_global_var_decl.h', diff --git a/src/Php/Parser/StdArrayParser.php b/src/Php/Parser/StdArrayParser.php index f41b254d..fc0ef340 100644 --- a/src/Php/Parser/StdArrayParser.php +++ b/src/Php/Parser/StdArrayParser.php @@ -75,7 +75,7 @@ trait StdArrayParser } } $index = $this->parseExpr($tmp->dim); - $nesting[] = '[' . Symbol::safeIndex($index, $info['sizes'][$level]) . ']'; + $nesting[] = '[' . Symbol::safeIndex($this->convertIntExpr($index), $info['sizes'][$level]) . ']'; $tmp = $tmp->var; $level++; } else { @@ -103,7 +103,7 @@ trait StdArrayParser if (!$this->isScalarInt($tmp->args[1]->value)) { $this->fatalError($tmp, 'std::array() expects second argument to be an integer'); } - $size = $this->parseScalar($tmp->args[1]->value); + $size = $tmp->args[1]->value->value; $nesting[] = $size; $typeExpr = $tmp->args[0]->value; if ($this->isClassConstFetch($typeExpr)) { @@ -135,7 +135,7 @@ trait StdArrayParser } } - $decl = str_repeat('std::array<', count($nesting)); + $decl = str_repeat(self::TYPE_STD_ARRAY . '<', count($nesting)); $decl .= $type; for ($i = count($nesting) - 1; $i >= 0; $i--) { $decl .= ', ' . $nesting[$i] . '>'; @@ -145,6 +145,6 @@ trait StdArrayParser 'type' => $type, 'sizes' => array_reverse($nesting), ]; - return ''; + return '// ' . $decl; } } \ No newline at end of file diff --git a/tests/aot/std-array/001.phpt b/tests/aot/std-array/001.phpt new file mode 100644 index 00000000..bd00ce01 --- /dev/null +++ b/tests/aot/std-array/001.phpt @@ -0,0 +1,27 @@ +--TEST-- +std array: 001 +--FILE-- +getMessage(); + } +} +?> +--EXPECT-- +array(1) { + [0]=> + int(2026) +} +array(1) { + [0]=> + int(0) +} +Array index out of bounds: index 100, size 100