bytecodealliance/wasmtime-go

No compiled main function from file called

Opened this issue · 4 comments

I am trying to run a program compiled into wasm from go. In Rust exists a guide for it, however, I have not found it in Go.

I have created the following hello.go file

package main

import "fmt"

func main() {
	fmt.Println("Hello, World!")
}

And compile to hello.wasm using tiny go. Using wasmtime hello.wasm the correct output is expected.
Now i want to call it from my go code:

package main

import (
	"log"

	"github.com/bytecodealliance/wasmtime-go"
)

func main() {

	store := wasmtime.NewStore(wasmtime.NewEngine())

	
	module, err := wasmtime.NewModuleFromFile(store.Engine, "/home/arejula27/workspaces/go-lab/wasm/hello.wasm")
	if err != nil {
		panic(err)
	}
	linker := wasmtime.NewLinker(store.Engine)

	linker.DefineModule(store, "", module)
	hello, err := linker.GetDefault(store, "")
	if err != nil {
		panic(err)
	}
	_, err = hello.Call(store)
	if err != nil {
		log.Fatal(err)
	}

}

With this code, no output is generated. Do you know how I can do it correctly?

Thanks for the report, but I believe the issue here is that you're ignoring the error from the DefineModule function. That should be returning an error indicating that WASI has not been configured. Once you do that I believe you should be able to see the same output.

I Will check it, however is there any guide or example to follow?

The error was the one that you said:

panic: unknown import: wasi_snapshot_preview1::sched_yield has not been defined

goroutine 1 [running]:
main.main()
        /home/arejula27/workspaces/go-lab/wasm/main.go:21 +0x114
exit status 2

How can i configure WASI?
I have added:

dir, err := os.MkdirTemp("", "out")
	if err != nil {
		log.Fatal(err)
	}
	defer os.RemoveAll(dir)
	stdoutPath := filepath.Join(dir, "stdout")
	engine := wasmtime.NewEngine()
	wasiConfig := wasmtime.NewWasiConfig()
	wasiConfig.SetStdoutFile(stdoutPath)
	store := wasmtime.NewStore(engine)
	store.SetWasi(wasiConfig)

And it continue to fail

This isn't included in the documentation due to a bug but there's an example of WASI in the source.