refactor(config): 将编译器选项配置迁移至常量类

- 移除独立的 compiler_options.php 配置文件
- 将编译器选项定义移至 Php/Constants.php 中的 COMPILER_OPTIONS 常量
- 更新 CompilerBase.php 中的 getNamespacedClassName 调用方式
- 修改 Constants.php 中的 UNSUPPORTED_FUNCTIONS 为数组类型
- 更新 Translator.php 中配置加载方式,使用 Constants::COMPILER_OPTIONS 替代 require
- 添加 objval 函数的相关测试用例
pull/1/head
韩天峰 4 months ago
parent 5bf12a10ed
commit 12ff420d87
  1. 4
      src/Php/CompilerBase.php
  2. 74
      src/Php/Constants.php
  3. 2
      src/Php/Translator.php
  4. 72
      src/config/compiler_options.php
  5. 37
      tests/aot/objval.phpt

@ -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)) {

@ -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,
],
];
}

@ -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();

@ -1,72 +0,0 @@
<?php
return [
'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,
],
];

@ -0,0 +1,37 @@
--TEST--
objval
--FILE--
<?php
class TestObjval
{
public int $a;
public int $b;
public function __construct(int $a, int $b)
{
$this->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)
Loading…
Cancel
Save