From 9744cc9e86ecd74e7846926b9520e986dd0398d0 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Fri, 17 Apr 2026 17:30:48 +0800 Subject: [PATCH] =?UTF-8?q?feat(build):=20=E6=B7=BB=E5=8A=A0=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=99=A8=E6=9E=84=E5=BB=BA=E5=8A=9F=E8=83=BD=E5=B9=B6?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=A1=B9=E7=9B=AE=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 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 示例文件 --- examples/error/impl.php | 12 ++++++++++++ project.yml | 8 +++++--- src/Php/Symbol.php | 5 +++++ src/Php/Translator.php | 3 ++- src/compiler-build.php | 26 ++++++++++++++++++++++++++ src/gen_stub.php | 2 +- src/template/extension.cc.php | 20 +++++++++++--------- 7 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 examples/error/impl.php create mode 100644 src/compiler-build.php diff --git a/examples/error/impl.php b/examples/error/impl.php new file mode 100644 index 00000000..5d230c7c --- /dev/null +++ b/examples/error/impl.php @@ -0,0 +1,12 @@ +foo(); +} \ No newline at end of file diff --git a/project.yml b/project.yml index aba23544..59ebd5c0 100644 --- a/project.yml +++ b/project.yml @@ -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 diff --git a/src/Php/Symbol.php b/src/Php/Symbol.php index 6ce94c11..df5d3200 100644 --- a/src/Php/Symbol.php +++ b/src/Php/Symbol.php @@ -44,4 +44,9 @@ class Symbol { return 'php::constant'; } + + public static function getClassEntrySafe(): string + { + return 'php::getClassEntrySafe'; + } } diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 371702dc..e81d5476 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -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_')) . '"', ]; } diff --git a/src/compiler-build.php b/src/compiler-build.php new file mode 100644 index 00000000..2be1eaf8 --- /dev/null +++ b/src/compiler-build.php @@ -0,0 +1,26 @@ +setIndent(' '); + // 扫描所有 PHP 文件,预处理 + $files = $translator->prepare($path); + // 生成 C++ 文件 + $sourceFiles = $translator->convert($files); + // 编译所有 C++ 文件 + $objectFiles = $translator->compile($sourceFiles); + // 连接所有目标文件,生成可执行文件 + $translator->build($objectFiles); +} diff --git a/src/gen_stub.php b/src/gen_stub.php index 4ad837a2..13debda9 100755 --- a/src/gen_stub.php +++ b/src/gen_stub.php @@ -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; }, diff --git a/src/template/extension.cc.php b/src/template/extension.cc.php index c3d93de6..2fa4ed5c 100644 --- a/src/template/extension.cc.php +++ b/src/template/extension.cc.php @@ -119,15 +119,17 @@ foreach ($this->functions as $functionDef): // clang-format on PHP_MINIT_FUNCTION(getModuleName()?>) { - // class/interface class entries - genClassPropertyInit()?> - -// register symbols -registerSymbols as $registerSymbolFn): -?> - (module_number); - + zend_first_try { + // class/interface class entries + genClassPropertyInit()?> + + // register symbols + registerSymbols as $registerSymbolFn): + ?> + (module_number); + + } zend_end_try(); return SUCCESS; }