feat(php): 添加对一等可调用语法的支持

- 在编译器中实现 foo(...) 语法的占位符处理逻辑
- 添加对函数调用占位符的识别和生成支持
- 新增 first-class callable 语法的测试用例
- 实现可调用性检查的正确处理机制
pull/1/head
韩天峰 3 months ago
parent 1111cc0c27
commit 2da2bc094f
  1. 4
      src/Php/CompilerBase.php
  2. 39
      tests/aot/callable/first-class-callable.phpt

@ -2781,6 +2781,10 @@ class CompilerBase extends \PhpAot\Core\Translator
$nativeFn = $this->findNativeFunction($name);
if ($nativeFn) {
$expr->setAttribute('nativeCall', $nativeFn);
// 函数调用占位符,不是真实的函数调用
if (count($expr->args) === 1 and $this->isPlaceholderExpr($expr->args[0])) {
return $this->genPlaceHolder($this->identifierToStr($expr->name));
}
$this->checkNativeCallArgs($expr, $this->getFunction($nativeFn), $expr->args, $name);
try {
return self::PREFIX . $nativeFn . '(' . $this->parseNativeCallArgs($expr->args, $nativeFn) . ')';

@ -0,0 +1,39 @@
--TEST--
First-class callable syntax foo(...)
--FILE--
<?php
function multiply(int $a, int $b): int {
return $a * $b;
}
class Calculator {
public function add(int $a, int $b): int {
return $a + $b;
}
public static function sub(int $a, int $b): int {
return $a - $b;
}
}
function main(): void {
$mul = multiply(...);
echo $mul(6, 7) . "\n";
$calc = new Calculator();
$add = $calc->add(...);
echo $add(10, 5) . "\n";
$sub = Calculator::sub(...);
echo $sub(10, 3) . "\n";
var_dump(is_callable('multiply'));
var_dump(is_callable('nonexistent'));
}
?>
--EXPECT--
42
15
7
bool(true)
bool(false)
Loading…
Cancel
Save