Skip to content

Commit a0f0d38

Browse files
fix: wasi32-wasm compatibility
1 parent b1e572f commit a0f0d38

File tree

2 files changed

+32
-43
lines changed

2 files changed

+32
-43
lines changed

build.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub fn build(b: *std.Build) !void {
104104

105105
// Benchmarks and examples
106106
const benchmarks = try buildBenchmarks(b, target);
107-
const examples = try buildExamples(b, target, optimize, static_lib);
107+
const examples = if (emit_examples) try buildExamples(b, target, optimize, static_lib) else null;
108108

109109
// Test Executable
110110
const test_exe: *Step.Compile = test_exe: {
@@ -150,7 +150,7 @@ pub fn build(b: *std.Build) !void {
150150
} } },
151151
).step);
152152
};
153-
if (emit_examples) for (examples) |exe| {
153+
if (examples) |list| for (list) |exe| {
154154
b.getInstallStep().dependOn(&b.addInstallArtifact(
155155
exe,
156156
.{ .dest_dir = .{ .override = .{

src/backend/wasi_poll.zig

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ const posix = std.posix;
66
const queue = @import("../queue.zig");
77
const heap = @import("../heap.zig");
88
const xev = @import("../main.zig").WasiPoll;
9+
const looppkg = @import("../loop.zig");
10+
const Callback = looppkg.Callback(@This());
11+
const CallbackAction = looppkg.CallbackAction;
12+
const CompletionState = looppkg.CompletionState;
13+
const noopCallback = looppkg.NoopCallback(@This());
914

1015
/// True if this backend is available on this platform.
1116
pub fn available() bool {
@@ -252,10 +257,10 @@ pub const Loop = struct {
252257
self.batch.array[0] = .{
253258
.userdata = 0,
254259
.u = .{
255-
.tag = wasi.EVENTTYPE_CLOCK,
260+
.tag = wasi.eventtype_t.CLOCK,
256261
.u = .{
257262
.clock = .{
258-
.id = @as(u32, @bitCast(posix.CLOCK.MONOTONIC)),
263+
.id = .MONOTONIC,
259264
.timeout = timeout,
260265
.precision = 1 * std.time.ns_per_ms,
261266
.flags = wasi.SUBSCRIPTION_CLOCK_ABSTIME,
@@ -374,9 +379,9 @@ pub const Loop = struct {
374379

375380
.shutdown => |v| res: {
376381
const how: wasi.sdflags_t = switch (v.how) {
377-
.both => wasi.SHUT.WR | wasi.SHUT.RD,
378-
.recv => wasi.SHUT.RD,
379-
.send => wasi.SHUT.WR,
382+
.both => .{ .WR = true, .RD = true },
383+
.recv => .{ .RD = true },
384+
.send => .{ .WR = true },
380385
};
381386

382387
break :res .{
@@ -580,7 +585,7 @@ pub const Loop = struct {
580585
fn timer_next(next_ms: u64) wasi.timestamp_t {
581586
// Get the absolute time we'll execute this timer next.
582587
var now_ts: wasi.timestamp_t = undefined;
583-
switch (wasi.clock_time_get(@as(u32, @bitCast(posix.CLOCK.MONOTONIC)), 1, &now_ts)) {
588+
switch (wasi.clock_time_get(.MONOTONIC, 1, &now_ts)) {
584589
.SUCCESS => {},
585590
.INVAL => unreachable,
586591
else => unreachable,
@@ -593,7 +598,7 @@ pub const Loop = struct {
593598

594599
fn get_now() !wasi.timestamp_t {
595600
var ts: wasi.timestamp_t = undefined;
596-
return switch (wasi.clock_time_get(posix.CLOCK.MONOTONIC, 1, &ts)) {
601+
return switch (wasi.clock_time_get(.MONOTONIC, 1, &ts)) {
597602
.SUCCESS => ts,
598603
.INVAL => error.UnsupportedClock,
599604
else => |err| posix.unexpectedErrno(err),
@@ -608,7 +613,7 @@ pub const Completion = struct {
608613

609614
/// Userdata and callback for when the completion is finished.
610615
userdata: ?*anyopaque = null,
611-
callback: xev.Callback = xev.noopCallback,
616+
callback: Callback = noopCallback,
612617

613618
//---------------------------------------------------------------
614619
// Internal fields
@@ -665,7 +670,7 @@ pub const Completion = struct {
665670
.read => |v| .{
666671
.userdata = @intFromPtr(self),
667672
.u = .{
668-
.tag = wasi.EVENTTYPE_FD_READ,
673+
.tag = wasi.eventtype_t.FD_READ,
669674
.u = .{
670675
.fd_read = .{
671676
.fd = v.fd,
@@ -677,7 +682,7 @@ pub const Completion = struct {
677682
.pread => |v| .{
678683
.userdata = @intFromPtr(self),
679684
.u = .{
680-
.tag = wasi.EVENTTYPE_FD_READ,
685+
.tag = wasi.eventtype_t.FD_READ,
681686
.u = .{
682687
.fd_read = .{
683688
.fd = v.fd,
@@ -689,7 +694,7 @@ pub const Completion = struct {
689694
.write => |v| .{
690695
.userdata = @intFromPtr(self),
691696
.u = .{
692-
.tag = wasi.EVENTTYPE_FD_WRITE,
697+
.tag = wasi.eventtype_t.FD_WRITE,
693698
.u = .{
694699
.fd_write = .{
695700
.fd = v.fd,
@@ -701,7 +706,7 @@ pub const Completion = struct {
701706
.pwrite => |v| .{
702707
.userdata = @intFromPtr(self),
703708
.u = .{
704-
.tag = wasi.EVENTTYPE_FD_WRITE,
709+
.tag = wasi.eventtype_t.FD_WRITE,
705710
.u = .{
706711
.fd_write = .{
707712
.fd = v.fd,
@@ -713,7 +718,7 @@ pub const Completion = struct {
713718
.accept => |v| .{
714719
.userdata = @intFromPtr(self),
715720
.u = .{
716-
.tag = wasi.EVENTTYPE_FD_READ,
721+
.tag = wasi.eventtype_t.FD_READ,
717722
.u = .{
718723
.fd_read = .{
719724
.fd = v.socket,
@@ -725,7 +730,7 @@ pub const Completion = struct {
725730
.recv => |v| .{
726731
.userdata = @intFromPtr(self),
727732
.u = .{
728-
.tag = wasi.EVENTTYPE_FD_READ,
733+
.tag = wasi.eventtype_t.FD_READ,
729734
.u = .{
730735
.fd_read = .{
731736
.fd = v.fd,
@@ -737,7 +742,7 @@ pub const Completion = struct {
737742
.send => |v| .{
738743
.userdata = @intFromPtr(self),
739744
.u = .{
740-
.tag = wasi.EVENTTYPE_FD_WRITE,
745+
.tag = wasi.eventtype_t.FD_WRITE,
741746
.u = .{
742747
.fd_write = .{
743748
.fd = v.fd,
@@ -773,7 +778,7 @@ pub const Completion = struct {
773778
.accept => |*op| res: {
774779
var out_fd: posix.fd_t = undefined;
775780
break :res .{
776-
.accept = switch (wasi.sock_accept(op.socket, 0, &out_fd)) {
781+
.accept = switch (wasi.sock_accept(op.socket, .{}, &out_fd)) {
777782
.SUCCESS => out_fd,
778783
else => |err| posix.unexpectedErrno(err),
779784
},
@@ -1518,31 +1523,15 @@ test "wasi: file" {
15181523
var loop = try Loop.init(.{});
15191524
defer loop.deinit();
15201525

1526+
var tmpdir = testing.tmpDir(.{});
1527+
defer tmpdir.cleanup();
1528+
15211529
// Create a file
1522-
const path = "zig-cache/wasi-test-file.txt";
1523-
const dir = std.fs.cwd();
1524-
// We can't use dir.createFile yet: https://github.com/ziglang/zig/issues/14324
1525-
const f = f: {
1526-
const w = wasi;
1527-
const oflags = w.O.CREAT | w.O.TRUNC;
1528-
const base: w.rights_t = w.RIGHT.FD_WRITE |
1529-
w.RIGHT.FD_READ |
1530-
w.RIGHT.FD_DATASYNC |
1531-
w.RIGHT.FD_SEEK |
1532-
w.RIGHT.FD_TELL |
1533-
w.RIGHT.FD_FDSTAT_SET_FLAGS |
1534-
w.RIGHT.FD_SYNC |
1535-
w.RIGHT.FD_ALLOCATE |
1536-
w.RIGHT.FD_ADVISE |
1537-
w.RIGHT.FD_FILESTAT_SET_TIMES |
1538-
w.RIGHT.FD_FILESTAT_SET_SIZE |
1539-
w.RIGHT.FD_FILESTAT_GET |
1540-
w.RIGHT.POLL_FD_READWRITE;
1541-
const fdflags: w.fdflags_t = w.FDFLAG.SYNC | w.FDFLAG.RSYNC | w.FDFLAG.DSYNC;
1542-
const fd = try posix.openatWasi(dir.fd, path, 0x0, oflags, 0x0, base, fdflags);
1543-
break :f std.fs.File{ .handle = fd };
1544-
};
1545-
defer dir.deleteFile(path) catch unreachable;
1530+
const path = "wasi-test-file.txt";
1531+
const f = try tmpdir.dir.createFile(path, .{
1532+
.truncate = true,
1533+
.read = true,
1534+
});
15461535
defer f.close();
15471536

15481537
// Start a reader
@@ -1641,7 +1630,7 @@ test "wasi: file" {
16411630
try loop.run(.until_done);
16421631

16431632
// Read and verify we've written
1644-
const f_verify = try dir.openFile(path, .{});
1633+
const f_verify = try tmpdir.dir.openFile(path, .{});
16451634
defer f_verify.close();
16461635
read_len = try f_verify.readAll(&read_buf);
16471636
try testing.expectEqualStrings(write_buf, read_buf[0..read_len.?]);

0 commit comments

Comments
 (0)