Skip to content

Add poll_oneoff for POSIX hosts to WASI.swift #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let DarwinPlatforms: [Platform]

let package = Package(
name: "WasmKit",
platforms: [.macOS(.v10_13), .iOS(.v12)],
platforms: [.macOS(.v13), .iOS(.v16)],
products: [
.executable(name: "wasmkit-cli", targets: ["CLI"]),
.library(name: "WasmKit", targets: ["WasmKit"]),
Expand Down
1 change: 1 addition & 0 deletions Sources/WASI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add_wasmkit_library(WASI
FileSystem.swift
GuestMemorySupport.swift
Clock.swift
Poll.swift
RandomBufferGenerator.swift
WASI.swift
)
Expand Down
83 changes: 83 additions & 0 deletions Sources/WASI/Poll.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import SystemPackage
import WasmTypes

#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Android)
import Android
#elseif os(Windows)
import ucrt
#else
#error("Unsupported Platform")
#endif

extension FdTable {
func fileDescriptor(fd: WASIAbi.Fd) throws -> FileDescriptor {
guard case let .file(entry) = self[fd], let fd = (entry as? FdWASIEntry)?.fd else {
throw WASIAbi.Errno.EBADF
}

return fd
}
}

func poll(
subscriptions: some Sequence<WASIAbi.Subscription>,
events: UnsafeGuestBufferPointer<WASIAbi.Event>,
_ fdTable: FdTable
) throws -> WASIAbi.Size {
#if os(Windows)
throw WASIAbi.Errno.ENOTSUP
#else
var pollfds = [pollfd]()
var fdUserData = [WASIAbi.UserData]()
var timeoutMilliseconds = UInt.max
var clockUserData: WASIAbi.UserData?

for subscription in subscriptions {
let union = subscription.union
switch union {
case .clock(let clock):
timeoutMilliseconds = min(timeoutMilliseconds, .init(clock.timeout / 1_000_000))
clockUserData = subscription.userData
case .fdRead(let fd):
pollfds.append(.init(fd: try fdTable.fileDescriptor(fd: fd).rawValue, events: .init(POLLIN), revents: 0))
fdUserData.append(subscription.userData)
case .fdWrite(let fd):
pollfds.append(.init(fd: try fdTable.fileDescriptor(fd: fd).rawValue, events: .init(POLLOUT), revents: 0))
fdUserData.append(subscription.userData)
}
}

let result = poll(&pollfds, .init(pollfds.count), .init(timeoutMilliseconds))
let err = errno // Preserve `errno` global immediately after `poll`
var updatedEvents: WASIAbi.Size = 0
if result == 0, let clockUserData {
updatedEvents += 1
events[0] = .init(userData: clockUserData, error: .SUCCESS, eventType: .clock, fdReadWrite: .init(nBytes: 0, flags: .init(rawValue: 0)))
} else if result > 0 {
for (i, fd) in pollfds.enumerated() {
updatedEvents += 1
switch fd.revents {
case .init(POLLIN):
events[.init(i)] = .init(userData: fdUserData[i], error: .SUCCESS, eventType: .fdRead, fdReadWrite: .init(nBytes: 0, flags: []))
case .init(POLLOUT):
events[.init(i)] = .init(userData: fdUserData[i], error: .SUCCESS, eventType: .fdWrite, fdReadWrite: .init(nBytes: 0, flags: []))
default: throw WASIAbi.Errno.ENOTSUP
}
}
} else {
switch err {
case ENOMEM: throw WASIAbi.Errno.ENOMEM
case EINTR: throw WASIAbi.Errno.EINTR
case EINVAL: throw WASIAbi.Errno.EINVAL
default: throw WASIAbi.Errno.ENOTSUP
}
}
return updatedEvents
#endif
}
Loading
Loading