Skip to content

Commit

Permalink
[jnigen] Exclude invalid Dart identifiers by default
Browse files Browse the repository at this point in the history
  • Loading branch information
HosseinYousefi committed Oct 8, 2024
1 parent 290f21a commit 61c2a09
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pkgs/jnigen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.12.2-wip

- Now excludes invalid identifiers by default.

## 0.12.1

- Support implementing generic functions in interfaces.
Expand Down
32 changes: 32 additions & 0 deletions pkgs/jnigen/lib/src/bindings/excluder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ extension on ClassMember {
bool get isPrivate => !isPublic;
}

// TODO(https://github.com/dart-lang/native/issues/1164): Kotlin compiler
// appends the method name with a dash and a hash code when arguments contain
// inline classes. This is because inline classes do not have any runtime type
// and the typical operator overloading supported by JVM cannot work for them.
//
// Once we support inline classes, we can relax the following constraints.
final _validDartIdentifier = RegExp(r'^[a-zA-Z_$][a-zA-Z0-9_$]*$');

extension on String {
bool get isInvalidDartIdentifier =>
!_validDartIdentifier.hasMatch(this) &&
this != '<init>' &&
this != '<clinit>';
}

class Excluder extends Visitor<Classes, void> {
final Config config;

Expand All @@ -24,6 +39,11 @@ class Excluder extends Visitor<Classes, void> {
if (excluded) {
log.fine('Excluded class ${classDecl.binaryName}');
}
if (classDecl.name.isInvalidDartIdentifier) {
log.warning('Excluded class ${classDecl.binaryName}: the name is not a'
' valid Dart identifer');
return true;
}
return excluded;
});
final classExcluder = _ClassExcluder(config);
Expand Down Expand Up @@ -51,6 +71,12 @@ class _ClassExcluder extends Visitor<ClassDecl, void> {
if (excluded) {
log.fine('Excluded method ${node.binaryName}#${method.name}');
}
if (method.name.isInvalidDartIdentifier) {
log.warning(
'Excluded method ${node.binaryName}#${method.name}: the name is not'
' a valid Dart identifer');
return false;
}
return !excluded;
}).toList();
node.fields = node.fields.where((field) {
Expand All @@ -59,6 +85,12 @@ class _ClassExcluder extends Visitor<ClassDecl, void> {
if (excluded) {
log.fine('Excluded field ${node.binaryName}#${field.name}');
}
if (field.name.isInvalidDartIdentifier) {
log.warning(
'Excluded field ${node.binaryName}#${field.name}: the name is not'
' a valid Dart identifer');
return false;
}
return !excluded;
}).toList();
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/jnigen/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

environment:
Expand Down

0 comments on commit 61c2a09

Please sign in to comment.