| author | |
| committer | |
| log | 866651a5a3397e5d10ee90f007f6ff6fa82c702d |
| tree | 01a2bca0ad8c6c8daf0015c8c99e91aa21ae1b5a |
| parent | 1bc92b1fde6e812de4a45ab3b2131d16151114d7 |
| parent | 200f9ea6fb91849d2288f194169b3c9123c33bb5 |
| signature |
Add doc example for extracting WASI preopens2 files changed, 46 insertions(+), 3 deletions(-)
doc/langref.html.in+24-3| ... | ... | @@ -9702,7 +9702,7 @@ The result is 3</code></pre> |
| 9702 | 9702 | {#header_open|WASI#} |
| 9703 | 9703 | <p>Zig's support for WebAssembly System Interface (WASI) is under active development. |
| 9704 | 9704 | Example of using the standard library and reading command line arguments:</p> |
| 9705 | {#code_begin|exe|wasi#} | |
| 9705 | {#code_begin|exe|args#} | |
| 9706 | 9706 | {#target_wasi#} |
| 9707 | 9707 | const std = @import("std"); |
| 9708 | 9708 | |
| ... | ... | @@ -9716,10 +9716,31 @@ pub fn main() !void { |
| 9716 | 9716 | } |
| 9717 | 9717 | } |
| 9718 | 9718 | {#code_end#} |
| 9719 | <pre><code>$ wasmer run wasi.wasm 123 hello | |
| 9720 | 0: wasi.wasm | |
| 9719 | <pre><code>$ wasmtime args.wasm 123 hello | |
| 9720 | 0: args.wasm | |
| 9721 | 9721 | 1: 123 |
| 9722 | 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 | 9744 | {#header_close#} |
| 9724 | 9745 | {#header_close#} |
| 9725 | 9746 | {#header_open|Targets#} |
lib/std/fs/wasi.zig+22| ... | ... | @@ -35,6 +35,14 @@ pub const Preopen = struct { |
| 35 | 35 | .@"type" = .{ .Dir = path }, |
| 36 | 36 | }; |
| 37 | 37 | } |
| 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 | } | |
| 38 | 46 | }; |
| 39 | 47 | |
| 40 | 48 | /// Dynamically-sized array list of WASI preopens. This struct is a |
| ... | ... | @@ -138,3 +146,17 @@ pub const PreopenList = struct { |
| 138 | 146 | return self.buffer.toOwnedSlice(); |
| 139 | 147 | } |
| 140 | 148 | }; |
| 149 | ||
| 150 | test "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 | } |