Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No compiled main function from file called #212

Open
arejula27 opened this issue Feb 24, 2024 · 4 comments
Open

No compiled main function from file called #212

arejula27 opened this issue Feb 24, 2024 · 4 comments

Comments

@arejula27
Copy link

arejula27 commented Feb 24, 2024

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?

@alexcrichton
Copy link
Member

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.

@arejula27
Copy link
Author

arejula27 commented Feb 26, 2024

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

@arejula27
Copy link
Author

arejula27 commented Feb 26, 2024

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

@alexcrichton
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants