Skip to content

[jnigen] Support use arenas on nullable JObjects #2239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions pkgs/jni/lib/src/jobject.dart
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,15 @@ class JObject {
void releasedBy(Arena arena) => arena.onReleaseAll(release);
}

extension JObjectUseExtension<T extends JObject> on T {
extension JObjectUseExtension<T extends JObject?> on T {
/// Applies [callback] on this object and then delete the underlying JNI
/// reference, returning the result of [callback].
R use<R>(R Function(T) callback) {
try {
final result = callback(this);
return result;
} finally {
release();
this?.release();
}
}
}
2 changes: 2 additions & 0 deletions pkgs/jni/test/jobject_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ void run({required TestRunnerCallback testRunner}) {
});
});
expect(randomInt, lessThan(15));
const JObject? nullableJObject = null;
expect(nullableJObject.use((_) => 'foo'), equals('foo'));
});

// The JObject and JClass have NativeFinalizer. However, it's possible to
Expand Down
Loading