From 27c98537ccac576cfb79e9407a5afc0c36667f7d Mon Sep 17 00:00:00 2001 From: Tarrin Neal Date: Sat, 28 Sep 2024 04:26:25 -0700 Subject: [PATCH] [pigeon] fix int bug (#7725) fixes https://github.com/flutter/flutter/issues/155512 --- packages/pigeon/CHANGELOG.md | 4 ++ packages/pigeon/lib/dart_generator.dart | 64 +++++++++---------- packages/pigeon/lib/generator_tools.dart | 2 +- .../background_platform_channels.gen.dart | 17 +++++ .../lib/src/generated/multiple_arity.gen.dart | 17 +++++ .../src/generated/nullable_returns.gen.dart | 17 +++++ .../lib/src/generated/primitive.gen.dart | 17 +++++ packages/pigeon/pubspec.yaml | 2 +- packages/pigeon/test/dart_generator_test.dart | 31 +++++++++ 9 files changed, 136 insertions(+), 35 deletions(-) diff --git a/packages/pigeon/CHANGELOG.md b/packages/pigeon/CHANGELOG.md index f7fe2a3ba1af..d94d1ab79ca8 100644 --- a/packages/pigeon/CHANGELOG.md +++ b/packages/pigeon/CHANGELOG.md @@ -1,3 +1,7 @@ +## 22.4.1 + +* [dart] Fixes bug where special handling of ints is ignored if no custom types are used. + ## 22.4.0 * Adds support for non-nullable types in collections. diff --git a/packages/pigeon/lib/dart_generator.dart b/packages/pigeon/lib/dart_generator.dart index 2bd1e4117f7a..b6d81bbdd770 100644 --- a/packages/pigeon/lib/dart_generator.dart +++ b/packages/pigeon/lib/dart_generator.dart @@ -339,44 +339,42 @@ class DartGenerator extends StructuredGenerator { indent.write('class $_pigeonCodec extends StandardMessageCodec'); indent.addScoped(' {', '}', () { indent.writeln('const $_pigeonCodec();'); - if (enumeratedTypes.isNotEmpty) { - indent.writeln('@override'); - indent.write('void writeValue(WriteBuffer buffer, Object? value) '); - indent.addScoped('{', '}', () { - indent.writeScoped('if (value is int) {', '}', () { - indent.writeln('buffer.putUint8(4);'); - indent.writeln('buffer.putInt64(value);'); - }, addTrailingNewline: false); - - enumerate(enumeratedTypes, - (int index, final EnumeratedType customType) { - writeEncodeLogic(customType); - }); - indent.addScoped(' else {', '}', () { - indent.writeln('super.writeValue(buffer, value);'); - }); + indent.writeln('@override'); + indent.write('void writeValue(WriteBuffer buffer, Object? value) '); + indent.addScoped('{', '}', () { + indent.writeScoped('if (value is int) {', '}', () { + indent.writeln('buffer.putUint8(4);'); + indent.writeln('buffer.putInt64(value);'); + }, addTrailingNewline: false); + + enumerate(enumeratedTypes, + (int index, final EnumeratedType customType) { + writeEncodeLogic(customType); }); - indent.newln(); - indent.writeln('@override'); - indent.write('Object? readValueOfType(int type, ReadBuffer buffer) '); + indent.addScoped(' else {', '}', () { + indent.writeln('super.writeValue(buffer, value);'); + }); + }); + indent.newln(); + indent.writeln('@override'); + indent.write('Object? readValueOfType(int type, ReadBuffer buffer) '); + indent.addScoped('{', '}', () { + indent.write('switch (type) '); indent.addScoped('{', '}', () { - indent.write('switch (type) '); - indent.addScoped('{', '}', () { - for (final EnumeratedType customType in enumeratedTypes) { - if (customType.enumeration < maximumCodecFieldKey) { - writeDecodeLogic(customType); - } + for (final EnumeratedType customType in enumeratedTypes) { + if (customType.enumeration < maximumCodecFieldKey) { + writeDecodeLogic(customType); } - if (root.requiresOverflowClass) { - writeDecodeLogic(overflowClass); - } - indent.writeln('default:'); - indent.nest(1, () { - indent.writeln('return super.readValueOfType(type, buffer);'); - }); + } + if (root.requiresOverflowClass) { + writeDecodeLogic(overflowClass); + } + indent.writeln('default:'); + indent.nest(1, () { + indent.writeln('return super.readValueOfType(type, buffer);'); }); }); - } + }); }); } diff --git a/packages/pigeon/lib/generator_tools.dart b/packages/pigeon/lib/generator_tools.dart index 1474098aeac0..b5029a68aaae 100644 --- a/packages/pigeon/lib/generator_tools.dart +++ b/packages/pigeon/lib/generator_tools.dart @@ -14,7 +14,7 @@ import 'ast.dart'; /// The current version of pigeon. /// /// This must match the version in pubspec.yaml. -const String pigeonVersion = '22.4.0'; +const String pigeonVersion = '22.4.1'; /// Read all the content from [stdin] to a String. String readStdin() { diff --git a/packages/pigeon/platform_tests/shared_test_plugin_code/lib/src/generated/background_platform_channels.gen.dart b/packages/pigeon/platform_tests/shared_test_plugin_code/lib/src/generated/background_platform_channels.gen.dart index 9fb5e58df030..3bcbaaff4703 100644 --- a/packages/pigeon/platform_tests/shared_test_plugin_code/lib/src/generated/background_platform_channels.gen.dart +++ b/packages/pigeon/platform_tests/shared_test_plugin_code/lib/src/generated/background_platform_channels.gen.dart @@ -21,6 +21,23 @@ PlatformException _createConnectionError(String channelName) { class _PigeonCodec extends StandardMessageCodec { const _PigeonCodec(); + @override + void writeValue(WriteBuffer buffer, Object? value) { + if (value is int) { + buffer.putUint8(4); + buffer.putInt64(value); + } else { + super.writeValue(buffer, value); + } + } + + @override + Object? readValueOfType(int type, ReadBuffer buffer) { + switch (type) { + default: + return super.readValueOfType(type, buffer); + } + } } class BackgroundApi2Host { diff --git a/packages/pigeon/platform_tests/shared_test_plugin_code/lib/src/generated/multiple_arity.gen.dart b/packages/pigeon/platform_tests/shared_test_plugin_code/lib/src/generated/multiple_arity.gen.dart index 0ccb1f5e643e..05197c29c3ce 100644 --- a/packages/pigeon/platform_tests/shared_test_plugin_code/lib/src/generated/multiple_arity.gen.dart +++ b/packages/pigeon/platform_tests/shared_test_plugin_code/lib/src/generated/multiple_arity.gen.dart @@ -32,6 +32,23 @@ List wrapResponse( class _PigeonCodec extends StandardMessageCodec { const _PigeonCodec(); + @override + void writeValue(WriteBuffer buffer, Object? value) { + if (value is int) { + buffer.putUint8(4); + buffer.putInt64(value); + } else { + super.writeValue(buffer, value); + } + } + + @override + Object? readValueOfType(int type, ReadBuffer buffer) { + switch (type) { + default: + return super.readValueOfType(type, buffer); + } + } } class MultipleArityHostApi { diff --git a/packages/pigeon/platform_tests/shared_test_plugin_code/lib/src/generated/nullable_returns.gen.dart b/packages/pigeon/platform_tests/shared_test_plugin_code/lib/src/generated/nullable_returns.gen.dart index 6f69283f744e..99cb3a3bf928 100644 --- a/packages/pigeon/platform_tests/shared_test_plugin_code/lib/src/generated/nullable_returns.gen.dart +++ b/packages/pigeon/platform_tests/shared_test_plugin_code/lib/src/generated/nullable_returns.gen.dart @@ -32,6 +32,23 @@ List wrapResponse( class _PigeonCodec extends StandardMessageCodec { const _PigeonCodec(); + @override + void writeValue(WriteBuffer buffer, Object? value) { + if (value is int) { + buffer.putUint8(4); + buffer.putInt64(value); + } else { + super.writeValue(buffer, value); + } + } + + @override + Object? readValueOfType(int type, ReadBuffer buffer) { + switch (type) { + default: + return super.readValueOfType(type, buffer); + } + } } class NullableReturnHostApi { diff --git a/packages/pigeon/platform_tests/shared_test_plugin_code/lib/src/generated/primitive.gen.dart b/packages/pigeon/platform_tests/shared_test_plugin_code/lib/src/generated/primitive.gen.dart index 552938c43ae6..e6f864538078 100644 --- a/packages/pigeon/platform_tests/shared_test_plugin_code/lib/src/generated/primitive.gen.dart +++ b/packages/pigeon/platform_tests/shared_test_plugin_code/lib/src/generated/primitive.gen.dart @@ -32,6 +32,23 @@ List wrapResponse( class _PigeonCodec extends StandardMessageCodec { const _PigeonCodec(); + @override + void writeValue(WriteBuffer buffer, Object? value) { + if (value is int) { + buffer.putUint8(4); + buffer.putInt64(value); + } else { + super.writeValue(buffer, value); + } + } + + @override + Object? readValueOfType(int type, ReadBuffer buffer) { + switch (type) { + default: + return super.readValueOfType(type, buffer); + } + } } class PrimitiveHostApi { diff --git a/packages/pigeon/pubspec.yaml b/packages/pigeon/pubspec.yaml index c2e58e407617..fef6a7c6fff1 100644 --- a/packages/pigeon/pubspec.yaml +++ b/packages/pigeon/pubspec.yaml @@ -2,7 +2,7 @@ name: pigeon description: Code generator tool to make communication between Flutter and the host platform type-safe and easier. repository: https://github.com/flutter/packages/tree/main/packages/pigeon issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+pigeon%22 -version: 22.4.0 # This must match the version in lib/generator_tools.dart +version: 22.4.1 # This must match the version in lib/generator_tools.dart environment: sdk: ^3.3.0 diff --git a/packages/pigeon/test/dart_generator_test.dart b/packages/pigeon/test/dart_generator_test.dart index 93aacafd1cd1..4dca4b416844 100644 --- a/packages/pigeon/test/dart_generator_test.dart +++ b/packages/pigeon/test/dart_generator_test.dart @@ -1771,4 +1771,35 @@ name: foobar final String mainCode = mainCodeSink.toString(); expect(mainCode, contains('List wrapResponse(')); }); + + test('writes custom int codec without custom types', () { + final Root root = Root( + apis: [ + AstHostApi(name: 'Api', methods: [ + Method( + name: 'doit', + location: ApiLocation.host, + returnType: const TypeDeclaration( + baseName: 'int', + isNullable: true, + ), + parameters: []) + ]) + ], + classes: [], + enums: [], + ); + final StringBuffer sink = StringBuffer(); + const DartGenerator generator = DartGenerator(); + generator.generate( + const DartOptions(), + root, + sink, + dartPackageName: DEFAULT_PACKAGE_NAME, + ); + final String code = sink.toString(); + expect(code, contains('if (value is int) {')); + expect(code, contains('buffer.putUint8(4);')); + expect(code, contains('buffer.putInt64(value);')); + }); }