| Crates.io | wit-bindgen-mbt |
| lib.rs | wit-bindgen-mbt |
| version | 0.1.0 |
| created_at | 2024-02-04 20:27:49.330319+00 |
| updated_at | 2024-02-04 20:27:49.330319+00 |
| description | The MoonBit bindings generator for WIT and the component model. |
| homepage | https://gitee.com/fantix/componentize-mbt |
| repository | https://gitee.com/fantix/componentize-mbt |
| max_upload_size | |
| id | 1126633 |
| size | 138,408 |
在 MoonBit 正式支持 component model 之前,临时用于构建 component 的命令行工具。
wit 文件夹;wit 文件夹下,按需创建 .wit 文件;wit/deps.toml 文件,安装 cargo install wit-deps-cli
进行依赖管理,比如引入 WASI 的接口;cargo run -- bindgen wit -w ... 生成 WIT 对应的 MoonBit 绑定代码;init_guest() 安置实现实例;moon build --output-wat 编译出 WAT(这里之所以不用 wasm,是利用了 MoonBit
的一个隐藏缺陷,生成 WAT 时并不会检查 ABI import,便于我们下一步链接 component 相关的
wasm 代码);cargo run -- componentize wit --wat ...,将 WAT 包装成 component WASM。bind-gen读取 WIT 文件,生成 MoonBit 的 binding 代码。
pub(readonly) struct Wasi {
clocks : WasiClocks
}
pub(readonly) struct WasiClocks {
monotonic_clock : WasiClocksMonotonicClock
}
pub(readonly) type WasiClocksMonotonicClock Unit
pub fn now(self : WasiClocksMonotonicClock) -> Int64 {
// Impl
0L
}
pub let wasi : Wasi = Wasi::{
clocks: WasiClocks::{ monotonic_clock: WasiClocksMonotonicClock(()) },
}
test "use import" {
if wasi.clocks.monotonic_clock.now() != 0L {
abort("failed")
}
}
pub trait ExportsWasiClocksMonotonicClock {
now(Self) -> Int64
}
struct GuestImpl {
mut wasi_clocks_monotonic_clock: Option[ExportsWasiClocksMonotonicClock]
} derive(Default)
let guest_impl: GuestImpl = GuestImpl::default()
pub fn init_guest[T: ExportsWasiClocksMonotonicClock](guest: T) {
guest_impl.wasi_clocks_monotonic_clock = Some(guest as ExportsWasiClocksMonotonicClock)
}
// export_name = "now"
pub fn __export_now() -> Int64 {
guest_impl.wasi_clocks_monotonic_clock.unwrap().now()
}
// 用户端代码
struct App {}
fn ExportsWasiClocksMonotonicClock::now(self: App) -> Int64 {
// Impl
0L
}
let app: App = App::{}
fn init {
init_guest(app)
}
test "extern call" {
if ffi_now() != 0L {
abort("failed")
}
}
componentize输入 WIT 和一个 .wat 文件,合成出符合
component model 规范的
.wasm 文件。实现流程:
moonbit.memory 重命名为 memory;export _start 修改为 start;.wasm 文件。