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 {...@@ -8434,6 +8434,8 @@ fn concat(allocator: *Allocator, a: []const u8, b: []const u8) ![]u8 {
8434 </li>8434 </li>
8435 <li>Are you linking libc? In this case, {#syntax#}std.heap.c_allocator{#endsyntax#} is likely8435 <li>Are you linking libc? In this case, {#syntax#}std.heap.c_allocator{#endsyntax#} is likely
8436 the right choice, at least for your main allocator.</li>8436 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>
8437 <li>8439 <li>
8438 Is the maximum number of bytes that you will need bounded by a number known at8440 Is the maximum number of bytes that you will need bounded by a number known at
8439 {#link|comptime#}? In this case, use {#syntax#}std.heap.FixedBufferAllocator{#endsyntax#} or8441 {#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>...@@ -8964,8 +8966,12 @@ all your base are belong to us</code></pre>
8964 {#header_close#}8966 {#header_close#}
8965 {#header_close#}8967 {#header_close#}
8966 {#header_open|WebAssembly#}8968 {#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>
8967 {#header_open|Freestanding#}8971 {#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#}
8969 {#target_wasm#}8975 {#target_wasm#}
8970extern fn print(i32) void;8976extern fn print(i32) void;
89718977
...@@ -8974,7 +8980,22 @@ export fn add(a: i32, b: i32) void {...@@ -8974,7 +8980,22 @@ export fn add(a: i32, b: i32) void {
8974}8980}
8975 {#code_end#}8981 {#code_end#}
8976 {#header_close#}8982 {#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>
8977 {#header_open|WASI#}8997 {#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>
8978 {#code_begin|exe|wasi#}8999 {#code_begin|exe|wasi#}
8979 {#target_wasi#}9000 {#target_wasi#}
8980const std = @import("std");9001const std = @import("std");
...@@ -8988,6 +9009,10 @@ pub fn main() !void {...@@ -8988,6 +9009,10 @@ pub fn main() !void {
8988 }9009 }
8989}9010}
8990 {#code_end#}9011 {#code_end#}
9012 <pre><code>$ wasmer run wasi.wasm 123 hello
90130: wasi.wasm
90141: 123
90152: hello</code></pre>
8991 {#header_close#}9016 {#header_close#}
8992 {#header_close#}9017 {#header_close#}
8993 {#header_open|Targets#}9018 {#header_open|Targets#}
...@@ -9228,6 +9253,7 @@ Available libcs:...@@ -9228,6 +9253,7 @@ Available libcs:
9228 s390x-linux-musl9253 s390x-linux-musl
9229 sparc-linux-gnu9254 sparc-linux-gnu
9230 sparcv9-linux-gnu9255 sparcv9-linux-gnu
9256 wasm32-freestanding-musl
9231 x86_64-linux-gnu9257 x86_64-linux-gnu
9232 x86_64-linux-gnux329258 x86_64-linux-gnux32
9233 x86_64-linux-musl</code></pre>9259 x86_64-linux-musl</code></pre>