Files
swift-mirror/test/stdlib/Bridgeable.swift
Doug Gregor df4429fc5d Perform the class-or-ObjC-existential checks first in the runtime bridging entrypoints.
We decided that classes and Objective-C existentials are always going
to be bridged verbatim, ignoring any _BridgedToObjectiveC conformances.
While the compiler isn't preventing conformance of a class type to
_BridgedToObjectiveC, we ignore this case and will find a way to lock
it down if _BridgedToObjectiveC loses its underscore.

The motivation here is that (1) we don't really have a use case for a
class that bridges to Objective-C in any way other than just being an
Objective-C class, and (2) the class-or-ObjC-existential check is
ridiculously cheap compared to the
thread-safe-hash-table-over-a-dlsym-call used to find witness tables,
so do the cheap, common check first.



Swift SVN r18177
2014-05-16 05:18:34 +00:00

91 lines
2.7 KiB
Swift

// RUN: %target-run-simple-swift | FileCheck %s
// CHECK: testing...
println("testing...")
class C {}
func bridgedStatus<T>(_: T.Type) -> String {
let bridged = isBridgedToObjectiveC(T.self)
let verbatim = isBridgedVerbatimToObjectiveC(T.self)
if !bridged && verbatim {
return "IS NOT BRIDGED BUT IS VERBATIM?!"
}
return bridged ?
verbatim ? "is bridged verbatim" : "is custom-bridged"
: "is unbridged"
}
func testBridging<T>(x: T, name: String) {
println("\(name) \(bridgedStatus(T.self))")
var b : String
if let result: AnyObject = bridgeToObjectiveC(x) {
b = "bridged as " + (
(result as C) ? "C" : (result as T) ? "itself" : "an unknown type")
}
else {
b = "did not bridge"
}
println("\(name) instance \(b)")
}
//===----------------------------------------------------------------------===//
struct BridgedValueType : _BridgedToObjectiveC {
static func getObjectiveCType() -> Any.Type {
return C.self
}
func bridgeToObjectiveC() -> C {
return C()
}
static func bridgeFromObjectiveC(x: C) -> BridgedValueType? {
fatal("implement")
}
}
// CHECK-NEXT: BridgedValueType is custom-bridged
// CHECK-NEXT: BridgedValueType instance bridged as C
testBridging(BridgedValueType(), "BridgedValueType")
//===----------------------------------------------------------------------===//
struct UnbridgedValueType {}
// CHECK-NEXT: UnbridgedValueType is unbridged
// CHECK-NEXT: UnbridgedValueType instance did not bridge
testBridging(UnbridgedValueType(), "UnbridgedValueType")
//===----------------------------------------------------------------------===//
class PlainClass {}
// CHECK-NEXT: PlainClass is bridged verbatim
// CHECK-NEXT: PlainClass instance bridged as itself
testBridging(PlainClass(), "PlainClass")
//===----------------------------------------------------------------------===//
struct ConditionallyBridged<T>
: _BridgedToObjectiveC, _ConditionallyBridgedToObjectiveC {
static func getObjectiveCType() -> Any.Type {
return C.self
}
func bridgeToObjectiveC() -> C {
return C()
}
static func bridgeFromObjectiveC(x: C) -> ConditionallyBridged<T>? {
fatal("implement")
}
static func isBridgedToObjectiveC() -> Bool {
return !((T.self as Any) as String.Type)
}
}
// CHECK-NEXT: ConditionallyBridged<Int> is custom-bridged
// CHECK-NEXT: ConditionallyBridged<Int> instance bridged as C
testBridging(ConditionallyBridged<Int>(), "ConditionallyBridged<Int>")
// CHECK-NEXT: ConditionallyBridged<String> is unbridged
// CHECK-NEXT: ConditionallyBridged<String> instance did not bridge
testBridging(
ConditionallyBridged<String>(), "ConditionallyBridged<String>")
// CHECK-NEXT: done.
println("done.")