feat(php): 添加对引用参数和命名参数的错误检查

- 在参数解析过程中添加了对引用参数(byRef)的支持检查
- 在函数调用中添加了对命名参数的支持检查
- 当遇到引用参数时抛出致命错误
- 当遇到命名参数时抛出致命错误
- 更新示例文件以注释相关测试代码
pull/1/head
韩天峰 7 months ago
parent 120e89ac79
commit 0723e9f01f
  1. 28
      examples/ref.php
  2. 9
      src/Php/CompilerBase.php

@ -1,11 +1,27 @@
<?php <?php
//function test(&$b) {
// $b[] = 4;
//}
function test2($a, $b, $c)
{
return $a + $b + $c;
}
function main() function main()
{ {
$a = [1, 2, 3]; // test2(1, 2, 3);
$b = &$a; // test2(c: 2, b: 3, a: 1);
$b[] = 5; // var_dump(str_contains(haystack: 'hello', needle: 'll'));
// $a = [1, 2, 3];
// $b = &$a;
// $b[] = 5;
//
// $c = &$b;
// $c [] = 6;
// var_dump($a, $b, $c);
$c = &$b; // test($a);
$c [] = 6; // var_dump($a);
var_dump($a, $b, $c);
} }

@ -783,6 +783,9 @@ class CompilerBase extends \PhpAot\Core\Translator
if ($this->stubFile and !$param->type) { if ($this->stubFile and !$param->type) {
throw new \RuntimeException('No type for ' . $this->parseIdentifier($param->var)); throw new \RuntimeException('No type for ' . $this->parseIdentifier($param->var));
} }
if ($param->byRef) {
$this->fatalError($param, 'ByRef parameters are not supported');
}
$name = $this->parseIdentifier($param->var); $name = $this->parseIdentifier($param->var);
$type = $this->parseParameterType($param, $name); $type = $this->parseParameterType($param, $name);
$list[] = $type . ' ' . $name; $list[] = $type . ' ' . $name;
@ -1790,6 +1793,9 @@ class CompilerBase extends \PhpAot\Core\Translator
{ {
$list_args = []; $list_args = [];
foreach ($args as $i => $arg) { foreach ($args as $i => $arg) {
if ($arg->name !== null) {
$this->fatalError($arg, 'Named arguments are not supported');
}
$argInfo = $this->getArgInfo($arg, $nativeFunc, $i); $argInfo = $this->getArgInfo($arg, $nativeFunc, $i);
$list_args[] = $this->getTypeConvertedArg($arg, $argInfo); $list_args[] = $this->getTypeConvertedArg($arg, $argInfo);
} }
@ -1807,6 +1813,9 @@ class CompilerBase extends \PhpAot\Core\Translator
$list_args = []; $list_args = [];
foreach ($args as $i => $arg) { foreach ($args as $i => $arg) {
if ($arg->name !== null) {
$this->fatalError($arg, 'Named arguments are not supported');
}
if ($this->isVarExpr($arg->value)) { if ($this->isVarExpr($arg->value)) {
$name = $this->parseIdentifier($arg->value); $name = $this->parseIdentifier($arg->value);
// 调用了不存在的变量,可能是引用 // 调用了不存在的变量,可能是引用

Loading…
Cancel
Save