Skip to content

Commit ba14406

Browse files
authored
Merge pull request #1572 from millenomi/LinuxAsBridging-4.2
[4.2] Adopt ‘as’ bridging on Linux
2 parents caca409 + 7eb2e6f commit ba14406

33 files changed

+151
-137
lines changed

Foundation/AffineTransform.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
476476
}
477477
}
478478

479-
extension AffineTransform : _ObjectTypeBridgeable {
479+
extension AffineTransform : _ObjectiveCBridgeable {
480480
public static func _isBridgedToObjectiveC() -> Bool {
481481
return true
482482
}

Foundation/Array.swift

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

10-
extension Array : _ObjectTypeBridgeable {
10+
extension Array : _ObjectiveCBridgeable {
1111

1212
public typealias _ObjectType = NSArray
1313
public func _bridgeToObjectiveC() -> _ObjectType {

Foundation/Bridging.swift

+6-25
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,10 @@
1212

1313
import CoreFoundation
1414

15-
// Support protocols for casting
16-
public protocol _ObjectBridgeable {
17-
func _bridgeToAnyObject() -> AnyObject
18-
}
19-
2015
public protocol _StructBridgeable {
2116
func _bridgeToAny() -> Any
2217
}
2318

24-
/// - Note: This is a similar interface to the _ObjectiveCBridgeable protocol
25-
public protocol _ObjectTypeBridgeable : _ObjectBridgeable {
26-
associatedtype _ObjectType : AnyObject
27-
28-
func _bridgeToObjectiveC() -> _ObjectType
29-
30-
static func _forceBridgeFromObjectiveC(_ source: _ObjectType, result: inout Self?)
31-
32-
@discardableResult
33-
static func _conditionallyBridgeFromObjectiveC(_ source: _ObjectType, result: inout Self?) -> Bool
34-
35-
static func _unconditionallyBridgeFromObjectiveC(_ source: _ObjectType?) -> Self
36-
}
37-
3819
/// - Note: This does not exist currently on Darwin but it is the inverse correlation to the bridge types such that a
3920
/// reference type can be converted via a callout to a conversion method.
4021
public protocol _StructTypeBridgeable : _StructBridgeable {
@@ -44,7 +25,7 @@ public protocol _StructTypeBridgeable : _StructBridgeable {
4425
}
4526

4627
// Default adoption of the type specific variants to the Any variant
47-
extension _ObjectTypeBridgeable {
28+
extension _ObjectiveCBridgeable {
4829
public func _bridgeToAnyObject() -> AnyObject {
4930
return _bridgeToObjectiveC()
5031
}
@@ -74,8 +55,8 @@ internal protocol _NSBridgeable {
7455

7556

7657
/// - Note: This is an internal boxing value for containing abstract structures
77-
internal final class _SwiftValue : NSObject, NSCopying {
78-
internal private(set) var value: Any
58+
internal final class _SwiftValue : NSObject, NSCopying, _NSSwiftValue {
59+
public private(set) var value: Any
7960

8061
static func fetch(_ object: AnyObject?) -> Any? {
8162
if let obj = object {
@@ -108,10 +89,8 @@ internal final class _SwiftValue : NSObject, NSCopying {
10889
static func store(_ value: Any) -> NSObject {
10990
if let val = value as? NSObject {
11091
return val
111-
} else if let val = value as? _ObjectBridgeable {
112-
return val._bridgeToAnyObject() as! NSObject
11392
} else {
114-
return _SwiftValue(value)
93+
return (value as AnyObject) as! NSObject
11594
}
11695
}
11796

@@ -144,4 +123,6 @@ internal final class _SwiftValue : NSObject, NSCopying {
144123
public func copy(with zone: NSZone?) -> Any {
145124
return _SwiftValue(value)
146125
}
126+
127+
public static let null: AnyObject = NSNull()
147128
}

Foundation/Calendar.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ extension Calendar : CustomDebugStringConvertible, CustomStringConvertible, Cust
11631163
}
11641164
}
11651165

1166-
extension Calendar: _ObjectTypeBridgeable {
1166+
extension Calendar: _ObjectiveCBridgeable {
11671167
public typealias _ObjectType = NSCalendar
11681168

11691169
@_semantics("convertToObjectiveC")

Foundation/CharacterSet.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
481481

482482

483483
// MARK: Objective-C Bridging
484-
extension CharacterSet : _ObjectTypeBridgeable {
484+
extension CharacterSet : _ObjectiveCBridgeable {
485485
public static func _isBridgedToObjectiveC() -> Bool {
486486
return true
487487
}

Foundation/Data.swift

+1-7
Original file line numberDiff line numberDiff line change
@@ -1893,13 +1893,7 @@ extension Data {
18931893

18941894
/// Provides bridging functionality for struct Data to class NSData and vice-versa.
18951895

1896-
#if DEPLOYMENT_RUNTIME_SWIFT
1897-
internal typealias DataBridgeType = _ObjectTypeBridgeable
1898-
#else
1899-
internal typealias DataBridgeType = _ObjectiveCBridgeable
1900-
#endif
1901-
1902-
extension Data : DataBridgeType {
1896+
extension Data : _ObjectiveCBridgeable {
19031897
@_semantics("convertToObjectiveC")
19041898
public func _bridgeToObjectiveC() -> NSData {
19051899
return _backing.bridgedReference(_sliceRange)

Foundation/Date.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ extension Date : CustomDebugStringConvertible, CustomStringConvertible, CustomRe
230230
}
231231
}
232232

233-
extension Date : _ObjectTypeBridgeable {
233+
extension Date : _ObjectiveCBridgeable {
234234
@_semantics("convertToObjectiveC")
235235
public func _bridgeToObjectiveC() -> NSDate {
236236
return NSDate(timeIntervalSinceReferenceDate: _time)

Foundation/DateComponents.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ extension DateComponents : CustomStringConvertible, CustomDebugStringConvertible
352352

353353
// MARK: - Bridging
354354

355-
extension DateComponents : _ObjectTypeBridgeable {
355+
extension DateComponents : _ObjectiveCBridgeable {
356356
public static func _isBridgedToObjectiveC() -> Bool {
357357
return true
358358
}

Foundation/DateInterval.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ extension DateInterval : CustomStringConvertible, CustomDebugStringConvertible,
187187
}
188188
}
189189

190-
extension DateInterval : _ObjectTypeBridgeable {
190+
extension DateInterval : _ObjectiveCBridgeable {
191191
public static func _isBridgedToObjectiveC() -> Bool {
192192
return true
193193
}

Foundation/Dictionary.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import CoreFoundation
1111

12-
extension Dictionary : _ObjectTypeBridgeable {
12+
extension Dictionary : _ObjectiveCBridgeable {
1313

1414
public typealias _ObjectType = NSDictionary
1515
public func _bridgeToObjectiveC() -> _ObjectType {

Foundation/IndexPath.swift

+1-7
Original file line numberDiff line numberDiff line change
@@ -774,13 +774,7 @@ extension IndexPath : CustomStringConvertible, CustomDebugStringConvertible, Cus
774774
}
775775
}
776776

777-
#if DEPLOYMENT_RUNTIME_SWIFT
778-
internal typealias IndexPathBridgeType = _ObjectTypeBridgeable
779-
#else
780-
internal typealias IndexPathBridgeType = _ObjectiveCBridgeable
781-
#endif
782-
783-
extension IndexPath : IndexPathBridgeType {
777+
extension IndexPath : _ObjectiveCBridgeable {
784778
public static func _getObjectiveCType() -> Any.Type {
785779
return NSIndexPath.self
786780
}

Foundation/IndexSet.swift

+1-7
Original file line numberDiff line numberDiff line change
@@ -790,13 +790,7 @@ private func _toNSRange(_ r: Range<IndexSet.Element>) -> NSRange {
790790
return NSRange(location: r.lowerBound, length: r.upperBound - r.lowerBound)
791791
}
792792

793-
#if DEPLOYMENT_RUNTIME_SWIFT
794-
internal typealias IndexSetBridgeType = _ObjectTypeBridgeable
795-
#else
796-
internal typealias IndexSetBridgeType = _ObjectiveCBridgeable
797-
#endif
798-
799-
extension IndexSet : IndexSetBridgeType {
793+
extension IndexSet : _ObjectiveCBridgeable {
800794
public static func _getObjectiveCType() -> Any.Type {
801795
return NSIndexSet.self
802796
}

Foundation/JSONSerialization.swift

+15-6
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ open class JSONSerialization : NSObject {
6262
return true
6363
}
6464

65-
if obj is String || obj is NSNull || obj is Int || obj is Bool || obj is UInt ||
66-
obj is Int8 || obj is Int16 || obj is Int32 || obj is Int64 ||
67-
obj is UInt8 || obj is UInt16 || obj is UInt32 || obj is UInt64 {
68-
return true
65+
if !(obj is _NSNumberCastingWithoutBridging) {
66+
if obj is String || obj is NSNull || obj is Int || obj is Bool || obj is UInt ||
67+
obj is Int8 || obj is Int16 || obj is Int32 || obj is Int64 ||
68+
obj is UInt8 || obj is UInt16 || obj is UInt32 || obj is UInt64 {
69+
return true
70+
}
6971
}
7072

7173
// object is a Double and is not NaN or infinity
@@ -315,12 +317,19 @@ private struct JSONWriter {
315317
self.writer = writer
316318
}
317319

318-
mutating func serializeJSON(_ obj: Any?) throws {
320+
mutating func serializeJSON(_ object: Any?) throws {
321+
322+
var toSerialize = object
319323

320-
guard let obj = obj else {
324+
if let number = toSerialize as? _NSNumberCastingWithoutBridging {
325+
toSerialize = number._swiftValueOfOptimalType
326+
}
327+
328+
guard let obj = toSerialize else {
321329
try serializeNull()
322330
return
323331
}
332+
324333
// For better performance, the most expensive conditions to evaluate should be last.
325334
switch (obj) {
326335
case let str as String:

Foundation/Locale.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ public struct Locale : CustomStringConvertible, CustomDebugStringConvertible, Ha
439439
}
440440

441441

442-
extension Locale : _ObjectTypeBridgeable {
442+
extension Locale : _ObjectiveCBridgeable {
443443
public static func _isBridgedToObjectiveC() -> Bool {
444444
return true
445445
}

Foundation/Measurement.swift

+1-7
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,8 @@ extension Measurement {
206206

207207
// Implementation note: similar to NSArray, NSDictionary, etc., NSMeasurement's import as an ObjC generic type is suppressed by the importer. Eventually we will need a more general purpose mechanism to correctly import generic types.
208208

209-
#if DEPLOYMENT_RUNTIME_SWIFT
210-
internal typealias MeasurementBridgeType = _ObjectTypeBridgeable
211-
#else
212-
internal typealias MeasurementBridgeType = _ObjectiveCBridgeable
213-
#endif
214-
215209
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
216-
extension Measurement : MeasurementBridgeType {
210+
extension Measurement : _ObjectiveCBridgeable {
217211
@_semantics("convertToObjectiveC")
218212
public func _bridgeToObjectiveC() -> NSMeasurement {
219213
return NSMeasurement(doubleValue: value, unit: unit)

Foundation/NSArray.swift

+4-5
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,12 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
334334
if val1 != val2 {
335335
return false
336336
}
337-
} else if let val1 = object(at: idx) as? _ObjectBridgeable,
338-
let val2 = otherArray[idx] as? _ObjectBridgeable {
339-
if !(val1._bridgeToAnyObject() as! NSObject).isEqual(val2._bridgeToAnyObject()) {
337+
} else {
338+
let val1 = object(at: idx)
339+
let val2 = otherArray[idx]
340+
if !((val1 as AnyObject) as! NSObject).isEqual(val2 as AnyObject) {
340341
return false
341342
}
342-
} else {
343-
return false
344343
}
345344
}
346345

Foundation/NSDecimalNumber.swift

+13-10
Original file line numberDiff line numberDiff line change
@@ -297,28 +297,28 @@ open class NSDecimalNumber : NSNumber {
297297
// return 'd' for double
298298

299299
open override var int8Value: Int8 {
300-
return Int8(decimal.doubleValue)
300+
return Int8(exactly: decimal.doubleValue) ?? 0 as Int8
301301
}
302302
open override var uint8Value: UInt8 {
303-
return UInt8(decimal.doubleValue)
303+
return UInt8(exactly: decimal.doubleValue) ?? 0 as UInt8
304304
}
305305
open override var int16Value: Int16 {
306-
return Int16(decimal.doubleValue)
306+
return Int16(exactly: decimal.doubleValue) ?? 0 as Int16
307307
}
308308
open override var uint16Value: UInt16 {
309-
return UInt16(decimal.doubleValue)
309+
return UInt16(exactly: decimal.doubleValue) ?? 0 as UInt16
310310
}
311311
open override var int32Value: Int32 {
312-
return Int32(decimal.doubleValue)
312+
return Int32(exactly: decimal.doubleValue) ?? 0 as Int32
313313
}
314314
open override var uint32Value: UInt32 {
315-
return UInt32(decimal.doubleValue)
315+
return UInt32(exactly: decimal.doubleValue) ?? 0 as UInt32
316316
}
317317
open override var int64Value: Int64 {
318-
return Int64(decimal.doubleValue)
318+
return Int64(exactly: decimal.doubleValue) ?? 0 as Int64
319319
}
320320
open override var uint64Value: UInt64 {
321-
return UInt64(decimal.doubleValue)
321+
return UInt64(exactly: decimal.doubleValue) ?? 0 as UInt64
322322
}
323323
open override var floatValue: Float {
324324
return Float(decimal.doubleValue)
@@ -330,17 +330,20 @@ open class NSDecimalNumber : NSNumber {
330330
return !decimal.isZero
331331
}
332332
open override var intValue: Int {
333-
return Int(decimal.doubleValue)
333+
return Int(exactly: decimal.doubleValue) ?? 0 as Int
334334
}
335335
open override var uintValue: UInt {
336-
return UInt(decimal.doubleValue)
336+
return UInt(exactly: decimal.doubleValue) ?? 0 as UInt
337337
}
338338

339339
open override func isEqual(_ value: Any?) -> Bool {
340340
guard let other = value as? NSDecimalNumber else { return false }
341341
return self.decimal == other.decimal
342342
}
343343

344+
override var _swiftValueOfOptimalType: Any {
345+
return decimal
346+
}
344347
}
345348

346349
// return an approximate double value

Foundation/NSDictionary.swift

+4-5
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,12 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
395395
if otherValue != value {
396396
return false
397397
}
398-
} else if let otherBridgeable = otherDictionary[key as! AnyHashable] as? _ObjectBridgeable,
399-
let bridgeable = object(forKey: key)! as? _ObjectBridgeable {
400-
if !(otherBridgeable._bridgeToAnyObject() as! NSObject).isEqual(bridgeable._bridgeToAnyObject()) {
398+
} else {
399+
let otherBridgeable = otherDictionary[key as! AnyHashable]
400+
let bridgeable = object(forKey: key)!
401+
if !((otherBridgeable as AnyObject) as! NSObject).isEqual(bridgeable as AnyObject) {
401402
return false
402403
}
403-
} else {
404-
return false
405404
}
406405
}
407406

0 commit comments

Comments
 (0)