Skip to content

Commit fabd82b

Browse files
committed
Add Subscription type to WASIAbi
Testing Swift Concurrency with WebAssembly System Interface requires a missing `subscription` record per the corresponding standard specification: https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#-subscription-record This change adds a subscription union type, with the ABI described in this standard document.
1 parent 6b00f8f commit fabd82b

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Sources/WASI/WASI.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,52 @@ enum WASIAbi {
471471

472472
typealias UserData = UInt64
473473

474+
struct Subscription: Equatable {
475+
enum Union: Equatable, GuestPointee {
476+
case clock(Clock)
477+
case fdRead(Fd)
478+
case fdWrite(Fd)
479+
480+
static let sizeInGuest: UInt32 = 40
481+
static let alignInGuest: UInt32 = max(Clock.alignInGuest, Fd.alignInGuest)
482+
483+
static func readFromGuest(_ pointer: UnsafeGuestRawPointer) -> Self {
484+
var pointer = pointer
485+
let tag = UInt8.readFromGuest(&pointer)
486+
487+
switch tag {
488+
case 0:
489+
return .clock(.readFromGuest(&pointer))
490+
491+
case 1:
492+
return .fdRead(.readFromGuest(&pointer))
493+
494+
case 2:
495+
return .fdWrite(.readFromGuest(&pointer))
496+
497+
default:
498+
// FIXME: should this throw?
499+
fatalError()
500+
}
501+
}
502+
503+
static func writeToGuest(at pointer: UnsafeGuestRawPointer, value: Self) {
504+
var pointer = pointer
505+
switch value {
506+
case .clock(let clock):
507+
UInt8.writeToGuest(at: &pointer, value: 0)
508+
Clock.writeToGuest(at: &pointer, value: clock)
509+
case .fdRead(let fd):
510+
UInt8.writeToGuest(at: &pointer, value: 1)
511+
Fd.writeToGuest(at: &pointer, value: fd)
512+
case .fdWrite(let fd):
513+
UInt8.writeToGuest(at: &pointer, value: 2)
514+
Fd.writeToGuest(at: &pointer, value: fd)
515+
}
516+
}
517+
}
518+
}
519+
474520
enum ClockId: UInt32 {
475521
/// The clock measuring real time. Time value zero corresponds with
476522
/// 1970-01-01T00:00:00Z.

0 commit comments

Comments
 (0)