Skip to content

Commit 46b1921

Browse files
committed
Resolve comments
1 parent 66ae4dd commit 46b1921

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
lines changed

pkgs/swift2objc/lib/src/transformer/_core/primitive_wrappers.dart

+18-21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:collection/collection.dart';
6+
57
import '../../ast/_core/interfaces/declaration.dart';
68
import '../../ast/_core/shared/referred_type.dart';
79
import '../../ast/declarations/built_in/built_in_declaration.dart';
@@ -11,13 +13,6 @@ import '../../parser/_core/utils.dart';
1113
import '../../transformer/_core/utils.dart';
1214
import '../transform.dart';
1315

14-
final primitiveWrappers = <ReferredType, ReferredType>{
15-
intType: _createWrapperClass(intType),
16-
floatType: _createWrapperClass(floatType),
17-
doubleType: _createWrapperClass(doubleType),
18-
boolType: _createWrapperClass(boolType),
19-
};
20-
2116
ReferredType _createWrapperClass(DeclaredType primitiveType) {
2217
final property = PropertyDeclaration(
2318
id: primitiveType.id.addIdSuffix('wrappedInstance'),
@@ -40,29 +35,31 @@ ReferredType _createWrapperClass(DeclaredType primitiveType) {
4035

4136
(ReferredType, bool) getWrapperIfNeeded(
4237
ReferredType type,
43-
bool isThrows,
38+
bool shouldWrapPrimitives,
4439
TransformationMap transformationMap,
4540
) {
46-
if (type is! DeclaredType || !_isPrimitiveType(type) || !isThrows) {
41+
if (type is! DeclaredType || !shouldWrapPrimitives) {
4742
return (type, false);
4843
}
4944

5045
final wrapper = getPrimitiveWrapper(type);
46+
if (wrapper == null) {
47+
return (type, false);
48+
}
49+
5150
transformationMap[type.declaration] = (wrapper as DeclaredType).declaration;
5251
return (wrapper, true);
5352
}
5453

55-
ReferredType getPrimitiveWrapper(DeclaredType other) {
56-
return primitiveWrappers.entries
57-
.firstWhere(
58-
(entry) => entry.key.sameAs(other),
59-
)
60-
.value;
61-
}
54+
ReferredType? getPrimitiveWrapper(DeclaredType other) {
55+
final primitiveWrappers = <(ReferredType, ReferredType)>[
56+
(intType, _createWrapperClass(intType)),
57+
(floatType, _createWrapperClass(floatType)),
58+
(doubleType, _createWrapperClass(doubleType)),
59+
(boolType, _createWrapperClass(boolType)),
60+
];
6261

63-
bool _isPrimitiveType(DeclaredType type) {
64-
return type.name == 'Int' ||
65-
type.name == 'Float' ||
66-
type.name == 'Double' ||
67-
type.name == 'Bool';
62+
return primitiveWrappers
63+
.firstWhereOrNull((pair) => pair.$1.sameAs(other))
64+
?.$2;
6865
}

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ import 'unique_namer.dart';
1818

1919
(String value, ReferredType type) maybeWrapValue(ReferredType type,
2020
String value, UniqueNamer globalNamer, TransformationMap transformationMap,
21-
{bool iswrapperPrimitive = false}) {
22-
if (iswrapperPrimitive) {
23-
final wrapper = getPrimitiveWrapper(type as DeclaredType) as DeclaredType;
24-
return ('${wrapper.name}($value)', wrapper);
21+
{bool shouldWrapPrimitives = false}) {
22+
if (shouldWrapPrimitives) {
23+
final (wrappedType, returnsWrappedPrimitive) =
24+
getWrapperIfNeeded(type, shouldWrapPrimitives, transformationMap);
25+
assert(returnsWrappedPrimitive);
26+
return ('${(wrappedType as DeclaredType).name}($value)', wrappedType);
2527
}
28+
2629
if (type.isObjCRepresentable) {
2730
return (value, type);
2831
}

pkgs/swift2objc/lib/src/transformer/transformers/transform_function.dart

+10-5
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,13 @@ MethodDeclaration _transformFunction(
9191
transformationMap,
9292
);
9393

94-
final (type, isWrapper) = getWrapperIfNeeded(
95-
transformedReturnType, originalFunction.throws, transformationMap);
94+
final shouldWrapPrimitives = originalFunction.throws &&
95+
transformedReturnType is DeclaredType &&
96+
getPrimitiveWrapper(transformedReturnType) != null;
97+
98+
final (_, type) = maybeWrapValue(
99+
transformedReturnType, '', globalNamer, transformationMap,
100+
shouldWrapPrimitives: shouldWrapPrimitives);
96101

97102
final transformedMethod = MethodDeclaration(
98103
id: originalFunction.id,
@@ -112,7 +117,7 @@ MethodDeclaration _transformFunction(
112117
transformedMethod,
113118
globalNamer,
114119
transformationMap,
115-
isWrapper,
120+
shouldWrapPrimitives,
116121
originalCallGenerator: originalCallStatementGenerator,
117122
);
118123

@@ -150,7 +155,7 @@ List<String> _generateStatements(
150155
MethodDeclaration transformedMethod,
151156
UniqueNamer globalNamer,
152157
TransformationMap transformationMap,
153-
bool isWrapper, {
158+
bool shouldWrapPrimitives, {
154159
required String Function(String arguments) originalCallGenerator,
155160
}) {
156161
final localNamer = UniqueNamer();
@@ -180,7 +185,7 @@ List<String> _generateStatements(
180185
resultName,
181186
globalNamer,
182187
transformationMap,
183-
iswrapperPrimitive: isWrapper,
188+
shouldWrapPrimitives: shouldWrapPrimitives,
184189
);
185190

186191
assert(wrapperType.sameAs(transformedMethod.returnType));

pkgs/swift2objc/lib/src/transformer/transformers/transform_variable.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Declaration _transformVariable(
8181
if (originalVariable.async) 'await'
8282
].join(' ');
8383

84-
final (type, isWrapper) = getWrapperIfNeeded(
84+
final (type, _) = getWrapperIfNeeded(
8585
transformedType, originalVariable.throws, transformationMap);
8686

8787
return MethodDeclaration(

pkgs/swift2objc/pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ topics:
1515
- codegen
1616

1717
dependencies:
18+
collection: ^1.19.1
1819
logging: ^1.3.0
1920
meta: ^1.16.0
2021
path: ^1.9.0

0 commit comments

Comments
 (0)