@@ -2619,7 +2619,7 @@ struct _BridgedToObjectiveCWitnessTable {
2619
2619
2620
2620
// func bridgeToObjectiveC() -> ObjectiveCType
2621
2621
HeapObject *(*bridgeToObjectiveC)(OpaqueValue *self, const Metadata *Self);
2622
- // func bridgeFromObjectiveC(x: ObjectiveCType) -> Self
2622
+ // func bridgeFromObjectiveC(x: ObjectiveCType) -> Self?
2623
2623
OpaqueExistentialContainer (*bridgeFromObjectiveC)(HeapObject *sourceValue,
2624
2624
const Metadata *self,
2625
2625
const Metadata *selfType);
@@ -2689,29 +2689,41 @@ extern "C" HeapObject *swift_bridgeToObjectiveC(OpaqueValue *value,
2689
2689
return nullptr ;
2690
2690
}
2691
2691
2692
+ // @asmname("swift_bridgeFromObjectiveC")
2693
+ // func bridgeFromObjectiveC<NativeType>(
2694
+ // x: AnyObject, nativeType: NativeType.Type
2695
+ // ) -> NativeType?
2692
2696
extern " C" OpaqueExistentialContainer
2693
2697
swift_bridgeFromObjectiveC (HeapObject *sourceValue, const Metadata *nativeType,
2694
2698
const Metadata *nativeType_) {
2699
+ // Check if the type conforms to _BridgedToObjectiveC.
2695
2700
const auto *bridgeWitness = findBridgeWitness (nativeType);
2696
2701
if (bridgeWitness) {
2702
+ // Check if the type also conforms to _ConditionallyBridgedToObjectiveC.
2697
2703
if (auto conditionalWitness = findConditionalBridgeWitness (nativeType)) {
2698
2704
if (!conditionalWitness->isBridgedToObjectiveC (nativeType, nativeType)) {
2705
+ // This type is not bridged.
2706
+ // Release the value because it is passed at +1.
2699
2707
swift_release (sourceValue);
2700
2708
return _TFSs26_injectNothingIntoOptionalU__FT_GSqQ__ (nativeType);
2701
2709
}
2702
2710
}
2703
2711
2704
- // Check if sourceValue has the type required by the protocol.
2712
+ // Check if sourceValue has the ObjectiveCType type required by the
2713
+ // protocol.
2705
2714
const Metadata *objectiveCType =
2706
2715
bridgeWitness->getObjectiveCType (nativeType, nativeType);
2707
2716
HeapObject *sourceValueAsObjectiveCType =
2708
2717
reinterpret_cast <HeapObject *>(const_cast <void *>(swift_dynamicCast (
2709
2718
reinterpret_cast <const void *>(sourceValue), objectiveCType)));
2710
2719
if (!sourceValueAsObjectiveCType) {
2720
+ // The type is not correct.
2711
2721
swift_release (sourceValue);
2712
2722
return _TFSs26_injectNothingIntoOptionalU__FT_GSqQ__ (nativeType);
2713
2723
}
2714
2724
2725
+ // The type matches. bridgeFromObjectiveC returns `Self?`, this function
2726
+ // returns `NativeType`, so we don't need to re-wrap the optional.
2715
2727
return bridgeWitness->bridgeFromObjectiveC (sourceValueAsObjectiveCType,
2716
2728
nativeType, nativeType);
2717
2729
}
@@ -2724,6 +2736,8 @@ swift_bridgeFromObjectiveC(HeapObject *sourceValue, const Metadata *nativeType,
2724
2736
return _TFSs26_injectNothingIntoOptionalU__FT_GSqQ__ (nativeType);
2725
2737
}
2726
2738
2739
+ // sourceValue is bridged verbatim. Box it and pass through
2740
+ // injectValueIntoOptional.
2727
2741
ValueBuffer sourceValueBufer;
2728
2742
*reinterpret_cast <OpaqueValue **>(&sourceValueBufer) =
2729
2743
reinterpret_cast <OpaqueValue *>(sourceValue);
@@ -2740,6 +2754,7 @@ swift_bridgeFromObjectiveC(HeapObject *sourceValue, const Metadata *nativeType,
2740
2754
reinterpret_cast <OpaqueValue *>(&outValue), nativeType);
2741
2755
}
2742
2756
2757
+ // We could not bridge the value.
2743
2758
swift_release (sourceValue);
2744
2759
return _TFSs26_injectNothingIntoOptionalU__FT_GSqQ__ (nativeType);
2745
2760
}
0 commit comments