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