| ... | @@ -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 likely | 8435 | <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 at | 8440 | 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#} or | 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,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#} |
| 8970 | extern fn print(i32) void; | 8976 | extern fn print(i32) void; |
| 8971 | | 8977 | |
| ... | @@ -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'); |
| | 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 | {#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#} |
| 8980 | const std = @import("std"); | 9001 | const 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 |
| | 9013 | 0: wasi.wasm |
| | 9014 | 1: 123 |
| | 9015 | 2: 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-musl | 9253 | s390x-linux-musl |
| 9229 | sparc-linux-gnu | 9254 | sparc-linux-gnu |
| 9230 | sparcv9-linux-gnu | 9255 | sparcv9-linux-gnu |
| | 9256 | wasm32-freestanding-musl |
| 9231 | x86_64-linux-gnu | 9257 | x86_64-linux-gnu |
| 9232 | x86_64-linux-gnux32 | 9258 | x86_64-linux-gnux32 |
| 9233 | x86_64-linux-musl</code></pre> | 9259 | x86_64-linux-musl</code></pre> |