因此,我在生成的.wast文件中有以下导入(免责声明:我没有亲自编写wasm文件):
(import "index" "bigDecimal.fromString" (func $fimport$1 (param i32) (result i32)))我需要用Rust编写主机导入函数。我不能在Rust中使用&str,但是导入也需要一个i32。我猜我需要传递一个指向字符串的指针,定义为Rust?谁能给我指明正确的方向吗?这是在洗衣时间做的例子吗?
let from_string = Func::wrap(&store, |a: i32| {
println!("a={}", a);
});提前一吨谢谢!
发布于 2022-05-08 14:00:26
您说得对,参数a是指向Wasm模块内存中的字符串的指针。您可以通过wasmtime::Memory::data_ptr(...)函数访问它。要从调用方获得内存,可以向闭包中添加一个类型为wasmtime::Caller的参数。此参数不能出现在wasm模块函数签名中,而只能出现在主机函数签名中。
我希望这个简短的例子能有所帮助:
let read_wasm_string_func = Func::wrap(&store, |mut caller: Caller<'_, WasiCtx>, ptr_wasm: i32| -> i32 {
let memory = caller.get_export("memory").unwrap().into_memory().unwrap();
unsafe {
let ptr_native = memory.data_ptr(&caller).offset(ptr_wasm as isize);
// Do something with the pointer to turn it into a number
// return int
return num;
}
});https://stackoverflow.com/questions/67434474
复制相似问题