langref: update WASI preopens example

This commit is contained in:
Andrew Kelley 2022-12-05 17:21:33 -07:00
parent 823d1e7087
commit 4451786c66

View File

@ -10961,24 +10961,28 @@ pub fn main() !void {
{#code_begin|exe|preopens#} {#code_begin|exe|preopens#}
{#target_wasi#} {#target_wasi#}
const std = @import("std"); const std = @import("std");
const PreopenList = std.fs.wasi.PreopenList; const fs = std.fs;
pub fn main() !void { pub fn main() !void {
var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){}; var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){};
const gpa = general_purpose_allocator.allocator(); const gpa = general_purpose_allocator.allocator();
var preopens = PreopenList.init(gpa); var arena_instance = std.heap.ArenaAllocator.init(gpa);
defer preopens.deinit(); defer arena_instance.deinit();
const arena = arena_instance.allocator();
try preopens.populate(null); const preopens = try fs.wasi.preopensAlloc(arena);
for (preopens.asSlice()) |preopen, i| { for (preopens.names) |preopen, i| {
std.debug.print("{}: {}\n", .{ i, preopen }); std.debug.print("{}: {s}\n", .{ i, preopen });
} }
} }
{#code_end#} {#code_end#}
{#shell_samp#}$ wasmtime --dir=. preopens.wasm {#shell_samp#}$ wasmtime --dir=. preopens.wasm
0: Preopen{ .fd = 3, .type = PreopenType{ .Dir = '.' } } 0: stdin
1: stdout
2: stderr
3: .
{#end_shell_samp#} {#end_shell_samp#}
{#header_close#} {#header_close#}
{#header_close#} {#header_close#}