Skip to content

Commit 9184e28

Browse files
committed
wasi: new struct for wasi
1 parent 56a3341 commit 9184e28

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

src/wasi.zig

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
pub const wasm = @import("./wasm.zig");
2+
3+
pub const WasiConfig = opaque {
4+
/// Options to inherit when inherriting configs
5+
/// By default all is `true` as you often want to
6+
/// inherit everything rather than something specifically.
7+
const InheritOptions = struct {
8+
argv: bool = true,
9+
env: bool = true,
10+
std_in: bool = true,
11+
std_out: bool = true,
12+
std_err: bool = true,
13+
};
14+
15+
pub fn init() !*WasiConfig {
16+
return wasi_config_new() orelse error.ConfigInit;
17+
}
18+
19+
pub fn deinit(self: *WasiConfig) void {
20+
wasi_config_delete(self);
21+
}
22+
23+
/// Allows to inherit the native environment into the current config.
24+
/// Inherits everything by default.
25+
pub fn inherit(self: *WasiConfig, options: InheritOptions) void {
26+
if (options.argv) self.inheritArgv();
27+
if (options.env) self.inheritEnv();
28+
if (options.std_in) self.inheritStdIn();
29+
if (options.std_out) self.inheritStdOut();
30+
if (options.std_err) self.inheritStdErr();
31+
}
32+
33+
pub fn inheritArgv(self: *WasiConfig) void {
34+
wasi_config_inherit_argv(self);
35+
}
36+
37+
pub fn inheritEnv(self: *WasiConfig) void {
38+
wasi_config_inherit_env(self);
39+
}
40+
41+
pub fn inheritStdIn(self: *WasiConfig) void {
42+
wasi_config_inherit_stdin(self);
43+
}
44+
45+
pub fn inheritStdOut(self: *WasiConfig) void {
46+
wasi_config_inherit_stdout(self);
47+
}
48+
49+
pub fn inheritStdErr(self: *WasiConfig) void {
50+
wasi_config_inherit_stderr(self);
51+
}
52+
53+
extern "c" fn wasi_config_new() ?*WasiConfig;
54+
extern "c" fn wasi_config_delete(?*WasiConfig) void;
55+
extern "c" fn wasi_config_inherit_argv(?*WasiConfig) void;
56+
extern "c" fn wasi_config_inherit_env(?*WasiConfig) void;
57+
extern "c" fn wasi_config_inherit_stdin(?*WasiConfig) void;
58+
extern "c" fn wasi_config_inherit_stdout(?*WasiConfig) void;
59+
extern "c" fn wasi_config_inherit_stderr(?*WasiConfig) void;
60+
};

0 commit comments

Comments
 (0)