Skip to content

WebAssembly

Hajime Hoshi edited this page Aug 26, 2018 · 28 revisions

As there are differences between the latest Go and Go 1.11 beta, I recommend to use the latest Go here.

The easiest way is to use WasmServe with Go 1.11 or later.

Note that you'd need the master branch of Ebiten (v1.7.x doesn't work with WebAssembly).

Get the latest Go

  1. Checkout the latest Go (https://go.googlesource.com/go)
  2. Compile the compiler by running ./make.bash

Compile your game

GOOS=js GOARCH=wasm /path/to/latest/gocode/bin/go build -o yourgame.wasm github.com/yourname/yourgame

Copy wasm_exec.js to execute the Wasm binary

cp /path/to/latest/gocode/misc/wasm_exec.js .

Create an HTML

<!DOCTYPE html>
<script src="wasm_exec.js"></script>
<script>
// Polyfill
if (!WebAssembly.instantiateStreaming) {
  WebAssembly.instantiateStreaming = async (resp, importObject) => {
    const source = await (await resp).arrayBuffer();
    return await WebAssembly.instantiate(source, importObject);
  };
}

const go = new Go();
WebAssembly.instantiateStreaming(fetch("yourgame.wasm"), go.importObject).then(result => {
  go.run(result.instance);
});
</script>

TBD

Clone this wiki locally