From 32fd3ed7164c5056a96432f937e1237eda88ae3c Mon Sep 17 00:00:00 2001 From: Arnau478 Date: Tue, 22 Aug 2023 20:13:49 +0200 Subject: [PATCH 1/7] Updated to zig 0.11.0 --- README.md | 5 ----- build.zig | 32 ++++++++++++++++++++++---------- deps.zig | 14 -------------- gyro.zzz | 10 ---------- src/main.zig | 26 +++++++++++++------------- 5 files changed, 35 insertions(+), 52 deletions(-) delete mode 100644 deps.zig delete mode 100644 gyro.zzz diff --git a/README.md b/README.md index 4ce3d7c..efa8b8b 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,3 @@ as [wasmer-zig] or [wasmtime-zig]. [wasmer-zig]: https://github.com/kubkon/wasmer-zig [wasmtime-zig]: https://github.com/kubkon/wasmtime-zig - -To add this library as your dependency, we strongly recommend [gyro]. - -[gyro]: https://github.com/mattnite/gyro - diff --git a/build.zig b/build.zig index 17ba4f8..a8cae7e 100644 --- a/build.zig +++ b/build.zig @@ -1,17 +1,29 @@ const std = @import("std"); -pub fn build(b: *std.build.Builder) void { - // Standard release options allow the person running `zig build` to select - // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. - const mode = b.standardReleaseOptions(); +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); - const lib = b.addStaticLibrary("wasm-zig", "src/main.zig"); - lib.setBuildMode(mode); - lib.install(); + const optimize = b.standardOptimizeOption(.{}); - var main_tests = b.addTest("src/main.zig"); - main_tests.setBuildMode(mode); + const lib = b.addStaticLibrary(.{ + .name = "wasm-zig", + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + lib.linkLibC(); + + b.installArtifact(lib); + + var main_tests = b.addTest(.{ + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + main_tests.linkLibC(); + + const run_main_tests = b.addRunArtifact(main_tests); const test_step = b.step("test", "Run library tests"); - test_step.dependOn(&main_tests.step); + test_step.dependOn(&run_main_tests.step); } diff --git a/deps.zig b/deps.zig deleted file mode 100644 index 51c74be..0000000 --- a/deps.zig +++ /dev/null @@ -1,14 +0,0 @@ -const std = @import("std"); -pub const pkgs = struct { - pub fn addAllTo(artifact: *std.build.LibExeObjStep) void { - @setEvalBranchQuota(1_000_000); - inline for (std.meta.declarations(pkgs)) |decl| { - if (decl.is_pub and decl.data == .Var) { - artifact.addPackage(@field(pkgs, decl.name)); - } - } - } -}; - -pub const base_dirs = struct { -}; diff --git a/gyro.zzz b/gyro.zzz deleted file mode 100644 index 11d55b9..0000000 --- a/gyro.zzz +++ /dev/null @@ -1,10 +0,0 @@ -pkgs: - wasm: - version: 0.0.0 - description: "Common Wasm runtime binding to C API" - source_url: "https://github.com/zigwasm/wasm-zig" - - root: src/main.zig - files: - README.md - LICENSE diff --git a/src/main.zig b/src/main.zig index 6844ee7..82a7fcc 100644 --- a/src/main.zig +++ b/src/main.zig @@ -102,7 +102,7 @@ pub const Module = opaque { fn cb(params: ?*const Valtype, results: ?*Valtype) callconv(.C) ?*Trap { _ = params; _ = results; - const func = @intToPtr(fn () void, CALLBACK); + const func = @as(fn () void, @ptrFromInt(CALLBACK)); func(); return null; } @@ -134,7 +134,7 @@ pub const Func = opaque { }, else => @compileError("only functions can be used as callbacks into Wasm"), } - CALLBACK = @ptrToInt(callback); + CALLBACK = @intFromPtr(callback); var args = ValtypeVec.empty(); var results = ValtypeVec.empty(); @@ -175,10 +175,10 @@ pub const Func = opaque { const args_len = args.len; comptime var wasm_args: [args_len]Value = undefined; - inline for (wasm_args) |*arg, i| { + inline for (wasm_args, 0..) |*arg, i| { arg.* = switch (@TypeOf(args[i])) { - i32, u32 => .{ .kind = .i32, .of = .{ .i32 = @bitCast(i32, args[i]) } }, - i64, u64 => .{ .kind = .i64, .of = .{ .i64 = @bitCast(i64, args[i]) } }, + i32, u32 => .{ .kind = .i32, .of = .{ .i32 = @as(i32, @bitCast(args[i])) } }, + i64, u64 => .{ .kind = .i64, .of = .{ .i64 = @as(i64, @bitCast(args[i])) } }, f32 => .{ .kind = .f32, .of = .{ .f32 = args[i] } }, f64 => .{ .kind = .f64, .of = .{ .f64 = args[i] } }, *Func => .{ .kind = .funcref, .of = .{ .ref = args[i] } }, @@ -194,7 +194,7 @@ pub const Func = opaque { const final_args = ValVec{ .size = args_len, - .data = if (args_len == 0) undefined else @ptrCast([*]Value, &wasm_args), + .data = if (args_len == 0) undefined else @as([*]Value, @ptrCast(&wasm_args)), }; var result_list = ValVec.initWithCapacity(result_len); @@ -216,12 +216,12 @@ pub const Func = opaque { if (!matchesKind(ResultType, result_ty.kind)) return CallError.InvalidResultType; return switch (ResultType) { - i32, u32 => @intCast(ResultType, result_ty.of.i32), - i64, u64 => @intCast(ResultType, result_ty.of.i64), + i32, u32 => @as(ResultType, @intCast(result_ty.of.i32)), + i64, u64 => @as(ResultType, @intCast(result_ty.of.i64)), f32 => result_ty.of.f32, f64 => result_ty.of.f64, - *Func => @ptrCast(?*Func, result_ty.of.ref).?, - *Extern => @ptrCast(?*Extern, result_ty.of.ref).?, + *Func => @as(?*Func, @ptrCast(result_ty.of.ref)).?, + *Extern => @as(?*Extern, @ptrCast(result_ty.of.ref)).?, else => |ty| @compileError("Unsupported result type '" ++ @typeName(ty) ++ "'"), }; } @@ -302,7 +302,7 @@ pub const Instance = opaque { var exports = module.exports(); defer exports.deinit(); - return for (exports.toSlice()) |export_type, index| { + return for (exports.toSlice(), 0..) |export_type, index| { const ty = export_type orelse continue; const type_name = ty.name(); defer type_name.deinit(); @@ -674,7 +674,7 @@ pub const Value = extern struct { pub const Valtype = opaque { /// Initializes a new `Valtype` based on the given `Valkind` pub fn init(valKind: Valkind) *Valtype { - return wasm_valtype_new(@enumToInt(valKind)); + return wasm_valtype_new(@intFromEnum(valKind)); } pub fn deinit(self: *Valtype) void { @@ -683,7 +683,7 @@ pub const Valtype = opaque { /// Returns the `Valkind` of the given `Valtype` pub fn kind(self: *Valtype) Valkind { - return @intToEnum(Valkind, wasm_valtype_kind(self)); + return @as(Valkind, @enumFromInt(wasm_valtype_kind(self))); } extern "c" fn wasm_valtype_new(kind: u8) *Valtype; From f6665fa247694bc30e04d5a7b9bcc72c5a80689f Mon Sep 17 00:00:00 2001 From: Arnau478 Date: Tue, 22 Aug 2023 20:42:29 +0200 Subject: [PATCH 2/7] Added module support --- build.zig | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/build.zig b/build.zig index a8cae7e..3f30a4a 100644 --- a/build.zig +++ b/build.zig @@ -1,6 +1,6 @@ const std = @import("std"); -pub fn build(b: *std.Build) void { +pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); @@ -15,6 +15,12 @@ pub fn build(b: *std.Build) void { b.installArtifact(lib); + const module = b.createModule(.{ + .source_file = .{ .path = "src/main.zig" }, + }); + + try b.modules.put(b.dupe("wasm"), module); + var main_tests = b.addTest(.{ .root_source_file = .{ .path = "src/main.zig" }, .target = target, From 41539f053d65e913de3ca5ef4db4ad7497c9a242 Mon Sep 17 00:00:00 2001 From: Arnau478 Date: Tue, 22 Aug 2023 21:13:52 +0200 Subject: [PATCH 3/7] builtin.Type.Fn.args -> builtin.Type.Fn.params --- src/main.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.zig b/src/main.zig index 82a7fcc..a3b43fb 100644 --- a/src/main.zig +++ b/src/main.zig @@ -128,7 +128,7 @@ pub const Func = opaque { const cb_meta = @typeInfo(@TypeOf(callback)); switch (cb_meta) { .Fn => { - if (cb_meta.Fn.args.len > 0 or cb_meta.Fn.return_type.? != void) { + if (cb_meta.Fn.params.len > 0 or cb_meta.Fn.return_type.? != void) { @compileError("only callbacks with no input args and no results are currently supported"); } }, From aa595203137dcf44abc70fa1c43dd0ba075eb8ca Mon Sep 17 00:00:00 2001 From: Arnau478 Date: Tue, 22 Aug 2023 21:23:55 +0200 Subject: [PATCH 4/7] Update the method to get callback address Now functions are not pointers, so `@intFromPtr(callback)` should be changed to `@intFromPtr(&callback)` --- src/main.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.zig b/src/main.zig index a3b43fb..b0d3da9 100644 --- a/src/main.zig +++ b/src/main.zig @@ -134,7 +134,7 @@ pub const Func = opaque { }, else => @compileError("only functions can be used as callbacks into Wasm"), } - CALLBACK = @intFromPtr(callback); + CALLBACK = @intFromPtr(&callback); var args = ValtypeVec.empty(); var results = ValtypeVec.empty(); From e3f3e78d30ad3c474586cc6ac23a071af2addba5 Mon Sep 17 00:00:00 2001 From: Arnau478 Date: Tue, 22 Aug 2023 21:28:41 +0200 Subject: [PATCH 5/7] Now functions are not pointers by themselves `Callback` should now be a pointer --- src/main.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.zig b/src/main.zig index b0d3da9..6be9c53 100644 --- a/src/main.zig +++ b/src/main.zig @@ -584,7 +584,7 @@ pub const ExportTypeVec = extern struct { extern "c" fn wasm_exporttype_vec_delete(*ExportTypeVec) void; }; -pub const Callback = fn (?*const Valtype, ?*Valtype) callconv(.C) ?*Trap; +pub const Callback = *fn (?*const Valtype, ?*Valtype) callconv(.C) ?*Trap; pub const ByteVec = extern struct { size: usize, From 9db4d80803b1b416634b70ec86ff752489947bdd Mon Sep 17 00:00:00 2001 From: Arnau478 Date: Tue, 22 Aug 2023 21:33:36 +0200 Subject: [PATCH 6/7] Function pointers should be `const` --- src/main.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.zig b/src/main.zig index 6be9c53..e9c8023 100644 --- a/src/main.zig +++ b/src/main.zig @@ -584,7 +584,7 @@ pub const ExportTypeVec = extern struct { extern "c" fn wasm_exporttype_vec_delete(*ExportTypeVec) void; }; -pub const Callback = *fn (?*const Valtype, ?*Valtype) callconv(.C) ?*Trap; +pub const Callback = *const fn (?*const Valtype, ?*Valtype) callconv(.C) ?*Trap; pub const ByteVec = extern struct { size: usize, From 674092ab42e693c4d333758f6616e6b54c370a16 Mon Sep 17 00:00:00 2001 From: Arnau478 Date: Tue, 22 Aug 2023 21:37:13 +0200 Subject: [PATCH 7/7] `CALLBACK` should be casted to a pointer `CALLBACK` should be casted to a `*const fn () void` instead of `fn () void` --- src/main.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.zig b/src/main.zig index e9c8023..d693d85 100644 --- a/src/main.zig +++ b/src/main.zig @@ -102,7 +102,7 @@ pub const Module = opaque { fn cb(params: ?*const Valtype, results: ?*Valtype) callconv(.C) ?*Trap { _ = params; _ = results; - const func = @as(fn () void, @ptrFromInt(CALLBACK)); + const func = @as(*const fn () void, @ptrFromInt(CALLBACK)); func(); return null; }