Skip to content

Commit 7319d3a

Browse files
[pigeon] Don't wrap non-nullable primitives in Obj-C (flutter#5214)
Updates the Objective-C generator to use `BOOL`, `NSInteger`, and `double` for non-nullable versions of those primitives, instead of boxing them with `NSNumber*`. This makes the code more idiomatic, brings them into alignment with enum behavior, and importantly helps minimize the chances of clients having issues with silent conversion of `NSNumber*` to `BOOL` in the long term (although it may cause some such issues in the short term when people update; the changelog contains a prominent warning about this). As an incidental fix, this also fixes the use of `strong` for collections and strings, which is not best practice, since I was already changing related code. Fixes flutter#116680 Fixes flutter#127401
1 parent 35b15de commit 7319d3a

File tree

20 files changed

+484
-379
lines changed

20 files changed

+484
-379
lines changed

packages/pigeon/CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
## 13.0.0
2+
3+
* **Breaking Change** [objc] Eliminates boxing of non-nullable primitive types
4+
(bool, int, double). Changes required:
5+
* Implementations of host API methods that take non-nullable
6+
primitives will need to be updated to match the new signatures.
7+
* Calls to Flutter API methods that take non-nullable primitives will need to
8+
be updated to pass unboxed values.
9+
* Calls to non-nullable primitive property methods on generated data classes
10+
will need to be updated.
11+
* **WARNING**: Current versions of `Xcode` do not appear to warn about
12+
implicit `NSNumber *` to `BOOL` conversions, so code that is no longer
13+
correct after this breaking change may compile without warning. For example,
14+
`myGeneratedClass.aBoolProperty = @NO` can silently set `aBoolProperty` to
15+
`YES`. Any data class or Flutter API interactions involving `bool`s should
16+
be carefully audited by hand when updating.
17+
18+
19+
120
## 12.0.1
221

322
* [swift] Adds protocol for Flutter APIs.

packages/pigeon/example/app/macos/Runner/messages.g.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ typedef NS_ENUM(NSUInteger, PGNCode) {
3636
@property(nonatomic, copy, nullable) NSString *name;
3737
@property(nonatomic, copy, nullable) NSString *description;
3838
@property(nonatomic, assign) PGNCode code;
39-
@property(nonatomic, strong) NSDictionary<NSString *, NSString *> *data;
39+
@property(nonatomic, copy) NSDictionary<NSString *, NSString *> *data;
4040
@end
4141

4242
/// The codec used by PGNExampleHostApi.
@@ -46,8 +46,8 @@ NSObject<FlutterMessageCodec> *PGNExampleHostApiGetCodec(void);
4646
/// @return `nil` only when `error != nil`.
4747
- (nullable NSString *)getHostLanguageWithError:(FlutterError *_Nullable *_Nonnull)error;
4848
/// @return `nil` only when `error != nil`.
49-
- (nullable NSNumber *)addNumber:(NSNumber *)a
50-
toNumber:(NSNumber *)b
49+
- (nullable NSNumber *)addNumber:(NSInteger)a
50+
toNumber:(NSInteger)b
5151
error:(FlutterError *_Nullable *_Nonnull)error;
5252
- (void)sendMessageMessage:(PGNMessageData *)message
5353
completion:(void (^)(NSNumber *_Nullable, FlutterError *_Nullable))completion;

packages/pigeon/example/app/macos/Runner/messages.g.m

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,17 @@ + (PGNMessageData *)fromList:(NSArray *)list {
6363
pigeonResult.description = GetNullableObjectAtIndex(list, 1);
6464
pigeonResult.code = [GetNullableObjectAtIndex(list, 2) integerValue];
6565
pigeonResult.data = GetNullableObjectAtIndex(list, 3);
66-
NSAssert(pigeonResult.data != nil, @"");
6766
return pigeonResult;
6867
}
6968
+ (nullable PGNMessageData *)nullableFromList:(NSArray *)list {
7069
return (list) ? [PGNMessageData fromList:list] : nil;
7170
}
7271
- (NSArray *)toList {
7372
return @[
74-
(self.name ?: [NSNull null]),
75-
(self.description ?: [NSNull null]),
73+
self.name ?: [NSNull null],
74+
self.description ?: [NSNull null],
7675
@(self.code),
77-
(self.data ?: [NSNull null]),
76+
self.data ?: [NSNull null],
7877
];
7978
}
8079
@end
@@ -160,8 +159,8 @@ void SetUpPGNExampleHostApi(id<FlutterBinaryMessenger> binaryMessenger,
160159
api);
161160
[channel setMessageHandler:^(id _Nullable message, FlutterReply callback) {
162161
NSArray *args = message;
163-
NSNumber *arg_a = GetNullableObjectAtIndex(args, 0);
164-
NSNumber *arg_b = GetNullableObjectAtIndex(args, 1);
162+
NSInteger arg_a = [GetNullableObjectAtIndex(args, 0) integerValue];
163+
NSInteger arg_b = [GetNullableObjectAtIndex(args, 1) integerValue];
165164
FlutterError *error;
166165
NSNumber *output = [api addNumber:arg_a toNumber:arg_b error:&error];
167166
callback(wrapResult(output, error));

packages/pigeon/lib/generator_tools.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import 'ast.dart';
1313
/// The current version of pigeon.
1414
///
1515
/// This must match the version in pubspec.yaml.
16-
const String pigeonVersion = '12.0.1';
16+
const String pigeonVersion = '13.0.0';
1717

1818
/// Read all the content from [stdin] to a String.
1919
String readStdin() {

packages/pigeon/lib/objc_generator.dart

Lines changed: 227 additions & 118 deletions
Large diffs are not rendered by default.

packages/pigeon/platform_tests/alternate_language_test_plugin/example/ios/RunnerTests/MultipleArityTest.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ - (void)testSimple {
2525
MultipleArityFlutterApi *api =
2626
[[MultipleArityFlutterApi alloc] initWithBinaryMessenger:binaryMessenger];
2727
XCTestExpectation *expectation = [self expectationWithDescription:@"subtraction"];
28-
[api subtractX:@(30)
29-
y:@(10)
28+
[api subtractX:30
29+
y:10
3030
completion:^(NSNumber *_Nonnull result, FlutterError *_Nullable error) {
3131
XCTAssertNil(error);
3232
XCTAssertEqual(20, result.intValue);

packages/pigeon/platform_tests/alternate_language_test_plugin/example/ios/RunnerTests/NullFieldsTest.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ @interface NullFieldsTest : XCTestCase
2929
@implementation NullFieldsTest
3030

3131
- (void)testMakeWithValues {
32-
NullFieldsSearchRequest *request = [NullFieldsSearchRequest makeWithQuery:@"hello" identifier:@1];
32+
NullFieldsSearchRequest *request = [NullFieldsSearchRequest makeWithQuery:@"hello" identifier:1];
3333

3434
NullFieldsSearchReplyTypeBox *typeWrapper =
3535
[[NullFieldsSearchReplyTypeBox alloc] initWithValue:NullFieldsSearchReplyTypeSuccess];
@@ -48,7 +48,7 @@ - (void)testMakeWithValues {
4848
}
4949

5050
- (void)testMakeRequestWithNulls {
51-
NullFieldsSearchRequest *request = [NullFieldsSearchRequest makeWithQuery:nil identifier:@1];
51+
NullFieldsSearchRequest *request = [NullFieldsSearchRequest makeWithQuery:nil identifier:1];
5252
XCTAssertNil(request.query);
5353
}
5454

@@ -121,13 +121,13 @@ - (void)testReplyFromListWithNulls {
121121
}
122122

123123
- (void)testRequestToListWithValuess {
124-
NullFieldsSearchRequest *request = [NullFieldsSearchRequest makeWithQuery:@"hello" identifier:@1];
124+
NullFieldsSearchRequest *request = [NullFieldsSearchRequest makeWithQuery:@"hello" identifier:1];
125125
NSArray *list = [request toList];
126126
XCTAssertEqual(@"hello", list[0]);
127127
}
128128

129129
- (void)testRequestToListWithNulls {
130-
NullFieldsSearchRequest *request = [NullFieldsSearchRequest makeWithQuery:nil identifier:@1];
130+
NullFieldsSearchRequest *request = [NullFieldsSearchRequest makeWithQuery:nil identifier:1];
131131
NSArray *list = [request toList];
132132
XCTAssertEqual([NSNull null], list[0]);
133133
}
@@ -139,7 +139,7 @@ - (void)testReplyToListWithValuess {
139139
makeWithResult:@"result"
140140
error:@"error"
141141
indices:@[ @1, @2, @3 ]
142-
request:[NullFieldsSearchRequest makeWithQuery:@"hello" identifier:@1]
142+
request:[NullFieldsSearchRequest makeWithQuery:@"hello" identifier:1]
143143
type:typeWrapper];
144144
NSArray *list = [reply toList];
145145
NSArray *indices = @[ @1, @2, @3 ];

packages/pigeon/platform_tests/alternate_language_test_plugin/example/ios/RunnerTests/PrimitiveTest.m

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ - (void)testIntPrimitive {
2121
[[EchoBinaryMessenger alloc] initWithCodec:PrimitiveFlutterApiGetCodec()];
2222
PrimitiveFlutterApi *api = [[PrimitiveFlutterApi alloc] initWithBinaryMessenger:binaryMessenger];
2323
XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
24-
[api anIntValue:@1
24+
[api anIntValue:1
2525
completion:^(NSNumber *_Nonnull result, FlutterError *_Nullable err) {
2626
XCTAssertEqualObjects(@1, result);
2727
[expectation fulfill];
@@ -34,10 +34,11 @@ - (void)testBoolPrimitive {
3434
[[EchoBinaryMessenger alloc] initWithCodec:PrimitiveFlutterApiGetCodec()];
3535
PrimitiveFlutterApi *api = [[PrimitiveFlutterApi alloc] initWithBinaryMessenger:binaryMessenger];
3636
XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
37-
NSNumber *arg = @YES;
37+
BOOL arg = YES;
3838
[api aBoolValue:arg
3939
completion:^(NSNumber *_Nonnull result, FlutterError *_Nullable err) {
40-
XCTAssertEqualObjects(arg, result);
40+
XCTAssertNotNil(result);
41+
XCTAssertEqual(arg, result.boolValue);
4142
[expectation fulfill];
4243
}];
4344
[self waitForExpectations:@[ expectation ] timeout:1.0];
@@ -48,10 +49,11 @@ - (void)testDoublePrimitive {
4849
[[EchoBinaryMessenger alloc] initWithCodec:PrimitiveFlutterApiGetCodec()];
4950
PrimitiveFlutterApi *api = [[PrimitiveFlutterApi alloc] initWithBinaryMessenger:binaryMessenger];
5051
XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
51-
NSNumber *arg = @(1.5);
52+
NSInteger arg = 1.5;
5253
[api aDoubleValue:arg
5354
completion:^(NSNumber *_Nonnull result, FlutterError *_Nullable err) {
54-
XCTAssertEqualObjects(arg, result);
55+
XCTAssertNotNil(result);
56+
XCTAssertEqual(arg, result.integerValue);
5557
[expectation fulfill];
5658
}];
5759
[self waitForExpectations:@[ expectation ] timeout:1.0];

packages/pigeon/platform_tests/alternate_language_test_plugin/example/macos/Runner.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@
259259
isa = PBXProject;
260260
attributes = {
261261
LastSwiftUpdateCheck = 0920;
262-
LastUpgradeCheck = 1300;
262+
LastUpgradeCheck = 1430;
263263
ORGANIZATIONNAME = "";
264264
TargetAttributes = {
265265
331C80D4294CF70F00263BE5 = {

packages/pigeon/platform_tests/alternate_language_test_plugin/example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1300"
3+
LastUpgradeVersion = "1430"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

0 commit comments

Comments
 (0)