diff --git a/build.zig b/build.zig index 43294bb..cb0fe32 100644 --- a/build.zig +++ b/build.zig @@ -104,7 +104,7 @@ pub fn build(b: *std.Build) !void { // Benchmarks and examples const benchmarks = try buildBenchmarks(b, target); - const examples = try buildExamples(b, target, optimize, static_lib); + const examples = if (emit_examples) try buildExamples(b, target, optimize, static_lib) else null; // Test Executable const test_exe: *Step.Compile = test_exe: { @@ -150,7 +150,7 @@ pub fn build(b: *std.Build) !void { } } }, ).step); }; - if (emit_examples) for (examples) |exe| { + if (examples) |list| for (list) |exe| { b.getInstallStep().dependOn(&b.addInstallArtifact( exe, .{ .dest_dir = .{ .override = .{ diff --git a/src/backend/wasi_poll.zig b/src/backend/wasi_poll.zig index a3cfd18..2b1ddb4 100644 --- a/src/backend/wasi_poll.zig +++ b/src/backend/wasi_poll.zig @@ -6,6 +6,11 @@ const posix = std.posix; const queue = @import("../queue.zig"); const heap = @import("../heap.zig"); const xev = @import("../main.zig").WasiPoll; +const looppkg = @import("../loop.zig"); +const Callback = looppkg.Callback(@This()); +const CallbackAction = looppkg.CallbackAction; +const CompletionState = looppkg.CompletionState; +const noopCallback = looppkg.NoopCallback(@This()); /// True if this backend is available on this platform. pub fn available() bool { @@ -252,10 +257,10 @@ pub const Loop = struct { self.batch.array[0] = .{ .userdata = 0, .u = .{ - .tag = wasi.EVENTTYPE_CLOCK, + .tag = wasi.eventtype_t.CLOCK, .u = .{ .clock = .{ - .id = @as(u32, @bitCast(posix.CLOCK.MONOTONIC)), + .id = .MONOTONIC, .timeout = timeout, .precision = 1 * std.time.ns_per_ms, .flags = wasi.SUBSCRIPTION_CLOCK_ABSTIME, @@ -374,9 +379,9 @@ pub const Loop = struct { .shutdown => |v| res: { const how: wasi.sdflags_t = switch (v.how) { - .both => wasi.SHUT.WR | wasi.SHUT.RD, - .recv => wasi.SHUT.RD, - .send => wasi.SHUT.WR, + .both => .{ .WR = true, .RD = true }, + .recv => .{ .RD = true }, + .send => .{ .WR = true }, }; break :res .{ @@ -580,7 +585,7 @@ pub const Loop = struct { fn timer_next(next_ms: u64) wasi.timestamp_t { // Get the absolute time we'll execute this timer next. var now_ts: wasi.timestamp_t = undefined; - switch (wasi.clock_time_get(@as(u32, @bitCast(posix.CLOCK.MONOTONIC)), 1, &now_ts)) { + switch (wasi.clock_time_get(.MONOTONIC, 1, &now_ts)) { .SUCCESS => {}, .INVAL => unreachable, else => unreachable, @@ -593,7 +598,7 @@ pub const Loop = struct { fn get_now() !wasi.timestamp_t { var ts: wasi.timestamp_t = undefined; - return switch (wasi.clock_time_get(posix.CLOCK.MONOTONIC, 1, &ts)) { + return switch (wasi.clock_time_get(.MONOTONIC, 1, &ts)) { .SUCCESS => ts, .INVAL => error.UnsupportedClock, else => |err| posix.unexpectedErrno(err), @@ -608,7 +613,7 @@ pub const Completion = struct { /// Userdata and callback for when the completion is finished. userdata: ?*anyopaque = null, - callback: xev.Callback = xev.noopCallback, + callback: Callback = noopCallback, //--------------------------------------------------------------- // Internal fields @@ -665,7 +670,7 @@ pub const Completion = struct { .read => |v| .{ .userdata = @intFromPtr(self), .u = .{ - .tag = wasi.EVENTTYPE_FD_READ, + .tag = wasi.eventtype_t.FD_READ, .u = .{ .fd_read = .{ .fd = v.fd, @@ -677,7 +682,7 @@ pub const Completion = struct { .pread => |v| .{ .userdata = @intFromPtr(self), .u = .{ - .tag = wasi.EVENTTYPE_FD_READ, + .tag = wasi.eventtype_t.FD_READ, .u = .{ .fd_read = .{ .fd = v.fd, @@ -689,7 +694,7 @@ pub const Completion = struct { .write => |v| .{ .userdata = @intFromPtr(self), .u = .{ - .tag = wasi.EVENTTYPE_FD_WRITE, + .tag = wasi.eventtype_t.FD_WRITE, .u = .{ .fd_write = .{ .fd = v.fd, @@ -701,7 +706,7 @@ pub const Completion = struct { .pwrite => |v| .{ .userdata = @intFromPtr(self), .u = .{ - .tag = wasi.EVENTTYPE_FD_WRITE, + .tag = wasi.eventtype_t.FD_WRITE, .u = .{ .fd_write = .{ .fd = v.fd, @@ -713,7 +718,7 @@ pub const Completion = struct { .accept => |v| .{ .userdata = @intFromPtr(self), .u = .{ - .tag = wasi.EVENTTYPE_FD_READ, + .tag = wasi.eventtype_t.FD_READ, .u = .{ .fd_read = .{ .fd = v.socket, @@ -725,7 +730,7 @@ pub const Completion = struct { .recv => |v| .{ .userdata = @intFromPtr(self), .u = .{ - .tag = wasi.EVENTTYPE_FD_READ, + .tag = wasi.eventtype_t.FD_READ, .u = .{ .fd_read = .{ .fd = v.fd, @@ -737,7 +742,7 @@ pub const Completion = struct { .send => |v| .{ .userdata = @intFromPtr(self), .u = .{ - .tag = wasi.EVENTTYPE_FD_WRITE, + .tag = wasi.eventtype_t.FD_WRITE, .u = .{ .fd_write = .{ .fd = v.fd, @@ -773,7 +778,7 @@ pub const Completion = struct { .accept => |*op| res: { var out_fd: posix.fd_t = undefined; break :res .{ - .accept = switch (wasi.sock_accept(op.socket, 0, &out_fd)) { + .accept = switch (wasi.sock_accept(op.socket, .{}, &out_fd)) { .SUCCESS => out_fd, else => |err| posix.unexpectedErrno(err), }, @@ -1518,31 +1523,15 @@ test "wasi: file" { var loop = try Loop.init(.{}); defer loop.deinit(); + var tmpdir = testing.tmpDir(.{}); + defer tmpdir.cleanup(); + // Create a file - const path = "zig-cache/wasi-test-file.txt"; - const dir = std.fs.cwd(); - // We can't use dir.createFile yet: https://github.com/ziglang/zig/issues/14324 - const f = f: { - const w = wasi; - const oflags = w.O.CREAT | w.O.TRUNC; - const base: w.rights_t = w.RIGHT.FD_WRITE | - w.RIGHT.FD_READ | - w.RIGHT.FD_DATASYNC | - w.RIGHT.FD_SEEK | - w.RIGHT.FD_TELL | - w.RIGHT.FD_FDSTAT_SET_FLAGS | - w.RIGHT.FD_SYNC | - w.RIGHT.FD_ALLOCATE | - w.RIGHT.FD_ADVISE | - w.RIGHT.FD_FILESTAT_SET_TIMES | - w.RIGHT.FD_FILESTAT_SET_SIZE | - w.RIGHT.FD_FILESTAT_GET | - w.RIGHT.POLL_FD_READWRITE; - const fdflags: w.fdflags_t = w.FDFLAG.SYNC | w.FDFLAG.RSYNC | w.FDFLAG.DSYNC; - const fd = try posix.openatWasi(dir.fd, path, 0x0, oflags, 0x0, base, fdflags); - break :f std.fs.File{ .handle = fd }; - }; - defer dir.deleteFile(path) catch unreachable; + const path = "wasi-test-file.txt"; + const f = try tmpdir.dir.createFile(path, .{ + .truncate = true, + .read = true, + }); defer f.close(); // Start a reader @@ -1641,7 +1630,7 @@ test "wasi: file" { try loop.run(.until_done); // Read and verify we've written - const f_verify = try dir.openFile(path, .{}); + const f_verify = try tmpdir.dir.openFile(path, .{}); defer f_verify.close(); read_len = try f_verify.readAll(&read_buf); try testing.expectEqualStrings(write_buf, read_buf[0..read_len.?]);