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..3f30a4a 100644 --- a/build.zig +++ b/build.zig @@ -1,17 +1,35 @@ 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); + + 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, + .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..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 = @intToPtr(fn () void, CALLBACK); + const func = @as(*const fn () void, @ptrFromInt(CALLBACK)); func(); return null; } @@ -128,13 +128,13 @@ 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"); } }, 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(); @@ -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, @@ -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;