TypePHP 编译器
https://swoole.com/aot/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
2.4 KiB
47 lines
2.4 KiB
<blog>
|
|
|
|
# 扩展模块生成
|
|
|
|
`genExtension()` 是流水线里最大的一块生成逻辑,产出 `extension-<target>.cc`——一个标准 Zend 扩展模块。它把前面收集的所有类、函数、常量、全局变量缝合进 Zend 的生命周期钩子(MINIT / MINIT / RINIT / RSHUTDOWN)。
|
|
|
|
Sources: [src/Translator.php](src/Translator.php#L739-L1105)
|
|
|
|
## 生成结构
|
|
|
|
```mermaid
|
|
flowchart TD
|
|
A["extension-<target>.cc"] --> B["全局变量 / 类表 / 函数表 / 属性表 声明"]
|
|
B --> C["php_get_class / php_get_func / php_get_method / php_get_prop"]
|
|
C --> D["ext_functions[] (ZEND_FE 列表)"]
|
|
D --> E["PHP_MINIT: 注册类/反射处理器/符号"]
|
|
E --> F["PHP_RINIT: php_app_init + eval(main)"]
|
|
F --> G["PHP_RSHUTDOWN: php_app_clean + request_shutdown"]
|
|
G --> H["zend_module_entry"]
|
|
```
|
|
|
|
Sources: [src/Translator.php](src/Translator.php#L769-L1087)
|
|
|
|
## 关键片段
|
|
|
|
- **符号解析函数**:`php_get_class` / `php_get_func` / `php_get_method` / `php_get_prop` 用缓存的 `class_map` / `func_map` / `prop_map` 把字符串名映射到运行时 `zend_class_entry*` / `zend_function*`,带 `UNEXPECTED` 快路径。
|
|
- **MINIT**:安装 Fiber/Generator 类、反射属性处理器,逐个注册类并设置属性处理器;最后调用各 `registerSymbols` 函数。
|
|
- **RINIT**:`php_app_init()` 注册常量、全局变量、静态属性、类数组常量;`bin` 模式下 `php::eval` 执行 `main()`(按 `$argc/$argv` 或空参两种签名)。
|
|
- **RSHUTDOWN**:`php_app_clean()` 清理全局变量、VAR 常量、类数组常量(含继承来的数组常量),`ext` 模式额外清空三张映射表以支持模块重载。
|
|
|
|
Sources: [src/Translator.php](src/Translator.php#L791-L837) · [src/Translator.php](src/Translator.php#L884-L1066)
|
|
|
|
## 模式差异
|
|
|
|
- `bin`:嵌入 `cli_set_process_title`,`RINIT` 直接跑 `main()`;
|
|
- `lib`:Linux 下导出 `save_ps_args` 占位符;`getModuleName()` 仍生成模块入口;
|
|
- `ext`:用 `ZEND_GET_MODULE()`,跳过 `main()`,RSHUTDOWN 清空映射表。
|
|
|
|
Sources: [src/Translator.php](src/Translator.php#L754-L767) · [src/Translator.php](src/Translator.php#L1090-L1096)
|
|
|
|
## 相关阅读
|
|
|
|
- 函数/类声明如何生成 → [函数与类生成](gen-functions-classes)
|
|
- 生成的 `.cc` 如何被编译/缓存 → [增量编译缓存](incremental-cache)
|
|
- 三种产物形态 → [编译模式](compilation-modes)
|
|
|
|
</blog>
|
|
|