绑定与工具链
目标:了解跨语言绑定的生成方式与常用工具。
常用工具
- wit-bindgen:多语言绑定生成器(Rust/TS/…)。
- cargo component:Rust 组件项目脚手架与构建。
- wasm-tools:查看/封装/组合组件(
component new/compose/wit
)。
典型流程
- 用 WIT 定义接口与 world;
- 通过语言工具生成绑定(或手写实现);
- 构建 core wasm →
wasm-tools component new
封装为 component; wasm-tools component wit
验证导入导出;- 在宿主(如 Wasmtime 组件 API)加载并调用。
示例:
- 组件化与组合:examples/ch05/components_composition
- HTTP 宿主调用组件:examples/ch06/component_http_host
注意事项
- 字符串/字节的所有权与拷贝次数;
- 大对象优先使用 record/扁平结构;
- 错误统一用
result<T,E>
或具名variant
; - 在 CI 固定工具版本,避免升级引入不兼容。
命令与示例输出
构建与封装:
cargo build -Zunstable-options --target wasm32-wasi --release
wasm-tools component new \
target/wasm32-wasi/release/my-demo.wasm \
-o target/my-demo.component.wasm
查看组件 WIT:
wasm-tools component wit target/my-demo.component.wasm
# 可能输出(节选):
# package demo:math
# world app {
# export demo:math/algebra@0.1.0;
# }
组合两个组件:
wasm-tools component compose \
--adapt some-adapter.wasm \
add.component.wasm inc.component.wasm \
-o glue.component.wasm