Skip to content

Commit 0cd16df

Browse files
committed
Remove all uses of CF from the framework's interface outside of Linux or Darwin
1 parent 02d9151 commit 0cd16df

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

Sources/Foundation/Port.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ open class SocketPort : Port {
659659
}
660660

661661
open override func schedule(in runLoop: RunLoop, forMode mode: RunLoop.Mode) {
662-
let loop = runLoop.getCFRunLoop()
662+
let loop = runLoop.currentCFRunLoop
663663
let loopKey = ObjectIdentifier(loop)
664664

665665
core.lock.synchronized {
@@ -685,7 +685,7 @@ open class SocketPort : Port {
685685
}
686686

687687
open override func remove(from runLoop: RunLoop, forMode mode: RunLoop.Mode) {
688-
let loop = runLoop.getCFRunLoop()
688+
let loop = runLoop.currentCFRunLoop
689689
let loopKey = ObjectIdentifier(loop)
690690

691691
core.lock.synchronized {

Sources/Foundation/RunLoop.swift

+21-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10+
#if os(Linux) || os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
1011
import CoreFoundation
12+
#else
13+
@_implementationOnly import CoreFoundation
14+
#endif
1115

1216
internal let kCFRunLoopEntry = CFRunLoopActivity.entry.rawValue
1317
internal let kCFRunLoopBeforeTimers = CFRunLoopActivity.beforeTimers.rawValue
@@ -83,10 +87,24 @@ open class RunLoop: NSObject {
8387
}
8488
}
8589

90+
// On platforms where it's available, getCFRunLoop() can be overridden and we use it below.
91+
// Make sure we honor the override -- var currentCFRunLoop will do so on platforms where overrides are available.
92+
93+
#if os(Linux) || os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
94+
internal var currentCFRunLoop: CFRunLoop { getCFRunLoop() }
95+
8696
@available(*, deprecated, message: "Directly accessing the run loop may cause your code to not become portable in the future.")
8797
open func getCFRunLoop() -> CFRunLoop {
8898
return _cfRunLoop
8999
}
100+
#else
101+
internal var currentCFRunLoop: CFRunLoop { _cfRunLoop }
102+
103+
@available(*, unavailable, message: "Core Foundation is not available on your platform.")
104+
open func getCFRunLoop() -> Never {
105+
fatalError()
106+
}
107+
#endif
90108

91109
open func add(_ timer: Timer, forMode mode: RunLoop.Mode) {
92110
CFRunLoopAddTimer(_cfRunLoop, timer._cfObject, mode._cfStringUniquingKnown)
@@ -206,7 +224,7 @@ extension RunLoop {
206224
}
207225

208226
public func perform(inModes modes: [RunLoop.Mode], block: @escaping () -> Void) {
209-
CFRunLoopPerformBlock(getCFRunLoop(), (modes.map { $0._cfStringUniquingKnown })._cfObject, block)
227+
CFRunLoopPerformBlock(currentCFRunLoop, (modes.map { $0._cfStringUniquingKnown })._cfObject, block)
210228
}
211229

212230
public func perform(_ block: @escaping () -> Void) {
@@ -219,13 +237,13 @@ extension RunLoop {
219237
extension RunLoop {
220238
@available(*, deprecated, message: "For XCTest use only.")
221239
public func _stop() {
222-
CFRunLoopStop(getCFRunLoop())
240+
CFRunLoopStop(currentCFRunLoop)
223241
}
224242

225243
@available(*, deprecated, message: "For XCTest use only.")
226244
public func _observe(_ activities: _Activities, in mode: RunLoop.Mode = .default, repeats: Bool = true, order: Int = 0, handler: @escaping (_Activity) -> Void) -> _Observer {
227245
let observer = _Observer(activities: activities, repeats: repeats, order: order, handler: handler)
228-
CFRunLoopAddObserver(self.getCFRunLoop(), observer.cfObserver, mode._cfStringUniquingKnown)
246+
CFRunLoopAddObserver(self.currentCFRunLoop, observer.cfObserver, mode._cfStringUniquingKnown)
229247
return observer
230248
}
231249

Sources/Foundation/Stream.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ open class InputStream: Stream {
172172
}
173173

174174
open override func schedule(in aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
175-
CFReadStreamScheduleWithRunLoop(_stream, aRunLoop.getCFRunLoop(), mode.rawValue._cfObject)
175+
CFReadStreamScheduleWithRunLoop(_stream, aRunLoop.currentCFRunLoop, mode.rawValue._cfObject)
176176
}
177177

178178
open override func remove(from aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
179-
CFReadStreamUnscheduleFromRunLoop(_stream, aRunLoop.getCFRunLoop(), mode.rawValue._cfObject)
179+
CFReadStreamUnscheduleFromRunLoop(_stream, aRunLoop.currentCFRunLoop, mode.rawValue._cfObject)
180180
}
181181
}
182182

@@ -251,11 +251,11 @@ open class OutputStream : Stream {
251251
}
252252

253253
open override func schedule(in aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
254-
CFWriteStreamScheduleWithRunLoop(_stream, aRunLoop.getCFRunLoop(), mode.rawValue._cfObject)
254+
CFWriteStreamScheduleWithRunLoop(_stream, aRunLoop.currentCFRunLoop, mode.rawValue._cfObject)
255255
}
256256

257257
open override func remove(from aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
258-
CFWriteStreamUnscheduleFromRunLoop(_stream, aRunLoop.getCFRunLoop(), mode.rawValue._cfObject)
258+
CFWriteStreamUnscheduleFromRunLoop(_stream, aRunLoop.currentCFRunLoop, mode.rawValue._cfObject)
259259
}
260260
}
261261

0 commit comments

Comments
 (0)