diff --git a/doc/langref.html.in b/doc/langref.html.in index d578be4ba8..3808ea706a 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -8434,6 +8434,8 @@ fn concat(allocator: *Allocator, a: []const u8, b: []const u8) ![]u8 {
  • Are you linking libc? In this case, {#syntax#}std.heap.c_allocator{#endsyntax#} is likely the right choice, at least for your main allocator.
  • +
  • Are you building for WebAssembly? In this case, {#syntax#}std.heap.wasm_allocator{#endsyntax#} is likely + the only possible choice for your main allocator.
  • Is the maximum number of bytes that you will need bounded by a number known at {#link|comptime#}? In this case, use {#syntax#}std.heap.FixedBufferAllocator{#endsyntax#} or @@ -8964,8 +8966,12 @@ all your base are belong to us {#header_close#} {#header_close#} {#header_open|WebAssembly#} +

    Zig supports building for WebAssembly out of the box. There is also a specialized {#syntax#}std.heap.wasm_allocator{#endsyntax#} + memory allocator for WebAssembly environments.

    {#header_open|Freestanding#} - {#code_begin|lib|wasm#} +

    For embedded 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.

    + {#code_begin|lib|math#} {#target_wasm#} extern fn print(i32) void; @@ -8974,7 +8980,22 @@ export fn add(a: i32, b: i32) void { } {#code_end#} {#header_close#} +

    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
    {#header_open|WASI#} +

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

    {#code_begin|exe|wasi#} {#target_wasi#} const std = @import("std"); @@ -8988,6 +9009,10 @@ pub fn main() !void { } } {#code_end#} +
    $ wasmer run wasi.wasm 123 hello
    +0: wasi.wasm
    +1: 123
    +2: hello
    {#header_close#} {#header_close#} {#header_open|Targets#} @@ -9228,6 +9253,7 @@ Available libcs: s390x-linux-musl sparc-linux-gnu sparcv9-linux-gnu + wasm32-freestanding-musl x86_64-linux-gnu x86_64-linux-gnux32 x86_64-linux-musl