feat(php): 添加对原生方法调用的支持

- 在 ClassDef 中添加 hasMethod 方法用于检查类中是否存在指定方法
- 实现 parseNativeCallArgs 方法用于解析原生函数调用参数
- 修改 parseCallArgs 方法以支持原生函数参数解析
- 移除对 nativeFunction 变量的依赖并简化参数处理逻辑
- 添加对原生方法调用的识别和解析功能
- 实现 parseNativeMethodCall 方法用于处理对象的原生方法调用
- 添加 isNativeMethod 方法判断是否为原生方法调用
pull/1/head
韩天峰 7 months ago
parent 3959fee828
commit 62e5615421
  1. 5
      src/Php/ClassDef.php
  2. 51
      src/Php/CompilerBase.php

@ -25,4 +25,9 @@ class ClassDef extends ClassLikeDef
$this->flags = $flags;
parent::__construct($name, $namespace);
}
public function hasMethod(string $method): bool
{
return isset($this->methods[$method]);
}
}

@ -1441,12 +1441,22 @@ class CompilerBase extends \PhpAot\Core\Translator
}
}
protected function parseNativeCallArgs(array $args, string $nativeFunc): string
{
$list_args = [];
foreach ($args as $i => $arg) {
$argInfo = $this->getArgInfo($arg, $nativeFunc, $i);
$list_args[] = $this->getTypeConvertedArg($arg, $argInfo);
}
return implode(', ', $list_args);
}
protected function parseCallArgs(array $args, string $funcName = '', string $className = ''): string
{
if (!$className) {
$nativeFunction = $this->isNativeFunction($funcName);
} else {
$nativeFunction = false;
if ($this->isNativeFunction($funcName)) {
return $this->parseNativeCallArgs($args, $funcName);
}
}
$list_args = [];
@ -1457,7 +1467,7 @@ class CompilerBase extends \PhpAot\Core\Translator
if (!$this->hasVar($name)) {
$this->addLocalVar($name, self::TYPE_REF);
$this->beforeStmtLines[] = $name . ' = php::newReference();';
} elseif (!$nativeFunction and $funcName and Reflection::isReferenceArg($funcName, $i)) {
} elseif ($funcName and Reflection::isReferenceArg($funcName, $i)) {
// 需要引用类型的参数,使用临时变量作为引用,并替换掉实际的参数
$tmpVar = $this->genTmpVarName();
$this->addLocalVar($tmpVar, self::TYPE_REF);
@ -1467,7 +1477,7 @@ class CompilerBase extends \PhpAot\Core\Translator
continue;
}
} elseif ($this->isPropertyFetch($arg->value)) {
if (!$nativeFunction and $funcName and Reflection::isReferenceArg($funcName, $i)) {
if ($funcName and Reflection::isReferenceArg($funcName, $i)) {
$obj = $this->parseIdentifier($arg->value->var);
$list_args[] = $obj . '.getPropertyReference(' . $this->identifierToStr($arg->value->name) . ')';
continue;
@ -1477,12 +1487,7 @@ class CompilerBase extends \PhpAot\Core\Translator
if ($arg->unpack) {
$this->fatalError($arg, "The syntax for variable parameter expansion is not supported");
}
if ($nativeFunction) {
$argInfo = $this->getArgInfo($arg, $funcName, $i);
$list_args[] = $this->getTypeConvertedArg($arg, $argInfo);
} else {
$list_args[] = $this->parseArg($arg);
}
$list_args[] = $this->parseArg($arg);
}
return implode(', ', $list_args);
}
@ -2376,6 +2381,10 @@ class CompilerBase extends \PhpAot\Core\Translator
{
$object = $this->convertToObject($expr->var);
$method = $this->parseIdentifier($expr->name);
$nativeFunc = $this->getNativeFunctionName($method, $this->namespace, $this->class);
if ($this->isNativeMethod($object, $nativeFunc)) {
return $this->parseNativeMethodCall($object, $nativeFunc, $expr->args);
}
if (empty($expr->args)) {
return $object . '.exec("' . $method . '")';
} else {
@ -2697,8 +2706,26 @@ class CompilerBase extends \PhpAot\Core\Translator
abort($expr);
}
private function parseContinue(mixed $v): string
protected function parseContinue(mixed $v): string
{
return 'continue;';
}
protected function isNativeMethod(string $object, string $nativeFunc): bool
{
if ($object === 'this_') {
return $this->isNativeFunction($nativeFunc);
}
return false;
}
protected function parseNativeMethodCall(string $object, string $nativeFunc, array $args): string
{
if (count($args) === 0) {
return self::PREFIX . $nativeFunc . '(' . $object . ')';
} else {
return self::PREFIX .$nativeFunc . '(' . $object . ', ' . $this->parseNativeCallArgs($args, $nativeFunc) . ')';
}
}
}

Loading…
Cancel
Save