fix(compiler): 修复异常处理机制和二进制文件命名问题

- 将 zend_try/zend_catch/zend_end_try 替换为标准的 try/catch 语法
- 移除旧的 throw_exception 函数并实现新的异常抛出机制
- 修改主函数中的异常处理逻辑以使用新的异常捕获方式
- 修复二进制文件名中连字符被替换为下划线的问题
- 更新异常处理流程以正确捕获和显示错误信息
pull/1/head
韩天峰 7 months ago
parent 8d8cb358d5
commit 1fa3b4596b
  1. 2
      run-tests.php
  2. 6
      src/Php/CompilerBase.php
  3. 17
      src/cpp/main.cc

@ -4204,7 +4204,7 @@ function compile_php_file(string $file): string
file_put_contents($file, "<?php\nfunction main() {\n" . substr($data, 5, -2) . "\n}\n"); file_put_contents($file, "<?php\nfunction main() {\n" . substr($data, 5, -2) . "\n}\n");
} }
system('./bin/compiler.php ' . $file); system('./bin/compiler.php ' . $file);
$binary_file = basename($file, '.php'); $binary_file = str_replace('-', '_', basename($file, '.php'));
if (!file_exists($binary_file)) { if (!file_exists($binary_file)) {
throw new Exception('Compilation failed'); throw new Exception('Compilation failed');
} }

@ -2624,7 +2624,7 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function parseTryCatch(mixed $v): string protected function parseTryCatch(mixed $v): string
{ {
$code = $this->parseBeforeStmtLines() . PHP_EOL; $code = $this->parseBeforeStmtLines() . PHP_EOL;
$code .= 'zend_try {'; $code .= 'try {';
$stmts = $v->stmts; $stmts = $v->stmts;
$code .= PHP_EOL; $code .= PHP_EOL;
@ -2639,7 +2639,7 @@ class CompilerBase extends \PhpAot\Core\Translator
$exVar = $this->genTmpVarName(); $exVar = $this->genTmpVarName();
$this->addLocalVar($exVar, self::TYPE_OBJECT); $this->addLocalVar($exVar, self::TYPE_OBJECT);
$code .= 'zend_catch {' . PHP_EOL; $code .= 'catch(zend_object *_ex) {' . PHP_EOL;
if ($catches) { if ($catches) {
$code .= $this->getIndent() . $exVar . ' = php::catchException();' . PHP_EOL; $code .= $this->getIndent() . $exVar . ' = php::catchException();' . PHP_EOL;
$this->indentLevel++; $this->indentLevel++;
@ -2648,7 +2648,7 @@ class CompilerBase extends \PhpAot\Core\Translator
} }
$this->indentLevel--; $this->indentLevel--;
} }
$code .= '}' . PHP_EOL . 'zend_end_try();' . PHP_EOL; $code .= '}' . PHP_EOL;
if ($finally) { if ($finally) {
$code .= $this->parseStmts($finally->stmts); $code .= $this->parseStmts($finally->stmts);
$code .= PHP_EOL; $code .= PHP_EOL;

@ -11,13 +11,9 @@ extern void php_app_init();
extern void php_app_clean(); extern void php_app_clean();
extern zend_module_entry *php_embed_get_module(); 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) { int main(int cpp_argc, char **cpp_argv) {
php_embed_init(cpp_argc, 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(); zend_module_entry *module = php_embed_get_module();
if (zend_register_module_ex(module, MODULE_PERSISTENT) == NULL) { if (zend_register_module_ex(module, MODULE_PERSISTENT) == NULL) {
@ -34,19 +30,14 @@ int main(int cpp_argc, char **cpp_argv) {
#if PPROF_ON #if PPROF_ON
ProfilerStart("profile.out"); ProfilerStart("profile.out");
#endif #endif
zend_first_try { try {
php::request_init(); php::request_init();
php_app_init(); php_app_init();
php::eval("main();"); php::eval("main();");
} } catch (zend_object *e) {
zend_catch {
rc = EG(exit_status); rc = EG(exit_status);
if (EG(exception)) { zend_exception_error(e, E_ERROR);
zend_exception_error(EG(exception), E_ERROR);
zend_clear_exception();
}
} }
zend_end_try();
#if PPROF_ON #if PPROF_ON
ProfilerStop(); ProfilerStop();
#endif #endif

Loading…
Cancel
Save