From 12ff420d873318d13872202e476f13c973a816cf Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 22 Apr 2026 13:53:37 +0800 Subject: [PATCH] =?UTF-8?q?refactor(config):=20=E5=B0=86=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E5=99=A8=E9=80=89=E9=A1=B9=E9=85=8D=E7=BD=AE=E8=BF=81=E7=A7=BB?= =?UTF-8?q?=E8=87=B3=E5=B8=B8=E9=87=8F=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除独立的 compiler_options.php 配置文件 - 将编译器选项定义移至 Php/Constants.php 中的 COMPILER_OPTIONS 常量 - 更新 CompilerBase.php 中的 getNamespacedClassName 调用方式 - 修改 Constants.php 中的 UNSUPPORTED_FUNCTIONS 为数组类型 - 更新 Translator.php 中配置加载方式,使用 Constants::COMPILER_OPTIONS 替代 require - 添加 objval 函数的相关测试用例 --- src/Php/CompilerBase.php | 4 +- src/Php/Constants.php | 74 ++++++++++++++++++++++++++++++++- src/Php/Translator.php | 2 +- src/config/compiler_options.php | 72 -------------------------------- tests/aot/objval.phpt | 37 +++++++++++++++++ 5 files changed, 113 insertions(+), 76 deletions(-) delete mode 100644 src/config/compiler_options.php create mode 100644 tests/aot/objval.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 36e53958..6f5483d6 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -1515,7 +1515,7 @@ class CompilerBase extends \PhpAot\Core\Translator if (count($expr->args) === 2 and $fn === 'objval') { $argClass = $expr->args[1]->value; if ($this->isScalarString($argClass)) { - return $this->getNamespacedClassName($this->parseIdentifier($argClass)); + return $this->getNamespacedClassName($argClass->value); } if ($this->isClassConstFetch($argClass)) { if ($this->isNameExpr($argClass->class) and $this->isIdExpr($argClass->name) and $this->parseIdentifier($argClass->name) === 'class') { @@ -4325,7 +4325,7 @@ class CompilerBase extends \PhpAot\Core\Translator $class = $this->getNamespacedClassName($class); if ($const === 'class') { - return '"' . $this->escapeString($class) . '"'; + return $this->getLiteralString($class); } if (($self or $this->isNameExpr($expr->class)) and $this->isIdExpr($expr->name)) { if ($this->hasClass($class)) { diff --git a/src/Php/Constants.php b/src/Php/Constants.php index 4a83d0ab..3c1706ae 100644 --- a/src/Php/Constants.php +++ b/src/Php/Constants.php @@ -62,7 +62,79 @@ class Constants 'this_', // phpx keywords ]; - public const UNSUPPORTED_FUNCTIONS = [ + public const array UNSUPPORTED_FUNCTIONS = [ 'extract', ]; + + public const array COMPILER_OPTIONS = [ + 'optimize' => [ + 'prefix' => 'O', + 'longPrefix' => 'optimize', + 'description' => 'Set the optimization level of the gcc compiler to 0 by default', + 'required' => false, + 'castTo' => 'int', + 'defaultValue' => 0, + ], + 'output' => [ + 'prefix' => 'o', + 'longPrefix' => 'output', + 'description' => 'Output file', + ], + 'help' => [ + 'prefix' => 'h', + 'longPrefix' => 'help', + 'description' => 'Show help', + 'noValue' => true, + ], + 'version' => [ + 'prefix' => 'v', + 'longPrefix' => 'version', + 'description' => 'Show Version', + 'noValue' => true, + ], + 'profile' => [ + 'longPrefix' => 'profile', + 'description' => 'Enable performance profiling', + 'required' => false, + 'noValue' => true, + ], + 'noLiteralStrings' => [ + 'longPrefix' => 'no-literal-strings', + 'description' => 'Disable literal strings optimization', + 'required' => false, + 'noValue' => true, + ], + 'force' => [ + 'prefix' => 'f', + 'longPrefix' => 'force', + 'description' => 'Force compile even if cache exists', + 'required' => false, + 'noValue' => true, + ], + 'mode' => [ + 'longPrefix' => 'mode', + 'prefix' => 'm', + 'description' => 'Build mode, -m bin(binary) or -m ext(extension), default: bin', + 'required' => false, + 'defaultValue' => 'bin', + ], + 'debug-line' => [ + 'longPrefix' => 'debug-line', + 'description' => 'Enable debug line', + 'required' => false, + 'defaultValue' => 0, + ], + 'debug-info' => [ + 'longPrefix' => 'debug-info', + 'description' => 'Enable debug info', + 'required' => false, + ], + 'job' => [ + 'prefix' => 'j', + 'longPrefix' => 'job', + 'description' => 'Number of jobs to run in parallel', + 'required' => false, + 'defaultValue' => 4, + ], + ]; } diff --git a/src/Php/Translator.php b/src/Php/Translator.php index d78c16c0..ce9377e9 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -49,7 +49,7 @@ class Translator extends Preprocessor public function __construct(string $rootPath) { parent::__construct($rootPath); - $this->climate->arguments->add(require __DIR__ . '/../config/compiler_options.php'); + $this->climate->arguments->add(Constants::COMPILER_OPTIONS); $this->preprocessArgvAdvanced(); $this->climate->arguments->parse(); diff --git a/src/config/compiler_options.php b/src/config/compiler_options.php deleted file mode 100644 index 94f62ea8..00000000 --- a/src/config/compiler_options.php +++ /dev/null @@ -1,72 +0,0 @@ - [ - 'prefix' => 'O', - 'longPrefix' => 'optimize', - 'description' => 'Set the optimization level of the gcc compiler to 0 by default', - 'required' => false, - 'castTo' => 'int', - 'defaultValue' => 0, - ], - 'output' => [ - 'prefix' => 'o', - 'longPrefix' => 'output', - 'description' => 'Output file', - ], - 'help' => [ - 'prefix' => 'h', - 'longPrefix' => 'help', - 'description' => 'Show help', - 'noValue' => true, - ], - 'version' => [ - 'prefix' => 'v', - 'longPrefix' => 'version', - 'description' => 'Show Version', - 'noValue' => true, - ], - 'profile' => [ - 'longPrefix' => 'profile', - 'description' => 'Enable performance profiling', - 'required' => false, - 'noValue' => true, - ], - 'noLiteralStrings' => [ - 'longPrefix' => 'no-literal-strings', - 'description' => 'Disable literal strings optimization', - 'required' => false, - 'noValue' => true, - ], - 'force' => [ - 'prefix' => 'f', - 'longPrefix' => 'force', - 'description' => 'Force compile even if cache exists', - 'required' => false, - 'noValue' => true, - ], - 'mode' => [ - 'longPrefix' => 'mode', - 'prefix' => 'm', - 'description' => 'Build mode, -m bin(binary) or -m ext(extension), default: bin', - 'required' => false, - 'defaultValue' => 'bin', - ], - 'debug-line' => [ - 'longPrefix' => 'debug-line', - 'description' => 'Enable debug line', - 'required' => false, - 'defaultValue' => 0, - ], - 'debug-info' => [ - 'longPrefix' => 'debug-info', - 'description' => 'Enable debug info', - 'required' => false, - ], - 'job' => [ - 'prefix' => 'j', - 'longPrefix' => 'job', - 'description' => 'Number of jobs to run in parallel', - 'required' => false, - 'defaultValue' => 4, - ], -]; \ No newline at end of file diff --git a/tests/aot/objval.phpt b/tests/aot/objval.phpt new file mode 100644 index 00000000..9e4ddd8f --- /dev/null +++ b/tests/aot/objval.phpt @@ -0,0 +1,37 @@ +--TEST-- +objval +--FILE-- +a = $a; + $this->b = $b; + } + + public function test() + { + return $this->a + $this->b; + } +} + +function main() +{ + $obj = new TestObjval(1, 2); + $arr = array(); + $arr['obj'] = $obj; + $obj2 = objval($arr['obj'], 'TestObjval'); + var_dump($obj2->test()); + + $obj3 = objval($obj, TestObjval::class); + var_dump($obj3->test()); +} +?> +--EXPECT-- +int(3) +int(3) \ No newline at end of file