Skip to content

Commit af360f6

Browse files
committed
Add Clock subscription type to WASIAbi
The change declares a clock subscription type in according with the WebAssembly System Interface standard ABI, which is required for testing clocks and timers in Embedded Swift for WebAssembly. The ABI for this type is specified publicly in corresponding ABI documentation: https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#subscription_clock rdar://149935761
1 parent 3c89f3f commit af360f6

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Sources/WASI/WASI.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,48 @@ enum WASIAbi {
429429
case END = 2
430430
}
431431

432+
struct Clock: GuestPointee {
433+
struct Flags: OptionSet, GuestPrimitivePointee {
434+
let rawValue: UInt16
435+
436+
static let isAbsoluteTime = Self(rawValue: 1)
437+
}
438+
439+
let id: ClockId
440+
let timeout: Timestamp
441+
let precision: Timestamp
442+
let flags: Flags
443+
444+
static let sizeInGuest: UInt32 = 32
445+
static let alignInGuest: UInt32 = max(ClockId.alignInGuest, Timestamp.alignInGuest, Flags.alignInGuest)
446+
447+
static func readFromGuest(_ pointer: UnsafeGuestRawPointer) -> Self {
448+
var pointer = pointer
449+
return .init(
450+
id: .readFromGuest(&pointer),
451+
timeout: .readFromGuest(&pointer),
452+
precision: .readFromGuest(&pointer),
453+
flags: .readFromGuest(&pointer)
454+
)
455+
}
456+
457+
static func writeToGuest(at pointer: UnsafeGuestRawPointer, value: Self) {
458+
var pointer = pointer
459+
ClockId.writeToGuest(at: &pointer, value: value.id)
460+
Timestamp.writeToGuest(at: &pointer, value: value.timeout)
461+
Timestamp.writeToGuest(at: &pointer, value: value.precision)
462+
Flags.writeToGuest(at: &pointer, value: value.flags)
463+
}
464+
}
465+
466+
enum EventType: UInt8 {
467+
case clock
468+
case fdRead
469+
case fdWrite
470+
}
471+
472+
typealias UserData = UInt64
473+
432474
enum ClockId: UInt32 {
433475
/// The clock measuring real time. Time value zero corresponds with
434476
/// 1970-01-01T00:00:00Z.

0 commit comments

Comments
 (0)