Skip to content
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

Adopt ‘as’ bridging on Linux. #1526

Merged
merged 1 commit into from
May 31, 2018
Merged
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 Foundation/AffineTransform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
}
}

extension AffineTransform : _ObjectTypeBridgeable {
extension AffineTransform : _ObjectiveCBridgeable {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main change is removing the parallel type _ObjectTypeBridgeable and replacing it with the newly-exposed-on-Linux _ObjectiveCBridgeable.

public static func _isBridgedToObjectiveC() -> Bool {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion Foundation/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//

extension Array : _ObjectTypeBridgeable {
extension Array : _ObjectiveCBridgeable {

public typealias _ObjectType = NSArray
public func _bridgeToObjectiveC() -> _ObjectType {
Expand Down
31 changes: 6 additions & 25 deletions Foundation/Bridging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,10 @@

import CoreFoundation

// Support protocols for casting
public protocol _ObjectBridgeable {
func _bridgeToAnyObject() -> AnyObject
}

public protocol _StructBridgeable {
func _bridgeToAny() -> Any
}

/// - Note: This is a similar interface to the _ObjectiveCBridgeable protocol
public protocol _ObjectTypeBridgeable : _ObjectBridgeable {
associatedtype _ObjectType : AnyObject

func _bridgeToObjectiveC() -> _ObjectType

static func _forceBridgeFromObjectiveC(_ source: _ObjectType, result: inout Self?)

@discardableResult
static func _conditionallyBridgeFromObjectiveC(_ source: _ObjectType, result: inout Self?) -> Bool

static func _unconditionallyBridgeFromObjectiveC(_ source: _ObjectType?) -> Self
}

/// - Note: This does not exist currently on Darwin but it is the inverse correlation to the bridge types such that a
/// reference type can be converted via a callout to a conversion method.
public protocol _StructTypeBridgeable : _StructBridgeable {
Expand All @@ -44,7 +25,7 @@ public protocol _StructTypeBridgeable : _StructBridgeable {
}

// Default adoption of the type specific variants to the Any variant
extension _ObjectTypeBridgeable {
extension _ObjectiveCBridgeable {
public func _bridgeToAnyObject() -> AnyObject {
return _bridgeToObjectiveC()
}
Expand Down Expand Up @@ -74,8 +55,8 @@ internal protocol _NSBridgeable {


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

static func fetch(_ object: AnyObject?) -> Any? {
if let obj = object {
Expand Down Expand Up @@ -108,10 +89,8 @@ internal final class _SwiftValue : NSObject, NSCopying {
static func store(_ value: Any) -> NSObject {
if let val = value as? NSObject {
return val
} else if let val = value as? _ObjectBridgeable {
return val._bridgeToAnyObject() as! NSObject
} else {
return _SwiftValue(value)
return (value as AnyObject) as! NSObject
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this change leads to crash in cases like this:

class A {}
let array: NSArray = [A()]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}

Expand Down Expand Up @@ -144,4 +123,6 @@ internal final class _SwiftValue : NSObject, NSCopying {
public func copy(with zone: NSZone?) -> Any {
return _SwiftValue(value)
}

public static let null: AnyObject = NSNull()
}
2 changes: 1 addition & 1 deletion Foundation/Calendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ extension Calendar : CustomDebugStringConvertible, CustomStringConvertible, Cust
}
}

extension Calendar: _ObjectTypeBridgeable {
extension Calendar: _ObjectiveCBridgeable {
public typealias _ObjectType = NSCalendar

@_semantics("convertToObjectiveC")
Expand Down
2 changes: 1 addition & 1 deletion Foundation/CharacterSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb


// MARK: Objective-C Bridging
extension CharacterSet : _ObjectTypeBridgeable {
extension CharacterSet : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
Expand Down
8 changes: 1 addition & 7 deletions Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1893,13 +1893,7 @@ extension Data {

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

#if DEPLOYMENT_RUNTIME_SWIFT
internal typealias DataBridgeType = _ObjectTypeBridgeable
#else
internal typealias DataBridgeType = _ObjectiveCBridgeable
#endif

extension Data : DataBridgeType {
extension Data : _ObjectiveCBridgeable {
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSData {
return _backing.bridgedReference(_sliceRange)
Expand Down
2 changes: 1 addition & 1 deletion Foundation/Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ extension Date : CustomDebugStringConvertible, CustomStringConvertible, CustomRe
}
}

extension Date : _ObjectTypeBridgeable {
extension Date : _ObjectiveCBridgeable {
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSDate {
return NSDate(timeIntervalSinceReferenceDate: _time)
Expand Down
2 changes: 1 addition & 1 deletion Foundation/DateComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ extension DateComponents : CustomStringConvertible, CustomDebugStringConvertible

// MARK: - Bridging

extension DateComponents : _ObjectTypeBridgeable {
extension DateComponents : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion Foundation/DateInterval.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ extension DateInterval : CustomStringConvertible, CustomDebugStringConvertible,
}
}

extension DateInterval : _ObjectTypeBridgeable {
extension DateInterval : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion Foundation/Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import CoreFoundation

extension Dictionary : _ObjectTypeBridgeable {
extension Dictionary : _ObjectiveCBridgeable {

public typealias _ObjectType = NSDictionary
public func _bridgeToObjectiveC() -> _ObjectType {
Expand Down
8 changes: 1 addition & 7 deletions Foundation/IndexPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -774,13 +774,7 @@ extension IndexPath : CustomStringConvertible, CustomDebugStringConvertible, Cus
}
}

#if DEPLOYMENT_RUNTIME_SWIFT
internal typealias IndexPathBridgeType = _ObjectTypeBridgeable
#else
internal typealias IndexPathBridgeType = _ObjectiveCBridgeable
#endif

extension IndexPath : IndexPathBridgeType {
extension IndexPath : _ObjectiveCBridgeable {
public static func _getObjectiveCType() -> Any.Type {
return NSIndexPath.self
}
Expand Down
8 changes: 1 addition & 7 deletions Foundation/IndexSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -790,13 +790,7 @@ private func _toNSRange(_ r: Range<IndexSet.Element>) -> NSRange {
return NSRange(location: r.lowerBound, length: r.upperBound - r.lowerBound)
}

#if DEPLOYMENT_RUNTIME_SWIFT
internal typealias IndexSetBridgeType = _ObjectTypeBridgeable
#else
internal typealias IndexSetBridgeType = _ObjectiveCBridgeable
#endif

extension IndexSet : IndexSetBridgeType {
extension IndexSet : _ObjectiveCBridgeable {
public static func _getObjectiveCType() -> Any.Type {
return NSIndexSet.self
}
Expand Down
21 changes: 15 additions & 6 deletions Foundation/JSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ open class JSONSerialization : NSObject {
return true
}

if obj is String || obj is NSNull || obj is Int || obj is Bool || obj is UInt ||
obj is Int8 || obj is Int16 || obj is Int32 || obj is Int64 ||
obj is UInt8 || obj is UInt16 || obj is UInt32 || obj is UInt64 {
return true
if !(obj is _NSNumberCastingWithoutBridging) {
if obj is String || obj is NSNull || obj is Int || obj is Bool || obj is UInt ||
obj is Int8 || obj is Int16 || obj is Int32 || obj is Int64 ||
obj is UInt8 || obj is UInt16 || obj is UInt32 || obj is UInt64 {
return true
}
}

// object is a Double and is not NaN or infinity
Expand Down Expand Up @@ -315,12 +317,19 @@ private struct JSONWriter {
self.writer = writer
}

mutating func serializeJSON(_ obj: Any?) throws {
mutating func serializeJSON(_ object: Any?) throws {

var toSerialize = object

guard let obj = obj else {
if let number = toSerialize as? _NSNumberCastingWithoutBridging {
toSerialize = number._swiftValueOfOptimalType
}

guard let obj = toSerialize else {
try serializeNull()
return
}

// For better performance, the most expensive conditions to evaluate should be last.
switch (obj) {
case let str as String:
Expand Down
2 changes: 1 addition & 1 deletion Foundation/Locale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ public struct Locale : CustomStringConvertible, CustomDebugStringConvertible, Ha
}


extension Locale : _ObjectTypeBridgeable {
extension Locale : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
Expand Down
8 changes: 1 addition & 7 deletions Foundation/Measurement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,8 @@ extension Measurement {

// 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.

#if DEPLOYMENT_RUNTIME_SWIFT
internal typealias MeasurementBridgeType = _ObjectTypeBridgeable
#else
internal typealias MeasurementBridgeType = _ObjectiveCBridgeable
#endif

@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
extension Measurement : MeasurementBridgeType {
extension Measurement : _ObjectiveCBridgeable {
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSMeasurement {
return NSMeasurement(doubleValue: value, unit: unit)
Expand Down
9 changes: 4 additions & 5 deletions Foundation/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,12 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
if val1 != val2 {
return false
}
} else if let val1 = object(at: idx) as? _ObjectBridgeable,
let val2 = otherArray[idx] as? _ObjectBridgeable {
if !(val1._bridgeToAnyObject() as! NSObject).isEqual(val2._bridgeToAnyObject()) {
} else {
let val1 = object(at: idx)
let val2 = otherArray[idx]
if !((val1 as AnyObject) as! NSObject).isEqual(val2 as AnyObject) {
return false
}
} else {
return false
}
}

Expand Down
23 changes: 13 additions & 10 deletions Foundation/NSDecimalNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,28 +297,28 @@ open class NSDecimalNumber : NSNumber {
// return 'd' for double

open override var int8Value: Int8 {
return Int8(decimal.doubleValue)
return Int8(exactly: decimal.doubleValue) ?? 0 as Int8
}
open override var uint8Value: UInt8 {
return UInt8(decimal.doubleValue)
return UInt8(exactly: decimal.doubleValue) ?? 0 as UInt8
}
open override var int16Value: Int16 {
return Int16(decimal.doubleValue)
return Int16(exactly: decimal.doubleValue) ?? 0 as Int16
}
open override var uint16Value: UInt16 {
return UInt16(decimal.doubleValue)
return UInt16(exactly: decimal.doubleValue) ?? 0 as UInt16
}
open override var int32Value: Int32 {
return Int32(decimal.doubleValue)
return Int32(exactly: decimal.doubleValue) ?? 0 as Int32
}
open override var uint32Value: UInt32 {
return UInt32(decimal.doubleValue)
return UInt32(exactly: decimal.doubleValue) ?? 0 as UInt32
}
open override var int64Value: Int64 {
return Int64(decimal.doubleValue)
return Int64(exactly: decimal.doubleValue) ?? 0 as Int64
}
open override var uint64Value: UInt64 {
return UInt64(decimal.doubleValue)
return UInt64(exactly: decimal.doubleValue) ?? 0 as UInt64
}
open override var floatValue: Float {
return Float(decimal.doubleValue)
Expand All @@ -330,17 +330,20 @@ open class NSDecimalNumber : NSNumber {
return !decimal.isZero
}
open override var intValue: Int {
return Int(decimal.doubleValue)
return Int(exactly: decimal.doubleValue) ?? 0 as Int
}
open override var uintValue: UInt {
return UInt(decimal.doubleValue)
return UInt(exactly: decimal.doubleValue) ?? 0 as UInt
}

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

override var _swiftValueOfOptimalType: Any {
return decimal
}
}

// return an approximate double value
Expand Down
9 changes: 4 additions & 5 deletions Foundation/NSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,12 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
if otherValue != value {
return false
}
} else if let otherBridgeable = otherDictionary[key as! AnyHashable] as? _ObjectBridgeable,
let bridgeable = object(forKey: key)! as? _ObjectBridgeable {
if !(otherBridgeable._bridgeToAnyObject() as! NSObject).isEqual(bridgeable._bridgeToAnyObject()) {
} else {
let otherBridgeable = otherDictionary[key as! AnyHashable]
let bridgeable = object(forKey: key)!
if !((otherBridgeable as AnyObject) as! NSObject).isEqual(bridgeable as AnyObject) {
return false
}
} else {
return false
}
}

Expand Down
Loading