1 回答
TA贡献1878条经验 获得超4个赞
我花了一点时间来确切地理解wasmtime Go-package是如何工作的,但最终我解决了我这样做的问题:
func main() {
dir, err := ioutil.TempDir("", "out")
check(err)
defer os.RemoveAll(dir)
stdoutPath := filepath.Join(dir, "stdout")
engine := wasmtime.NewEngine()
store := wasmtime.NewStore(engine)
linker := wasmtime.NewLinker(store)
// Configure WASI imports to write stdout into a file.
wasiConfig := wasmtime.NewWasiConfig()
wasiConfig.SetStdoutFile(stdoutPath)
wasi, err := wasmtime.NewWasiInstance(store, wasiConfig, "wasi_snapshot_preview1")
check(err)
// Link WASI
err = linker.DefineWasi(wasi)
check(err)
// Create our module
module, err := wasmtime.NewModuleFromFile(store.Engine, "wasm_file.wasm")
check(err)
instance, err := linker.Instantiate(module)
check(err)
fn := instance.GetExport("greet").Func()
memory := instance.GetExport("memory").Memory()
alloc := instance.GetExport("my_alloc").Func()
// // string for alloc
size2 := int32(len([]byte("Elvis")))
// //Allocate memomory
ptr2, err := alloc.Call(size2)
pointer, _ := ptr2.(int32)
buf := memory.UnsafeData()
for i, v := range []byte("Elvis") {
buf[pointer+int32(i)] = v
}
// Use string func
_, err = fn.Call(pointer, size2 )
check(err)
// Print WASM stdout
out, err := ioutil.ReadFile(stdoutPath)
check(err)
fmt.Print(string(out))
}
- 1 回答
- 0 关注
- 177 浏览
添加回答
举报