| ... | @@ -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 likely | 8467 | <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 at | 8472 | 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#} or | 8473 | {#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#} |
| 9002 | extern fn print(i32) void; | 9008 | extern fn print(i32) void; |
| 9003 | | 9009 | |
| ... | @@ -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'); |
| | 9017 | const source = fs.readFileSync("./math.wasm"); |
| | 9018 | const typedArray = new Uint8Array(source); |
| | 9019 | |
| | 9020 | WebAssembly.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 |
| | 9028 | The 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#} |
| 9012 | const std = @import("std"); | 9033 | const 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 |
| | 9045 | 0: wasi.wasm |
| | 9046 | 1: 123 |
| | 9047 | 2: 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-musl | 9285 | s390x-linux-musl |
| 9261 | sparc-linux-gnu | 9286 | sparc-linux-gnu |
| 9262 | sparcv9-linux-gnu | 9287 | sparcv9-linux-gnu |
| | 9288 | wasm32-freestanding-musl |
| 9263 | x86_64-linux-gnu | 9289 | x86_64-linux-gnu |
| 9264 | x86_64-linux-gnux32 | 9290 | x86_64-linux-gnux32 |
| 9265 | x86_64-linux-musl</code></pre> | 9291 | x86_64-linux-musl</code></pre> |