From 62e5615421998b9987ff335724ad5550367ecab8 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 21 Jan 2026 18:46:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(php):=20=E6=B7=BB=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=E5=8E=9F=E7=94=9F=E6=96=B9=E6=B3=95=E8=B0=83=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 ClassDef 中添加 hasMethod 方法用于检查类中是否存在指定方法 - 实现 parseNativeCallArgs 方法用于解析原生函数调用参数 - 修改 parseCallArgs 方法以支持原生函数参数解析 - 移除对 nativeFunction 变量的依赖并简化参数处理逻辑 - 添加对原生方法调用的识别和解析功能 - 实现 parseNativeMethodCall 方法用于处理对象的原生方法调用 - 添加 isNativeMethod 方法判断是否为原生方法调用 --- src/Php/ClassDef.php | 5 ++++ src/Php/CompilerBase.php | 51 ++++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/Php/ClassDef.php b/src/Php/ClassDef.php index 8880397b..2cde0475 100644 --- a/src/Php/ClassDef.php +++ b/src/Php/ClassDef.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]); + } } \ No newline at end of file diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index ea585906..99b27853 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -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) . ')'; + } + } }