| ... | @@ -9702,7 +9702,7 @@ The result is 3</code></pre> | ... | @@ -9702,7 +9702,7 @@ The result is 3</code></pre> |
| 9702 | {#header_open|WASI#} | 9702 | {#header_open|WASI#} |
| 9703 | <p>Zig's support for WebAssembly System Interface (WASI) is under active development. | 9703 | <p>Zig's support for WebAssembly System Interface (WASI) is under active development. |
| 9704 | Example of using the standard library and reading command line arguments:</p> | 9704 | Example of using the standard library and reading command line arguments:</p> |
| 9705 | {#code_begin|exe|wasi#} | 9705 | {#code_begin|exe|args#} |
| 9706 | {#target_wasi#} | 9706 | {#target_wasi#} |
| 9707 | const std = @import("std"); | 9707 | const std = @import("std"); |
| 9708 | | 9708 | |
| ... | @@ -9716,10 +9716,31 @@ pub fn main() !void { | ... | @@ -9716,10 +9716,31 @@ pub fn main() !void { |
| 9716 | } | 9716 | } |
| 9717 | } | 9717 | } |
| 9718 | {#code_end#} | 9718 | {#code_end#} |
| 9719 | <pre><code>$ wasmer run wasi.wasm 123 hello | 9719 | <pre><code>$ wasmtime args.wasm 123 hello |
| 9720 | 0: wasi.wasm | 9720 | 0: args.wasm |
| 9721 | 1: 123 | 9721 | 1: 123 |
| 9722 | 2: hello</code></pre> | 9722 | 2: hello</code></pre> |
| | 9723 | <p>A more interesting example would be extracting the list of preopens from the runtime. |
| | 9724 | This is now supported in the standard library via {#syntax#}std.fs.wasi.PreopenList{#endsyntax#}:</p> |
| | 9725 | {#code_begin|exe|preopens#} |
| | 9726 | {#target_wasi#} |
| | 9727 | const std = @import("std"); |
| | 9728 | const PreopenList = std.fs.wasi.PreopenList; |
| | 9729 | |
| | 9730 | pub fn main() !void { |
| | 9731 | var preopens = PreopenList.init(std.heap.page_allocator); |
| | 9732 | defer preopens.deinit(); |
| | 9733 | |
| | 9734 | try preopens.populate(); |
| | 9735 | |
| | 9736 | for (preopens.asSlice()) |preopen, i| { |
| | 9737 | std.debug.warn("{}: {}\n", .{ i, preopen }); |
| | 9738 | } |
| | 9739 | } |
| | 9740 | {#code_end#} |
| | 9741 | <pre><code>$ wasmtime --dir=. preopens.wasm |
| | 9742 | 0: { .fd = 3, .Dir = '.' } |
| | 9743 | </code></pre> |
| 9723 | {#header_close#} | 9744 | {#header_close#} |
| 9724 | {#header_close#} | 9745 | {#header_close#} |
| 9725 | {#header_open|Targets#} | 9746 | {#header_open|Targets#} |