wasm4: Can't run Go template

Steps to reproduce:

  1. Create new Go template with w4 new foobar --go
  2. Build it: make
  3. Run it: w4 run build/cart.wasm
  4. See in the console:
LinkError: WebAssembly.instantiate(): Import #0 module="env" function="tinygo_getCurrentStackPointer" error: function import requires a callable

Environment:

  • Fedora 37
  • wasm4 version 2.5.3
  • tinygo version 0.26.0 linux/amd64 (using go version go1.19.3 and LLVM version 15.0.0)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 19 (11 by maintainers)

Most upvoted comments

It seems that the problem with the latest versions of Go and TinyGo is still in the incorrect target.json. With these settings, Wasm-4 works for me without errors. In my case, versions Go 1.22.2 and TinyGo 0.31.2

target.json content: { “llvm-target”: “wasm32-unknown-unknown”, “cpu”: “generic”, “features”: “+mutable-globals,+nontrapping-fptoint,+sign-ext,+bulk-memory”, “build-tags”: [ “tinygo.wasm”, “wasm_unknown” ], “goos”: “linux”, “goarch”: “arm”, “linker”: “wasm-ld”, “rtlib”: “compiler-rt”, “scheduler”: “none”, “cflags”: [ “-mno-bulk-memory”, “-mnontrapping-fptoint”, “-msign-ext” ], “ldflags”: [ “–allow-undefined”, “–no-demangle”, “–import-memory”, “–initial-memory=65536”, “–max-memory=65536”, “–stack-first”, “–no-entry”, “-zstack-size=14752” ] }

ok so for now here is how to use wasm-4 with go

  1. installing proper version of tinygo (0.21.0) https://github.com/tinygo-org/tinygo/releases/tag/v0.21.0 You will need to ensure that the path to the tinygo executable file is in your PATH variable.

  2. installing proper go version (1.17) I use gvm. Atm I have both go1.17 and go1.22.4 installed with gvm.

  3. After creating the w4 new --go project. You should write in terminal gvm use go1.17 and for double check you write tinygo version which must say: tinygo version 0.21.0 linux/amd64 (using go version go1.17 and LLVM version 11.0.0)

After all of these, everything should work

BigAngryBob ran into other issues with the tinygo. Tinygo was invoking wasm-opt on its own, but not providing --enable-bulk-memory.

[wasm-validator error in function 0] unexpected false: Bulk memory operations require bulk memory [--enable-bulk-memory], on

His versions:

% w4 --version
2.5.4
% tinygo version
tinygo version 0.27.0 darwin/amd64 (using go version go1.20.4 and LLVM version 15.0.0)
% wasm-opt --version
wasm-opt version 113

This is still an issue, was trying the same thing and same error on the console. Any leads?

As @DenialAdams mentioned, tinygo is internally calling wasm-opt with its own flags. This error happens during the tinygo compilation phase, before the wasm-opt post-build pass.

I was able to compile successfully by replacing the template’s target.json with the one from the tinygo project: https://github.com/tinygo-org/tinygo/blob/release/targets/wasm.json (this one contains the -mbulk-memory flag, which bypasses the current compiling error)

However, I am now facing the issue: Uncaught (in promise) TypeError: import object field 'wasi_snapshot_preview1' is not an Object when I open the game in the browser through w4 run build/cart.wasm. Not sure how to fix that one.

I’m using go 1.21.1, tinygo 0.30.0, and w4 2.5.4.

tl;dr

  1. Replace contents of target.json in template with https://github.com/tinygo-org/tinygo/blob/release/targets/wasm.json
  2. Remove wasm-opt steps in Makefile, because tinygo already calls wasm-opt internally
  3. Since tinygo runs wasm-opt internally, the user needs to have wasm-opt installed 3.1) If tinygo was installed through a package manager, then a separate install of binaryen is needed to get wasm-opt 3.2) If tinygo was installed by downloading it from their GitHub releases, then wasm-opt already comes included in their bin folder. tinygo will automatically find this wasm-opt in their own code: https://github.com/tinygo-org/tinygo/blob/731532cd2b6353b60b443343b51296ec0fafae09/goenv/goenv.go#L143-L165

Hi, thanks for reporting this!

I’m actually not able to repro, but I wonder if something changed with the tinygo’s default wasm.json in 0.26, and we need to mirror any changes into the wasm4 project’s target.json. Eg: default-stack-size looks suspicious.