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 {
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