authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-06-11 22:06:57+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-06-11 22:30:02+02:00
loge4a8598ddda0d0ff289ac59a04f2ac965ef7e102
tree2a989ed48915da0a4d7533673e2c84bf094a4f4a
parenta282ac7a9119cd0961ea17e29c4a0e9b0baf60d0

Add doc example for extracting WASI preopens


1 files changed, 24 insertions(+), 3 deletions(-)

doc/langref.html.in+24-3
...@@ -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#}
9707const std = @import("std");9707const std = @import("std");
97089708
...@@ -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 hello9719 <pre><code>$ wasmtime args.wasm 123 hello
97200: wasi.wasm97200: args.wasm
97211: 12397211: 123
97222: hello</code></pre>97222: 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#}
9727const std = @import("std");
9728const PreopenList = std.fs.wasi.PreopenList;
9729
9730pub 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
97420: { .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#}