Skip to content
Closed

ignore #10232

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
eb7cac6
methods, basic types, sync, nullable-sync, async, and classes
tarrinneal Apr 15, 2025
9fa695d
broken nullable async methods
tarrinneal Apr 15, 2025
0aff0fd
refactor type logic, add codec
tarrinneal Apr 17, 2025
1ed8a8b
objects and enums
tarrinneal Apr 22, 2025
ee50607
full codec
tarrinneal May 9, 2025
4c6eb1a
error handling and more tests
tarrinneal May 29, 2025
f87efd4
fix codec
tarrinneal May 29, 2025
3e9d9f0
flutter api and plumbing through legacy apis and tests
tarrinneal Jun 27, 2025
6ad2e37
jnigen.yaml
tarrinneal Jun 27, 2025
9b55d44
ffi just getting started
tarrinneal Jul 7, 2025
25e8c30
gen fluff
tarrinneal Jul 11, 2025
df40332
not working
tarrinneal Jul 16, 2025
d90ecca
missing files
tarrinneal Jul 17, 2025
92933f8
gitignore
tarrinneal Jul 17, 2025
cb1c0fe
ios ffigen settings
tarrinneal Jul 17, 2025
a7cc7ce
remove macos min version from ffigen config
tarrinneal Jul 17, 2025
8fb8dca
no dylibs
tarrinneal Jul 17, 2025
ab64302
more basic, still can't see class
tarrinneal Jul 17, 2025
c7d00c6
upload macos
tarrinneal Jul 17, 2025
33f8fa1
fix ffigen files
tarrinneal Jul 17, 2025
4977cc2
test
tarrinneal Jul 29, 2025
586edf2
wip
tarrinneal Jul 29, 2025
74b1cb1
Merge branch 'Frillback' of github.com:tarrinneal/packages into Frill…
tarrinneal Jul 29, 2025
3736528
ns
tarrinneal Jul 29, 2025
31d182f
dunno
tarrinneal Jul 30, 2025
c63e598
something
tarrinneal Jul 30, 2025
6dc23e7
working again
tarrinneal Jul 30, 2025
982dfe6
Merge branch 'Frillback' of https://github.com/tarrinneal/packages in…
tarrinneal Jul 30, 2025
ecc088a
enums
tarrinneal Aug 12, 2025
927fd8d
and this one
tarrinneal Aug 12, 2025
877d60d
ffi support for classes, enums, and objects (partial testing)
tarrinneal Aug 26, 2025
910ac95
tests
tarrinneal Aug 26, 2025
b522d6c
fix enums, still need to resolve Any?
tarrinneal Sep 4, 2025
bd47b34
actually fix enums
tarrinneal Sep 5, 2025
4192a89
starting nullables, gonna be a mess
tarrinneal Sep 5, 2025
86e6907
nullable base types in classes
tarrinneal Sep 17, 2025
87f4f8a
add nullable enums (no tests for null things though.....)
tarrinneal Sep 18, 2025
ed3eb93
update to latest version
tarrinneal Sep 19, 2025
457edef
Not even I understand
tarrinneal Sep 24, 2025
9214479
fix null object?
tarrinneal Sep 25, 2025
aadd859
multi-arity and nullable enums in classes
tarrinneal Sep 26, 2025
8cb24b6
nested classes
tarrinneal Sep 27, 2025
d29e86a
typed lists and maps except int keys
tarrinneal Oct 14, 2025
b32d506
Merge branch 'main' of https://github.com/flutter/packages into Frill…
tarrinneal Oct 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
flutter api and plumbing through legacy apis and tests
  • Loading branch information
tarrinneal committed Jun 27, 2025
commit 3e9d9f0ac3435e6713002801b480ad02fece80d3
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ enum class Code(val raw: Int) {

companion object {
fun ofRaw(raw: Int): Code? {
return values().firstOrNull { it.raw == raw }
return entries.firstOrNull { it.raw == raw }
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions packages/pigeon/example/app/lib/src/messages.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,17 @@ class _PigeonCodec extends StandardMessageCodec {
}

class ExampleHostApi {
/// Constructor for [ExampleHostApi]. The [binaryMessenger] named argument is
/// available for dependency injection. If it is left null, the default
/// Constructor for [ExampleHostApi]. The [binaryMessenger] named argument is
/// available for dependency injection. If it is left null, the default
/// BinaryMessenger will be used which routes to the host platform.
ExampleHostApi(
{BinaryMessenger? binaryMessenger, String messageChannelSuffix = ''})
: pigeonVar_binaryMessenger = binaryMessenger,
ExampleHostApi({
BinaryMessenger? binaryMessenger,
String messageChannelSuffix = '',
}) : pigeonVar_binaryMessenger = binaryMessenger,
pigeonVar_messageChannelSuffix =
messageChannelSuffix.isNotEmpty ? '.$messageChannelSuffix' : '';
final BinaryMessenger? pigeonVar_binaryMessenger;

final BinaryMessenger? pigeonVar_binaryMessenger;
static const MessageCodec<Object?> pigeonChannelCodec = _PigeonCodec();

final String pigeonVar_messageChannelSuffix;
Expand Down
223 changes: 203 additions & 20 deletions packages/pigeon/lib/src/dart/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,22 @@ class _JniType {
}
}

String _jniArgumentsToDartArguments(
List<Parameter> parameters, {
bool isAsynchronous = false,
}) {
final List<String> argumentSignature = <String>[];
for (final Parameter parameter in parameters) {
final _JniType jniType = _JniType.fromTypeDeclaration(parameter.type);
argumentSignature.add(jniType.getToDartCall(
parameter.type,
varName: parameter.name,
forceConversion: isAsynchronous,
));
}
return argumentSignature.join(', ');
}

String _getNullableSymbol(bool nullable) => nullable ? '?' : '';

String _getForceNonNullSymbol(bool force) => force ? '!' : '';
Expand Down Expand Up @@ -452,6 +468,9 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
required String dartPackageName,
}) {
indent.writeln("import 'dart:async';");
if (generatorOptions.useJni) {
indent.writeln("import 'dart:io' show Platform;");
}
indent.writeln(
"import 'dart:typed_data' show Float64List, Int32List, Int64List, Uint8List;",
);
Expand Down Expand Up @@ -742,12 +761,6 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
Indent indent, {
required String dartPackageName,
}) {
if (generatorOptions.useJni &&
!root.containsProxyApi &&
!root.containsEventChannel &&
!root.containsFlutterApi) {
return;
}
void writeEncodeLogic(
EnumeratedType customType, int nonSerializedClassCount) {
indent.writeScoped('else if (value is ${customType.name}) {', '}', () {
Expand Down Expand Up @@ -858,6 +871,79 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
}
}

void _writeJniFlutterApi(
InternalDartOptions generatorOptions,
Root root,
Indent indent,
AstFlutterApi api, {
required String dartPackageName,
}) {
indent.writeScoped(
'final class ${api.name}Registrar with bridge.\$${api.name} {', '}',
() {
indent.writeln('${api.name}? dartApi;');
indent.newln();
indent.writeScoped('${api.name} register(', ') ', () {
indent.writeScoped('${api.name} api, {', '}', () {
indent.writeln('String name = defaultInstanceName,');
}, nestCount: 0);
}, addTrailingNewline: false);
indent.addScoped('{', '}', () {
indent.format('''
dartApi = api;
final bridge.${api.name} impl =
bridge.${api.name}.implement(this);
bridge.${api.name}Registrar()
.registerInstance(impl, JString.fromString(name));
return api;
''');
});

for (final Method method in api.methods) {
final _JniType jniReturnType =
_JniType.fromTypeDeclaration(method.returnType);
indent.newln();
indent.writeln('@override');
String signature = method.isAsynchronous
? 'JObject'
: jniReturnType.getJniCallReturnType(false);
signature += ' ${method.name}(';
signature +=
_getMethodParameterSignature(method.parameters, useJni: true);
signature += method.isAsynchronous
? '${method.parameters.isNotEmpty ? ', ' : ''}JObject continuation'
: '';
signature += ')';
indent.writeScoped('$signature {', '}', () {
indent.writeScoped('if (dartApi != null) {', '} ', () {
final String methodCall =
'dartApi!.${method.name}(${_jniArgumentsToDartArguments(method.parameters, isAsynchronous: method.isAsynchronous)})';
if (method.returnType.isVoid) {
if (method.isAsynchronous) {
indent.writeln('$methodCall;');
indent.writeln('return continuation;');
} else {
indent.writeln('return $methodCall;');
}
} else {
final _JniType returnType =
_JniType.fromTypeDeclaration(method.returnType);
indent.writeln(
'final ${jniReturnType.type.getFullName()} response = $methodCall;');
indent.writeln(
'return ${returnType.getToJniCall(method.returnType, 'response', returnType)};');
}
});
indent.writeScoped('else {', '}', () {
indent.writeln(
"throw ArgumentError('${api.name} was not registered.');");
});
});
if (method.isAsynchronous && method.returnType.isVoid) {}
}
});
}

/// Writes the code for host [Api], [api].
/// Example:
/// class FooCodec extends StandardMessageCodec {...}
Expand All @@ -880,6 +966,15 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
indent.newln();
addDocumentationComments(
indent, api.documentationComments, _docCommentSpec);
if (generatorOptions.useJni) {
_writeJniFlutterApi(
generatorOptions,
root,
indent,
api,
dartPackageName: dartPackageName,
);
}

indent.write('abstract class ${api.name} ');
indent.addScoped('{', '}', () {
Expand All @@ -903,9 +998,22 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
indent.writeln('$returnType ${func.name}($argSignature);');
indent.newln();
}
indent.write(
"static void setUp(${api.name}? api, {BinaryMessenger? binaryMessenger, String messageChannelSuffix = '',}) ");
indent.format('''
static void setUp(${api.name}? api, {
BinaryMessenger? binaryMessenger,
String messageChannelSuffix = '',
}) ''');

indent.addScoped('{', '}', () {
if (generatorOptions.useJni) {
indent.format('''
if (Platform.isAndroid && api != null) {
${api.name}Registrar().register(api, name: messageChannelSuffix.isEmpty
? defaultInstanceName
: messageChannelSuffix);
}
''');
}
indent.writeln(
r"messageChannelSuffix = messageChannelSuffix.isNotEmpty ? '.$messageChannelSuffix' : '';");

Expand Down Expand Up @@ -938,15 +1046,15 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
dartPackageName: dartPackageName);
}

void _writeJniApi(
void _writeJniHostApi(
InternalDartOptions generatorOptions,
Root root,
Indent indent,
AstHostApi api, {
required String dartPackageName,
}) {
final String dartApiName = api.name;
final String jniApiRegistrarName = 'bridge.${dartApiName}Registrar';
final String dartApiName = '${api.name}ForAndroid';
final String jniApiRegistrarName = 'bridge.${api.name}Registrar';
indent.newln();
indent.writeScoped('class $dartApiName {', '}', () {
indent.format('''
Expand Down Expand Up @@ -1040,9 +1148,8 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
required String dartPackageName,
}) {
if (generatorOptions.useJni) {
_writeJniApi(generatorOptions, root, indent, api,
_writeJniHostApi(generatorOptions, root, indent, api,
dartPackageName: dartPackageName);
return;
}
indent.newln();
bool first = true;
Expand All @@ -1051,20 +1158,61 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
indent.write('class ${api.name} ');
indent.addScoped('{', '}', () {
indent.format('''
/// Constructor for [${api.name}]. The [binaryMessenger] named argument is
/// available for dependency injection. If it is left null, the default
/// Constructor for [${api.name}]. The [binaryMessenger] named argument is
/// available for dependency injection. If it is left null, the default
/// BinaryMessenger will be used which routes to the host platform.
${api.name}({BinaryMessenger? binaryMessenger, String messageChannelSuffix = ''})
${api.name}({
BinaryMessenger? binaryMessenger,
String messageChannelSuffix = '',
${generatorOptions.useJni ? '${api.name}ForAndroid? jniApi,\n' : ''}})
: ${varNamePrefix}binaryMessenger = binaryMessenger,
${varNamePrefix}messageChannelSuffix = messageChannelSuffix.isNotEmpty ? '.\$messageChannelSuffix' : '';
final BinaryMessenger? ${varNamePrefix}binaryMessenger;
${varNamePrefix}messageChannelSuffix = messageChannelSuffix.isNotEmpty ? '.\$messageChannelSuffix' : ''${generatorOptions.useJni ? ',\n_jniApi = jniApi;\n' : ';'}
''');

if (generatorOptions.useJni) {
indent.format('''
/// Creates an instance of [${api.name}] that requests an instance of
/// [${api.name}ForAndroid] from the host platform with a matching instance name
/// to [messageChannelSuffix] or the default instance.
///
/// Throws [ArgumentError] if no matching instance can be found.
factory ${api.name}.createWithJniApi({
BinaryMessenger? binaryMessenger,
String messageChannelSuffix = '',
}) {
${api.name}ForAndroid? jniApi;
String jniApiInstanceName = '';
if (Platform.isAndroid) {
if (messageChannelSuffix.isEmpty) {
jniApi = ${api.name}ForAndroid.getInstance();
} else {
jniApiInstanceName = messageChannelSuffix;
jniApi = ${api.name}ForAndroid.getInstance(
channelName: messageChannelSuffix);
}
}
if (jniApi == null) {
throw ArgumentError(
'No ${api.name} instance with \${jniApiInstanceName.isEmpty ? 'no ' : ''} instance name \${jniApiInstanceName.isNotEmpty ? '"\$jniApiInstanceName"' : ''} "\$jniApiInstanceName "}found.');
}
return ${api.name}(
binaryMessenger: binaryMessenger,
messageChannelSuffix: messageChannelSuffix,
jniApi: jniApi,
);
}
''');
}

indent.writeln('final BinaryMessenger? ${varNamePrefix}binaryMessenger;');
indent.writeln(
'static const MessageCodec<Object?> $_pigeonChannelCodec = $_pigeonMessageCodec();');
indent.newln();
indent.writeln('final String $_suffixVarName;');
indent.newln();
if (generatorOptions.useJni) {
indent.writeln('final ${api.name}ForAndroid? _jniApi;');
}
for (final Method func in api.methods) {
if (!first) {
indent.newln();
Expand All @@ -1079,6 +1227,7 @@ final BinaryMessenger? ${varNamePrefix}binaryMessenger;
documentationComments: func.documentationComments,
channelName: makeChannelName(api, func, dartPackageName),
addSuffixVariable: true,
useJni: generatorOptions.useJni,
);
}
});
Expand Down Expand Up @@ -1539,8 +1688,7 @@ final BinaryMessenger? ${varNamePrefix}binaryMessenger;
Indent indent, {
required String dartPackageName,
}) {
if ((root.containsHostApi && !generatorOptions.useJni) ||
root.containsProxyApi) {
if (root.containsHostApi || root.containsProxyApi) {
_writeCreateConnectionError(indent);
}
if (root.containsFlutterApi ||
Expand Down Expand Up @@ -1890,13 +2038,22 @@ if (wrapped == null) {
required List<String> documentationComments,
required String channelName,
required bool addSuffixVariable,
bool useJni = false,
}) {
addDocumentationComments(indent, documentationComments, _docCommentSpec);
final String argSignature = _getMethodParameterSignature(parameters);
indent.write(
'Future<${_addGenericTypesNullable(returnType)}> $name($argSignature) async ',
);
indent.addScoped('{', '}', () {
if (useJni) {
indent.writeScoped('if (Platform.isAndroid && _jniApi != null) {', '}',
() {
indent.writeln('return _jniApi.$name(${parameters.map(
(Parameter e) => '${e.isNamed ? '${e.name}: ' : ''}${e.name}',
).join(', ')});');
});
}
_writeHostMethodMessageCall(
indent,
channelName: channelName,
Expand Down Expand Up @@ -2955,16 +3112,42 @@ String _getSafeArgumentName(int count, NamedType field) =>
String _getParameterName(int count, NamedType field) =>
field.name.isEmpty ? 'arg$count' : field.name;

String _getJniMethodParameterSignature(
Iterable<Parameter> parameters, {
bool addTrailingComma = false,
bool isAsynchronous = false,
}) {
String signature = '';
if (parameters.isEmpty) {
return signature;
}
for (final Parameter parameter in parameters) {
_JniType jniType = _JniType.fromTypeDeclaration(parameter.type);
signature +=
'${jniType.getJniCallReturnType(isAsynchronous)} ${parameter.name}${addTrailingComma || parameters.length > 1 ? ',' : ''}';
}
return signature;
}

/// Generates the parameters code for [func]
/// Example: (func, _getParameterName) -> 'String? foo, int bar'
String _getMethodParameterSignature(
Iterable<Parameter> parameters, {
bool addTrailingComma = false,
bool useJni = false,
bool isAsynchronous = false,
}) {
String signature = '';
if (parameters.isEmpty) {
return signature;
}
if (useJni) {
return _getJniMethodParameterSignature(
parameters,
addTrailingComma: addTrailingComma,
isAsynchronous: isAsynchronous,
);
}

final List<Parameter> requiredPositionalParams = parameters
.where((Parameter p) => p.isPositional && !p.isOptional)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class JnigenYamlGenerator extends Generator<InternalJnigenYamlOptions> {
''');
indent.writeScoped('classes:', '', () {
for (final Api api in root.apis) {
if (api is AstHostApi) {
if (api is AstHostApi || api is AstFlutterApi) {
indent.writeln("- '${api.name}'");
indent.writeln("- '${api.name}Registrar'");
}
Expand Down
Loading