Skip to content

Commit

Permalink
Merge branch 'main' into ffigenpublish
Browse files Browse the repository at this point in the history
  • Loading branch information
liamappelbe committed Oct 18, 2024
2 parents 34d2326 + cc34333 commit dca417b
Show file tree
Hide file tree
Showing 17 changed files with 319 additions and 75 deletions.
2 changes: 1 addition & 1 deletion pkgs/ffigen/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies:

dev_dependencies:
async: ^2.11.0
coverage: ^1.8.0
coverage: ^1.10.0
dart_flutter_team_lints: ^2.0.0
json_schema: ^5.1.1
leak_tracker: ^10.0.7
Expand Down
42 changes: 6 additions & 36 deletions pkgs/ffigen/test/native_objc_test/isolate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,6 @@ import '../test_utils.dart';
import 'isolate_bindings.dart';
import 'util.dart';

// TODO(https://github.com/dart-lang/coverage/issues/472): Delete this and use
// Isolate.run once the coverage bug is fixed.
Future<R> isolateRun<R>(FutureOr<R> computation()) async {
FutureOr<R> Function()? comp = computation;
Future<void> run(SendPort sendPort) async {
sendPort.send(await comp!());
comp = null;
sendPort.send(null);
Isolate.current.kill();
}

final port = ReceivePort();
final queue = StreamQueue(port);
final isolate = await Isolate.spawn(run, port.sendPort);
final result = await queue.next as R;
await queue.next; // Wait for isolate to release its reference to comp.
port.close();
return result;
}

void main() {
group('isolate', () {
setUpAll(() {
Expand All @@ -60,10 +40,6 @@ void main() {
sendable.value = 456;
sendPort.send(oldValue);
port.close();

// TODO(https://github.com/dart-lang/coverage/issues/472): Delete this.
sendPort.send(null);
Isolate.current.kill();
}

test('Sending object through a port', () async {
Expand All @@ -90,15 +66,14 @@ void main() {

sendable = null;
doGC();
// TODO(https://github.com/dart-lang/coverage/issues/472): Re-enable.
// expect(objectRetainCount(pointer), 0);
expect(objectRetainCount(pointer), 0);
}, skip: !canDoGC);

test('Capturing object in closure', () async {
Sendable? sendable = Sendable.new1();
sendable.value = 123;

final oldValue = await isolateRun(() {
final oldValue = await Isolate.run(() {
final oldValue = sendable!.value;
sendable!.value = 456;
return oldValue;
Expand All @@ -124,10 +99,6 @@ void main() {
final block = await queue.next as ObjCBlock<Void Function(Int32)>;
block(123);
port.close();

// TODO(https://github.com/dart-lang/coverage/issues/472): Delete this.
sendPort.send(null);
Isolate.current.kill();
}

test('Sending block through a port', () async {
Expand Down Expand Up @@ -156,8 +127,7 @@ void main() {

block = null;
doGC();
// TODO(https://github.com/dart-lang/coverage/issues/472): Re-enable.
// expect(blockRetainCount(pointer), 0);
expect(blockRetainCount(pointer), 0);
}, skip: !canDoGC);

ObjCBlock<Void Function(Int32)> makeBlock(Completer<int> completer) {
Expand All @@ -172,7 +142,7 @@ void main() {
final completer = Completer<int>();
ObjCBlock<Void Function(Int32)>? block = makeBlock(completer);

await isolateRun(() {
await Isolate.run(() {
block!(123);
});
final value = await completer.future;
Expand All @@ -193,7 +163,7 @@ void main() {
expect(objectRetainCount(pointer), 1);
expect(sendable.ref.isReleased, isFalse);

final (oldIsReleased, newIsReleased) = await isolateRun(() {
final (oldIsReleased, newIsReleased) = await Isolate.run(() {
final oldIsReleased = sendable.ref.isReleased;
sendable!.ref.release();
return (oldIsReleased, sendable.ref.isReleased);
Expand All @@ -213,7 +183,7 @@ void main() {

expect(sendable.ref.isReleased, isFalse);

await isolateRun(() {
await Isolate.run(() {
sendable!.ref.release();
});

Expand Down
7 changes: 7 additions & 0 deletions pkgs/jni/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.12.1-wip

- Add `JniUtils.fromReferenceAddress` which helps with sending `JObject`s
through method channels. You can send the address of the pointer as `long` and
reconstruct the class using the helper method.
- Fixed a bug where it would be possible for a type class inference to fail.

## 0.12.0

- **Breaking Change**: Renamed `castTo` to `as`.
Expand Down
13 changes: 13 additions & 0 deletions pkgs/jni/java/src/main/java/com/github/dart_lang/jni/JniUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

package com.github.dart_lang.jni;

public class JniUtils {
static {
System.loadLibrary("dartjni");
}

public static native Object fromReferenceAddress(long address);
}
5 changes: 3 additions & 2 deletions pkgs/jni/lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ abstract class JObjType<T extends JObject> extends JType<T>
}

/// Lowest common ancestor of two types in the inheritance tree.
JObjType _lowestCommonAncestor(JObjType a, JObjType b) {
JObjType<dynamic> _lowestCommonAncestor(
JObjType<dynamic> a, JObjType<dynamic> b) {
while (a.superCount > b.superCount) {
a = a.superType;
}
Expand All @@ -174,6 +175,6 @@ JObjType _lowestCommonAncestor(JObjType a, JObjType b) {
}

@internal
JObjType lowestCommonSuperType(List<JObjType> types) {
JObjType<dynamic> lowestCommonSuperType(List<JObjType<dynamic>> types) {
return types.reduce(_lowestCommonAncestor);
}
2 changes: 1 addition & 1 deletion pkgs/jni/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

name: jni
description: A library to access JNI from Dart and Flutter that acts as a support library for package:jnigen.
version: 0.12.0
version: 0.12.1-wip
repository: https://github.com/dart-lang/native/tree/main/pkgs/jni

topics:
Expand Down
3 changes: 3 additions & 0 deletions pkgs/jnigen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## 0.12.2-wip

- Now excludes invalid identifiers by default.
- Fixed a bug where if multiple jars have classes within the same package, only
one of them gets generated.
- Fixed a bug where it would be possible for a type class inference to fail.

## 0.12.1

Expand Down
8 changes: 8 additions & 0 deletions pkgs/jnigen/dartdoc_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
dartdoc:
categories:
"Implementing Java interfaces from Dart":
markdown: doc/interface_implementation.md
name: "Interface Implementation"
"Syntactic and semantic differences between Java and the generated Dart bindings":
markdown: doc/java_differences.md
name: "Java Differences"
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
## Implementing Java interfaces from Dart

> [!NOTE]
> This feature is experimental, and in
> [active development](https://github.com/dart-lang/native/issues/1569).
>
> To opt in to use this feature, add the following to your JNIgen configuration
> yaml:
>
> ```yaml
> enable_experiment:
> - interface_implementation
> ```
Let's take a simple Java interface like `Runnable` that has a single `void`
method called `run`:

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion pkgs/jnigen/example/in_app_java/jnigen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ output:
source_path:
- 'android/app/src/main/java'
classes:
- 'com.example.in_app_java.AndroidUtils' # from source_path
- 'com.example.in_app_java' # Generate the entire package
- 'androidx.emoji2.text.EmojiCompat' # From gradle's compile classpath
- 'androidx.emoji2.text.DefaultEmojiCompatConfig' # From gradle's compile classpath
- 'android.os.Build' # from gradle's compile classpath
Expand Down
Loading

0 comments on commit dca417b

Please sign in to comment.