Skip to content

Commit 7e25bb8

Browse files
[wasm] Port Sources/Foundation/FileHandle.swift
* Unsupport background read/write operations * Unsupport Pipe
1 parent 514e2bd commit 7e25bb8

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

Sources/Foundation/FileHandle.swift

+26-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
//
99

1010
@_implementationOnly import CoreFoundation
11+
#if canImport(Dispatch)
1112
import Dispatch
13+
#endif
1214

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

9193
private var _closeOnDealloc: Bool
9294

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

9598
private var readabilitySource: DispatchSourceProtocol? // Guarded by privateAsyncVariablesLock
@@ -213,6 +216,7 @@ open class FileHandle : NSObject {
213216
}
214217
}
215218
}
219+
#endif // canImport(Dispatch)
216220

217221
open var availableData: Data {
218222
_checkFileHandle()
@@ -626,6 +630,7 @@ open class FileHandle : NSObject {
626630
}
627631

628632
private func performOnQueueIfExists(_ block: () throws -> Void) throws {
633+
#if canImport(Dispatch)
629634
if let queue = queueIfExists {
630635
var theError: Swift.Error?
631636
queue.sync {
@@ -637,6 +642,9 @@ open class FileHandle : NSObject {
637642
} else {
638643
try block()
639644
}
645+
#else
646+
try block()
647+
#endif
640648
}
641649

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

662+
#if canImport(Dispatch)
654663
privateAsyncVariablesLock.lock()
655664
writabilitySource?.cancel()
656665
readabilitySource?.cancel()
@@ -659,6 +668,7 @@ open class FileHandle : NSObject {
659668
writabilitySource = nil
660669
readabilitySource = nil
661670
privateAsyncVariablesLock.unlock()
671+
#endif
662672

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

863873
open func readInBackgroundAndNotify(forModes modes: [RunLoop.Mode]?) {
874+
#if !canImport(Dispatch)
875+
NSUnsupported()
876+
#else
864877
_checkFileHandle()
865878

866879
privateAsyncVariablesLock.lock()
@@ -915,13 +928,17 @@ extension FileHandle {
915928
operation(data, error)
916929
}
917930
#endif
931+
#endif // canImport(Dispatch)
918932
}
919933

920934
open func readToEndOfFileInBackgroundAndNotify() {
921935
readToEndOfFileInBackgroundAndNotify(forModes: [.default])
922936
}
923937

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

@@ -963,11 +980,12 @@ extension FileHandle {
963980
NotificationQueue.default.enqueue(Notification(name: .NSFileHandleReadToEndOfFileCompletion, object: self, userInfo: userInfo), postingStyle: .asap, coalesceMask: .none, forModes: modes)
964981
}
965982
}
983+
#endif
966984
}
967985

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

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

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

10331054
owner.resume()
1055+
#endif
10341056
}
10351057
}
10361058

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

0 commit comments

Comments
 (0)