Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
HosseinYousefi committed Sep 13, 2023
1 parent b61750d commit 7fd2fb3
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 56 deletions.
56 changes: 37 additions & 19 deletions jni/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,57 @@
## 0.7.0
* **Breaking Change** ([#387](https://github.com/dart-lang/jnigen/issues/387)): Added `JBuffer` and `JByteBuffer` classes as default classes for `java.nio.Buffer` and `java.nio.ByteBuffer` respectively.
* **Breaking Change**: Made the type classes `final`.
* Fixed a bug where `addAll`, `removeAll` and `retainAll` in `JSet` would run their respective operation twice.
* Fixed a bug where `JList.insertAll` would not throw the potentially thrown Java exception.

- **Breaking Change** ([#387](https://github.com/dart-lang/jnigen/issues/387)):
Added `JBuffer` and `JByteBuffer` classes as default classes for
`java.nio.Buffer` and `java.nio.ByteBuffer` respectively.
- **Breaking Change**: Made the type classes `final`.
- Fixed a bug where `addAll`, `removeAll` and `retainAll` in `JSet` would run
their respective operation twice.
- Fixed a bug where `JList.insertAll` would not throw the potentially thrown
Java exception.

## 0.6.1
* Depend on the stable version of Dart 3.1.

- Depend on the stable version of Dart 3.1.

## 0.6.0
* **Breaking Change** ([#131](https://github.com/dart-lang/jnigen/issues/131)): Renamed `delete*` to `release*`.
* Added `PortProxy` and related methods used for interface implementation.
* Added the missing binding for `java.lang.Character`.

- **Breaking Change** ([#131](https://github.com/dart-lang/jnigen/issues/131)):
Renamed `delete*` to `release*`.
- Added `PortProxy` and related methods used for interface implementation.
- Added the missing binding for `java.lang.Character`.

## 0.5.0
* **Breaking Change** ([#137](https://github.com/dart-lang/jnigen/issues/137)): Java primitive types are now all lowercase like `jint`, `jshort`, ...
* The bindings for `java.util.Set`, `java.util.Map`, `java.util.List` and the numeric types like `java.lang.Integer`, `java.lang.Boolean`, ... are now included in `package:jni`.

- **Breaking Change** ([#137](https://github.com/dart-lang/jnigen/issues/137)):
Java primitive types are now all lowercase like `jint`, `jshort`, ...
- The bindings for `java.util.Set`, `java.util.Map`, `java.util.List` and the
numeric types like `java.lang.Integer`, `java.lang.Boolean`, ... are now
included in `package:jni`.

## 0.4.0
* Type classes now have `superCount` and `superType` getters used for type inference.

- Type classes now have `superCount` and `superType` getters used for type
inference.

## 0.3.0
* Added `PortContinuation` used for `suspend fun` in Kotlin.
* `dartjni` now depends on `dart_api_dl.h`.

- Added `PortContinuation` used for `suspend fun` in Kotlin.
- `dartjni` now depends on `dart_api_dl.h`.

## 0.2.1
* Added `.clang-format` to pub.

- Added `.clang-format` to pub.

## 0.2.0
* Added array support
* Added generic support
* `JniX` turned into `JX` for a more terse code.

- Added array support
- Added generic support
- `JniX` turned into `JX` for a more terse code.

## 0.1.1
* Windows support for running tests and examples on development machines.

- Windows support for running tests and examples on development machines.

## 0.1.0
* Initial version: Android and Linux support, JObject API

- Initial version: Android and Linux support, JObject API
11 changes: 11 additions & 0 deletions jni/lib/src/nio/jbuffer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ final class JBufferType extends JObjType<JBuffer> {
}
}

/// A container for data of a specific primitive type.
///
/// The bindings for `java.nio.Buffer`.
///
/// A buffer is a linear, finite sequence of elements of a specific primitive
/// type. Aside from its content, the essential properties of a buffer are its
/// [capacity], [limit], and [position].
///
/// There is one subclass of this class for each non-boolean primitive type.
/// We currently only have the bindings for `java.nio.ByteBuffer` in this
/// package as [JByteBuffer].
class JBuffer extends JObject {
@override
// ignore: overridden_fields
Expand Down
37 changes: 36 additions & 1 deletion jni/lib/src/nio/jbyte_buffer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,41 @@ final class JByteBufferType extends JObjType<JByteBuffer> {
}
}

/// A byte [JBuffer].
///
/// The bindings for `java.nio.ByteBuffer`.
///
/// This enables fast memory copying between Java and Dart when directly
/// allocated (See [JByteBuffer.allocateDirect]).
///
/// To create a [JByteBuffer] from the content of a [Uint8List],
/// use [JByteBuffer.fromList]. This uses direct allocation and will greatly
/// speed up the copying.
///
/// [asUint8List] provides a direct access to the underlying [Uint8List] that
/// this buffer uses. This means any changes to it will change the content of
/// the buffer and vice versa. You can use this to access to [Uint8List] methods
/// such as [Uint8List.setRange].
///
/// It is safe to use [asUint8List]. As long as the resulting list is
/// accessible, the associated Java buffer will also be kept alive. Once all the
/// instances of the original buffer and the lists produced from [asUint8List]
/// are inaccessible both in Java and Dart, Java will correctly garbage collects
/// the buffer and frees its underlying memory.
///
/// You can choose to [release] the original buffer when calling [asUint8List]
/// by setting the `releaseOriginal` parameter to `true`.
///
/// Example:
/// ```dart
/// final directBuffer = JByteBuffer.allocateDirect(3);
/// // [releaseOriginal] is `false` by default.
/// final data1 = directBuffer.asUint8List();
/// directBuffer.nextByte = 42; // No problem!
/// print(data1[0]); // prints 42!
/// final data2 = directBuffer.asUint8List(releaseOriginal: true);
/// // directBuffer.nextByte = 42; // throws[UseAfterReleaseException]!
/// ```
class JByteBuffer extends JBuffer {
@override
// ignore: overridden_fields
Expand Down Expand Up @@ -123,7 +158,7 @@ class JByteBuffer extends JBuffer {
/// Creates a [JByteBuffer] from the content of [list].
///
/// The [JByteBuffer] will be allocated using [JByteBuffer.allocateDirect].
factory JByteBuffer.fromList(List<int> list) {
factory JByteBuffer.fromList(Uint8List list) {
final buffer = JByteBuffer.allocateDirect(list.length);
buffer._asUint8ListUnsafe().setAll(0, list);
return buffer;
Expand Down
9 changes: 5 additions & 4 deletions jni/lib/src/util/jlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,11 @@ class JList<$E extends JObject> extends JObject with ListMixin<$E> {
Jni.env.IsInstanceOf(
(iterable as JObject).reference, _collectionClass.reference)) {
Jni.accessors.callMethodWithArgs(
reference,
_insertAllId,
JniCallType.booleanType,
[JValueInt(index), (iterable as JObject).reference]).boolean;
reference,
_insertAllId,
JniCallType.booleanType,
[JValueInt(index), (iterable as JObject).reference],
).boolean;
return;
}
super.insertAll(index, iterable);
Expand Down
22 changes: 19 additions & 3 deletions jni/test/jbyte_buffer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void run({required TestRunnerCallback testRunner}) {
using((arena) {
final buffer = testDataBuffer(arena);
expect(buffer.isReadOnly, false);
final readOnly = buffer.asReadOnlyBuffer();
final readOnly = buffer.asReadOnlyBuffer()..releasedBy(arena);
expect(readOnly.isReadOnly, true);
});
});
Expand Down Expand Up @@ -167,7 +167,7 @@ void run({required TestRunnerCallback testRunner}) {
final buffer = testDataBuffer(arena);
buffer.position = 1;
buffer.limit = 2;
final sliced = buffer.slice();
final sliced = buffer.slice()..releasedBy(arena);
expect(sliced.capacity, 1);
expect(sliced.nextByte, 2);
});
Expand All @@ -178,7 +178,7 @@ void run({required TestRunnerCallback testRunner}) {
final buffer = testDataBuffer(arena);
buffer.position = 1;
buffer.limit = 2;
final duplicate = buffer.duplicate();
final duplicate = buffer.duplicate()..releasedBy(arena);
expect(duplicate.capacity, 3);
expect(duplicate.position, 1);
expect(duplicate.limit, 2);
Expand Down Expand Up @@ -210,4 +210,20 @@ void run({required TestRunnerCallback testRunner}) {
expect(a.$type.hashCode, isNot(c.$type.hashCode));
});
});

testRunner('asUint8List releasing original', () {
using((arena) {
// Used as an example in [JByteBuffer].
final directBuffer = JByteBuffer.allocateDirect(3);
final data1 = directBuffer.asUint8List();
directBuffer.nextByte = 42; // No problem!
expect(data1[0], 42);
final data2 = directBuffer.asUint8List(releaseOriginal: true);
expect(
() => directBuffer.nextByte = 42,
throwsA(isA<UseAfterReleaseException>()),
);
expect(data2[0], 42);
});
});
}
87 changes: 59 additions & 28 deletions jnigen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,74 @@
## 0.7.0
* **Breaking Change** ([#387](https://github.com/dart-lang/jnigen/issues/387)): Added `JBuffer` and `JByteBuffer` classes as default classes for `java.nio.Buffer` and `java.nio.ByteBuffer` respectively.
* **Breaking Change**: Made the type classes `final`.
* Added `ignore_for_file: lines_longer_than_80_chars` to the generated file preamble.
* Added an explicit cast in generated `<Interface>.implement` code to allow `dart analyze` to pass when `strict-casts` is set.

- **Breaking Change** ([#387](https://github.com/dart-lang/jnigen/issues/387)):
Added `JBuffer` and `JByteBuffer` classes as default classes for
`java.nio.Buffer` and `java.nio.ByteBuffer` respectively.
- **Breaking Change**: Made the type classes `final`.
- Added `ignore_for_file: lines_longer_than_80_chars` to the generated file
preamble.
- Added an explicit cast in generated `<Interface>.implement` code to allow
`dart analyze` to pass when `strict-casts` is set.

## 0.6.0
* **Breaking Change** ([#131](https://github.com/dart-lang/jnigen/issues/131)): Renamed `delete*` to `release*`.
* **Breaking Change** ([#354](https://github.com/dart-lang/jnigen/issues/354)): Renamed constructors from `ctor1`, `ctor2`, ... to `new1`, `new2`, ...
* **Breaking Change**: Specifying a class always pulls in nested classes by default. If a nested class is specified in config, it will be an error.
* **Breaking Change**: Removed `suspend_fun_to_async` flag from the config. It's now happening by default since we read the Kotlin's metadata and reliably identify the `suspend fun`s.
* Fixed a bug where the nested classes would be generated incorrectly depending on the backend used for generation.
* Fixed a bug where ASM backend would produce the incorrect parent for multi-level nested classes.
* Fixed a bug where the backends would produce different descriptors for the same method.
* Added `enable_experiment` option to config.
* Created an experiment called `interface_implementation` which creates a `.implement` method for interfaces, so you can implement them using Dart.
* Save all `jnigen` logs to a file in `.dart_tool/jnigen/logs/`. This is useful for debugging.

- **Breaking Change** ([#131](https://github.com/dart-lang/jnigen/issues/131)):
Renamed `delete*` to `release*`.
- **Breaking Change** ([#354](https://github.com/dart-lang/jnigen/issues/354)):
Renamed constructors from `ctor1`, `ctor2`, ... to `new1`, `new2`, ...
- **Breaking Change**: Specifying a class always pulls in nested classes by
default. If a nested class is specified in config, it will be an error.
- **Breaking Change**: Removed `suspend_fun_to_async` flag from the config. It's
now happening by default since we read the Kotlin's metadata and reliably
identify the `suspend fun`s.
- Fixed a bug where the nested classes would be generated incorrectly depending
on the backend used for generation.
- Fixed a bug where ASM backend would produce the incorrect parent for
multi-level nested classes.
- Fixed a bug where the backends would produce different descriptors for the
same method.
- Added `enable_experiment` option to config.
- Created an experiment called `interface_implementation` which creates a
`.implement` method for interfaces, so you can implement them using Dart.
- Save all `jnigen` logs to a file in `.dart_tool/jnigen/logs/`. This is useful
for debugging.

## 0.5.0
* **Breaking Change** ([#72](https://github.com/dart-lang/jnigen/issues/72)): Removed support for `importMap` in favor of the newly added interop mechanism with importing yaml files.
* **Breaking Change** ([#72](https://github.com/dart-lang/jnigen/issues/72)): `java.util.Set`, `java.util.Map`, `java.util.List`, `java.util.Iterator` and the boxed types like `java.lang.Integer`, `java.lang.Double`, ... will be generated as their corresponding classes in `package:jni`.
* Strings now use UTF16.

- **Breaking Change** ([#72](https://github.com/dart-lang/jnigen/issues/72)):
Removed support for `importMap` in favor of the newly added interop mechanism
with importing yaml files.
- **Breaking Change** ([#72](https://github.com/dart-lang/jnigen/issues/72)):
`java.util.Set`, `java.util.Map`, `java.util.List`, `java.util.Iterator` and
the boxed types like `java.lang.Integer`, `java.lang.Double`, ... will be
generated as their corresponding classes in `package:jni`.
- Strings now use UTF16.

## 0.4.0
* **Breaking Change** ([#145](https://github.com/dart-lang/jnigen/issues/145)): Type arguments are now named instead of positional.
* Type parameters can now be inferred when possible.
* Fixed a bug where passing a `long` argument truncated it to `int` in pure dart bindings.
* Removed array extensions from the generated code.
* Added the ability to use source dependencies from Gradle.
* Fixed an issue with the field setter.
* Fixed an issue where exceptions were not properly thrown in pure Dart bindings.

- **Breaking Change** ([#145](https://github.com/dart-lang/jnigen/issues/145)):
Type arguments are now named instead of positional.
- Type parameters can now be inferred when possible.
- Fixed a bug where passing a `long` argument truncated it to `int` in pure dart
bindings.
- Removed array extensions from the generated code.
- Added the ability to use source dependencies from Gradle.
- Fixed an issue with the field setter.
- Fixed an issue where exceptions were not properly thrown in pure Dart
bindings.

## 0.3.0
* Added the option to convert Kotlin `suspend fun` to Dart async methods. Add `suspend_fun_to_async: true` to `jnigen.yaml`.

- Added the option to convert Kotlin `suspend fun` to Dart async methods. Add
`suspend_fun_to_async: true` to `jnigen.yaml`.

## 0.2.0
* Support generating bindings for generics.

- Support generating bindings for generics.

## 0.1.1
* Windows support for running tests and examples on development machines.

- Windows support for running tests and examples on development machines.

## 0.1.0
* Initial version: Basic bindings generation, maven and android utilities

- Initial version: Basic bindings generation, maven and android utilities
1 change: 0 additions & 1 deletion jnigen/lib/src/tools/android_sdk_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ task $_gradleGetSourcesTaskName(type: Copy) {
File(buildGradleOld).deleteSync();
}
if (procRes.exitCode != 0) {
print(procRes.stdout);
final inAndroidProject =
(androidProject == '.') ? '' : ' in $androidProject';
throw GradleException('\n\ngradle exited with status '
Expand Down

0 comments on commit 7fd2fb3

Please sign in to comment.