feat(build): 添加编译器构建功能并更新项目配置

- 新增 src/compiler-build.php 作为编译器入口文件
- 在 extension.cc.php 中添加 zend 异常处理保护机制
- 更新 project.yml 将项目类型从 ext 改为 bin,版本升级到 0.1.0
- 添加新的依赖文件路径到项目配置中
- 在 Php/Symbol.php 中添加 getClassEntrySafe 方法
- 修改 Php/Translator.php 使用安全的类入口获取方法
- 修复 __toString 方法的返回类型声明
- 添加 examples/error/impl.php 示例文件
pull/1/head
韩天峰 4 months ago
parent f8e1c771e4
commit 9744cc9e86
  1. 12
      examples/error/impl.php
  2. 8
      project.yml
  3. 5
      src/Php/Symbol.php
  4. 3
      src/Php/Translator.php
  5. 26
      src/compiler-build.php
  6. 2
      src/gen_stub.php
  7. 20
      src/template/extension.cc.php

@ -0,0 +1,12 @@
<?php
class Foo implements InterfaceA {
public function foo()
{
return "foo";
}
}
function main()
{
$o = new Foo;
echo $o->foo();
}

@ -1,6 +1,6 @@
name: swoole-compiler
type: ext
version: 0.0.1
type: bin
version: 0.1.0
cxxflags: |
-std=c++14
-Wall
@ -9,4 +9,6 @@ sources:
- ./src/Core
- ./src/functions.php
- ./src/gen_stub.php
- ./vendor/nikic/php-parser/lib/PhpParser/NodeVisitorAbstract.php
- ./src/compiler-build.php
- vendor/nikic/php-parser/lib/PhpParser/NodeVisitor.php
- vendor/nikic/php-parser/lib/PhpParser/NodeVisitorAbstract.php

@ -44,4 +44,9 @@ class Symbol
{
return 'php::constant';
}
public static function getClassEntrySafe(): string
{
return 'php::getClassEntrySafe';
}
}

@ -30,6 +30,7 @@ class Translator extends Preprocessor
{
public const string VERSION = '0.1.0';
public const string APP_NAME = 'Swoole-Compiler (AOT)';
protected string $targetName = 'app';
protected array $sourceDirs = [];
protected bool $verbose = false;
@ -801,7 +802,7 @@ class Translator extends Preprocessor
protected function getInternalCeInfo(string $ce): array
{
return [
'func' => 'php::getClassEntry',
'func' => Symbol::getClassEntrySafe(),
'args' => '"' . substr($ce, strlen(self::PREFIX . 'class_entry_')) . '"',
];
}

@ -0,0 +1,26 @@
<?php
use PhpAot\Php\Translator;
function main(int $argc, array $argv): void
{
define("ROOT_PATH", getcwd());
define('DEBUG', true);
require ROOT_PATH . '/vendor/autoload.php';
if (empty($argv[1])) {
die("php compiler.php [file]\n");
}
$path = $argv[1];
$translator = new Translator(ROOT_PATH);
$translator->setIndent(' ');
// 扫描所有 PHP 文件,预处理
$files = $translator->prepare($path);
// 生成 C++ 文件
$sourceFiles = $translator->convert($files);
// 编译所有 C++ 文件
$objectFiles = $translator->compile($sourceFiles);
// 连接所有目标文件,生成可执行文件
$translator->build($objectFiles);
}

@ -690,7 +690,7 @@ class Type {
return true;
}
public function __toString() {
public function __toString() : string {
$char = $this->isIntersection ? '&' : '|';
return implode($char, array_map(
function ($type) { return $type->name; },

@ -119,15 +119,17 @@ foreach ($this->functions as $functionDef):
// clang-format on
PHP_MINIT_FUNCTION(<?=$this->getModuleName()?>) {
// class/interface class entries
<?=$this->genClassPropertyInit()?>
// register symbols
<?php
foreach ($this->registerSymbols as $registerSymbolFn):
?>
<?= $registerSymbolFn ?>(module_number);
<?php endforeach; ?>
zend_first_try {
// class/interface class entries
<?=$this->genClassPropertyInit()?>
// register symbols
<?php
foreach ($this->registerSymbols as $registerSymbolFn):
?>
<?= $registerSymbolFn ?>(module_number);
<?php endforeach; ?>
} zend_end_try();
return SUCCESS;
}

Loading…
Cancel
Save