GithubHelp home page GithubHelp logo

philippgille / go-wasm Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 29 KB

Examples of how to work with WebAssembly and WASI in Go

License: GNU Affero General Public License v3.0

HTML 36.61% JavaScript 24.10% Go 39.29%
browser bun deno go golang node nodejs wasi wasm wasmer

go-wasm's Introduction

go-wasm

This repo contains examples of how to work with WebAssembly and WASI in the Go ecosystem.

Regular WASM is meant to be executed inside a browser (like Chrome or Firefox) or JavaScript runtime (like Node, Deno or Bun). As such it only has access to web APIs or runtime-specific APIs.

WASI on the other hand, as the name "WebAssembly System Interface" suggests, provides more direct access to the host system, like to the filesystem or network sockets. Similar to how the browser executes JavaScript and WebAssembly in a sandbox, WASI runtimes also execute WASI programs in a sandbox, requiring explicit permissions for things like file access.

Table of Contents

WASM

This works with Go versions prior to 1.21, as it's "regular" WASM, not WASI.

Compile the Go program to WebAssembly: GOOS=js GOARCH=wasm go build -o go-wasm.wasm

Execute WASM directly

Go WASM execution requires a Go-specific wrapper, wasm_exec.js. This can then be executed by any JavaScript runtime, in or outside the browser. The Go project provides this in $(go env GOROOT)/misc/wasm/wasm_exec.js.

The non-browser runtimes differ in how files are read from the host's filesystem, so for reading the wasm_exec.js file, in addition to this file itself, we need to have another runtime-specific wrapper.

Browser

For running the WASM program in the browser, you can serve the files with any web server that supports the application/wasm MIME type. You can use a regular Go server (http.ListenAndServe(...)), Caddy, or others.

cp go-wasm.wasm browser
cp $(go env GOROOT)/misc/wasm/wasm_exec.js browser/wasm_exec.js
cd browser
caddy file-server --listen :2015

Then in your browser visit http://localhost:2015.

Tested with Firefox 116.0.3

Node

cp go-wasm.wasm node
cp $(go env GOROOT)/misc/wasm/wasm_exec.js deno/wasm_exec.js
cd node
node node.js

The Go authors also provide a convenience script for this, so instead of copying the wasm_exec.js and writing our own node.js wrapper script, we can simply run:

$(go env GOROOT)/misc/wasm/go_js_wasm_exec go-wasm.wasm

Tested with Node v18.17.1

Deno

cp go-wasm.wasm deno
cp $(go env GOROOT)/misc/wasm/wasm_exec.js deno/wasm_exec.js
cd deno
deno run --allow-read=./go-wasm.wasm deno.js

โ„น๏ธ: Deno runs programs in a sandbox by default, so for reading the WASM file we need to grant the permission accordingly.

Tested with Deno 1.36.1

Bun

cp go-wasm.wasm bun
cp $(go env GOROOT)/misc/wasm/wasm_exec.js deno/wasm_exec.js
cd bun
bun bun.js

Tested with Bun 0.7.3

Embed WASM in Go

๐Ÿšง TODO

WASI

Go 1.21 has official WASI preview1 support, so we can compile Go programs to WASM that execute outside of a browser / JavaScript runtime.

Compile Go program to WASI: GOOS=wasip1 GOARCH=wasm go build -o go-wasi.wasm

Execute WASI directly

Any WASM runtime with WASI support, on any OS, can then execute this file:

Embed WASI in Go

Or we run the WASI program with an embeddable WASM runtime from within another Go program. Most of the โฌ†๏ธ listed runtimes also have Go libraries for embedding.

wazero

cp go-wasi.wasm embed-wasi/wazero
cd embed-wasi/wazero
go run .

wasmtime

๐Ÿšง TODO: https://github.com/bytecodealliance/wasmtime-go

Wasmer

๐Ÿšง TODO: https://github.com/wasmerio/wasmer-go

go-wasm's People

Contributors

philippgille avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.