WebAssembly

WebAssembly

Zig supports building for WebAssembly out of the box. There is also a specialized std.heap.wasm_allocator memory allocator for WebAssembly environments.

Freestanding

For host environments like the web browser and nodejs, build as a library using the freestanding OS target. Here's an example of running Zig code compiled to WebAssembly with nodejs.

math.zig

extern fn print(i32) void;

export fn add(a: i32, b: i32) void {
    print(a + b);
}
``` ```
$ zig build-lib math.zig -target wasm32-freestanding

test.js

const fs = require('fs');
const source = fs.readFileSync("./math.wasm");
const typedArray = new Uint8Array(source);

WebAssembly.instantiate(typedArray, {
  env: {
    print: (result) => { console.log(`The result is ${result}`); }
  }}).then(result => {
  const add = result.instance.exports.add;
  add(1, 2);
});
``` ```
$ node test.js
The result is 3

WASI

Zig's support for WebAssembly System Interface (WASI) is under active development. Example of using the standard library and reading command line arguments:

wasi.zig

const std = @import("std");

pub fn main() !void {
    const args = try std.process.argsAlloc(std.heap.wasm_allocator);
    defer std.process.argsFree(std.heap.wasm_allocator, args);

    for (args) |arg, i| {
        std.debug.warn("{}: {}\n", i, arg);
    }
}
``` ```
$ zig build-exe wasi.zig -target wasm32-wasi
``` ```
$ wasmer run wasi.wasm 123 hello
0: wasi.wasm
1: 123
2: hello