authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-27 22:37:15-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-05-27 22:37:15-04:00
logd1b6f29d225bbedd0afb97fce64f5d042df4d9c6
treec0f99e78e94a0a60ba6df73fb61c502ebf5b262a
parent3f302594b855e4d0dff9fbbfb2fe9d1d1801cb6e
parentb618a0b866edb546b33e6fdb540faeed06f0e8bd
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2523 from shritesh/wasmdoc

docs: wasm

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

doc/langref.html.in+27-1
......@@ -8466,6 +8466,8 @@ fn concat(allocator: *Allocator, a: []const u8, b: []const u8) ![]u8 {
84668466 </li>
84678467 <li>Are you linking libc? In this case, {#syntax#}std.heap.c_allocator{#endsyntax#} is likely
84688468 the right choice, at least for your main allocator.</li>
8469 <li>Are you building for WebAssembly? In this case, {#syntax#}std.heap.wasm_allocator{#endsyntax#} is likely
8470 the right choice for your main allocator as it uses WebAssembly's memory instructions.</li>
84698471 <li>
84708472 Is the maximum number of bytes that you will need bounded by a number known at
84718473 {#link|comptime#}? In this case, use {#syntax#}std.heap.FixedBufferAllocator{#endsyntax#} or
......@@ -8996,8 +8998,12 @@ all your base are belong to us</code></pre>
89968998 {#header_close#}
89978999 {#header_close#}
89989000 {#header_open|WebAssembly#}
9001 <p>Zig supports building for WebAssembly out of the box. There is also a specialized {#syntax#}std.heap.wasm_allocator{#endsyntax#}
9002 memory allocator for WebAssembly environments.</p>
89999003 {#header_open|Freestanding#}
9000 {#code_begin|lib|wasm#}
9004 <p>For host environments like the web browser and nodejs, build as a library using the freestanding OS target.
9005 Here's an example of running Zig code compiled to WebAssembly with nodejs.</p>
9006 {#code_begin|lib|math#}
90019007 {#target_wasm#}
90029008extern fn print(i32) void;
90039009
......@@ -9006,7 +9012,22 @@ export fn add(a: i32, b: i32) void {
90069012}
90079013 {#code_end#}
90089014 {#header_close#}
9015 <p class="file">test.js</p>
9016 <pre><code>const fs = require('fs');
9017const source = fs.readFileSync("./math.wasm");
9018const typedArray = new Uint8Array(source);
9019
9020WebAssembly.instantiate(typedArray, {
9021 env: {
9022 print: (result) =&gt; { console.log(`The result is ${result}`); }
9023 }}).then(result =&gt; {
9024 const add = result.instance.exports.add;
9025 add(1, 2);
9026});</code></pre>
9027 <pre><code>$ node test.js
9028The result is 3</code></pre>
90099029 {#header_open|WASI#}
9030 <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>
90109031 {#code_begin|exe|wasi#}
90119032 {#target_wasi#}
90129033const std = @import("std");
......@@ -9020,6 +9041,10 @@ pub fn main() !void {
90209041 }
90219042}
90229043 {#code_end#}
9044 <pre><code>$ wasmer run wasi.wasm 123 hello
90450: wasi.wasm
90461: 123
90472: hello</code></pre>
90239048 {#header_close#}
90249049 {#header_close#}
90259050 {#header_open|Targets#}
......@@ -9260,6 +9285,7 @@ Available libcs:
92609285 s390x-linux-musl
92619286 sparc-linux-gnu
92629287 sparcv9-linux-gnu
9288 wasm32-freestanding-musl
92639289 x86_64-linux-gnu
92649290 x86_64-linux-gnux32
92659291 x86_64-linux-musl</code></pre>