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

Update #9

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
zig-cache
zig-cache/
zig-out/
20 changes: 20 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const interface_mod = b.addModule("interface.zig", .{
.source_file = .{ .path = "src/interface.zig" },
});

const exe = b.addTest(.{
.root_source_file = .{ .path = "src/examples.zig" },
.target = target,
.optimize = optimize,
});

exe.addModule("interface", interface_mod);

b.getInstallStep().dependOn(&b.addRunArtifact(exe).step);
}
137 changes: 70 additions & 67 deletions examples.zig → src/examples.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const interface = @import("interface.zig");
const interface = @import("interface");
const Interface = interface.Interface;
const SelfType = interface.SelfType;

Expand All @@ -11,7 +11,7 @@ test "Simple NonOwning interface" {
const NonOwningTest = struct {
fn run() !void {
const Fooer = Interface(struct {
foo: fn (*SelfType) usize,
foo: *const fn (*SelfType) usize,
}, interface.Storage.NonOwning);

const TestFooer = struct {
Expand All @@ -30,8 +30,8 @@ test "Simple NonOwning interface" {
var fooer = try Fooer.init(&f);
defer fooer.deinit();

expectEqual(@as(usize, 42), fooer.call("foo", .{}));
expectEqual(@as(usize, 43), fooer.call("foo", .{}));
try expectEqual(@as(usize, 42), fooer.call("foo", .{}));
try expectEqual(@as(usize, 43), fooer.call("foo", .{}));
}
};

Expand All @@ -42,7 +42,7 @@ test "Simple NonOwning interface" {
test "Comptime only interface" {
// return error.SkipZigTest;
const TestIFace = Interface(struct {
foo: fn (*SelfType, u8) u8,
foo: *const fn (*SelfType, u8) u8,
}, interface.Storage.Comptime);

const TestType = struct {
Expand All @@ -56,16 +56,16 @@ test "Comptime only interface" {
};

comptime var iface = try TestIFace.init(TestType{ .state = 0 });
expectEqual(@as(u8, 42), iface.call("foo", .{42}));
try expectEqual(@as(u8, 42), iface.call("foo", .{42}));
}

test "Owning interface with optional function and a non-method function" {
const OwningOptionalFuncTest = struct {
fn run() !void {
const TestOwningIface = Interface(struct {
someFn: ?fn (*const SelfType, usize, usize) usize,
otherFn: fn (*SelfType, usize) anyerror!void,
thirdFn: fn(usize) usize,
someFn: ?*const fn (*const SelfType, usize, usize) usize,
otherFn: *const fn (*SelfType, usize) anyerror!void,
thirdFn: *const fn (usize) usize,
}, interface.Storage.Owning);

const TestStruct = struct {
Expand All @@ -92,67 +92,70 @@ test "Owning interface with optional function and a non-method function" {
defer iface_instance.deinit();

try iface_instance.call("otherFn", .{100});
expectEqual(@as(usize, 42), iface_instance.call("someFn", .{ 0, 42 }).?);
expectEqual(@as(usize, 101), iface_instance.call("thirdFn", .{ 100 }));
try expectEqual(@as(usize, 42), iface_instance.call("someFn", .{ 0, 42 }).?);
try expectEqual(@as(usize, 101), iface_instance.call("thirdFn", .{100}));
}
};

try OwningOptionalFuncTest.run();
}

test "Interface with virtual async function implemented by an async function" {
const AsyncIFace = Interface(struct {
const async_call_stack_size = 1024;

foo: fn (*SelfType) callconv(.Async) void,
}, interface.Storage.NonOwning);

const Impl = struct {
const Self = @This();

state: usize,
frame: anyframe = undefined,

pub fn foo(self: *Self) void {
suspend {
self.frame = @frame();
}
self.state += 1;
suspend;
self.state += 1;
}
};

var i = Impl{ .state = 0 };
var instance = try AsyncIFace.init(&i);
_ = async instance.call("foo", .{});

expectEqual(@as(usize, 0), i.state);
resume i.frame;
expectEqual(@as(usize, 1), i.state);
resume i.frame;
expectEqual(@as(usize, 2), i.state);
}

test "Interface with virtual async function implemented by a blocking function" {
const AsyncIFace = Interface(struct {
readBytes: fn (*SelfType, []u8) callconv(.Async) anyerror!void,
}, interface.Storage.Inline(8));

const Impl = struct {
const Self = @This();

pub fn readBytes(self: Self, outBuf: []u8) void {
for (outBuf) |*c| {
c.* = 3;
}
}
};

var instance = try AsyncIFace.init(Impl{});

var buf: [256]u8 = undefined;
try await async instance.call("readBytes", .{buf[0..]});

expectEqual([_]u8{3} ** 256, buf);
}
// TODO: Include async tests when async is implemented in stage2!

// test "Interface with virtual async function implemented by an async function" {
// const AsyncIFace = Interface(struct {
// const async_call_stack_size = 1024;

// foo: *const fn (*SelfType) callconv(.Async) void,
// }, interface.Storage.NonOwning);

// const Impl = struct {
// const Self = @This();

// state: usize,
// frame: anyframe = undefined,

// pub fn foo(self: *Self) void {
// suspend {
// self.frame = @frame();
// }
// self.state += 1;
// suspend {}
// self.state += 1;
// }
// };

// var i = Impl{ .state = 0 };
// var instance = try AsyncIFace.init(&i);
// _ = async instance.call("foo", .{});

// try expectEqual(@as(usize, 0), i.state);
// resume i.frame;
// try expectEqual(@as(usize, 1), i.state);
// resume i.frame;
// try expectEqual(@as(usize, 2), i.state);
// }

// test "Interface with virtual async function implemented by a blocking function" {
// const AsyncIFace = Interface(struct {
// readBytes: *const fn (*SelfType, []u8) callconv(.Async) anyerror!void,
// }, interface.Storage.Inline(8));

// const Impl = struct {
// const Self = @This();

// pub fn readBytes(self: Self, outBuf: []u8) void {
// _ = self;
// for (outBuf) |*c| {
// c.* = 3;
// }
// }
// };

// var instance = try AsyncIFace.init(Impl{});

// var buf: [256]u8 = undefined;
// try await async instance.call("readBytes", .{buf[0..]});

// try expectEqual([_]u8{3} ** 256, buf);
// }
Loading