From 1fa3b4596bfad2bc323b2020b9a6dca842c40a70 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 28 Jan 2026 15:59:55 +0800 Subject: [PATCH] =?UTF-8?q?fix(compiler):=20=E4=BF=AE=E5=A4=8D=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86=E6=9C=BA=E5=88=B6=E5=92=8C=E4=BA=8C?= =?UTF-8?q?=E8=BF=9B=E5=88=B6=E6=96=87=E4=BB=B6=E5=91=BD=E5=90=8D=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 zend_try/zend_catch/zend_end_try 替换为标准的 try/catch 语法 - 移除旧的 throw_exception 函数并实现新的异常抛出机制 - 修改主函数中的异常处理逻辑以使用新的异常捕获方式 - 修复二进制文件名中连字符被替换为下划线的问题 - 更新异常处理流程以正确捕获和显示错误信息 --- run-tests.php | 2 +- src/Php/CompilerBase.php | 6 +++--- src/cpp/main.cc | 17 ++++------------- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/run-tests.php b/run-tests.php index d9019f04..99771d4e 100755 --- a/run-tests.php +++ b/run-tests.php @@ -4204,7 +4204,7 @@ function compile_php_file(string $file): string file_put_contents($file, "parseBeforeStmtLines() . PHP_EOL; - $code .= 'zend_try {'; + $code .= 'try {'; $stmts = $v->stmts; $code .= PHP_EOL; @@ -2639,7 +2639,7 @@ class CompilerBase extends \PhpAot\Core\Translator $exVar = $this->genTmpVarName(); $this->addLocalVar($exVar, self::TYPE_OBJECT); - $code .= 'zend_catch {' . PHP_EOL; + $code .= 'catch(zend_object *_ex) {' . PHP_EOL; if ($catches) { $code .= $this->getIndent() . $exVar . ' = php::catchException();' . PHP_EOL; $this->indentLevel++; @@ -2648,7 +2648,7 @@ class CompilerBase extends \PhpAot\Core\Translator } $this->indentLevel--; } - $code .= '}' . PHP_EOL . 'zend_end_try();' . PHP_EOL; + $code .= '}' . PHP_EOL; if ($finally) { $code .= $this->parseStmts($finally->stmts); $code .= PHP_EOL; diff --git a/src/cpp/main.cc b/src/cpp/main.cc index 56c81119..30450089 100644 --- a/src/cpp/main.cc +++ b/src/cpp/main.cc @@ -11,13 +11,9 @@ extern void php_app_init(); extern void php_app_clean(); extern zend_module_entry *php_embed_get_module(); -static void throw_exception(zend_object *ex) { - zend_bailout(); -} - int main(int cpp_argc, char **cpp_argv) { php_embed_init(cpp_argc, cpp_argv); - zend_throw_exception_hook = throw_exception; + php::throw_impl = [](zend_object *ex) { throw ex; }; zend_module_entry *module = php_embed_get_module(); if (zend_register_module_ex(module, MODULE_PERSISTENT) == NULL) { @@ -34,19 +30,14 @@ int main(int cpp_argc, char **cpp_argv) { #if PPROF_ON ProfilerStart("profile.out"); #endif - zend_first_try { + try { php::request_init(); php_app_init(); php::eval("main();"); - } - zend_catch { + } catch (zend_object *e) { rc = EG(exit_status); - if (EG(exception)) { - zend_exception_error(EG(exception), E_ERROR); - zend_clear_exception(); - } + zend_exception_error(e, E_ERROR); } - zend_end_try(); #if PPROF_ON ProfilerStop(); #endif