authorgravatar for shritesh@shritesh.comShritesh Bhattarai <shritesh@shritesh.com> 2019-05-19 19:26:10-07:00
committergravatar for shritesh@shritesh.comShritesh Bhattarai <shritesh@shritesh.com> 2019-05-19 19:32:28-07:00
log3b6fc3fdc7618502a9338c4266c4d1aa1ad94bf3
treec944be18aed0dfd685743db5bd429058c66bafd7
parent1c73c08298fc3a19886038d935e45cf74ba152fb

docs: wasm


1 files changed, 27 insertions(+), 1 deletions(-)

doc/langref.html.in+27-1
......@@ -8434,6 +8434,8 @@ fn concat(allocator: *Allocator, a: []const u8, b: []const u8) ![]u8 {
84348434 </li>
84358435 <li>Are you linking libc? In this case, {#syntax#}std.heap.c_allocator{#endsyntax#} is likely
84368436 the right choice, at least for your main allocator.</li>
8437 <li>Are you building for WebAssembly? In this case, {#syntax#}std.heap.wasm_allocator{#endsyntax#} is likely
8438 the only possible choice for your main allocator.</li>
84378439 <li>
84388440 Is the maximum number of bytes that you will need bounded by a number known at
84398441 {#link|comptime#}? In this case, use {#syntax#}std.heap.FixedBufferAllocator{#endsyntax#} or
......@@ -8964,8 +8966,12 @@ all your base are belong to us</code></pre>
89648966 {#header_close#}
89658967 {#header_close#}
89668968 {#header_open|WebAssembly#}
8969 <p>Zig supports building for WebAssembly out of the box. There is also a specialized {#syntax#}std.heap.wasm_allocator{#endsyntax#}
8970 memory allocator for WebAssembly environments.</p>
89678971 {#header_open|Freestanding#}
8968 {#code_begin|lib|wasm#}
8972 <p>For embedded environments like the web browser and nodejs, build as a library using the freestanding OS target.
8973 Here's an example of running Zig code compiled to WebAssembly with nodejs.</p>
8974 {#code_begin|lib|math#}
89698975 {#target_wasm#}
89708976extern fn print(i32) void;
89718977
......@@ -8974,7 +8980,22 @@ export fn add(a: i32, b: i32) void {
89748980}
89758981 {#code_end#}
89768982 {#header_close#}
8983 <p class="file">test.js</p>
8984 <pre><code>const fs = require('fs');
8985const source = fs.readFileSync("./math.wasm");
8986const typedArray = new Uint8Array(source);
8987
8988WebAssembly.instantiate(typedArray, {
8989 env: {
8990 print: (result) =&gt; { console.log(`The result is ${result}`); }
8991 }}).then(result =&gt; {
8992 const add = result.instance.exports.add;
8993 add(1, 2);
8994});</code></pre>
8995 <pre><code>$ node test.js
8996The result is 3</code></pre>
89778997 {#header_open|WASI#}
8998 <p>Zig's support for WebAssembly System Interface (WASI) is under active development. Example of using the standard library and reading command line arguments:</p>
89788999 {#code_begin|exe|wasi#}
89799000 {#target_wasi#}
89809001const std = @import("std");
......@@ -8988,6 +9009,10 @@ pub fn main() !void {
89889009 }
89899010}
89909011 {#code_end#}
9012 <pre><code>$ wasmer run wasi.wasm 123 hello
90130: wasi.wasm
90141: 123
90152: hello</code></pre>
89919016 {#header_close#}
89929017 {#header_close#}
89939018 {#header_open|Targets#}
......@@ -9228,6 +9253,7 @@ Available libcs:
92289253 s390x-linux-musl
92299254 sparc-linux-gnu
92309255 sparcv9-linux-gnu
9256 wasm32-freestanding-musl
92319257 x86_64-linux-gnu
92329258 x86_64-linux-gnux32
92339259 x86_64-linux-musl</code></pre>