Skip to content

Commit d997b68

Browse files
committed
add int collection tests, change codec to always send long
1 parent 7c1a05c commit d997b68

File tree

47 files changed

+2190
-2053
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2190
-2053
lines changed

packages/pigeon/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 21.2.1
2+
3+
* [dart] Changes codec to send int64 instead of int32.
4+
* Adds tests to validate collections of ints.
5+
16
## 21.2.0
27

38
* Removes restriction on number of custom types.

packages/pigeon/example/app/android/app/src/main/java/io/flutter/plugins/Messages.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ protected Object readValueOfType(byte type, @NonNull ByteBuffer buffer) {
232232
case (byte) 129:
233233
{
234234
Object value = readValue(buffer);
235-
return value == null ? null : Code.values()[(int) value];
235+
return value == null ? null : Code.values()[((Long) value).intValue()];
236236
}
237237
case (byte) 130:
238238
return MessageData.fromList((ArrayList<Object>) readValue(buffer));
@@ -339,13 +339,10 @@ static void setUp(
339339
(message, reply) -> {
340340
ArrayList<Object> wrapped = new ArrayList<>();
341341
ArrayList<Object> args = (ArrayList<Object>) message;
342-
Number aArg = (Number) args.get(0);
343-
Number bArg = (Number) args.get(1);
342+
Long aArg = (Long) args.get(0);
343+
Long bArg = (Long) args.get(1);
344344
try {
345-
Long output =
346-
api.add(
347-
(aArg == null) ? null : aArg.longValue(),
348-
(bArg == null) ? null : bArg.longValue());
345+
Long output = api.add(aArg, bArg);
349346
wrapped.add(0, output);
350347
} catch (Throwable exception) {
351348
wrapped = wrapError(exception);

packages/pigeon/example/app/android/app/src/main/kotlin/dev/flutter/pigeon_example_app/Messages.g.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private object MessagesPigeonCodec : StandardMessageCodec() {
8888
override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? {
8989
return when (type) {
9090
129.toByte() -> {
91-
return (readValue(buffer) as Int?)?.let { Code.ofRaw(it) }
91+
return (readValue(buffer) as Long?)?.let { Code.ofRaw(it.toInt()) }
9292
}
9393
130.toByte() -> {
9494
return (readValue(buffer) as? List<Any?>)?.let { MessageData.fromList(it) }
@@ -161,8 +161,8 @@ interface ExampleHostApi {
161161
if (api != null) {
162162
channel.setMessageHandler { message, reply ->
163163
val args = message as List<Any?>
164-
val aArg = args[0].let { num -> if (num is Int) num.toLong() else num as Long }
165-
val bArg = args[1].let { num -> if (num is Int) num.toLong() else num as Long }
164+
val aArg = args[0] as Long
165+
val bArg = args[1] as Long
166166
val wrapped: List<Any?> =
167167
try {
168168
listOf(api.add(aArg, bArg))

packages/pigeon/example/app/lib/src/messages.g.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ class _PigeonCodec extends StandardMessageCodec {
7474
const _PigeonCodec();
7575
@override
7676
void writeValue(WriteBuffer buffer, Object? value) {
77-
if (value is Code) {
77+
if (value is int) {
78+
buffer.putUint8(4);
79+
buffer.putInt64(value);
80+
} else if (value is Code) {
7881
buffer.putUint8(129);
7982
writeValue(buffer, value.index);
8083
} else if (value is MessageData) {

packages/pigeon/lib/cpp_generator.dart

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -867,9 +867,7 @@ class CppSourceGenerator extends StructuredGenerator<CppOptions> {
867867
// Returns the expression to convert the given EncodableValue to a field
868868
// value.
869869
String getValueExpression(NamedType field, String encodable) {
870-
if (field.type.baseName == 'int') {
871-
return '$encodable.LongValue()';
872-
} else if (field.type.baseName == 'Object') {
870+
if (field.type.baseName == 'Object') {
873871
return encodable;
874872
} else {
875873
final HostDatatype hostDatatype =
@@ -1636,17 +1634,7 @@ ${prefix}reply(EncodableValue(std::move(wrapped)));''';
16361634
if (hostType.isNullable) {
16371635
// Nullable arguments are always pointers, with nullptr corresponding to
16381636
// null.
1639-
if (hostType.datatype == 'int64_t') {
1640-
// The EncodableValue will either be an int32_t or an int64_t depending
1641-
// on the value, but the generated API requires an int64_t so that it can
1642-
// handle any case. Create a local variable for the 64-bit value...
1643-
final String valueVarName = '${argName}_value';
1644-
indent.writeln(
1645-
'const int64_t $valueVarName = $encodableArgName.IsNull() ? 0 : $encodableArgName.LongValue();');
1646-
// ... then declare the arg as a reference to that local.
1647-
indent.writeln(
1648-
'const auto* $argName = $encodableArgName.IsNull() ? nullptr : &$valueVarName;');
1649-
} else if (hostType.datatype == 'EncodableValue') {
1637+
if (hostType.datatype == 'EncodableValue') {
16501638
// Generic objects just pass the EncodableValue through directly.
16511639
indent.writeln('const auto* $argName = &$encodableArgName;');
16521640
} else if (hostType.isBuiltin) {

packages/pigeon/lib/dart_generator.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
286286
required String dartPackageName,
287287
}) {
288288
void writeEncodeLogic(EnumeratedType customType) {
289-
indent.writeScoped('if (value is ${customType.name}) {', '} else ', () {
289+
indent.writeScoped('else if (value is ${customType.name}) {', '}', () {
290290
if (customType.enumeration < maximumCodecFieldKey) {
291291
indent.writeln('buffer.putUint8(${customType.enumeration});');
292292
if (customType.type == CustomTypes.customClass) {
@@ -343,11 +343,16 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
343343
indent.writeln('@override');
344344
indent.write('void writeValue(WriteBuffer buffer, Object? value) ');
345345
indent.addScoped('{', '}', () {
346+
indent.writeScoped('if (value is int) {', '}', () {
347+
indent.writeln('buffer.putUint8(4);');
348+
indent.writeln('buffer.putInt64(value);');
349+
}, addTrailingNewline: false);
350+
346351
enumerate(enumeratedTypes,
347352
(int index, final EnumeratedType customType) {
348353
writeEncodeLogic(customType);
349354
});
350-
indent.addScoped('{', '}', () {
355+
indent.addScoped(' else {', '}', () {
351356
indent.writeln('super.writeValue(buffer, value);');
352357
});
353358
});

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 = '21.2.0';
16+
const String pigeonVersion = '21.2.1';
1717

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

packages/pigeon/lib/java_generator.dart

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ const String _codecName = 'PigeonCodec';
3030

3131
const String _overflowClassName = '${classNamePrefix}CodecOverflow';
3232

33-
// Used to create classes with type Int rather than long.
34-
const String _forceInt = '${varNamePrefix}forceInt';
35-
3633
/// Options that control how Java code will be generated.
3734
class JavaOptions {
3835
/// Creates a [JavaOptions] object
@@ -253,18 +250,11 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
253250
}
254251

255252
void _writeClassField(
256-
JavaOptions generatorOptions,
257-
Indent indent,
258-
NamedType field, {
259-
bool isPrimitive = false,
260-
}) {
253+
JavaOptions generatorOptions, Indent indent, NamedType field) {
261254
final HostDatatype hostDatatype = getFieldHostDatatype(
262255
field, (TypeDeclaration x) => _javaTypeForBuiltinDartType(x));
263-
final String nullability = isPrimitive
264-
? ''
265-
: field.type.isNullable
266-
? '@Nullable '
267-
: '@NonNull ';
256+
final String nullability =
257+
field.type.isNullable ? '@Nullable ' : '@NonNull ';
268258
addDocumentationComments(
269259
indent, field.documentationComments, _docCommentSpec);
270260

@@ -280,7 +270,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
280270
indent.writeScoped(
281271
'public void ${_makeSetter(field)}($nullability${hostDatatype.datatype} setterArg) {',
282272
'}', () {
283-
if (!field.type.isNullable && !isPrimitive) {
273+
if (!field.type.isNullable) {
284274
indent.writeScoped('if (setterArg == null) {', '}', () {
285275
indent.writeln(
286276
'throw new IllegalStateException("Nonnull field \\"${field.name}\\" is null.");');
@@ -306,7 +296,6 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
306296
generatorOptions,
307297
indent,
308298
field,
309-
isPrimitive: field.type.baseName == _forceInt,
310299
);
311300
indent.newln();
312301
}
@@ -490,7 +479,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
490479
indent
491480
.writeln('$_overflowClassName wrap = new $_overflowClassName();');
492481
indent.writeln(
493-
'wrap.setType(${customType.enumeration - maximumCodecFieldKey});');
482+
'wrap.setType(${customType.enumeration - maximumCodecFieldKey}L);');
494483
indent.writeln(
495484
'wrap.setWrapped($nullCheck((${customType.name}) value).$encodeString);');
496485
}
@@ -580,7 +569,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
580569
}) {
581570
final NamedType overflowInteration = NamedType(
582571
name: 'type',
583-
type: const TypeDeclaration(baseName: _forceInt, isNullable: false));
572+
type: const TypeDeclaration(baseName: 'int', isNullable: false));
584573
final NamedType overflowObject = NamedType(
585574
name: 'wrapped',
586575
type: const TypeDeclaration(baseName: 'Object', isNullable: true));
@@ -607,7 +596,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
607596
indent.format('''
608597
static @Nullable Object fromList(@NonNull ArrayList<Object> ${varNamePrefix}list) {
609598
$_overflowClassName wrapper = new $_overflowClassName();
610-
wrapper.setType((int) ${varNamePrefix}list.get(0));
599+
wrapper.setType((Long) ${varNamePrefix}list.get(0));
611600
wrapper.setWrapped(${varNamePrefix}list.get(1));
612601
return wrapper.unwrap();
613602
}
@@ -619,7 +608,7 @@ if (wrapped == null) {
619608
return null;
620609
}
621610
''');
622-
indent.writeScoped('switch (type) {', '}', () {
611+
indent.writeScoped('switch (type.intValue()) {', '}', () {
623612
for (int i = totalCustomCodecKeysAllowed; i < types.length; i++) {
624613
indent.writeln('case ${i - totalCustomCodecKeysAllowed}:');
625614
indent.nest(1, () {
@@ -628,7 +617,7 @@ if (wrapped == null) {
628617
'return ${types[i].name}.fromList((ArrayList<Object>) wrapped);');
629618
} else if (types[i].type == CustomTypes.customEnum) {
630619
indent.writeln(
631-
'return ${types[i].name}.values()[(int) wrapped];');
620+
'return ${_intToEnum('wrapped', types[i].name, false)};');
632621
}
633622
});
634623
}
@@ -766,13 +755,8 @@ if (wrapped == null) {
766755
const String output = 'output';
767756
final String outputExpression;
768757
indent.writeln('@SuppressWarnings("ConstantConditions")');
769-
if (func.returnType.baseName == 'int') {
770-
outputExpression =
771-
'listReply.get(0) == null ? null : ((Number) listReply.get(0)).longValue();';
772-
} else {
773-
outputExpression =
774-
'${_cast('listReply.get(0)', javaType: returnType)};';
775-
}
758+
outputExpression =
759+
'${_cast('listReply.get(0)', javaType: returnType)};';
776760
indent.writeln('$returnType $output = $outputExpression');
777761
indent.writeln('result.success($output);');
778762
}
@@ -951,16 +935,9 @@ if (wrapped == null) {
951935
indent.writeln(
952936
'ArrayList<Object> args = (ArrayList<Object>) message;');
953937
enumerate(method.parameters, (int index, NamedType arg) {
954-
// The StandardMessageCodec can give us [Integer, Long] for
955-
// a Dart 'int'. To keep things simple we just use 64bit
956-
// longs in Pigeon with Java.
957-
final bool isInt = arg.type.baseName == 'int';
958-
final String argType =
959-
isInt ? 'Number' : _javaTypeForDartType(arg.type);
938+
final String argType = _javaTypeForDartType(arg.type);
960939
final String argName = _getSafeArgumentName(index, arg);
961-
final String argExpression = isInt
962-
? '($argName == null) ? null : $argName.longValue()'
963-
: argName;
940+
final String argExpression = argName;
964941
String accessor = 'args.get($index)';
965942
if (argType != 'Object') {
966943
accessor = _cast(accessor, javaType: argType);
@@ -1176,9 +1153,10 @@ protected static ArrayList<Object> wrapError(@NonNull Throwable exception) {
11761153

11771154
/// Converts an expression that evaluates to an nullable int to an expression
11781155
/// that evaluates to a nullable enum.
1179-
String _intToEnum(String expression, String enumName, bool nullable) => nullable
1180-
? '$expression == null ? null : $enumName.values()[(int) $expression]'
1181-
: '$enumName.values()[(int) $expression]';
1156+
String _intToEnum(String expression, String enumName, bool nullable) {
1157+
final String toEnum = '$enumName.values()[((Long) $expression).intValue()]';
1158+
return nullable ? '$expression == null ? null : $toEnum' : toEnum;
1159+
}
11821160

11831161
String _getArgumentName(int count, NamedType argument) =>
11841162
argument.name.isEmpty ? 'arg$count' : argument.name;
@@ -1231,8 +1209,6 @@ String? _javaTypeForBuiltinDartType(TypeDeclaration type) {
12311209
'Int64List': 'long[]',
12321210
'Float64List': 'double[]',
12331211
'Object': 'Object',
1234-
// This is used to allow the creation of true `int`s for the codec overflow class.
1235-
_forceInt: 'int',
12361212
};
12371213
if (javaTypeForDartTypeMap.containsKey(type.baseName)) {
12381214
return javaTypeForDartTypeMap[type.baseName];
@@ -1271,11 +1247,7 @@ String _cast(String variable, {required String javaType}) {
12711247
String _castObject(NamedType field, String varName) {
12721248
final HostDatatype hostDatatype = getFieldHostDatatype(
12731249
field, (TypeDeclaration x) => _javaTypeForBuiltinDartType(x));
1274-
if (field.type.baseName == 'int') {
1275-
return '($varName == null) ? null : (($varName instanceof Integer) ? (Integer) $varName : (${hostDatatype.datatype}) $varName)';
1276-
} else {
1277-
return _cast(varName, javaType: hostDatatype.datatype);
1278-
}
1250+
return _cast(varName, javaType: hostDatatype.datatype);
12791251
}
12801252

12811253
/// Returns string of Result class type for method based on [TypeDeclaration].

packages/pigeon/lib/kotlin_generator.dart

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,9 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
344344
indent.writeln('${customType.name}.fromList(it)');
345345
});
346346
} else if (customType.type == CustomTypes.customEnum) {
347-
indent.write('return (readValue(buffer) as Int?)?.let ');
347+
indent.write('return (readValue(buffer) as Long?)?.let ');
348348
indent.addScoped('{', '}', () {
349-
indent.writeln('${customType.name}.ofRaw(it)');
349+
indent.writeln('${customType.name}.ofRaw(it.toInt())');
350350
});
351351
}
352352
});
@@ -418,7 +418,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
418418
}) {
419419
final NamedType overflowInt = NamedType(
420420
name: 'type',
421-
type: const TypeDeclaration(baseName: 'Int', isNullable: false));
421+
type: const TypeDeclaration(baseName: 'int', isNullable: false));
422422
final NamedType overflowObject = NamedType(
423423
name: 'wrapped',
424424
type: const TypeDeclaration(baseName: 'Object', isNullable: true));
@@ -445,7 +445,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
445445
companion object {
446446
fun fromList(${varNamePrefix}list: List<Any?>): Any? {
447447
val wrapper = ${generatorOptions.fileSpecificClassNameComponent}$_overflowClassName(
448-
type = ${varNamePrefix}list[0] as Int,
448+
type = ${varNamePrefix}list[0] as Long,
449449
wrapped = ${varNamePrefix}list[1],
450450
);
451451
return wrapper.unwrap()
@@ -459,14 +459,15 @@ if (wrapped == null) {
459459
return null
460460
}
461461
''');
462-
indent.writeScoped('when (type) {', '}', () {
462+
indent.writeScoped('when (type.toInt()) {', '}', () {
463463
for (int i = totalCustomCodecKeysAllowed; i < types.length; i++) {
464464
indent.writeScoped('${i - totalCustomCodecKeysAllowed} ->', '', () {
465465
if (types[i].type == CustomTypes.customClass) {
466466
indent.writeln(
467467
'return ${types[i].name}.fromList(wrapped as List<Any?>)');
468468
} else if (types[i].type == CustomTypes.customEnum) {
469-
indent.writeln('return ${types[i].name}.ofRaw(wrapped as Int)');
469+
indent.writeln(
470+
'return ${types[i].name}.ofRaw((wrapped as Long).toInt())');
470471
}
471472
});
472473
}
@@ -1009,13 +1010,5 @@ String _cast(Indent indent, String variable, {required TypeDeclaration type}) {
10091010
if (type.isNullable && typeString == 'Any') {
10101011
return variable;
10111012
}
1012-
if (typeString == 'Int' || typeString == 'Long') {
1013-
return '$variable${_castInt(type.isNullable)}';
1014-
}
10151013
return '$variable as ${_nullSafeKotlinTypeForDartType(type)}';
10161014
}
1017-
1018-
String _castInt(bool isNullable) {
1019-
final String nullability = isNullable ? '?' : '';
1020-
return '.let { num -> if (num is Int) num.toLong() else num as Long$nullability }';
1021-
}

packages/pigeon/lib/objc_generator.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,6 @@ String? _nsnumberExtractionMethod(
14401440
/// Example: ('FOO', ['Foo', 'Bar']) -> 'FOOFoo *, FOOBar *').
14411441
String _flattenTypeArguments(String? classPrefix, List<TypeDeclaration> args) {
14421442
final String result = args.map<String>((TypeDeclaration e) {
1443-
// print(e);
14441443
if (e.isEnum) {
14451444
return _enumName(e.baseName,
14461445
prefix: classPrefix, box: true, suffix: ' *');

0 commit comments

Comments
 (0)