malcolmstill / zware

Zig WebAssembly Runtime Engine
MIT License
293 stars 10 forks source link

zware-gen: Improve interface #204

Closed malcolmstill closed 1 year ago

malcolmstill commented 1 year ago

Description

Wrap the calls to .wasm module functions in a struct that carries its instance

E.g. for fib.wasm we get:

pub const Api = struct {
        instance: *zware.Instance,

        const Self = @This();

        pub fn init(instance: *zware.Instance) Self {
                return .{ .instance = instance };
        }

        pub fn fib(self: *Self, param0: i32) !i32 {
                var in = [_]u64{@bitCast(@as(i64, param0))};
                var out = [_]u64{0};
                try self.instance.invoke("fib", in[0..], out[0..], .{});
                return @bitCast(@as(u32, @truncate(out[0])));
        }
};