Skip to content

Commit

Permalink
non-null
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrinneal committed Aug 3, 2023
1 parent 051d20a commit 765618f
Show file tree
Hide file tree
Showing 43 changed files with 913 additions and 163 deletions.
5 changes: 5 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 10.1.5

* Adds primitive enum support.
* Fixes Objc nullable enums.

## 10.1.4

* Adds package name to method channel strings to avoid potential collisions between plugins.
Expand Down
7 changes: 7 additions & 0 deletions packages/pigeon/example/app/macos/Runner/messages.g.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ typedef NS_ENUM(NSUInteger, PGNCode) {
PGNCodeTwo = 1,
};

@class PGNCodeWrapper;

/// Wrapper for PGNCode to manage primitive and nullable use.
@interface PGNCodeWrapper : NSObject
@property(nonatomic, assign) PGNCode value;
@end

@class PGNMessageData;

@interface PGNMessageData : NSObject
Expand Down
6 changes: 6 additions & 0 deletions packages/pigeon/example/app/macos/Runner/messages.g.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
#error File requires ARC to be enabled.
#endif

@interface PGNCodeWrapper ()
@end

@implementation PGNCodeWrapper
@end

static NSArray *wrapResult(id result, FlutterError *error) {
if (error) {
return @[
Expand Down
44 changes: 35 additions & 9 deletions packages/pigeon/lib/cpp_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -888,9 +888,13 @@ class CppSourceGenerator extends StructuredGenerator<CppOptions> {
indent.writeln(
'std::unique_ptr<EncodableValue> response = GetCodec().DecodeMessage(reply, reply_size);');
indent.writeln('const auto& $encodedReplyName = *response;');
_writeEncodableValueArgumentUnwrapping(indent, returnType,
argName: successCallbackArgument,
encodableArgName: encodedReplyName);
_writeEncodableValueArgumentUnwrapping(
indent,
root,
returnType,
argName: successCallbackArgument,
encodableArgName: encodedReplyName,
);
}
indent.writeln('on_success($successCallbackArgument);');
});
Expand Down Expand Up @@ -968,8 +972,13 @@ class CppSourceGenerator extends StructuredGenerator<CppOptions> {
indent.writeln('return;');
});
}
_writeEncodableValueArgumentUnwrapping(indent, hostType,
argName: argName, encodableArgName: encodableArgName);
_writeEncodableValueArgumentUnwrapping(
indent,
root,
hostType,
argName: argName,
encodableArgName: encodableArgName,
);
methodArgument.add(argName);
});
}
Expand Down Expand Up @@ -1205,9 +1214,15 @@ return EncodableValue(EncodableList{
} else {
final HostDatatype hostType = getHostDatatype(returnType, root.classes,
root.enums, _shortBaseCppTypeForBuiltinDartType);
const String extractedValue = 'std::move(output).TakeValue()';
final String wrapperType =
hostType.isBuiltin ? 'EncodableValue' : 'CustomEncodableValue';
String enumPrefix = '';
if (isEnum(root, returnType)) {
enumPrefix = '(int) ';
}
final String extractedValue =
'${enumPrefix}std::move(output).TakeValue()';
final String wrapperType = hostType.isBuiltin || isEnum(root, returnType)
? 'EncodableValue'
: 'CustomEncodableValue';
if (returnType.isNullable) {
// The value is a std::optional, so needs an extra layer of
// handling.
Expand Down Expand Up @@ -1297,6 +1312,7 @@ ${prefix}reply(EncodableValue(std::move(wrapped)));''';
/// existing EncodableValue variable called [encodableArgName].
void _writeEncodableValueArgumentUnwrapping(
Indent indent,
Root root,
HostDatatype hostType, {
required String argName,
required String encodableArgName,
Expand All @@ -1320,6 +1336,9 @@ ${prefix}reply(EncodableValue(std::move(wrapped)));''';
} else if (hostType.isBuiltin) {
indent.writeln(
'const auto* $argName = std::get_if<${hostType.datatype}>(&$encodableArgName);');
} else if (hostType.isEnum) {
indent.writeln(
'const auto* $argName = &((${hostType.datatype})std::get<int>($encodableArgName));');
} else {
indent.writeln(
'const auto* $argName = &(std::any_cast<const ${hostType.datatype}&>(std::get<CustomEncodableValue>($encodableArgName)));');
Expand All @@ -1342,6 +1361,9 @@ ${prefix}reply(EncodableValue(std::move(wrapped)));''';
} else if (hostType.isBuiltin) {
indent.writeln(
'const auto& $argName = std::get<${hostType.datatype}>($encodableArgName);');
} else if (hostType.isEnum) {
indent.writeln(
'const ${hostType.datatype}& $argName = (${hostType.datatype})$encodableArgName.LongValue();');
} else {
indent.writeln(
'const auto& $argName = std::any_cast<const ${hostType.datatype}&>(std::get<CustomEncodableValue>($encodableArgName));');
Expand Down Expand Up @@ -1390,7 +1412,11 @@ String _getSafeArgumentName(int count, NamedType argument) =>
/// Returns a non-nullable variant of [type].
HostDatatype _nonNullableType(HostDatatype type) {
return HostDatatype(
datatype: type.datatype, isBuiltin: type.isBuiltin, isNullable: false);
datatype: type.datatype,
isBuiltin: type.isBuiltin,
isNullable: false,
isEnum: type.isEnum,
);
}

String _pascalCaseFromCamelCase(String camelCase) =>
Expand Down
20 changes: 17 additions & 3 deletions packages/pigeon/lib/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ $resultAt != null
codecName = _getCodecName(api);
_writeCodec(indent, codecName, api, root);
}
final List<String> customEnumNames =
root.enums.map((Enum x) => x.name).toList();
indent.newln();
bool first = true;
addDocumentationComments(
Expand Down Expand Up @@ -553,9 +555,21 @@ final BinaryMessenger? _binaryMessenger;
final String nullHandler = func.returnType.isNullable
? (genericCastCall.isEmpty ? '' : '?')
: '!';
final String returnStatement = func.returnType.isVoid
? 'return;'
: 'return $nullablyTypedAccessor$nullHandler$genericCastCall;';
String returnStatement = 'return';
if (customEnumNames.contains(returnType)) {
if (func.returnType.isNullable) {
returnStatement =
'$returnStatement $nullablyTypedAccessor == null ? null : $returnType.values[$accessor as int]';
} else {
returnStatement =
'$returnStatement $returnType.values[$accessor! as int]';
}
} else if (!func.returnType.isVoid) {
returnStatement =
'$returnStatement $nullablyTypedAccessor$nullHandler$genericCastCall';
}
returnStatement = '$returnStatement;';

indent.format('''
final List<Object?>? replyList =
\t\tawait channel.send($sendArgument) as List<Object?>?;
Expand Down
24 changes: 20 additions & 4 deletions packages/pigeon/lib/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'ast.dart';
/// The current version of pigeon.
///
/// This must match the version in pubspec.yaml.
const String pigeonVersion = '10.1.4';
const String pigeonVersion = '10.1.5';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down Expand Up @@ -176,6 +176,7 @@ class HostDatatype {
required this.datatype,
required this.isBuiltin,
required this.isNullable,
required this.isEnum,
});

/// The [String] that can be printed into host code to represent the type.
Expand All @@ -186,6 +187,9 @@ class HostDatatype {

/// `true` if the type corresponds to a nullable Dart datatype.
final bool isNullable;

/// `true if the type is a custom enum.
final bool isEnum;
}

/// Calculates the [HostDatatype] for the provided [NamedType].
Expand Down Expand Up @@ -226,20 +230,32 @@ HostDatatype _getHostDatatype(TypeDeclaration type, List<Class> classes,
? customResolver(type.baseName)
: type.baseName;
return HostDatatype(
datatype: customName, isBuiltin: false, isNullable: type.isNullable);
datatype: customName,
isBuiltin: false,
isNullable: type.isNullable,
isEnum: false,
);
} else if (enums.map((Enum x) => x.name).contains(type.baseName)) {
final String customName = customResolver != null
? customResolver(type.baseName)
: type.baseName;
return HostDatatype(
datatype: customName, isBuiltin: false, isNullable: type.isNullable);
datatype: customName,
isBuiltin: false,
isNullable: type.isNullable,
isEnum: true,
);
} else {
throw Exception(
'unrecognized datatype ${fieldName == null ? '' : 'for field:"$fieldName" '}of type:"${type.baseName}"');
}
} else {
return HostDatatype(
datatype: datatype, isBuiltin: true, isNullable: type.isNullable);
datatype: datatype,
isBuiltin: true,
isNullable: type.isNullable,
isEnum: false,
);
}
}

Expand Down
6 changes: 5 additions & 1 deletion packages/pigeon/lib/java_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,12 @@ Result<$returnType> $resultName =
indent.writeln('$call;');
indent.writeln('wrapped.add(0, null);');
} else {
String enumTag = '';
if (isEnum(root, method.returnType)) {
enumTag = '.index';
}
indent.writeln('$returnType output = $call;');
indent.writeln('wrapped.add(0, output);');
indent.writeln('wrapped.add(0, output$enumTag);');
}
});
indent.add(' catch (Throwable exception) ');
Expand Down
6 changes: 5 additions & 1 deletion packages/pigeon/lib/kotlin_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,11 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
indent.writeln(call);
indent.writeln('wrapped = listOf<Any?>(null)');
} else {
indent.writeln('wrapped = listOf<Any?>($call)');
String enumTag = '';
if (isEnum(root, method.returnType)) {
enumTag = '.raw';
}
indent.writeln('wrapped = listOf<Any?>($call$enumTag)');
}
}, addTrailingNewline: false);
indent.add(' catch (exception: Throwable) ');
Expand Down
Loading

0 comments on commit 765618f

Please sign in to comment.