Skip to content

Commit 4c3db5f

Browse files
authored
[swift2objc] fix: support optional primitives by boxing as wrapper types (dart-lang#3140)
1 parent 6bf102a commit 4c3db5f

4 files changed

Lines changed: 136 additions & 0 deletions

File tree

‎pkgs/swift2objc/lib/src/transformer/_core/utils.dart‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ import 'unique_namer.dart';
4444
);
4545
}
4646

47+
if (type is OptionalType) {
48+
final (wrappedChildType, childIsPrimitive) = maybeGetPrimitiveWrapper(
49+
type.child,
50+
true,
51+
state,
52+
);
53+
if (childIsPrimitive) {
54+
final wrapperName = (wrappedChildType as DeclaredType).name;
55+
return (
56+
'$value == nil ? nil : $wrapperName($value!)',
57+
OptionalType(wrappedChildType),
58+
);
59+
}
60+
}
61+
4762
if (type.isObjCRepresentable) {
4863
return (value, type);
4964
}

‎pkgs/swift2objc/lib/src/transformer/transformers/transform_referred_type.dart‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ ReferredType transformReferredType(
1717
UniqueNamer globalNamer,
1818
TransformationState state,
1919
) {
20+
if (type is OptionalType) {
21+
final (wrappedChildType, childIsPrimitive) = maybeGetPrimitiveWrapper(
22+
type.child,
23+
true,
24+
state,
25+
);
26+
if (childIsPrimitive) {
27+
return OptionalType(wrappedChildType);
28+
}
29+
}
30+
2031
if (type is InoutType) {
2132
final (wrappedPrimitive, hasWrappedPrimitive) = maybeGetPrimitiveWrapper(
2233
type.child,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Foundation
2+
public class MyClass {
3+
public func optionalIntReturn() -> Int? { return nil }
4+
public func optionalFloatReturn() -> Float? { return nil }
5+
public func optionalDoubleReturn() -> Double? { return nil }
6+
public func optionalBoolReturn() -> Bool? { return nil }
7+
public func optionalIntArg(label param: Int?) {}
8+
public func optionalBoolArg(label param: Bool?) {}
9+
public var optionalIntProperty: Int?
10+
}
11+
public func globalOptionalIntReturn() -> Int? { return nil }
12+
public func globalOptionalIntArg(label param: Int?) {}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Test preamble text
2+
3+
import Foundation
4+
5+
@objc public class GlobalsWrapper: NSObject {
6+
@objc static public func globalOptionalIntArgWrapper(label param: IntWrapper?) {
7+
return globalOptionalIntArg(label: param?.wrappedInstance)
8+
}
9+
10+
@objc static public func globalOptionalIntReturnWrapper() -> IntWrapper? {
11+
let result = globalOptionalIntReturn()
12+
return result == nil ? nil : IntWrapper(result!)
13+
}
14+
15+
}
16+
17+
@objc public class MyClassWrapper: NSObject {
18+
var wrappedInstance: MyClass
19+
20+
@objc public var optionalIntProperty: IntWrapper? {
21+
get {
22+
wrappedInstance.optionalIntProperty == nil ? nil : IntWrapper(wrappedInstance.optionalIntProperty!)
23+
}
24+
set {
25+
wrappedInstance.optionalIntProperty = newValue?.wrappedInstance
26+
}
27+
}
28+
29+
init(_ wrappedInstance: MyClass) {
30+
self.wrappedInstance = wrappedInstance
31+
}
32+
33+
@objc public func optionalBoolReturn() -> BoolWrapper? {
34+
let result = wrappedInstance.optionalBoolReturn()
35+
return result == nil ? nil : BoolWrapper(result!)
36+
}
37+
38+
@objc public func optionalFloatReturn() -> FloatWrapper? {
39+
let result = wrappedInstance.optionalFloatReturn()
40+
return result == nil ? nil : FloatWrapper(result!)
41+
}
42+
43+
@objc public func optionalDoubleReturn() -> DoubleWrapper? {
44+
let result = wrappedInstance.optionalDoubleReturn()
45+
return result == nil ? nil : DoubleWrapper(result!)
46+
}
47+
48+
@objc public func optionalIntArg(label param: IntWrapper?) {
49+
return wrappedInstance.optionalIntArg(label: param?.wrappedInstance)
50+
}
51+
52+
@objc public func optionalBoolArg(label param: BoolWrapper?) {
53+
return wrappedInstance.optionalBoolArg(label: param?.wrappedInstance)
54+
}
55+
56+
@objc public func optionalIntReturn() -> IntWrapper? {
57+
let result = wrappedInstance.optionalIntReturn()
58+
return result == nil ? nil : IntWrapper(result!)
59+
}
60+
61+
}
62+
63+
@objc public class BoolWrapper: NSObject {
64+
var wrappedInstance: Bool
65+
66+
init(_ wrappedInstance: Bool) {
67+
self.wrappedInstance = wrappedInstance
68+
}
69+
70+
}
71+
72+
@objc public class DoubleWrapper: NSObject {
73+
var wrappedInstance: Double
74+
75+
init(_ wrappedInstance: Double) {
76+
self.wrappedInstance = wrappedInstance
77+
}
78+
79+
}
80+
81+
@objc public class FloatWrapper: NSObject {
82+
var wrappedInstance: Float
83+
84+
init(_ wrappedInstance: Float) {
85+
self.wrappedInstance = wrappedInstance
86+
}
87+
88+
}
89+
90+
@objc public class IntWrapper: NSObject {
91+
var wrappedInstance: Int
92+
93+
init(_ wrappedInstance: Int) {
94+
self.wrappedInstance = wrappedInstance
95+
}
96+
97+
}
98+

0 commit comments

Comments
 (0)