tessi / wasmex

Execute WebAssembly from Elixir
MIT License
528 stars 27 forks source link

Unable to run go code compiled to WASM #520

Closed yasoob closed 5 months ago

yasoob commented 5 months ago

Hi!

I tried to compile this go code to wasm:

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello from Go!")
}

Using this command:

GOOS=wasip1 GOARCH=wasm go build -o main.wasm

This produced a WASM binary that I was able to run using wasmtime:

$ wasmtime main.wasm
Hello from Go!

However, I am unable to run this using Wasmex:

iex(11)> bytes = File.read!("./native/main.wasm")
<<0, 97, 115, 109, 1, 0, 0, 0, 0, 242, 128, 128, 128, 0, 10, 103, 111, 58, 98,
  117, 105, 108, 100, 105, 100, 255, 32, 71, 111, 32, 98, 117, 105, 108, 100,
  32, 73, 68, 58, 32, 34, 102, 77, 66, 84, 53, 95, 69, 83, 122, ...>>
iex(12)> {:ok, pid} = Wasmex.start_link(%{bytes: bytes}) 
** (MatchError) no match of right hand side value: {:error, "unknown import: `wasi_snapshot_preview1::sched_yield` has not been defined"}
    (stdlib 5.2) erl_eval.erl:498: :erl_eval.expr/6
    iex:12: (file)

I tried to compile the Go code using tinygo as well. I was able to run the output using Wasmtime but got this error using Wasmex:

iex(12)> bytes = File.read!("./native/main.wasm")
<<0, 97, 115, 109, 1, 0, 0, 0, 1, 43, 8, 96, 1, 127, 0, 96, 0, 0, 96, 4, 127,
  127, 127, 127, 1, 127, 96, 2, 127, 127, 0, 96, 1, 127, 1, 127, 96, 2, 127,
  127, 1, 127, 96, 0, 1, 127, 96, 4, 127, 127, ...>>
iex(13)> {:ok, pid} = Wasmex.start_link(%{bytes: bytes}) # starts a GenServer running a Wasm instance
** (MatchError) no match of right hand side value: {:error, "unknown import: `wasi_snapshot_preview1::fd_write` has not been defined"}
    (stdlib 5.2) erl_eval.erl:498: :erl_eval.expr/6
    iex:13: (file)

My wasmex version is the latest one and I was compiling this using Go v1.21. Am I doing something wrong? :/

yasoob commented 5 months ago

I am sorry. I am dumb and new to WASM. I was able to resolve this by using this command:

{:ok, pid} = Wasmex.start_link(%{bytes: bytes, wasi: true})