refactor(php): 统一PHP函数命名空间从std到fn

- 将所有php::std::前缀的函数调用更改为php::fn::前缀
- 替换AssignOpTrait.php中的pow函数调用路径
- 更新BinaryOpTrait.php中的mod和pow函数调用路径
- 修改CompilerBase.php中的头文件引用,合并为统一的phpx_std.h
- 在CompilerBase.php的throw语句中添加析构函数警告检查
- 更新FuncCallOptimizer.php中的各类数学和标准库函数调用路径
- 移除Translator.php中重复的标准库源文件引用
- 更新UniversalMethodCall.php中的parseStr函数调用路径
pull/1/head
韩天峰 2 months ago
parent 527c7d62da
commit ef6a9fb8e5
  1. 13
      src/Php/CompilerBase.php
  2. 46
      src/Php/Optimizer/FuncCallOptimizer.php
  3. 2
      src/Php/Parser/AssignOpTrait.php
  4. 4
      src/Php/Parser/BinaryOpTrait.php
  5. 13
      src/Php/Translator.php
  6. 2
      src/Php/UniversalMethodCall.php

@ -216,13 +216,7 @@ class CompilerBase extends \PhpAot\Core\Translator
'phpx_big_float.h',
'phpx_decimal.h',
'php_aot_helper.h',
'php_std_core.h',
'php_std_string.h',
'php_std_misc.h',
'php_std_array.h',
'php_std_datetime.h',
'php_std_fs.h',
'php_std_math.h',
'phpx_std.h',
];
protected array $localHeaders = [];
protected array $internalFunctions = [];
@ -4848,6 +4842,9 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function parseThrow(mixed $expr): string
{
if ($this->method === '__destruct') {
$this->warning($expr, "Throwing exception in {$this->getFullClassName()}::__destruct() may cause memory leak");
}
if ($this->isNewExpr($expr->expr)) {
$ex = $this->parseExpr($expr->expr);
} elseif ($this->isVarExpr($expr->expr)) {
@ -4937,7 +4934,7 @@ class CompilerBase extends \PhpAot\Core\Translator
foreach ($expr->parts as $part) {
$list[] = $this->identifierToStr($part);
}
return 'php::std::shell_exec(php::concat({' . implode(', ', $list) . '}))';
return 'php::fn::shell_exec(php::concat({' . implode(', ', $list) . '}))';
}
protected function parseGoto(Node\Stmt\Goto_ $v): string

@ -129,26 +129,26 @@ trait FuncCallOptimizer
self::TYPE_BIGINT => 'php::BigInt::abs',
self::TYPE_BIGFLOAT => 'php::BigFloat::abs',
self::TYPE_DECIMAL => 'php::Decimal::abs',
'fallback' => 'php::std::abs',
'fallback' => 'php::fn::abs',
]],
'pow' => ['bigDispatch' => [
self::TYPE_BIGINT => 'php::BigInt::pow',
self::TYPE_DECIMAL => 'php::Decimal::pow',
'fallback' => 'php::std::pow',
'fallback' => 'php::fn::pow',
]],
'sqrt' => ['bigDispatch' => [
self::TYPE_BIGINT => 'php::BigInt::sqrt',
self::TYPE_DECIMAL => 'php::Decimal::sqrt',
self::TYPE_BIGFLOAT => 'php::BigFloat::sqrt',
'fallback' => 'php::std::sqrt',
'fallback' => 'php::fn::sqrt',
]],
'floor' => ['bigDispatch' => [
self::TYPE_DECIMAL => 'php::Decimal::floor',
'fallback' => 'php::std::floor',
'fallback' => 'php::fn::floor',
]],
'ceil' => ['bigDispatch' => [
self::TYPE_DECIMAL => 'php::Decimal::ceil',
'fallback' => 'php::std::ceil',
'fallback' => 'php::fn::ceil',
]],
// Type conversions
@ -227,9 +227,9 @@ trait FuncCallOptimizer
{
$target = $config['target'] ?? null;
if ($target === null) {
$target = 'php::std::' . $name;
$target = 'php::fn::' . $name;
} elseif (!str_starts_with($target, 'php::')) {
$target = 'php::std::' . $target;
$target = 'php::fn::' . $target;
}
$refInfo = $this->getArgReflectionInfo($name);
@ -563,7 +563,7 @@ trait FuncCallOptimizer
if (count($e->args) >= 3) {
return false;
}
return $this->dispatchFuncCall('is_callable', $e, ['target' => 'php::std::is_callable']);
return $this->dispatchFuncCall('is_callable', $e, ['target' => 'php::fn::is_callable']);
}
protected function genGetClassOptimized(string $n, Node\Expr\FuncCall $e, array $c): string
@ -572,7 +572,7 @@ trait FuncCallOptimizer
if ($this->isVarExpr($obj) && $this->isTypedObject($obj->name)) {
return $this->getLiteralString($this->getObjectType($obj->name));
}
return 'php::std::get_class(' . $this->parseIdentifier($obj) . ')';
return 'php::fn::get_class(' . $this->parseIdentifier($obj) . ')';
}
protected function genGetParentClass(string $n, Node\Expr\FuncCall $e, array $c): string
@ -589,7 +589,7 @@ trait FuncCallOptimizer
if ($cls && $cls->extends) return $this->getLiteralString($cls->extends);
if ($cls && !$cls->extends) return 'false';
}
return 'php::std::get_parent_class(' . $this->parseIdentifier($arg) . ')';
return 'php::fn::get_parent_class(' . $this->parseIdentifier($arg) . ')';
}
protected function genFunctionExistsOptimized(string $n, Node\Expr\FuncCall $e, array $c): string
@ -621,12 +621,12 @@ trait FuncCallOptimizer
{
$cnt = count($e->args);
if ($cnt >= 3) {
return 'php::std::array_keys_filter(' . $this->getArg($e, 0) . ', ' . $this->getArg($e, 1) . ', ' . $this->getArg($e, 2) . ')';
return 'php::fn::array_keys_filter(' . $this->getArg($e, 0) . ', ' . $this->getArg($e, 1) . ', ' . $this->getArg($e, 2) . ')';
}
if ($cnt >= 2) {
return 'php::std::array_keys_filter(' . $this->getArg($e, 0) . ', ' . $this->getArg($e, 1) . ', false)';
return 'php::fn::array_keys_filter(' . $this->getArg($e, 0) . ', ' . $this->getArg($e, 1) . ', false)';
}
return 'php::std::array_keys(' . $this->getArg($e, 0) . ')';
return 'php::fn::array_keys(' . $this->getArg($e, 0) . ')';
}
protected function genArrayKeyExists(string $n, Node\Expr\FuncCall $e, array $c): string
@ -646,12 +646,12 @@ trait FuncCallOptimizer
}
$args = count($e->args);
if ($args >= 3) {
return 'php::std::round(' . $this->getArg($e, 0) . ', ' . $this->convertIntExpr($this->getArg($e, 1)) . ', ' . $this->convertIntExpr($this->getArg($e, 2)) . ')';
return 'php::fn::round(' . $this->getArg($e, 0) . ', ' . $this->convertIntExpr($this->getArg($e, 1)) . ', ' . $this->convertIntExpr($this->getArg($e, 2)) . ')';
}
if ($args >= 2) {
return 'php::std::round(' . $this->getArg($e, 0) . ', ' . $this->convertIntExpr($this->getArg($e, 1)) . ')';
return 'php::fn::round(' . $this->getArg($e, 0) . ', ' . $this->convertIntExpr($this->getArg($e, 1)) . ')';
}
return 'php::std::round(' . $this->getArg($e, 0) . ')';
return 'php::fn::round(' . $this->getArg($e, 0) . ')';
}
protected function genCount(string $n, Node\Expr\FuncCall $e, array $c): string
@ -659,9 +659,9 @@ trait FuncCallOptimizer
$folded = $this->doFoldCountLiteral($e);
if ($folded !== false) return $folded;
if (count($e->args) >= 2) {
return 'php::std::count(' . $this->getArg($e, 0) . ', ' . $this->convertIntExpr($this->getArg($e, 1)) . ')';
return 'php::fn::count(' . $this->getArg($e, 0) . ', ' . $this->convertIntExpr($this->getArg($e, 1)) . ')';
}
return 'php::std::count(' . $this->getArg($e, 0) . ')';
return 'php::fn::count(' . $this->getArg($e, 0) . ')';
}
protected function genDefine(string $n, Node\Expr\FuncCall $e, array $c): string|false
@ -672,9 +672,9 @@ trait FuncCallOptimizer
}
$args = count($e->args) >= 3 ? 3 : 2;
if ($args == 3) {
return 'php::std::define(' . $this->getArg($e, 0) . ', ' . $this->getArg($e, 1) . ', ' . $this->getArg($e, 2) . ')';
return 'php::fn::define(' . $this->getArg($e, 0) . ', ' . $this->getArg($e, 1) . ', ' . $this->getArg($e, 2) . ')';
}
return 'php::std::define(' . $this->getArg($e, 0) . ', ' . $this->getArg($e, 1) . ')';
return 'php::fn::define(' . $this->getArg($e, 0) . ', ' . $this->getArg($e, 1) . ')';
}
// =========================================================================
@ -740,9 +740,9 @@ trait FuncCallOptimizer
return 'true';
}
$funcName = $this->getLiteralString($nameLower);
return 'php::std::function_exists(' . $funcName . ')';
return 'php::fn::function_exists(' . $funcName . ')';
}
return 'php::std::function_exists(' . $this->parseIdentifier($funcName) . ')';
return 'php::fn::function_exists(' . $this->parseIdentifier($funcName) . ')';
}
protected function genGetClass(Node\Expr\FuncCall $expr): string
@ -751,7 +751,7 @@ trait FuncCallOptimizer
if ($this->isVarExpr($object) and $this->isTypedObject($object->name)) {
return $this->getLiteralString($this->getObjectType($object->name));
}
return 'php::std::get_class(' . $this->parseIdentifier($object) . ')';
return 'php::fn::get_class(' . $this->parseIdentifier($object) . ')';
}
protected function genCompactOrig(Node\Expr\FuncCall $expr): string

@ -347,7 +347,7 @@ trait AssignOpTrait
return $var . '.append(' . $rightExprStr . ')';
}
if ($this->isAssignOpPow($op)) {
$powExpr = 'php::math::pow(' . $var . ', ' . $rightExprStr . ')';
$powExpr = 'php::fn::pow(' . $var . ', ' . $rightExprStr . ')';
return $var . ' = ' . $this->convertVarType($var, $powExpr);
}
return $var . ' ' . $op . ' ' . $rightExprStr;

@ -122,7 +122,7 @@ trait BinaryOpTrait
}
if ($op === '%' and !($leftType === self::TYPE_INT and $rightType === self::TYPE_INT)) {
return 'php::math::mod(' . $leftExpr . ', ' . $rightExpr . ')';
return 'php::fn::mod(' . $leftExpr . ', ' . $rightExpr . ')';
}
return '((' . $leftExpr . ') ' . $op . ' (' . $rightExpr . '))';
@ -190,7 +190,7 @@ trait BinaryOpTrait
}
$left = $this->parseIdentifier($expr->left);
$right = $this->parseIdentifier($expr->right);
return 'php::math::pow(' . $left . ', ' . $right . ')';
return 'php::fn::pow(' . $left . ', ' . $right . ')';
}
protected function parseBinaryOpBitwiseAnd(Expr\BinaryOp\BitwiseAnd $expr): string

@ -398,7 +398,7 @@ class Translator extends Preprocessor
public function prepare(string $path): array
{
// shell_exec 和 define 已通过 php::std:: 直接调用,无需动态符号表
// shell_exec 和 define 已通过 php::fn:: 直接调用,无需动态符号表
// 根据平台检查库文件(仅在构建二进制文件时需要)
if ($this->isBuildModeBin()) {
@ -681,7 +681,7 @@ CODE;
$code .= '// register constants' . PHP_EOL;
foreach ($this->constants as $name => $const) {
$code .= "{$name} = {$const->value};\n";
$code .= 'php::std::define(' . $this->genCharPtr($const->name, true) . ', ' . $name . ');' . PHP_EOL;
$code .= 'php::fn::define(' . $this->genCharPtr($const->name, true) . ', ' . $name . ');' . PHP_EOL;
}
$code .= '// global vars ' . PHP_EOL;
foreach ($this->globalVars as $name => $type) {
@ -970,15 +970,6 @@ CODE;
// 生成扩展模块的源文件
$sourceFiles[] = $this->genExtension();
// stdlib: 直接 C++ 包装的 Zend 内置函数
$sourceFiles[] = $this->getPhpxDir() . '/src/misc/php_std_core.cpp';
$sourceFiles[] = $this->getPhpxDir() . '/src/misc/php_std_array.cpp';
$sourceFiles[] = $this->getPhpxDir() . '/src/misc/php_std_string.cpp';
$sourceFiles[] = $this->getPhpxDir() . '/src/misc/php_std_datetime.cpp';
$sourceFiles[] = $this->getPhpxDir() . '/src/misc/php_std_fs.cpp';
$sourceFiles[] = $this->getPhpxDir() . '/src/misc/php_std_math.cpp';
$sourceFiles[] = $this->getPhpxDir() . '/src/misc/php_std_misc.cpp';
// embed 需要 main 函数,以及 cli 的内置函数定义
if ($this->isBuildModeBin()) {
$sourceFiles[] = $this->getPhpxDir() . '/src/misc/main.cc';

@ -90,7 +90,7 @@ trait UniversalMethodCall
'trim' => ['handler' => 'php_fn', 'fn' => 'trim', 'return_type' => CompilerBase::TYPE_STR, 'min_args' => 0, 'max_args' => 2],
'lTrim' => ['handler' => 'php_fn', 'fn' => 'ltrim', 'return_type' => CompilerBase::TYPE_STR, 'min_args' => 0, 'max_args' => 1],
'rTrim' => ['handler' => 'php_fn', 'fn' => 'rtrim', 'return_type' => CompilerBase::TYPE_STR, 'min_args' => 0, 'max_args' => 1],
'parseStr' => ['handler' => 'cpp_fn', 'fn' => 'php::std::parse_str', 'return_type' => CompilerBase::TYPE_ARRAY, 'min_args' => 0, 'max_args' => 0],
'parseStr' => ['handler' => 'cpp_fn', 'fn' => 'php::fn::parse_str', 'return_type' => CompilerBase::TYPE_ARRAY, 'min_args' => 0, 'max_args' => 0],
'parseUrl' => ['handler' => 'php_fn', 'fn' => 'parse_url', 'return_type' => CompilerBase::TYPE_VAR, 'min_args' => 0, 'max_args' => 1],
'contains' => ['handler' => 'php_fn', 'fn' => 'str_contains', 'return_type' => CompilerBase::TYPE_BOOL, 'min_args' => 1, 'max_args' => 1],
'incr' => ['handler' => 'php_fn', 'fn' => 'str_increment', 'return_type' => CompilerBase::TYPE_STR, 'min_args' => 0, 'max_args' => 0],

Loading…
Cancel
Save