Skip to content

Commit 13be8f6

Browse files
authored
[pigeon] Fix standalone Native Interop @FlutterApi issues (#13609)
Split out from #13576 per review. These are non-breaking fixes that can land on 29.x ahead of the 30.0.0 multi-engine changes. - [dart] Prefixes the internal `_<Api>Registrar` members (`dartApi`, `register`) with `pigeonVar_`. This means `@FlutterApi` methods named `register` or `dartApi` no longer collide with the JNIgen interface when `useJni` is enabled, so those names are no longer reserved. - [dart] `<FlutterApi>.implement` now defaults `name` to the default instance name and treats an empty `name` as the default. Previously it registered under `''`, which lookups never matched. - Pins `objective_c` to `<9.6.3` in the test packages and the native interop example app, to work around a debug-mode object validity check bug on the iOS simulator (dart-lang/native#3788). work towards flutter/flutter#195877 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 59b7e18 commit 13be8f6

12 files changed

Lines changed: 445 additions & 349 deletions

File tree

‎packages/pigeon/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 29.1.2
2+
3+
* [dart] Fixes `@FlutterApi` methods named `register` or `dartApi` failing with `useJni`.
4+
* [dart] Fixes `@FlutterApi` `implement` registering under an empty name instead of the default instance.
5+
16
## 29.1.1
27

38
* Updates README to label Native Interop as Preview and warn about multi-engine `@HostApi` registrations.

‎packages/pigeon/example/native_interop_app/pubspec.yaml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ dependencies:
1111
flutter:
1212
sdk: flutter
1313
jni: ^1.1.1
14-
objective_c: ^9.6.2
14+
# TODO(tarrinneal): Remove the upper bound once objective_c's debug object
15+
# validity check handles runtime-allocated classes on the iOS simulator.
16+
# See https://github.com/dart-lang/native/pull/3788.
17+
# 9.6.3's check masks the isa with a class mask that is too narrow there, so
18+
# it rejects valid ObjCProtocolBuilder instances.
19+
objective_c: ">=9.6.2 <9.6.3"
1520

1621
dev_dependencies:
1722
build_runner: ^2.1.10

‎packages/pigeon/lib/src/dart/dart_generator.dart‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,11 +1344,11 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
13441344
}) {
13451345
final mixin = generatorOptions.useJni ? ' with $_jniBridgePrefix.\$${api.name}' : '';
13461346
indent.writeScoped('final class _${api.name}Registrar$mixin {', '}', () {
1347-
indent.writeln('_${api.name}Registrar(this.dartApi);');
1347+
indent.writeln('_${api.name}Registrar(this.${varNamePrefix}dartApi);');
13481348
indent.newln();
1349-
indent.writeln('final ${api.name} dartApi;');
1349+
indent.writeln('final ${api.name} ${varNamePrefix}dartApi;');
13501350
indent.newln();
1351-
indent.writeScoped('${api.name} register({String name = defaultInstanceName}) {', '}', () {
1351+
indent.writeScoped('${api.name} ${varNamePrefix}register({String name = defaultInstanceName}) {', '}', () {
13521352
if (generatorOptions.useJni) {
13531353
indent.writeScoped('if (Platform.isAndroid) {', '}', () {
13541354
indent.writeln(
@@ -1376,7 +1376,7 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
13761376
() {
13771377
indent.writeScoped('try {', '}', () {
13781378
final methodCall =
1379-
'dartApi.${method.name}(${indexMap(method.parameters, (int index, Parameter p) {
1379+
'${varNamePrefix}dartApi.${method.name}(${indexMap(method.parameters, (int index, Parameter p) {
13801380
final _FfiType ffiType = _FfiType.fromTypeDeclaration(p.type);
13811381
return ffiType.getToDartCall(p.type, varName: _getSafeArgumentName(index, p), forceNullable: true);
13821382
}).join(', ')})';
@@ -1473,7 +1473,7 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
14731473
indent.writeln(');');
14741474
});
14751475
}
1476-
indent.writeln('return dartApi;');
1476+
indent.writeln('return ${varNamePrefix}dartApi;');
14771477
});
14781478

14791479
if (generatorOptions.useJni) {
@@ -1497,7 +1497,7 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
14971497
final String jniMethodName = jniNamer.nextApiMethod(method).name;
14981498
indent.writeScoped('$returnType $jniMethodName($params) {', '}', () {
14991499
final methodCall =
1500-
'dartApi.${method.name}(${indexMap(method.parameters, (int index, Parameter p) {
1500+
'${varNamePrefix}dartApi.${method.name}(${indexMap(method.parameters, (int index, Parameter p) {
15011501
final _JniType jniType = _JniType.fromTypeDeclaration(p.type);
15021502
return jniType.getToDartCall(p.type, varName: _getSafeArgumentName(index, p));
15031503
}).join(', ')})';
@@ -1541,7 +1541,7 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
15411541
${api.name} ${classMemberNamePrefix}register_${api.name}(
15421542
${api.name} api, {
15431543
String name = defaultInstanceName,
1544-
}) => _${api.name}Registrar(api).register(name: name);
1544+
}) => _${api.name}Registrar(api).${varNamePrefix}register(name: name);
15451545
''');
15461546
}
15471547

@@ -1650,9 +1650,9 @@ final class ${api.name}Registrar {
16501650
if (usesNativeInterop(generatorOptions)) {
16511651
indent.format('''
16521652
static ${api.name} implement(${api.name} api, {
1653-
String name = '',
1653+
String name = defaultInstanceName,
16541654
}) {
1655-
return ${api.name}Registrar().register(api, name: name);
1655+
return ${api.name}Registrar().register(api, name: name.isEmpty ? defaultInstanceName : name);
16561656
}
16571657
''');
16581658
}

‎packages/pigeon/lib/src/generator_tools.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import 'generator.dart';
1616
/// The current version of pigeon.
1717
///
1818
/// This must match the version in pubspec.yaml.
19-
const String pigeonVersion = '29.1.1';
19+
const String pigeonVersion = '29.1.2';
2020

2121
/// Default plugin package name.
2222
const String defaultPluginPackageName = 'dev.flutter.pigeon';

‎packages/pigeon/lib/src/pigeon_lib_internal.dart‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -559,24 +559,19 @@ class DartGeneratorAdapter implements GeneratorAdapter {
559559
),
560560
);
561561
}
562-
errors.addAll(_errorOnNativeInteropReservedMethodNames(root, useJni: dartOptions.useJni));
562+
errors.addAll(_errorOnNativeInteropReservedMethodNames(root));
563563
return errors;
564564
}
565565
}
566566

567567
/// Returns errors for API methods whose names conflict with members of the
568568
/// Dart classes that native interop generates for the API.
569-
List<Error> _errorOnNativeInteropReservedMethodNames(Root root, {required bool useJni}) {
569+
List<Error> _errorOnNativeInteropReservedMethodNames(Root root) {
570570
final errors = <Error>[];
571571
for (final Api api in root.apis) {
572572
final Set<String> reservedNames = switch (api) {
573573
AstHostApi() => const <String>{'createWithNativeInteropApi', 'getInstance'},
574-
// The JNI registrar implements the JNIgen interface for the API, so it
575-
// shares a namespace with the API's methods.
576-
AstFlutterApi() => <String>{
577-
'implement',
578-
if (useJni) ...<String>{'dartApi', 'register'},
579-
},
574+
AstFlutterApi() => const <String>{'implement'},
580575
_ => const <String>{},
581576
};
582577
for (final Method method in api.methods) {

‎packages/pigeon/platform_tests/shared_test_plugin_code/lib/src/generated/native_interop_tests.gen.dart‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10561,9 +10561,12 @@ abstract class NativeInteropFlutterIntegrationCoreApi {
1056110561

1056210562
static NativeInteropFlutterIntegrationCoreApi implement(
1056310563
NativeInteropFlutterIntegrationCoreApi api, {
10564-
String name = '',
10564+
String name = defaultInstanceName,
1056510565
}) {
10566-
return NativeInteropFlutterIntegrationCoreApiRegistrar().register(api, name: name);
10566+
return NativeInteropFlutterIntegrationCoreApiRegistrar().register(
10567+
api,
10568+
name: name.isEmpty ? defaultInstanceName : name,
10569+
);
1056710570
}
1056810571
}
1056910572

@@ -11215,8 +11218,11 @@ abstract class NativeInteropReservedNamesFlutterApi {
1121511218

1121611219
static NativeInteropReservedNamesFlutterApi implement(
1121711220
NativeInteropReservedNamesFlutterApi api, {
11218-
String name = '',
11221+
String name = defaultInstanceName,
1121911222
}) {
11220-
return NativeInteropReservedNamesFlutterApiRegistrar().register(api, name: name);
11223+
return NativeInteropReservedNamesFlutterApiRegistrar().register(
11224+
api,
11225+
name: name.isEmpty ? defaultInstanceName : name,
11226+
);
1122111227
}
1122211228
}

0 commit comments

Comments
 (0)