Skip to content

misc/wasm: add support for loading wasm files inside WebWorker #26192

@leidegre

Description

@leidegre

What version of Go are you using (go version)?

go1.11beta1 windows/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

set GOARCH=wasm
set GOBIN=
set GOCACHE=C:\Users\leidegre\AppData\Local\go-build
set GOEXE=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=js
set GOPATH=C:\Users\leidegre\go
set GOPROXY=
set GORACE=
set GOROOT=C:\Users\leidegre\sdk\go1.11beta1
set GOTMPDIR=
set GOTOOLDIR=C:\Users\leidegre\sdk\go1.11beta1\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-fPIC -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\leidegre\AppData\Local\Temp\go-build848467850=/tmp/go-build -gno-record-gcc-switches
set VGOMODROOT=

What did you do?

I tried to run the compiled code in a WebWorker process. It did not work at first but through a series of changes I got it to work, the issue was primarily with the bootstrapping process. Tested in Firefox, Chrome and Edge.

What did you expect to see?

It would be nice if the bootstrapping code from misc/wasm worked out of the box for WebWorker processes.

What did you see instead?

Errors that globals, like window did not exist.

Suggested change

The offending line https://github.com/golang/go/blob/master/misc/wasm/wasm_exec.js#L30 can be changed to something like this to work around the issue.

if (typeof window !== "undefined") {
  window.global = window;
} else if (typeof self !== "undefined") {
  self.global = self;
} else {
  throw new Error("cannot export Go")
}

self is the equivalent of window when running within a WebWorker process.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureRequestIssues asking for a new feature that does not need a proposal.FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.arch-wasmWebAssembly issues

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions