wasm4: Can't run Go template
Steps to reproduce:
- Create new Go template with
w4 new foobar --go - Build it:
make - Run it:
w4 run build/cart.wasm - 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)
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
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.
installing proper go version (1.17) I use gvm. Atm I have both go1.17 and go1.22.4 installed with gvm.
After creating the
w4 new --go project. You should write in terminalgvm use go1.17and for double check you writetinygo versionwhich 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], onHis versions:
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-optpost-build pass.I was able to compile successfully by replacing the template’s
target.jsonwith the one from the tinygo project: https://github.com/tinygo-org/tinygo/blob/release/targets/wasm.json (this one contains the-mbulk-memoryflag, 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 Objectwhen I open the game in the browser throughw4 run build/cart.wasm. Not sure how to fix that one.I’m using go
1.21.1, tinygo0.30.0, and w42.5.4.tl;dr
target.jsonin template with https://github.com/tinygo-org/tinygo/blob/release/targets/wasm.jsonwasm-optsteps in Makefile, because tinygo already callswasm-optinternallywasm-optinternally, the user needs to havewasm-optinstalled 3.1) If tinygo was installed through a package manager, then a separate install ofbinaryenis needed to getwasm-opt3.2) If tinygo was installed by downloading it from their GitHub releases, thenwasm-optalready comes included in theirbinfolder. tinygo will automatically find thiswasm-optin their own code: https://github.com/tinygo-org/tinygo/blob/731532cd2b6353b60b443343b51296ec0fafae09/goenv/goenv.go#L143-L165Hi, 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-sizelooks suspicious.