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 {...@@ -8466,6 +8466,8 @@ fn concat(allocator: *Allocator, a: []const u8, b: []const u8) ![]u8 {
8466 </li>8466 </li>
8467 <li>Are you linking libc? In this case, {#syntax#}std.heap.c_allocator{#endsyntax#} is likely8467 <li>Are you linking libc? In this case, {#syntax#}std.heap.c_allocator{#endsyntax#} is likely
8468 the right choice, at least for your main allocator.</li>8468 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>
8469 <li>8471 <li>
8470 Is the maximum number of bytes that you will need bounded by a number known at8472 Is the maximum number of bytes that you will need bounded by a number known at
8471 {#link|comptime#}? In this case, use {#syntax#}std.heap.FixedBufferAllocator{#endsyntax#} or8473 {#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>...@@ -8996,8 +8998,12 @@ all your base are belong to us</code></pre>
8996 {#header_close#}8998 {#header_close#}
8997 {#header_close#}8999 {#header_close#}
8998 {#header_open|WebAssembly#}9000 {#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>
8999 {#header_open|Freestanding#}9003 {#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#}
9001 {#target_wasm#}9007 {#target_wasm#}
9002extern fn print(i32) void;9008extern fn print(i32) void;
90039009
...@@ -9006,7 +9012,22 @@ export fn add(a: i32, b: i32) void {...@@ -9006,7 +9012,22 @@ export fn add(a: i32, b: i32) void {
9006}9012}
9007 {#code_end#}9013 {#code_end#}
9008 {#header_close#}9014 {#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>
9009 {#header_open|WASI#}9029 {#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>
9010 {#code_begin|exe|wasi#}9031 {#code_begin|exe|wasi#}
9011 {#target_wasi#}9032 {#target_wasi#}
9012const std = @import("std");9033const std = @import("std");
...@@ -9020,6 +9041,10 @@ pub fn main() !void {...@@ -9020,6 +9041,10 @@ pub fn main() !void {
9020 }9041 }
9021}9042}
9022 {#code_end#}9043 {#code_end#}
9044 <pre><code>$ wasmer run wasi.wasm 123 hello
90450: wasi.wasm
90461: 123
90472: hello</code></pre>
9023 {#header_close#}9048 {#header_close#}
9024 {#header_close#}9049 {#header_close#}
9025 {#header_open|Targets#}9050 {#header_open|Targets#}
...@@ -9260,6 +9285,7 @@ Available libcs:...@@ -9260,6 +9285,7 @@ Available libcs:
9260 s390x-linux-musl9285 s390x-linux-musl
9261 sparc-linux-gnu9286 sparc-linux-gnu
9262 sparcv9-linux-gnu9287 sparcv9-linux-gnu
9288 wasm32-freestanding-musl
9263 x86_64-linux-gnu9289 x86_64-linux-gnu
9264 x86_64-linux-gnux329290 x86_64-linux-gnux32
9265 x86_64-linux-musl</code></pre>9291 x86_64-linux-musl</code></pre>