Skip to content

Commit aa94f42

Browse files
[jnigen] Exclude invalid Dart identifiers by default (#1637)
1 parent fa6ffec commit aa94f42

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

pkgs/jnigen/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.12.2-wip
2+
3+
- Now excludes invalid identifiers by default.
4+
15
## 0.12.1
26

37
- Support implementing generic functions in interfaces.

pkgs/jnigen/lib/src/bindings/excluder.dart

+32
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ extension on ClassMember {
1111
bool get isPrivate => !isPublic;
1212
}
1313

14+
// TODO(https://github.com/dart-lang/native/issues/1164): Kotlin compiler
15+
// appends the method name with a dash and a hash code when arguments contain
16+
// inline classes. This is because inline classes do not have any runtime type
17+
// and the typical operator overloading supported by JVM cannot work for them.
18+
//
19+
// Once we support inline classes, we can relax the following constraints.
20+
final _validDartIdentifier = RegExp(r'^[a-zA-Z_$][a-zA-Z0-9_$]*$');
21+
22+
extension on String {
23+
bool get isInvalidDartIdentifier =>
24+
!_validDartIdentifier.hasMatch(this) &&
25+
this != '<init>' &&
26+
this != '<clinit>';
27+
}
28+
1429
class Excluder extends Visitor<Classes, void> {
1530
final Config config;
1631

@@ -24,6 +39,11 @@ class Excluder extends Visitor<Classes, void> {
2439
if (excluded) {
2540
log.fine('Excluded class ${classDecl.binaryName}');
2641
}
42+
if (classDecl.name.isInvalidDartIdentifier) {
43+
log.warning('Excluded class ${classDecl.binaryName}: the name is not a'
44+
' valid Dart identifer');
45+
return true;
46+
}
2747
return excluded;
2848
});
2949
final classExcluder = _ClassExcluder(config);
@@ -51,6 +71,12 @@ class _ClassExcluder extends Visitor<ClassDecl, void> {
5171
if (excluded) {
5272
log.fine('Excluded method ${node.binaryName}#${method.name}');
5373
}
74+
if (method.name.isInvalidDartIdentifier) {
75+
log.warning(
76+
'Excluded method ${node.binaryName}#${method.name}: the name is not'
77+
' a valid Dart identifer');
78+
return false;
79+
}
5480
return !excluded;
5581
}).toList();
5682
node.fields = node.fields.where((field) {
@@ -59,6 +85,12 @@ class _ClassExcluder extends Visitor<ClassDecl, void> {
5985
if (excluded) {
6086
log.fine('Excluded field ${node.binaryName}#${field.name}');
6187
}
88+
if (field.name.isInvalidDartIdentifier) {
89+
log.warning(
90+
'Excluded field ${node.binaryName}#${field.name}: the name is not'
91+
' a valid Dart identifer');
92+
return false;
93+
}
6294
return !excluded;
6395
}).toList();
6496
}

pkgs/jnigen/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
name: jnigen
66
description: A Dart bindings generator for Java and Kotlin that uses JNI under the hood to interop with Java virtual machine.
7-
version: 0.12.1
7+
version: 0.12.2-wip
88
repository: https://github.com/dart-lang/native/tree/main/pkgs/jnigen
99

1010
environment:

0 commit comments

Comments
 (0)