| ... | ... | @@ -8434,6 +8434,8 @@ fn concat(allocator: *Allocator, a: []const u8, b: []const u8) ![]u8 { |
| 8434 | 8434 | </li> |
| 8435 | 8435 | <li>Are you linking libc? In this case, {#syntax#}std.heap.c_allocator{#endsyntax#} is likely |
| 8436 | 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 | 8439 | <li> |
| 8438 | 8440 | Is the maximum number of bytes that you will need bounded by a number known at |
| 8439 | 8441 | {#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 | 8966 | {#header_close#} |
| 8965 | 8967 | {#header_close#} |
| 8966 | 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 | 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 | 8975 | {#target_wasm#} |
| 8970 | 8976 | extern fn print(i32) void; |
| 8971 | 8977 | |
| ... | ... | @@ -8974,7 +8980,22 @@ export fn add(a: i32, b: i32) void { |
| 8974 | 8980 | } |
| 8975 | 8981 | {#code_end#} |
| 8976 | 8982 | {#header_close#} |
| 8983 | <p class="file">test.js</p> |
| 8984 | <pre><code>const fs = require('fs'); |
| 8985 | const source = fs.readFileSync("./math.wasm"); |
| 8986 | const typedArray = new Uint8Array(source); |
| 8987 | |
| 8988 | WebAssembly.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 |
| 8996 | The result is 3</code></pre> |
| 8977 | 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 | 8999 | {#code_begin|exe|wasi#} |
| 8979 | 9000 | {#target_wasi#} |
| 8980 | 9001 | const std = @import("std"); |
| ... | ... | @@ -8988,6 +9009,10 @@ pub fn main() !void { |
| 8988 | 9009 | } |
| 8989 | 9010 | } |
| 8990 | 9011 | {#code_end#} |
| 9012 | <pre><code>$ wasmer run wasi.wasm 123 hello |
| 9013 | 0: wasi.wasm |
| 9014 | 1: 123 |
| 9015 | 2: hello</code></pre> |
| 8991 | 9016 | {#header_close#} |
| 8992 | 9017 | {#header_close#} |
| 8993 | 9018 | {#header_open|Targets#} |
| ... | ... | @@ -9228,6 +9253,7 @@ Available libcs: |
| 9228 | 9253 | s390x-linux-musl |
| 9229 | 9254 | sparc-linux-gnu |
| 9230 | 9255 | sparcv9-linux-gnu |
| 9256 | wasm32-freestanding-musl |
| 9231 | 9257 | x86_64-linux-gnu |
| 9232 | 9258 | x86_64-linux-gnux32 |
| 9233 | 9259 | x86_64-linux-musl</code></pre> |