79145325

Date: 2024-10-31 15:34:01
Score: 1
Natty:
Report link

This issue can help: https://github.com/bytecodealliance/wasmtime-go/issues/80

And yet another example: https://github.com/bytecodealliance/wasmtime-go/blob/a1a8116cf3965d0e228229f3b3a497ba6da6a9b7/func_test.go#L422

You should use wasmtime.Linker.DefineFunc() to link host-func by name. Something like this:

...
linker := wasmtime.NewLinker(engine)
err := linker.DefineWasi()
if err != nil {
    return err
}
store := wasmtime.NewStore(engine)
wasiConfig := wasmtime.NewWasiConfig()
...
store.SetWasi(wasiConfig)
err = linker.DefineFunc(store, "env", "HostFunc", func() {
    fmt.Println("HOSTCALL!!!")
})
if err != nil {
    return err
}
instance, err := linker.Instantiate(store, module)
if err != nil {
    return err
}
...

And in the guest:

package main

//export HostFunc
func HostFunc()

func main() {
    HostFunc()
}
Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: ants