feat(compiler): 添加项目配置和扩展忽略功能

- 在 Translator 类中新增 ignoreExtensions 属性用于存储忽略的扩展
- 修改构造函数逻辑,将 main 函数从内部函数列表中移除
- 更新属性初始化代码,修复命名空间获取方式
- 在项目配置解析中添加对 ext-* 格式扩展的识别和忽略处理
- 添加测试用例验证对象构造器数组属性默认值功能
- 添加引用操作相关测试用例
- 新增项目配置文件示例
pull/1/head
韩天峰 4 months ago
parent 84b9340998
commit ee3dbc9717
  1. 9
      examples/test/project.yml
  2. 8
      src/Php/Translator.php
  3. 2
      src/compiler-build.php
  4. 52
      tests/aot/object_ctor/001.phpt
  5. 35
      tests/aot/ref/008.phpt

@ -0,0 +1,9 @@
name: test
type: ext
version: 0.0.1
ignore:
- ext-redis
- ext-POD_MYSQL
sources:
- call.php
- main.php

@ -35,6 +35,7 @@ class Translator extends Preprocessor
protected bool $verbose = false;
protected array $phpSrcFiles = [];
protected array $ignorePaths = [];
protected array $ignoreExtensions = [];
protected array $argInfoHeaderFiles = [];
protected array $registerSymbols = [];
@ -60,6 +61,7 @@ class Translator extends Preprocessor
$this->noLiteralStrings = $this->climate->arguments->get('noLiteralStrings');
$this->enableProfiler = $this->climate->arguments->defined('profile');
$this->internalFunctions = array_flip(get_defined_functions()['internal']);
unset($this->internalFunctions['main']);
$this->internalConstants = get_defined_constants();
if ($this->climate->arguments->defined('help')) {
$this->showUsage();
@ -604,7 +606,7 @@ class Translator extends Preprocessor
$code .= $classDef->ctorInit;
$code .= "auto obj = create_object_{$className}(class_type);\n";
foreach ($classDef->properties as $property) {
$fullPropName = $classDef->getNamespacedName() . '::' . $property->name;
$fullPropName = $classDef->getNamespacedName(false) . '::' . $property->name;
if (isset($this->defaultPropertyList[$fullPropName])) {
$code .= "do {\n";
$code .= "auto value = {$this->defaultPropertyList[$fullPropName]};\n";
@ -787,6 +789,10 @@ class Translator extends Preprocessor
$this->error('`ignore` must be array');
}
foreach ($cfg['ignore'] as $src) {
if (preg_match('/ext-([a-z0-9_]+)/i', $src, $matches)) {
$this->ignoreExtensions[] = $matches[1];
continue;
}
$realPath = $this->getAbsolutePath($src, $projectDir);
if (!$realPath) {
$this->error('Source file not exists: `' . $src . '`');

@ -12,6 +12,8 @@ function main(int $argc, array $argv): void
die("php compiler.php [file]\n");
}
global $translator;
$path = $argv[1];
$translator = new Translator(ROOT_PATH);
$translator->setIndent(' ');

@ -0,0 +1,52 @@
--TEST--
default array property
--FILE--
<?php
namespace App{
class Test {
public const string TYPE_BOOL = 'php::Bool';
public const string TYPE_INT = 'php::Int';
public const string TYPE_FLOAT = 'php::Float';
protected array $array1 = [
'int' => self::TYPE_INT,
'float' => self::TYPE_FLOAT,
'bool' => self::TYPE_BOOL,
];
protected $array2;
function __construct() {
$this->array2 = ['php', 'java',];
}
function bar() {
var_dump($this->array1);
var_dump($this->array2);
}
}
}
namespace {
function main() {
$obj = new App\Test;
$obj->bar();
}
}
?>
--EXPECT--
array(3) {
["int"]=>
string(8) "php::Int"
["float"]=>
string(10) "php::Float"
["bool"]=>
string(9) "php::Bool"
}
array(2) {
[0]=>
string(3) "php"
[1]=>
string(4) "java"
}

@ -0,0 +1,35 @@
--TEST--
class const 001
--FILE--
<?php
class WorkerA
{
public function foo(): array {
$list = [377, 64, 688, 2];
$ref = &$list;
$this->sort($ref);
return $list;
}
public function sort(array &$list) {
sort($list);
}
}
function main()
{
$o = new WorkerA;
var_dump($o->foo());
}
?>
--EXPECT--
array(4) {
[0]=>
int(2)
[1]=>
int(64)
[2]=>
int(377)
[3]=>
int(688)
}
Loading…
Cancel
Save