Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to zig 0.11.0 #5

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

38 changes: 28 additions & 10 deletions build.zig
Original file line number Diff line number Diff line change
@@ -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);
}
14 changes: 0 additions & 14 deletions deps.zig

This file was deleted.

10 changes: 0 additions & 10 deletions gyro.zzz

This file was deleted.

30 changes: 15 additions & 15 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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();
Expand Down Expand Up @@ -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] } },
Expand All @@ -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);
Expand All @@ -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) ++ "'"),
};
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand Down