name: FFI 设计 description: 设计和实现与本地代码交互的外部函数接口的专业技能 category: 互操作性 allowed-tools:
- 读取
- 写入
- 编辑
- 全局匹配
- 文本搜索
- Bash
FFI 设计技能
概述
设计和实现与本地代码交互的外部函数接口的专业技能。
能力
- 设计 FFI 声明语法
- 实现语言间的类型编组(封送)
- 处理 C 调用约定(cdecl、stdcall、fastcall)
- 实现回调支持(本地代码调用托管代码)
- 处理字符串编码转换
- 实现结构体布局匹配(填充、对齐)
- 设计内存所有权转移规则
- 支持动态库加载
目标流程
- ffi-implementation.js
- interpreter-implementation.js
- bytecode-vm-implementation.js
- code-generation-llvm.js
依赖项
- libffi
- 平台 ABI 文档
使用指南
- 安全性:围绕不安全的 FFI 调用设计安全的包装器。
- 编组:明确记录类型编组规则。
- 所有权:在 FFI 边界明确内存所有权。
- 回调:谨慎处理回调的生命周期。
- 字符串:正确处理字符串编码转换(UTF-8、UTF-16)。
输出模式
{
"type": "object",
"properties": {
"callingConventions": {
"type": "array",
"items": { "type": "string" }
},
"marshalingRules": {
"type": "array",
"items": {
"type": "object",
"properties": {
"sourceType": { "type": "string" },
"targetType": { "type": "string" }
}
}
},
"callbackSupport": { "type": "boolean" },
"generatedFiles": {
"type": "array",
"items": { "type": "string" }
}
}
}