Skip to content

Commit

Permalink
[pigeon] fix int bug (#7725)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrinneal authored Sep 28, 2024
1 parent 03e1bfa commit 27c9853
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 35 deletions.
4 changes: 4 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
64 changes: 31 additions & 33 deletions packages/pigeon/lib/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -339,44 +339,42 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
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);');
});
});
}
});
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ List<Object?> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ List<Object?> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ List<Object?> 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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions packages/pigeon/test/dart_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1771,4 +1771,35 @@ name: foobar
final String mainCode = mainCodeSink.toString();
expect(mainCode, contains('List<Object?> wrapResponse('));
});

test('writes custom int codec without custom types', () {
final Root root = Root(
apis: <Api>[
AstHostApi(name: 'Api', methods: <Method>[
Method(
name: 'doit',
location: ApiLocation.host,
returnType: const TypeDeclaration(
baseName: 'int',
isNullable: true,
),
parameters: <Parameter>[])
])
],
classes: <Class>[],
enums: <Enum>[],
);
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);'));
});
}

0 comments on commit 27c9853

Please sign in to comment.