Skip to content

Commit 36060a0

Browse files
committed
Add poll_oneoff stub with new ABI types
Testing Swift Concurrency for WebAssembly requires presence of `epoll_oneoff`, which is absent in WasmKit. This change provides a stub according to the ABI specified in WebAssembly System Interface standard https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#poll_oneoff The stub has no concrete implementation, but provides placeholders that map to clock and file descriptor value that can be polled as specified in the standard.
1 parent ac56b0d commit 36060a0

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

Sources/WASI/WASI.swift

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,16 @@ protocol WASI {
211211

212212
/// Concurrently poll for the occurrence of a set of events.
213213
func poll_oneoff(
214-
subscriptions: UnsafeGuestRawPointer,
215-
events: UnsafeGuestRawPointer,
216-
numberOfSubscriptions: WASIAbi.Size
214+
subscriptions: UnsafeGuestBufferPointer<WASIAbi.Subscription>,
215+
events: UnsafeGuestBufferPointer<WASIAbi.Event>
217216
) throws -> WASIAbi.Size
218217

219218
/// Write high-quality random data into a buffer.
220219
func random_get(buffer: UnsafeGuestPointer<UInt8>, length: WASIAbi.Size)
221220
}
222221

223222
enum WASIAbi {
224-
enum Errno: UInt32, Error {
223+
enum Errno: UInt32, Error, GuestPointee {
225224
/// No error occurred. System call completed successfully.
226225
case SUCCESS = 0
227226
/// Argument list too long.
@@ -952,7 +951,6 @@ public struct WASIHostModule {
952951
extension WASI {
953952
var _hostModules: [String: WASIHostModule] {
954953
let unimplementedFunctionTypes: [String: FunctionType] = [
955-
"poll_oneoff": .init(parameters: [.i32, .i32, .i32, .i32], results: [.i32]),
956954
"proc_raise": .init(parameters: [.i32], results: [.i32]),
957955
"sched_yield": .init(parameters: [], results: [.i32]),
958956
"sock_accept": .init(parameters: [.i32, .i32, .i32], results: [.i32]),
@@ -1493,6 +1491,24 @@ extension WASI {
14931491
}
14941492
}
14951493

1494+
preview1["poll_oneoff"] = wasiFunction(
1495+
type: .init(parameters: [.i32, .i32, .i32, .i32], results: [.i32])
1496+
) { caller, arguments in
1497+
try withMemoryBuffer(caller: caller) { buffer in
1498+
let subscriptionsBaseAddress = UnsafeGuestPointer<WASIAbi.Subscription>(memorySpace: buffer, offset: arguments[0].i32)
1499+
let eventsBaseAddress = UnsafeGuestPointer<WASIAbi.Event>(memorySpace: buffer, offset: arguments[1].i32)
1500+
let size = try self.poll_oneoff(
1501+
subscriptions: .init(baseAddress: subscriptionsBaseAddress, count: arguments[2].i32),
1502+
events: .init(baseAddress: eventsBaseAddress, count: arguments[2].i32)
1503+
)
1504+
buffer.withUnsafeMutableBufferPointer(offset: .init(arguments[3].i32), count: MemoryLayout<UInt32>.size) { raw in
1505+
raw.withMemoryRebound(to: UInt32.self) { rebound in rebound[0] = size.littleEndian }
1506+
}
1507+
1508+
return [.i32(WASIAbi.Errno.SUCCESS.rawValue)]
1509+
}
1510+
}
1511+
14961512
return [
14971513
"wasi_snapshot_preview1": WASIHostModule(functions: preview1)
14981514
]
@@ -1977,11 +1993,24 @@ public class WASIBridgeToHost: WASI {
19771993
}
19781994

19791995
func poll_oneoff(
1980-
subscriptions: UnsafeGuestRawPointer,
1981-
events: UnsafeGuestRawPointer,
1982-
numberOfSubscriptions: WASIAbi.Size
1996+
subscriptions: UnsafeGuestBufferPointer<WASIAbi.Subscription>,
1997+
events: UnsafeGuestBufferPointer<WASIAbi.Event>
19831998
) throws -> WASIAbi.Size {
1984-
throw WASIAbi.Errno.ENOTSUP
1999+
for subscription in subscriptions {
2000+
switch subscription.union {
2001+
case .clock:
2002+
throw WASIAbi.Errno.ENOTSUP
2003+
2004+
case .fdRead(let fd), .fdWrite(let fd):
2005+
guard case let .file(entry) = self.fdTable[fd] else {
2006+
throw WASIAbi.Errno.EBADF
2007+
2008+
}
2009+
throw WASIAbi.Errno.ENOTSUP
2010+
}
2011+
}
2012+
2013+
return 0
19852014
}
19862015

19872016
func random_get(buffer: UnsafeGuestPointer<UInt8>, length: WASIAbi.Size) {

0 commit comments

Comments
 (0)