authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-12 13:40:30-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-06-12 13:40:30-04:00
log866651a5a3397e5d10ee90f007f6ff6fa82c702d
tree01a2bca0ad8c6c8daf0015c8c99e91aa21ae1b5a
parent1bc92b1fde6e812de4a45ab3b2131d16151114d7
parent200f9ea6fb91849d2288f194169b3c9123c33bb5
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5589 from kubkon/preopens-example

Add doc example for extracting WASI preopens

2 files changed, 46 insertions(+), 3 deletions(-)

doc/langref.html.in+24-3
......@@ -9702,7 +9702,7 @@ The result is 3</code></pre>
97029702 {#header_open|WASI#}
97039703 <p>Zig's support for WebAssembly System Interface (WASI) is under active development.
97049704 Example of using the standard library and reading command line arguments:</p>
9705 {#code_begin|exe|wasi#}
9705 {#code_begin|exe|args#}
97069706 {#target_wasi#}
97079707const std = @import("std");
97089708
......@@ -9716,10 +9716,31 @@ pub fn main() !void {
97169716 }
97179717}
97189718 {#code_end#}
9719 <pre><code>$ wasmer run wasi.wasm 123 hello
97200: wasi.wasm
9719 <pre><code>$ wasmtime args.wasm 123 hello
97200: args.wasm
972197211: 123
972297222: 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>
97239744 {#header_close#}
97249745 {#header_close#}
97259746 {#header_open|Targets#}
lib/std/fs/wasi.zig+22
......@@ -35,6 +35,14 @@ pub const Preopen = struct {
3535 .@"type" = .{ .Dir = path },
3636 };
3737 }
38
39 pub fn format(self: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, out_stream: var) !void {
40 try out_stream.print("{{ .fd = {}, ", .{self.fd});
41 switch (self.@"type") {
42 PreopenType.Dir => |path| try out_stream.print(".Dir = '{}'", .{path}),
43 }
44 return out_stream.print(" }}", .{});
45 }
3846};
3947
4048/// Dynamically-sized array list of WASI preopens. This struct is a
......@@ -138,3 +146,17 @@ pub const PreopenList = struct {
138146 return self.buffer.toOwnedSlice();
139147 }
140148};
149
150test "extracting WASI preopens" {
151 if (@import("builtin").os.tag != .wasi) return error.SkipZigTest;
152
153 var preopens = PreopenList.init(std.testing.allocator);
154 defer preopens.deinit();
155
156 try preopens.populate();
157
158 std.testing.expectEqual(@as(usize, 1), preopens.asSlice().len);
159 const preopen = preopens.find(".") orelse unreachable;
160 std.testing.expect(std.mem.eql(u8, ".", preopen.@"type".Dir));
161 std.testing.expectEqual(@as(usize, 3), preopen.fd);
162}