Commit 692ccd01b4

Cody Tapscott <topolarity@tapscott.me>
2022-03-01 18:42:34
stage2: Initialize WASI preopens on startup
1 parent 0896517
Changed files (1)
src/main.zig
@@ -162,6 +162,16 @@ pub fn main() anyerror!void {
         return mainArgs(gpa_tracy.allocator(), arena, args);
     }
 
+    // WASI: `--dir` instructs the WASM runtime to "preopen" a directory, making
+    // it available to the us, the guest program. This is the only way for us to
+    // access files/dirs on the host filesystem
+    if (builtin.os.tag == .wasi) {
+        // This sets our CWD to "/preopens/cwd"
+        // Dot-prefixed preopens like `--dir=.` are "mounted" at "/preopens/cwd"
+        // Other preopens like `--dir=lib` are "mounted" at "/"
+        try std.os.initPreopensWasi(std.heap.page_allocator, "/preopens/cwd");
+    }
+
     return mainArgs(gpa, arena, args);
 }