Skip to content

Commit 498d0be

Browse files
committed
Add Equatable and GuestPointee conformances
To support Swift Concurrency tested in WebAssembly System Interface environment, `Clock` and `Subscription` types should conform to `GuestPointee` types, which provides size and alignment for correct ABI implementation. This fixes current build issues, where these types didn't have correct size and alignment specified.
1 parent 36060a0 commit 498d0be

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

Sources/WASI/WASI.swift

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,8 @@ enum WASIAbi {
428428
case END = 2
429429
}
430430

431-
struct Clock: GuestPointee {
432-
struct Flags: OptionSet, GuestPrimitivePointee {
431+
struct Clock: Equatable, GuestPointee {
432+
struct Flags: OptionSet, GuestPointee {
433433
let rawValue: UInt16
434434

435435
static let isAbsoluteTime = Self(rawValue: 1)
@@ -462,15 +462,15 @@ enum WASIAbi {
462462
}
463463
}
464464

465-
enum EventType: UInt8 {
465+
enum EventType: UInt8, GuestPointee {
466466
case clock
467467
case fdRead
468468
case fdWrite
469469
}
470470

471471
typealias UserData = UInt64
472472

473-
struct Subscription: Equatable {
473+
struct Subscription: Equatable, GuestPointee {
474474
enum Union: Equatable, GuestPointee {
475475
case clock(Clock)
476476
case fdRead(Fd)
@@ -514,6 +514,22 @@ enum WASIAbi {
514514
}
515515
}
516516
}
517+
518+
let userData: UserData
519+
let union: Union
520+
static var sizeInGuest: UInt32 = 48
521+
static var alignInGuest: UInt32 = max(UserData.alignInGuest, Union.alignInGuest)
522+
523+
static func readFromGuest(_ pointer: UnsafeGuestRawPointer) -> Self {
524+
var pointer = pointer
525+
return .init(userData: .readFromGuest(&pointer), union: .readFromGuest(&pointer))
526+
}
527+
528+
static func writeToGuest(at pointer: UnsafeGuestRawPointer, value: Self) {
529+
var pointer = pointer
530+
UserData.writeToGuest(at: &pointer, value: value.userData)
531+
Union.writeToGuest(at: &pointer, value: value.union)
532+
}
517533
}
518534

519535
struct Event: Equatable, GuestPointee {
@@ -563,7 +579,7 @@ enum WASIAbi {
563579
}
564580
}
565581

566-
enum ClockId: UInt32 {
582+
enum ClockId: UInt32, GuestPointee {
567583
/// The clock measuring real time. Time value zero corresponds with
568584
/// 1970-01-01T00:00:00Z.
569585
case REALTIME = 0

Sources/WasmTypes/GuestMemory.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ extension GuestPrimitivePointee {
3838
}
3939

4040
/// Auto implementation of ``GuestPointee`` for ``RawRepresentable`` types
41-
extension GuestPrimitivePointee where Self: RawRepresentable, Self.RawValue: GuestPointee {
41+
extension GuestPointee where Self: RawRepresentable, Self.RawValue: GuestPointee {
42+
public static var sizeInGuest: UInt32 {
43+
RawValue.sizeInGuest
44+
}
45+
46+
public static var alignInGuest: UInt32 {
47+
RawValue.alignInGuest
48+
}
49+
4250
/// Reads a value of RawValue type and constructs a value of Self type
4351
public static func readFromGuest(_ pointer: UnsafeGuestRawPointer) -> Self {
4452
Self(rawValue: .readFromGuest(pointer))!

0 commit comments

Comments
 (0)