How do I include a wasm file compiled by golang in a chrome extension?
Asked Answered
F

1

10

I've tried everything in here:

Golang to wasm compilation

And I can't get past:

WebAssembly.instantiate(buffer, {wasi_snapshot_preview1: ""})
WebAssembly.instantiate(buffer, {go: {debug: ""}})

// (i'm making them empty strings just to show I need to add the key but obviously they are not strings.)

i.e. I get errors like:

Failed to load WebAssembly module: TypeError: WebAssembly.instantiate(): Import #0 module="wasi_snapshot_preview1" error: module is not an object or function

or module debug or runtime.resetMemoryDataView or everything in wasm_exec.js

But if I use the simple.wasm from:

https://github.com/inflatablegrade/Extension-with-WASM

it works! That wasm must have been compiled by c or rust and NOT golang? Is there a way to make this work from go?

P.S. that Extension-with-WASM is manifest 2.0 but I got it work with 3.0:

https://github.com/inflatablegrade/Extension-with-WASM/issues/1

Friday answered 8/7, 2023 at 20:39 Comment(1)
I'm not totally sure about this but I believe the WASI snapshot error from Go lang could be because of Go still not supporting WASI (Go 1.21 will support this, in this August tip.golang.org/doc/go1.21).Periwig
M
0

try compiling with -Oz flag GOOS=js GOARCH=wasm go build -o main.wasm -ldflags="-s -w" -gcflags="-trimpath=${PWD}" -trimpath .

and build GOOS=js GOARCH=wasm go build -o main.wasm -tags=js_wasm_profiling -ldflags="-s -w" -gcflags="-trimpath=${PWD}" -trimpath .

Magenmagena answered 10/7, 2023 at 21:8 Comment(5)
Failed to load WebAssembly module: TypeError: WebAssembly.instantiate(): Import #0 module="go" error: module is not an object or functionFriday
what version of go you have? run : go versionMagenmagena
go version go1.19.2 darwin/arm64, but I'll upgrade to latest now...Friday
same error with go version go1.20.5 darwin/arm64Friday
see golangbot.com/webassembly-using-go you import correctly syscall/js?Magenmagena

© 2022 - 2024 — McMap. All rights reserved.