From e4a8598ddda0d0ff289ac59a04f2ac965ef7e102 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 11 Jun 2020 22:06:57 +0200 Subject: [PATCH 1/3] Add doc example for extracting WASI preopens --- doc/langref.html.in | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/doc/langref.html.in b/doc/langref.html.in index e5c814d663..f3fd1d9e8c 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -9702,7 +9702,7 @@ The result is 3 {#header_open|WASI#}

Zig's support for WebAssembly System Interface (WASI) is under active development. Example of using the standard library and reading command line arguments:

- {#code_begin|exe|wasi#} + {#code_begin|exe|args#} {#target_wasi#} const std = @import("std"); @@ -9716,10 +9716,31 @@ pub fn main() !void { } } {#code_end#} -
$ wasmer run wasi.wasm 123 hello
-0: wasi.wasm
+      
$ wasmtime args.wasm 123 hello
+0: args.wasm
 1: 123
 2: hello
+

A more interesting example would be extracting the list of preopens from the runtime. + This is now supported in the standard library via {#syntax#}std.fs.wasi.PreopenList{#endsyntax#}:

+ {#code_begin|exe|preopens#} + {#target_wasi#} +const std = @import("std"); +const PreopenList = std.fs.wasi.PreopenList; + +pub fn main() !void { + var preopens = PreopenList.init(std.heap.page_allocator); + defer preopens.deinit(); + + try preopens.populate(); + + for (preopens.asSlice()) |preopen, i| { + std.debug.warn("{}: {}\n", .{ i, preopen }); + } +} + {#code_end#} +
$ wasmtime --dir=. preopens.wasm
+0: { .fd = 3, .Dir = '.' }
+
{#header_close#} {#header_close#} {#header_open|Targets#} From c7721bb3688dd5f69700c7a54b5ad30f8d1482e6 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 11 Jun 2020 22:31:08 +0200 Subject: [PATCH 2/3] Add custom format method for Preopen struct --- lib/std/fs/wasi.zig | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/std/fs/wasi.zig b/lib/std/fs/wasi.zig index 194e1d41a6..1b27227ddd 100644 --- a/lib/std/fs/wasi.zig +++ b/lib/std/fs/wasi.zig @@ -35,6 +35,14 @@ pub const Preopen = struct { .@"type" = .{ .Dir = path }, }; } + + pub fn format(self: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, out_stream: var) !void { + try out_stream.print("{{ .fd = {}, ", .{self.fd}); + switch (self.@"type") { + PreopenType.Dir => |path| try out_stream.print(".Dir = '{}'", .{path}), + } + return out_stream.print(" }}", .{}); + } }; /// Dynamically-sized array list of WASI preopens. This struct is a From 200f9ea6fb91849d2288f194169b3c9123c33bb5 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 11 Jun 2020 23:00:02 +0200 Subject: [PATCH 3/3] Add unit test for std.fs.wasi.PreopenList --- lib/std/fs/wasi.zig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/std/fs/wasi.zig b/lib/std/fs/wasi.zig index 1b27227ddd..f08c74c129 100644 --- a/lib/std/fs/wasi.zig +++ b/lib/std/fs/wasi.zig @@ -146,3 +146,17 @@ pub const PreopenList = struct { return self.buffer.toOwnedSlice(); } }; + +test "extracting WASI preopens" { + if (@import("builtin").os.tag != .wasi) return error.SkipZigTest; + + var preopens = PreopenList.init(std.testing.allocator); + defer preopens.deinit(); + + try preopens.populate(); + + std.testing.expectEqual(@as(usize, 1), preopens.asSlice().len); + const preopen = preopens.find(".") orelse unreachable; + std.testing.expect(std.mem.eql(u8, ".", preopen.@"type".Dir)); + std.testing.expectEqual(@as(usize, 3), preopen.fd); +}