wasmerio / wasmer-go

🐹🕸️ WebAssembly runtime for Go
https://pkg.go.dev/github.com/wasmerio/wasmer-go
MIT License
2.8k stars 161 forks source link

compile Go to WebAssembly and use it in the Wasmtime runtime #362

Open kwerner8 opened 1 year ago

kwerner8 commented 1 year ago

My goal is to compile a function written in Go to a WebAssembly Module and use it as an export in the Wasmtime runtime.

This is my Go code:

package main

func main() {
}

//adder
func adder(x,y int) int {
return x + y
}

I compiled it with "tinygo build -o test.wasm -target wasi ./main.go" (based on the FAQ example https://github.com/wasmerio/wasmer-go and the export example from tinygo https://github.com/tinygo-org/tinygo/blob/release/src/examples/wasm/export/wasm.go)

This creates a wasm file. In order to use it in Wasmtime I need to compile it to the .wat format. I normally use wasm2wat but if in this case I get an error message: error: invalid section code 12

Do you know how to solve this?

A workaround for this is open the test.wasm file in VS Code and then it automatically shows it in the wat format. If I try to call this .wat file in Wasmtime I get the following error message:

error: failed to parse wat
expected an instruction
       --> <anon>:3195:48
     |
3195 |                           call_indirect (type $t2) $T0
     |                                                    ^

Do you know how to solve this?

Is there a different way to use functions written in Go as an exported function in Wasmtime? I am open for any help.