Skip to content

[wasm] Port Sources/Foundation/FileHandle.swift #4916

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

Merged
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
29 changes: 26 additions & 3 deletions Sources/Foundation/FileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
//

@_implementationOnly import CoreFoundation
#if canImport(Dispatch)
import Dispatch
#endif

// FileHandle has a .read(upToCount:) method. Just invoking read() will cause an ambiguity warning. Use _read instead.
// Same with close()/.close().
Expand Down Expand Up @@ -90,6 +92,7 @@ open class FileHandle : NSObject {

private var _closeOnDealloc: Bool

#if canImport(Dispatch)
private var currentBackgroundActivityOwner: AnyObject? // Guarded by privateAsyncVariablesLock

private var readabilitySource: DispatchSourceProtocol? // Guarded by privateAsyncVariablesLock
Expand Down Expand Up @@ -213,6 +216,7 @@ open class FileHandle : NSObject {
}
}
}
#endif // canImport(Dispatch)

open var availableData: Data {
_checkFileHandle()
Expand Down Expand Up @@ -626,6 +630,7 @@ open class FileHandle : NSObject {
}

private func performOnQueueIfExists(_ block: () throws -> Void) throws {
#if canImport(Dispatch)
if let queue = queueIfExists {
var theError: Swift.Error?
queue.sync {
Expand All @@ -637,6 +642,9 @@ open class FileHandle : NSObject {
} else {
try block()
}
#else
try block()
#endif
}

@available(swift 5.0)
Expand All @@ -651,6 +659,7 @@ open class FileHandle : NSObject {
guard self != FileHandle._nulldeviceFileHandle else { return }
guard _isPlatformHandleValid else { return }

#if canImport(Dispatch)
privateAsyncVariablesLock.lock()
writabilitySource?.cancel()
readabilitySource?.cancel()
Expand All @@ -659,6 +668,7 @@ open class FileHandle : NSObject {
writabilitySource = nil
readabilitySource = nil
privateAsyncVariablesLock.unlock()
#endif

#if os(Windows)
// SR-13822 - Not Closing the file descriptor on Windows causes a Stack Overflow
Expand Down Expand Up @@ -861,6 +871,9 @@ extension FileHandle {
}

open func readInBackgroundAndNotify(forModes modes: [RunLoop.Mode]?) {
#if !canImport(Dispatch)
NSUnsupported()
#else
_checkFileHandle()

privateAsyncVariablesLock.lock()
Expand Down Expand Up @@ -915,13 +928,17 @@ extension FileHandle {
operation(data, error)
}
#endif
#endif // canImport(Dispatch)
}

open func readToEndOfFileInBackgroundAndNotify() {
readToEndOfFileInBackgroundAndNotify(forModes: [.default])
}

open func readToEndOfFileInBackgroundAndNotify(forModes modes: [RunLoop.Mode]?) {
#if !canImport(Dispatch) || !canImport(Dispatch)
NSUnsupported()
#else
privateAsyncVariablesLock.lock()
guard currentBackgroundActivityOwner == nil else { fatalError("No two activities can occur at the same time") }

Expand Down Expand Up @@ -963,11 +980,12 @@ extension FileHandle {
NotificationQueue.default.enqueue(Notification(name: .NSFileHandleReadToEndOfFileCompletion, object: self, userInfo: userInfo), postingStyle: .asap, coalesceMask: .none, forModes: modes)
}
}
#endif
}

@available(Windows, unavailable, message: "A SOCKET cannot be treated as a fd")
open func acceptConnectionInBackgroundAndNotify() {
#if os(Windows)
#if os(Windows) || !canImport(Dispatch)
NSUnsupported()
#else
acceptConnectionInBackgroundAndNotify(forModes: [.default])
Expand All @@ -976,7 +994,7 @@ extension FileHandle {

@available(Windows, unavailable, message: "A SOCKET cannot be treated as a fd")
open func acceptConnectionInBackgroundAndNotify(forModes modes: [RunLoop.Mode]?) {
#if os(Windows)
#if os(Windows) || !canImport(Dispatch)
NSUnsupported()
#else
let owner = monitor(forReading: true, resumed: false) { (handle, source) in
Expand Down Expand Up @@ -1014,6 +1032,9 @@ extension FileHandle {
}

open func waitForDataInBackgroundAndNotify(forModes modes: [RunLoop.Mode]?) {
#if !canImport(Dispatch)
NSUnsupported()
#else
let owner = monitor(forReading: true, resumed: false) { (handle, source) in
source.cancel()
DispatchQueue.main.async {
Expand All @@ -1031,6 +1052,7 @@ extension FileHandle {
privateAsyncVariablesLock.unlock()

owner.resume()
#endif
}
}

Expand All @@ -1052,6 +1074,8 @@ open class Pipe: NSObject {
closeOnDealloc: true)
self.fileHandleForWriting = FileHandle(handle: hWritePipe!,
closeOnDealloc: true)
#elseif os(WASI)
NSUnsupported()
#else
/// the `pipe` system call creates two `fd` in a malloc'ed area
let fds = UnsafeMutablePointer<Int32>.allocate(capacity: 2)
Expand All @@ -1078,4 +1102,3 @@ open class Pipe: NSObject {
super.init()
}
}