纯 BT 扩展
纯 BT 扩展
功能
纯 BT 扩展使用 BT 源码作为后端入口,适合封装普通脚本逻辑、业务规则和无需外部编译工具的能力。它的 manifest.kind 为 bt,manifest.abi 为 bts-bt-1。
语法
纯 BT 扩展的入口通常是 src/lib.bt:
{ "kind": "bt", "abi": "bts-bt-1", "entry": "src/lib.bt" }
加载时,宿主会解析并编译入口源码,但不会执行普通顶层逻辑。入口顶层只允许:
fn 声明 class 声明 空语句 大写常量名 = 字面量
不要在纯 BT 扩展入口顶层写 include、普通赋值、循环、I/O 调用、return 或 throw。
参数
入口函数和公开方法的参数数量必须和 bindings.json 一致。参数名由源码自己使用,参数类型由 bindings 声明并由运行时在调用边界检查。
源码示例
class Calc { value_num: 0 new(value) { this.value_num = value this } pub add(value) { this.value_num = this.value_num + value this } pub value() { this.value_num } pub close() { true } } fn calc(value) { Calc::new(value) }
对应规则:
| bindings 声明 | 纯 BT 源码要求 |
|---|---|
functions[].name = "calc" | 入口源码中必须有同名 fn calc(...)。 |
objects[].name = "Calc" | 入口源码中必须有同名 class Calc。 |
methods[].name = "add" | Calc 中必须有同名 pub add(...)。 |
| 参数数量 | 源码参数数量必须和 bindings 一致。 |
返回值
纯 BT 扩展返回原始类型时,运行时会检查实际返回值是否和 bindings 声明一致。返回对象类型时,必须返回普通对象或类实例。
value = calc(2).add(8).value() // 输出:10 print value
注意事项
- 类方法如果要对脚本公开,必须使用
pub。 - 返回链式对象的方法通常返回
this。 - 返回类型写成对象类型名时,该对象类型必须在
bindings.objects中声明。 - 纯 BT 扩展适合轻量业务逻辑;需要 Rust 生态、WASI 或更独立的状态管理时,应使用 WASM 扩展。