【发布时间】:2020-08-15 03:49:13
【问题描述】:
我正在开发一个 Rust 程序,我想在运行时使用 WebAssembly 作为脚本语言来驱动其行为。我们假设脚本也是用 Rust 编写的。我已经阅读了 Wasmer、Wasmtime 和 Lucet 的教程,虽然导出/导入的函数可以作为参数并返回 WASM 原始类型,但似乎没有简单/无痛的解决方案可以在 Rust 二进制文件和 WASM 脚本之间传递任意数据在其中运行。特别是字符串,必须由manually passing a primitive as a sort of pointer 处理到运行时的线性内存和长度。
wasm-bindgen 似乎可以解决我的问题,但它只针对浏览器中的 WASM-JS 通信。 Wasmer 和 Wasmtime 有 *-interface-types crates,但它们没有教程,并且是实验性的,可能会发生变化。我不需要通过structs,但我想实现这样的目标:
// in the host program
let argument: &[u8] = &[0, 1... n]; // dumb byte slice
let guest_result: Vec<u8> = wasm_runtime.call("guest_function", &[argument]);
// in the guest WASM library/script
let data_from_host: Vec<u8> = imported_function(some_other_bytes);
只需通过和返回Vec<u8>s 或&[u8]s 两种方式就足够了,我可以使用bincode 或其他方式反序列化它们。有没有人做到这一点?我可以像使用字符串一样共享线性内存,但这似乎不安全,尤其是在多个线程上使用多个脚本时。
tl;dr 我想使用 WASM 作为脚本语言并与 &[u8] 或 Vec<u8> 对话,但没有找到任何简单的方法。
【问题讨论】:
标签: types rust scripting communication webassembly