Skip to content

Commit 6ec79ec

Browse files
committed
Implement wrapper functions for linking
1 parent 15d5186 commit 6ec79ec

File tree

2 files changed

+119
-2
lines changed

2 files changed

+119
-2
lines changed

src/c.zig

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,113 @@ pub const InterruptHandle = opaque {
218218
extern fn wasmtime_interrupt_handle_delete(?*InterruptHandle) void;
219219
extern fn wasmtime_interrupt_handle_new(?*Store) ?*InterruptHandle;
220220
};
221+
222+
pub const WasiConfig = opaque {
223+
/// Options to inherit when inherriting configs
224+
/// By default all is `true` as you often want to
225+
/// inherit everything rather than something specifically.
226+
const InheritOptions = struct {
227+
argv: bool = true,
228+
env: bool = true,
229+
std_in: bool = true,
230+
std_out: bool = true,
231+
std_err: bool = true,
232+
};
233+
234+
pub fn init() !*WasiConfig {
235+
return wasi_config_new() orelse error.ConfigInit;
236+
}
237+
238+
pub fn deinit(self: *WasiConfig) void {
239+
wasi_config_delete(self);
240+
}
241+
242+
/// Allows to inherit the native environment into the current config.
243+
/// Inherits everything by default.
244+
pub fn inherit(self: *WasiConfig, options: InheritOptions) void {
245+
if (options.argv) self.inheritArgv();
246+
if (options.env) self.inheritEnv();
247+
if (options.std_in) self.inheritStdIn();
248+
if (options.std_out) self.inheritStdOut();
249+
if (options.std_err) self.inheritStdErr();
250+
}
251+
252+
pub fn inheritArgv(self: *WasiConfig) void {
253+
wasi_config_inherit_argv(self);
254+
}
255+
256+
pub fn inheritEnv(self: *WasiConfig) void {
257+
wasi_config_inherit_env(self);
258+
}
259+
260+
pub fn inheritStdIn(self: *WasiConfig) void {
261+
wasi_config_inherit_stdin(self);
262+
}
263+
264+
pub fn inheritStdOut(self: *WasiConfig) void {
265+
wasi_config_inherit_stdout(self);
266+
}
267+
268+
pub fn inheritStdErr(self: *WasiConfig) void {
269+
wasi_config_inherit_stderr(self);
270+
}
271+
272+
extern fn wasi_config_new() ?*WasiConfig;
273+
extern fn wasi_config_delete(?*WasiConfig) void;
274+
extern fn wasi_config_inherit_argv(?*WasiConfig) void;
275+
extern fn wasi_config_inherit_env(?*WasiConfig) void;
276+
extern fn wasi_config_inherit_stdin(?*WasiConfig) void;
277+
extern fn wasi_config_inherit_stdout(?*WasiConfig) void;
278+
extern fn wasi_config_inherit_stderr(?*WasiConfig) void;
279+
};
280+
281+
pub const WasiInstance = opaque {
282+
pub fn init(store: *Store, name: [*:0]const u8, config: ?*WasiConfig, trap: *?*Trap) !*WasiInstance {
283+
return wasi_instance_new(store, name, config, trap) orelse error.InstanceInit;
284+
}
285+
286+
pub fn deinit(self: *WasiInstance) void {
287+
was_instance_delete(self);
288+
}
289+
290+
extern fn wasi_instance_new(?*Store, [*:0]const u8, ?*WasiConfig, *?*Trap) ?*WasiInstance;
291+
extern fn was_instance_delete(?*WasiInstance) void;
292+
};
293+
294+
pub const Linker = opaque {
295+
pub fn init(store: *Store) !*Linker {
296+
return wasmtime_linker_new(store) orelse error.LinkerInit;
297+
}
298+
299+
pub fn deinit(self: *Linker) void {
300+
wasmtime_linker_delete(self);
301+
}
302+
303+
pub fn defineWasi(self: *Linker, wasi: *const WasiInstance) ?*WasmError {
304+
return wasmtime_linker_define_wasi(self, wasi);
305+
}
306+
307+
pub fn defineInstance(self: *Linker, name: *const ByteVec, instance: *const Instance) ?*WasmError {
308+
wasmtime_linker_define_instance(self, name, instance);
309+
}
310+
311+
pub fn instantiate(
312+
self: *const Linker,
313+
module: *const Module,
314+
instance: *?*Instance,
315+
trap: *?*Trap,
316+
) ?*WasmError {
317+
return wasmtime_linker_instantiate(self, module, instance, trap);
318+
}
319+
320+
extern fn wasmtime_linker_new(?*Store) ?*Linker;
321+
extern fn wasmtime_linker_delete(?*Linker) void;
322+
extern fn wasmtime_linker_define_wasi(?*Linker, ?*const WasiInstance) ?*WasmError;
323+
extern fn wasmtime_linker_define_instance(?*Linker, ?*const ByteVec, ?*const Instance) ?*WasmError;
324+
extern fn wasmtime_linker_instantiate(
325+
linker: ?*const Linker,
326+
module: ?*const Module,
327+
instance: *?*Instance,
328+
trap: *?*Trap,
329+
) ?*WasmError;
330+
};

src/main.zig

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@ pub const Error = error{
3535
};
3636

3737
pub const Config = opaque {
38-
pub fn init() !*Config {
39-
return wasm_config_new() orelse Error.ConfigInit;
38+
const Options = struct {
39+
interruptable: bool = false,
40+
};
41+
pub fn init(options: Options) !*Config {
42+
const config = wasm_config_new() orelse Error.ConfigInit;
43+
if (options.interruptable) {
44+
config.setInterruptable(true);
45+
}
46+
return config;
4047
}
4148

4249
pub fn setInterruptable(self: *Config, opt: bool) void {

0 commit comments

Comments
 (0)