From 7297b563e40ca1f1a9382310d19b5b0817c609e4 Mon Sep 17 00:00:00 2001 From: Hossein Yousefi Date: Thu, 18 Apr 2024 21:02:50 +0200 Subject: [PATCH 01/15] remove unused dartjni.h functions, change jarray to return int elements --- pkgs/jni/lib/src/jarray.dart | 8 ++++---- pkgs/jni/src/dartjni.h | 23 ----------------------- pkgs/jni/test/jarray_test.dart | 33 +++++++++++++++++++++------------ 3 files changed, 25 insertions(+), 39 deletions(-) diff --git a/pkgs/jni/lib/src/jarray.dart b/pkgs/jni/lib/src/jarray.dart index 3404ceb49..35a608ad1 100644 --- a/pkgs/jni/lib/src/jarray.dart +++ b/pkgs/jni/lib/src/jarray.dart @@ -202,22 +202,22 @@ extension CharArray on JArray { ); } - void operator []=(int index, String value) { + void operator []=(int index, int value) { RangeError.checkValidIndex(index, this); _allocate(sizeOf(), (ptr) { - ptr.value = value.codeUnits.first; + ptr.value = value; Jni.env.SetCharArrayRegion(reference.pointer, index, 1, ptr); }); } - void setRange(int start, int end, Iterable iterable, + void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { RangeError.checkValidRange(start, end, length); final size = end - start; final it = iterable.skip(skipCount).take(size); _allocate(sizeOf() * size, (ptr) { it.forEachIndexed((index, element) { - ptr[index] = element.codeUnits.first; + ptr[index] = element; }); Jni.env.SetCharArrayRegion(reference.pointer, start, size, ptr); }); diff --git a/pkgs/jni/src/dartjni.h b/pkgs/jni/src/dartjni.h index f834d1577..4433cbbe8 100644 --- a/pkgs/jni/src/dartjni.h +++ b/pkgs/jni/src/dartjni.h @@ -343,29 +343,6 @@ static inline jobject to_global_ref(jobject ref) { return g; } -// These functions are useful for C+Dart bindings, and not required for pure dart bindings. - -FFI_PLUGIN_EXPORT JniContext* GetJniContextPtr(); - -/// For use by jni_gen's generated code -/// don't use these. - -// these 2 fn ptr vars will be defined by generated code library -extern JniContext* (*context_getter)(void); -extern JNIEnv* (*env_getter)(void); - -// this function will be exported by generated code library -// it will set above 2 variables. -FFI_PLUGIN_EXPORT void setJniGetters(struct JniContext* (*cg)(void), - JNIEnv* (*eg)(void)); - -static inline void load_env() { - if (jniEnv == NULL) { - jni = context_getter(); - jniEnv = env_getter(); - } -} - static inline jthrowable check_exception() { jthrowable exception = (*jniEnv)->ExceptionOccurred(jniEnv); if (exception != NULL) (*jniEnv)->ExceptionClear(jniEnv); diff --git a/pkgs/jni/test/jarray_test.dart b/pkgs/jni/test/jarray_test.dart index fd4d11906..8686a07c1 100644 --- a/pkgs/jni/test/jarray_test.dart +++ b/pkgs/jni/test/jarray_test.dart @@ -48,24 +48,33 @@ void run({required TestRunnerCallback testRunner}) { using((arena) { final array = JArray(jchar.type, 3)..releasedBy(arena); expect(array.length, 3); - array[0] = 'ح'; - array[1] = '2'; - array[2] = '3'; - expect(array[0], 'ح'); - expect(array[1], '2'); - expect(array[2], '3'); - array.setRange(0, 3, ['4', '5', '6', '7'], 1); - expect(array[0], '5'); - expect(array[1], '6'); - expect(array[2], '7'); + array[0] = '~'.codeUnitAt(0); + array[1] = '2'.codeUnitAt(0); + array[2] = '3'.codeUnitAt(0); + expect(array[0], '~'.codeUnitAt(0)); + expect(array[1], '2'.codeUnitAt(0)); + expect(array[2], '3'.codeUnitAt(0)); + array.setRange( + 0, + 3, + [ + '4'.codeUnitAt(0), + '5'.codeUnitAt(0), + '6'.codeUnitAt(0), + '7'.codeUnitAt(0) + ], + 1); + expect(array[0], '5'.codeUnitAt(0)); + expect(array[1], '6'.codeUnitAt(0)); + expect(array[2], '7'.codeUnitAt(0)); expect(() { final _ = array[-1]; }, throwsRangeError); expect(() { - array[-1] = '4'; + array[-1] = '4'.codeUnitAt(0); }, throwsRangeError); expect(() { - array[3] = '4'; + array[3] = '4'.codeUnitAt(0); }, throwsRangeError); }); }); From d7ea673bc692136a3ce40102ffaba58cd95214b3 Mon Sep 17 00:00:00 2001 From: Hossein Yousefi Date: Tue, 23 Apr 2024 14:11:47 +0200 Subject: [PATCH 02/15] Add getRange method to `JArray` --- pkgs/jni/CHANGELOG.md | 3 +- pkgs/jni/lib/src/jarray.dart | 90 ++++++++++++++++++++++++++++++++-- pkgs/jni/test/jarray_test.dart | 89 +++++++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+), 5 deletions(-) diff --git a/pkgs/jni/CHANGELOG.md b/pkgs/jni/CHANGELOG.md index 7186b632d..a74f4e8ac 100644 --- a/pkgs/jni/CHANGELOG.md +++ b/pkgs/jni/CHANGELOG.md @@ -1,6 +1,7 @@ ## 0.9.0-wip -- No changes yet. +- Added `getRange` method to `JArray` of primitive types that returns a + `TypedData` list depending on the kind of the array. ## 0.8.0 diff --git a/pkgs/jni/lib/src/jarray.dart b/pkgs/jni/lib/src/jarray.dart index 35a608ad1..2a6cee32c 100644 --- a/pkgs/jni/lib/src/jarray.dart +++ b/pkgs/jni/lib/src/jarray.dart @@ -5,6 +5,7 @@ // ignore_for_file: unnecessary_cast, overridden_fields import 'dart:ffi'; +import 'dart:typed_data'; import 'package:collection/collection.dart'; import 'package:ffi/ffi.dart'; @@ -141,6 +142,16 @@ extension NativeArray on JArray { } } +extension on Allocator { + Pointer? get _nativeFree { + return switch (this) { + malloc => malloc.nativeFree, + calloc => calloc.nativeFree, + _ => null, + }; + } +} + extension BoolArray on JArray { bool operator [](int index) { return _elementAt(index, JniCallType.booleanType).boolean; @@ -154,6 +165,16 @@ extension BoolArray on JArray { }); } + Uint8List getRange(int start, int end, {Allocator allocator = malloc}) { + RangeError.checkValidRange(start, end, length); + final rangeLength = end - start; + final buffer = allocator + .allocate(sizeOf() * rangeLength); + Jni.env + .GetBooleanArrayRegion(reference.pointer, start, rangeLength, buffer); + return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); + } + void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { RangeError.checkValidRange(start, end, length); @@ -181,6 +202,15 @@ extension ByteArray on JArray { }); } + Int8List getRange(int start, int end, {Allocator allocator = malloc}) { + RangeError.checkValidRange(start, end, length); + final rangeLength = end - start; + final buffer = + allocator.allocate(sizeOf() * rangeLength); + Jni.env.GetByteArrayRegion(reference.pointer, start, rangeLength, buffer); + return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); + } + void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { RangeError.checkValidRange(start, end, length); @@ -196,10 +226,8 @@ extension ByteArray on JArray { } extension CharArray on JArray { - String operator [](int index) { - return String.fromCharCode( - _elementAt(index, JniCallType.charType).char, - ); + int operator [](int index) { + return _elementAt(index, JniCallType.charType).char; } void operator []=(int index, int value) { @@ -210,6 +238,15 @@ extension CharArray on JArray { }); } + Uint16List getRange(int start, int end, {Allocator allocator = malloc}) { + RangeError.checkValidRange(start, end, length); + final rangeLength = end - start; + final buffer = + allocator.allocate(sizeOf() * rangeLength); + Jni.env.GetCharArrayRegion(reference.pointer, start, rangeLength, buffer); + return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); + } + void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { RangeError.checkValidRange(start, end, length); @@ -237,6 +274,15 @@ extension ShortArray on JArray { }); } + Int16List getRange(int start, int end, {Allocator allocator = malloc}) { + RangeError.checkValidRange(start, end, length); + final rangeLength = end - start; + final buffer = + allocator.allocate(sizeOf() * rangeLength); + Jni.env.GetShortArrayRegion(reference.pointer, start, rangeLength, buffer); + return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); + } + void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { RangeError.checkValidRange(start, end, length); @@ -264,6 +310,15 @@ extension IntArray on JArray { }); } + Int32List getRange(int start, int end, {Allocator allocator = malloc}) { + RangeError.checkValidRange(start, end, length); + final rangeLength = end - start; + final buffer = + allocator.allocate(sizeOf() * rangeLength); + Jni.env.GetIntArrayRegion(reference.pointer, start, rangeLength, buffer); + return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); + } + void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { RangeError.checkValidRange(start, end, length); @@ -291,6 +346,15 @@ extension LongArray on JArray { }); } + Int64List getRange(int start, int end, {Allocator allocator = malloc}) { + RangeError.checkValidRange(start, end, length); + final rangeLength = end - start; + final buffer = + allocator.allocate(sizeOf() * rangeLength); + Jni.env.GetLongArrayRegion(reference.pointer, start, rangeLength, buffer); + return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); + } + void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { RangeError.checkValidRange(start, end, length); @@ -318,6 +382,15 @@ extension FloatArray on JArray { }); } + Float32List getRange(int start, int end, {Allocator allocator = malloc}) { + RangeError.checkValidRange(start, end, length); + final rangeLength = end - start; + final buffer = + allocator.allocate(sizeOf() * rangeLength); + Jni.env.GetFloatArrayRegion(reference.pointer, start, rangeLength, buffer); + return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); + } + void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { RangeError.checkValidRange(start, end, length); @@ -345,6 +418,15 @@ extension DoubleArray on JArray { }); } + Float64List getRange(int start, int end, {Allocator allocator = malloc}) { + RangeError.checkValidRange(start, end, length); + final rangeLength = end - start; + final buffer = allocator + .allocate(sizeOf() * rangeLength); + Jni.env.GetDoubleArrayRegion(reference.pointer, start, rangeLength, buffer); + return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); + } + void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { RangeError.checkValidRange(start, end, length); diff --git a/pkgs/jni/test/jarray_test.dart b/pkgs/jni/test/jarray_test.dart index 8686a07c1..5dc398e7f 100644 --- a/pkgs/jni/test/jarray_test.dart +++ b/pkgs/jni/test/jarray_test.dart @@ -2,6 +2,7 @@ // 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. +import 'dart:ffi'; import 'dart:io'; import 'package:jni/jni.dart'; @@ -29,6 +30,17 @@ void run({required TestRunnerCallback testRunner}) { expect(array[0], true); expect(array[1], false); expect(array[2], false); + final firstTwo = array.getRange(0, 2); + expect(firstTwo.length, 2); + expect(firstTwo.elementSizeInBytes, sizeOf()); + expect(firstTwo[0], 1); + expect(firstTwo[1], 0); + expect(() { + array.getRange(0, 4); + }, throwsRangeError); + expect(() { + array.setRange(0, 4, []); + }, throwsRangeError); array.setRange(0, 3, [false, true, true, true], 1); expect(array[0], true); expect(array[1], true); @@ -54,6 +66,17 @@ void run({required TestRunnerCallback testRunner}) { expect(array[0], '~'.codeUnitAt(0)); expect(array[1], '2'.codeUnitAt(0)); expect(array[2], '3'.codeUnitAt(0)); + final firstTwo = array.getRange(0, 2); + expect(firstTwo.length, 2); + expect(firstTwo.elementSizeInBytes, sizeOf()); + expect(firstTwo[0], '~'.codeUnitAt(0)); + expect(firstTwo[1], '2'.codeUnitAt(0)); + expect(() { + array.getRange(0, 4); + }, throwsRangeError); + expect(() { + array.setRange(0, 4, []); + }, throwsRangeError); array.setRange( 0, 3, @@ -88,6 +111,17 @@ void run({required TestRunnerCallback testRunner}) { expect(array[0], 1); expect(array[1], 2); expect(array[2], 3); + final firstTwo = array.getRange(0, 2); + expect(firstTwo.length, 2); + expect(firstTwo.elementSizeInBytes, sizeOf()); + expect(firstTwo[0], 1); + expect(firstTwo[1], 2); + expect(() { + array.getRange(0, 4); + }, throwsRangeError); + expect(() { + array.setRange(0, 4, []); + }, throwsRangeError); array.setRange(0, 3, [4, 5, 6, 7], 1); expect(array[0], 5); expect(array[1], 6); @@ -113,6 +147,17 @@ void run({required TestRunnerCallback testRunner}) { expect(array[0], 1); expect(array[1], 2); expect(array[2], 3); + final firstTwo = array.getRange(0, 2); + expect(firstTwo.length, 2); + expect(firstTwo.elementSizeInBytes, sizeOf()); + expect(firstTwo[0], 1); + expect(firstTwo[1], 2); + expect(() { + array.getRange(0, 4); + }, throwsRangeError); + expect(() { + array.setRange(0, 4, []); + }, throwsRangeError); array.setRange(0, 3, [4, 5, 6, 7], 1); expect(array[0], 5); expect(array[1], 6); @@ -138,6 +183,17 @@ void run({required TestRunnerCallback testRunner}) { expect(array[0], 1); expect(array[1], 2); expect(array[2], 3); + final firstTwo = array.getRange(0, 2); + expect(firstTwo.length, 2); + expect(firstTwo.elementSizeInBytes, sizeOf()); + expect(firstTwo[0], 1); + expect(firstTwo[1], 2); + expect(() { + array.getRange(0, 4); + }, throwsRangeError); + expect(() { + array.setRange(0, 4, []); + }, throwsRangeError); array.setRange(0, 3, [4, 5, 6, 7], 1); expect(array[0], 5); expect(array[1], 6); @@ -163,6 +219,17 @@ void run({required TestRunnerCallback testRunner}) { expect(array[0], 1); expect(array[1], 2); expect(array[2], 3 + 256 * 256 * 256 * 256 * 5); + final firstTwo = array.getRange(0, 2); + expect(firstTwo.length, 2); + expect(firstTwo.elementSizeInBytes, sizeOf()); + expect(firstTwo[0], 1); + expect(firstTwo[1], 2); + expect(() { + array.getRange(0, 4); + }, throwsRangeError); + expect(() { + array.setRange(0, 4, []); + }, throwsRangeError); array.setRange(0, 3, [4, 5, 6, 7], 1); expect(array[0], 5); expect(array[1], 6); @@ -189,6 +256,17 @@ void run({required TestRunnerCallback testRunner}) { expect(array[0], closeTo(0.5, epsilon)); expect(array[1], closeTo(2, epsilon)); expect(array[2], closeTo(3, epsilon)); + final firstTwo = array.getRange(0, 2); + expect(firstTwo.length, 2); + expect(firstTwo.elementSizeInBytes, sizeOf()); + expect(firstTwo[0], closeTo(0.5, epsilon)); + expect(firstTwo[1], closeTo(2, epsilon)); + expect(() { + array.getRange(0, 4); + }, throwsRangeError); + expect(() { + array.setRange(0, 4, []); + }, throwsRangeError); array.setRange(0, 3, [4, 5, 6, 7], 1); expect(array[0], closeTo(5, epsilon)); expect(array[1], closeTo(6, epsilon)); @@ -214,6 +292,17 @@ void run({required TestRunnerCallback testRunner}) { expect(array[0], closeTo(0.5, epsilon)); expect(array[1], closeTo(2, epsilon)); expect(array[2], closeTo(3, epsilon)); + final firstTwo = array.getRange(0, 2); + expect(firstTwo.length, 2); + expect(firstTwo.elementSizeInBytes, sizeOf()); + expect(firstTwo[0], closeTo(0.5, epsilon)); + expect(firstTwo[1], closeTo(2, epsilon)); + expect(() { + array.getRange(0, 4); + }, throwsRangeError); + expect(() { + array.setRange(0, 4, []); + }, throwsRangeError); array.setRange(0, 3, [4, 5, 6, 7], 1); expect(array[0], closeTo(5, epsilon)); expect(array[1], closeTo(6, epsilon)); From aa789a69240fb2bcab800da22ea9f6d427f4259f Mon Sep 17 00:00:00 2001 From: Hossein Yousefi Date: Tue, 23 Apr 2024 14:17:44 +0200 Subject: [PATCH 03/15] Edit changelog --- pkgs/jni/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkgs/jni/CHANGELOG.md b/pkgs/jni/CHANGELOG.md index a74f4e8ac..5f2ce987b 100644 --- a/pkgs/jni/CHANGELOG.md +++ b/pkgs/jni/CHANGELOG.md @@ -1,5 +1,9 @@ ## 0.9.0-wip +- **Breaking Change** + ([#1004](https://github.com/dart-lang/native/issues/1004)): Changed the return + type `operator []` of `JArray` to `int` instead of `String`. Similarly, + change the argument type of `operator []=` to accept `int`. - Added `getRange` method to `JArray` of primitive types that returns a `TypedData` list depending on the kind of the array. From e469521fd0f4cc03d27dd37f94e62c35c3590853 Mon Sep 17 00:00:00 2001 From: Hossein Yousefi Date: Tue, 23 Apr 2024 14:26:51 +0200 Subject: [PATCH 04/15] Use `AllocatorAlloc` extension to simplify --- pkgs/jni/lib/src/jarray.dart | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/pkgs/jni/lib/src/jarray.dart b/pkgs/jni/lib/src/jarray.dart index 2a6cee32c..4b7aaab36 100644 --- a/pkgs/jni/lib/src/jarray.dart +++ b/pkgs/jni/lib/src/jarray.dart @@ -205,8 +205,7 @@ extension ByteArray on JArray { Int8List getRange(int start, int end, {Allocator allocator = malloc}) { RangeError.checkValidRange(start, end, length); final rangeLength = end - start; - final buffer = - allocator.allocate(sizeOf() * rangeLength); + final buffer = allocator(rangeLength); Jni.env.GetByteArrayRegion(reference.pointer, start, rangeLength, buffer); return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); } @@ -241,8 +240,7 @@ extension CharArray on JArray { Uint16List getRange(int start, int end, {Allocator allocator = malloc}) { RangeError.checkValidRange(start, end, length); final rangeLength = end - start; - final buffer = - allocator.allocate(sizeOf() * rangeLength); + final buffer = allocator(rangeLength); Jni.env.GetCharArrayRegion(reference.pointer, start, rangeLength, buffer); return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); } @@ -277,8 +275,7 @@ extension ShortArray on JArray { Int16List getRange(int start, int end, {Allocator allocator = malloc}) { RangeError.checkValidRange(start, end, length); final rangeLength = end - start; - final buffer = - allocator.allocate(sizeOf() * rangeLength); + final buffer = allocator(rangeLength); Jni.env.GetShortArrayRegion(reference.pointer, start, rangeLength, buffer); return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); } @@ -313,8 +310,7 @@ extension IntArray on JArray { Int32List getRange(int start, int end, {Allocator allocator = malloc}) { RangeError.checkValidRange(start, end, length); final rangeLength = end - start; - final buffer = - allocator.allocate(sizeOf() * rangeLength); + final buffer = allocator(rangeLength); Jni.env.GetIntArrayRegion(reference.pointer, start, rangeLength, buffer); return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); } @@ -349,8 +345,7 @@ extension LongArray on JArray { Int64List getRange(int start, int end, {Allocator allocator = malloc}) { RangeError.checkValidRange(start, end, length); final rangeLength = end - start; - final buffer = - allocator.allocate(sizeOf() * rangeLength); + final buffer = allocator(rangeLength); Jni.env.GetLongArrayRegion(reference.pointer, start, rangeLength, buffer); return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); } @@ -385,8 +380,7 @@ extension FloatArray on JArray { Float32List getRange(int start, int end, {Allocator allocator = malloc}) { RangeError.checkValidRange(start, end, length); final rangeLength = end - start; - final buffer = - allocator.allocate(sizeOf() * rangeLength); + final buffer = allocator(rangeLength); Jni.env.GetFloatArrayRegion(reference.pointer, start, rangeLength, buffer); return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); } @@ -421,8 +415,7 @@ extension DoubleArray on JArray { Float64List getRange(int start, int end, {Allocator allocator = malloc}) { RangeError.checkValidRange(start, end, length); final rangeLength = end - start; - final buffer = allocator - .allocate(sizeOf() * rangeLength); + final buffer = allocator(rangeLength); Jni.env.GetDoubleArrayRegion(reference.pointer, start, rangeLength, buffer); return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); } From c80b41ed63b086ce8385217c0d805072113ea1b3 Mon Sep 17 00:00:00 2001 From: Hossein Yousefi Date: Tue, 23 Apr 2024 14:29:01 +0200 Subject: [PATCH 05/15] Test utf16 characters as well --- pkgs/jni/test/jarray_test.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkgs/jni/test/jarray_test.dart b/pkgs/jni/test/jarray_test.dart index 5dc398e7f..281e038ab 100644 --- a/pkgs/jni/test/jarray_test.dart +++ b/pkgs/jni/test/jarray_test.dart @@ -60,16 +60,16 @@ void run({required TestRunnerCallback testRunner}) { using((arena) { final array = JArray(jchar.type, 3)..releasedBy(arena); expect(array.length, 3); - array[0] = '~'.codeUnitAt(0); + array[0] = 'ح'.codeUnitAt(0); array[1] = '2'.codeUnitAt(0); array[2] = '3'.codeUnitAt(0); - expect(array[0], '~'.codeUnitAt(0)); + expect(array[0], 'ح'.codeUnitAt(0)); expect(array[1], '2'.codeUnitAt(0)); expect(array[2], '3'.codeUnitAt(0)); final firstTwo = array.getRange(0, 2); expect(firstTwo.length, 2); expect(firstTwo.elementSizeInBytes, sizeOf()); - expect(firstTwo[0], '~'.codeUnitAt(0)); + expect(firstTwo[0], 'ح'.codeUnitAt(0)); expect(firstTwo[1], '2'.codeUnitAt(0)); expect(() { array.getRange(0, 4); From 688dde6b0d6c3d49da75c35ee60ef56d2f500da0 Mon Sep 17 00:00:00 2001 From: Hossein Yousefi Date: Tue, 23 Apr 2024 16:40:43 +0200 Subject: [PATCH 06/15] Make `JArray.setRange` efficient --- pkgs/jni/lib/src/jarray.dart | 107 ++++++++++++++++------------------- 1 file changed, 50 insertions(+), 57 deletions(-) diff --git a/pkgs/jni/lib/src/jarray.dart b/pkgs/jni/lib/src/jarray.dart index 4b7aaab36..e79ba4ec6 100644 --- a/pkgs/jni/lib/src/jarray.dart +++ b/pkgs/jni/lib/src/jarray.dart @@ -132,11 +132,11 @@ class JArray extends JObject { extension NativeArray on JArray { void _allocate( - int size, + int rangeLength, void Function(Pointer ptr) use, ) { using((arena) { - final ptr = arena.allocate(size); + final ptr = arena.allocate(rangeLength); use(ptr); }, malloc); } @@ -178,13 +178,13 @@ extension BoolArray on JArray { void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { RangeError.checkValidRange(start, end, length); - final size = end - start; - final it = iterable.skip(skipCount).take(size); - _allocate(sizeOf() * size, (ptr) { + final rangeLength = end - start; + final it = iterable.skip(skipCount).take(rangeLength); + _allocate(sizeOf() * rangeLength, (ptr) { it.forEachIndexed((index, element) { ptr[index] = element ? 1 : 0; }); - Jni.env.SetBooleanArrayRegion(reference.pointer, start, size, ptr); + Jni.env.SetBooleanArrayRegion(reference.pointer, start, rangeLength, ptr); }); } } @@ -213,13 +213,12 @@ extension ByteArray on JArray { void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { RangeError.checkValidRange(start, end, length); - final size = end - start; - final it = iterable.skip(skipCount).take(size); - _allocate(sizeOf() * size, (ptr) { - it.forEachIndexed((index, element) { - ptr[index] = element; - }); - Jni.env.SetByteArrayRegion(reference.pointer, start, size, ptr); + final rangeLength = end - start; + _allocate(sizeOf() * rangeLength, (ptr) { + ptr + .asTypedList(rangeLength) + .setRange(0, rangeLength, iterable, skipCount); + Jni.env.SetByteArrayRegion(reference.pointer, start, rangeLength, ptr); }); } } @@ -248,13 +247,12 @@ extension CharArray on JArray { void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { RangeError.checkValidRange(start, end, length); - final size = end - start; - final it = iterable.skip(skipCount).take(size); - _allocate(sizeOf() * size, (ptr) { - it.forEachIndexed((index, element) { - ptr[index] = element; - }); - Jni.env.SetCharArrayRegion(reference.pointer, start, size, ptr); + final rangeLength = end - start; + _allocate(sizeOf() * rangeLength, (ptr) { + ptr + .asTypedList(rangeLength) + .setRange(0, rangeLength, iterable, skipCount); + Jni.env.SetCharArrayRegion(reference.pointer, start, rangeLength, ptr); }); } } @@ -283,13 +281,12 @@ extension ShortArray on JArray { void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { RangeError.checkValidRange(start, end, length); - final size = end - start; - final it = iterable.skip(skipCount).take(size); - _allocate(sizeOf() * size, (ptr) { - it.forEachIndexed((index, element) { - ptr[index] = element; - }); - Jni.env.SetShortArrayRegion(reference.pointer, start, size, ptr); + final rangeLength = end - start; + _allocate(sizeOf() * rangeLength, (ptr) { + ptr + .asTypedList(rangeLength) + .setRange(0, rangeLength, iterable, skipCount); + Jni.env.SetShortArrayRegion(reference.pointer, start, rangeLength, ptr); }); } } @@ -318,13 +315,12 @@ extension IntArray on JArray { void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { RangeError.checkValidRange(start, end, length); - final size = end - start; - final it = iterable.skip(skipCount).take(size); - _allocate(sizeOf() * size, (ptr) { - it.forEachIndexed((index, element) { - ptr[index] = element; - }); - Jni.env.SetIntArrayRegion(reference.pointer, start, size, ptr); + final rangeLength = end - start; + _allocate(sizeOf() * rangeLength, (ptr) { + ptr + .asTypedList(rangeLength) + .setRange(0, rangeLength, iterable, skipCount); + Jni.env.SetIntArrayRegion(reference.pointer, start, rangeLength, ptr); }); } } @@ -353,13 +349,12 @@ extension LongArray on JArray { void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { RangeError.checkValidRange(start, end, length); - final size = end - start; - final it = iterable.skip(skipCount).take(size); - _allocate(sizeOf() * size, (ptr) { - it.forEachIndexed((index, element) { - ptr[index] = element; - }); - Jni.env.SetLongArrayRegion(reference.pointer, start, size, ptr); + final rangeLength = end - start; + _allocate(sizeOf() * rangeLength, (ptr) { + ptr + .asTypedList(rangeLength) + .setRange(0, rangeLength, iterable, skipCount); + Jni.env.SetLongArrayRegion(reference.pointer, start, rangeLength, ptr); }); } } @@ -388,13 +383,12 @@ extension FloatArray on JArray { void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { RangeError.checkValidRange(start, end, length); - final size = end - start; - final it = iterable.skip(skipCount).take(size); - _allocate(sizeOf() * size, (ptr) { - it.forEachIndexed((index, element) { - ptr[index] = element; - }); - Jni.env.SetFloatArrayRegion(reference.pointer, start, size, ptr); + final rangeLength = end - start; + _allocate(sizeOf() * rangeLength, (ptr) { + ptr + .asTypedList(rangeLength) + .setRange(0, rangeLength, iterable, skipCount); + Jni.env.SetFloatArrayRegion(reference.pointer, start, rangeLength, ptr); }); } } @@ -423,13 +417,12 @@ extension DoubleArray on JArray { void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { RangeError.checkValidRange(start, end, length); - final size = end - start; - final it = iterable.skip(skipCount).take(size); - _allocate(sizeOf() * size, (ptr) { - it.forEachIndexed((index, element) { - ptr[index] = element; - }); - Jni.env.SetDoubleArrayRegion(reference.pointer, start, size, ptr); + final rangeLength = end - start; + _allocate(sizeOf() * rangeLength, (ptr) { + ptr + .asTypedList(rangeLength) + .setRange(0, rangeLength, iterable, skipCount); + Jni.env.SetDoubleArrayRegion(reference.pointer, start, rangeLength, ptr); }); } } @@ -448,8 +441,8 @@ extension ObjectArray on JArray { void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { RangeError.checkValidRange(start, end, length); - final size = end - start; - final it = iterable.skip(skipCount).take(size); + final rangeLength = end - start; + final it = iterable.skip(skipCount).take(rangeLength); it.forEachIndexed((index, element) { this[index] = element; }); From 9433da90f08db196da044ca376247bd453058600 Mon Sep 17 00:00:00 2001 From: Hossein Yousefi Date: Tue, 23 Apr 2024 17:59:20 +0200 Subject: [PATCH 07/15] Minor argument rename --- pkgs/jni/lib/src/jarray.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkgs/jni/lib/src/jarray.dart b/pkgs/jni/lib/src/jarray.dart index e79ba4ec6..7a8082c90 100644 --- a/pkgs/jni/lib/src/jarray.dart +++ b/pkgs/jni/lib/src/jarray.dart @@ -132,11 +132,11 @@ class JArray extends JObject { extension NativeArray on JArray { void _allocate( - int rangeLength, + int byteCount, void Function(Pointer ptr) use, ) { using((arena) { - final ptr = arena.allocate(rangeLength); + final ptr = arena.allocate(byteCount); use(ptr); }, malloc); } From 41bd217c75a36d6cc5a2e84b33f02fad5b09503b Mon Sep 17 00:00:00 2001 From: Hossein Yousefi Date: Tue, 23 Apr 2024 19:11:33 +0200 Subject: [PATCH 08/15] Improve JArray's operator []= performance --- pkgs/jni/lib/src/jarray.dart | 43 ++++--------- .../third_party/global_env_extensions.dart | 43 +++++++++++++ .../third_party/jni_bindings_generated.dart | 48 ++++++++++++++ pkgs/jni/src/dartjni.c | 64 +++++++++++++++++++ pkgs/jni/src/dartjni.h | 8 +++ 5 files changed, 174 insertions(+), 32 deletions(-) diff --git a/pkgs/jni/lib/src/jarray.dart b/pkgs/jni/lib/src/jarray.dart index 7a8082c90..6dd4c87a4 100644 --- a/pkgs/jni/lib/src/jarray.dart +++ b/pkgs/jni/lib/src/jarray.dart @@ -159,10 +159,8 @@ extension BoolArray on JArray { void operator []=(int index, bool value) { RangeError.checkValidIndex(index, this); - _allocate(sizeOf(), (ptr) { - ptr.value = value ? 1 : 0; - Jni.env.SetBooleanArrayRegion(reference.pointer, index, 1, ptr); - }); + Jni.accessors + .setBooleanArrayElement(reference.pointer, index, value ? 1 : 0); } Uint8List getRange(int start, int end, {Allocator allocator = malloc}) { @@ -196,10 +194,7 @@ extension ByteArray on JArray { void operator []=(int index, int value) { RangeError.checkValidIndex(index, this); - _allocate(sizeOf(), (ptr) { - ptr.value = value; - Jni.env.SetByteArrayRegion(reference.pointer, index, 1, ptr); - }); + Jni.accessors.setByteArrayElement(reference.pointer, index, value).check(); } Int8List getRange(int start, int end, {Allocator allocator = malloc}) { @@ -230,10 +225,7 @@ extension CharArray on JArray { void operator []=(int index, int value) { RangeError.checkValidIndex(index, this); - _allocate(sizeOf(), (ptr) { - ptr.value = value; - Jni.env.SetCharArrayRegion(reference.pointer, index, 1, ptr); - }); + Jni.accessors.setCharArrayElement(reference.pointer, index, value).check(); } Uint16List getRange(int start, int end, {Allocator allocator = malloc}) { @@ -264,10 +256,7 @@ extension ShortArray on JArray { void operator []=(int index, int value) { RangeError.checkValidIndex(index, this); - _allocate(sizeOf(), (ptr) { - ptr.value = value; - Jni.env.SetShortArrayRegion(reference.pointer, index, 1, ptr); - }); + Jni.accessors.setShortArrayElement(reference.pointer, index, value).check(); } Int16List getRange(int start, int end, {Allocator allocator = malloc}) { @@ -298,10 +287,7 @@ extension IntArray on JArray { void operator []=(int index, int value) { RangeError.checkValidIndex(index, this); - _allocate(sizeOf(), (ptr) { - ptr.value = value; - Jni.env.SetIntArrayRegion(reference.pointer, index, 1, ptr); - }); + Jni.accessors.setIntArrayElement(reference.pointer, index, value).check(); } Int32List getRange(int start, int end, {Allocator allocator = malloc}) { @@ -332,10 +318,7 @@ extension LongArray on JArray { void operator []=(int index, int value) { RangeError.checkValidIndex(index, this); - _allocate(sizeOf(), (ptr) { - ptr.value = value; - Jni.env.SetLongArrayRegion(reference.pointer, index, 1, ptr); - }); + Jni.accessors.setLongArrayElement(reference.pointer, index, value).check(); } Int64List getRange(int start, int end, {Allocator allocator = malloc}) { @@ -366,10 +349,7 @@ extension FloatArray on JArray { void operator []=(int index, double value) { RangeError.checkValidIndex(index, this); - _allocate(sizeOf(), (ptr) { - ptr.value = value; - Jni.env.SetFloatArrayRegion(reference.pointer, index, 1, ptr); - }); + Jni.accessors.setFloatArrayElement(reference.pointer, index, value).check(); } Float32List getRange(int start, int end, {Allocator allocator = malloc}) { @@ -400,10 +380,9 @@ extension DoubleArray on JArray { void operator []=(int index, double value) { RangeError.checkValidIndex(index, this); - _allocate(sizeOf(), (ptr) { - ptr.value = value; - Jni.env.SetDoubleArrayRegion(reference.pointer, index, 1, ptr); - }); + Jni.accessors + .setDoubleArrayElement(reference.pointer, index, value) + .check(); } Float64List getRange(int start, int end, {Allocator allocator = malloc}) { diff --git a/pkgs/jni/lib/src/third_party/global_env_extensions.dart b/pkgs/jni/lib/src/third_party/global_env_extensions.dart index 2a909ec71..a878f5a2b 100644 --- a/pkgs/jni/lib/src/third_party/global_env_extensions.dart +++ b/pkgs/jni/lib/src/third_party/global_env_extensions.dart @@ -1491,6 +1491,49 @@ class JniAccessors { JniResult getArrayElement(JArrayPtr array, int index, int type) => _getArrayElement(array, index, type); + late final _setBooleanArrayElement = ptr.ref.setBooleanArrayElement + .asFunction< + JThrowablePtr Function(JArrayPtr array, int index, int value)>(); + JThrowablePtr setBooleanArrayElement(JArrayPtr array, int index, int value) => + _setBooleanArrayElement(array, index, value); + + late final _setByteArrayElement = ptr.ref.setByteArrayElement.asFunction< + JThrowablePtr Function(JArrayPtr array, int index, int value)>(); + JThrowablePtr setByteArrayElement(JArrayPtr array, int index, int value) => + _setByteArrayElement(array, index, value); + + late final _setShortArrayElement = ptr.ref.setShortArrayElement.asFunction< + JThrowablePtr Function(JArrayPtr array, int index, int value)>(); + JThrowablePtr setShortArrayElement(JArrayPtr array, int index, int value) => + _setShortArrayElement(array, index, value); + + late final _setCharArrayElement = ptr.ref.setCharArrayElement.asFunction< + JThrowablePtr Function(JArrayPtr array, int index, int value)>(); + JThrowablePtr setCharArrayElement(JArrayPtr array, int index, int value) => + _setCharArrayElement(array, index, value); + + late final _setIntArrayElement = ptr.ref.setIntArrayElement.asFunction< + JThrowablePtr Function(JArrayPtr array, int index, int value)>(); + JThrowablePtr setIntArrayElement(JArrayPtr array, int index, int value) => + _setIntArrayElement(array, index, value); + + late final _setLongArrayElement = ptr.ref.setLongArrayElement.asFunction< + JThrowablePtr Function(JArrayPtr array, int index, int value)>(); + JThrowablePtr setLongArrayElement(JArrayPtr array, int index, int value) => + _setLongArrayElement(array, index, value); + + late final _setFloatArrayElement = ptr.ref.setFloatArrayElement.asFunction< + JThrowablePtr Function(JArrayPtr array, int index, double value)>(); + JThrowablePtr setFloatArrayElement( + JArrayPtr array, int index, double value) => + _setFloatArrayElement(array, index, value); + + late final _setDoubleArrayElement = ptr.ref.setDoubleArrayElement.asFunction< + JThrowablePtr Function(JArrayPtr array, int index, double value)>(); + JThrowablePtr setDoubleArrayElement( + JArrayPtr array, int index, double value) => + _setDoubleArrayElement(array, index, value); + late final _callMethod = ptr.ref.callMethod.asFunction< JniResult Function(JObjectPtr obj, JMethodIDPtr methodID, int callType, ffi.Pointer args)>(); diff --git a/pkgs/jni/lib/src/third_party/jni_bindings_generated.dart b/pkgs/jni/lib/src/third_party/jni_bindings_generated.dart index a575c812f..2c81d990c 100644 --- a/pkgs/jni/lib/src/third_party/jni_bindings_generated.dart +++ b/pkgs/jni/lib/src/third_party/jni_bindings_generated.dart @@ -442,6 +442,54 @@ final class JniAccessorsStruct extends ffi.Struct { JniResult Function(JArrayPtr array, ffi.Int index, ffi.Int type)>> getArrayElement; + external ffi.Pointer< + ffi.NativeFunction< + JThrowablePtr Function( + JArrayPtr array, ffi.Int index, JBooleanMarker value)>> + setBooleanArrayElement; + + external ffi.Pointer< + ffi.NativeFunction< + JThrowablePtr Function( + JArrayPtr array, ffi.Int index, JByteMarker value)>> + setByteArrayElement; + + external ffi.Pointer< + ffi.NativeFunction< + JThrowablePtr Function( + JArrayPtr array, ffi.Int index, JShortMarker value)>> + setShortArrayElement; + + external ffi.Pointer< + ffi.NativeFunction< + JThrowablePtr Function( + JArrayPtr array, ffi.Int index, JCharMarker value)>> + setCharArrayElement; + + external ffi.Pointer< + ffi.NativeFunction< + JThrowablePtr Function( + JArrayPtr array, ffi.Int index, JIntMarker value)>> + setIntArrayElement; + + external ffi.Pointer< + ffi.NativeFunction< + JThrowablePtr Function( + JArrayPtr array, ffi.Int index, JLongMarker value)>> + setLongArrayElement; + + external ffi.Pointer< + ffi.NativeFunction< + JThrowablePtr Function( + JArrayPtr array, ffi.Int index, JFloatMarker value)>> + setFloatArrayElement; + + external ffi.Pointer< + ffi.NativeFunction< + JThrowablePtr Function( + JArrayPtr array, ffi.Int index, JDoubleMarker value)>> + setDoubleArrayElement; + external ffi.Pointer< ffi.NativeFunction< JniResult Function(JObjectPtr obj, JMethodIDPtr methodID, diff --git a/pkgs/jni/src/dartjni.c b/pkgs/jni/src/dartjni.c index 0c2bff26e..0213a4f46 100644 --- a/pkgs/jni/src/dartjni.c +++ b/pkgs/jni/src/dartjni.c @@ -517,6 +517,62 @@ JniResult getArrayElement(jarray array, int index, int type) { return jniResult; } +jthrowable setBooleanArrayElement(jarray array, int index, jboolean value) { + attach_thread(); + (*jniEnv)->SetBooleanArrayRegion(jniEnv, array, index, 1, &value); + jthrowable exception = check_exception(); + return exception; +} + +jthrowable setByteArrayElement(jarray array, int index, jbyte value) { + attach_thread(); + (*jniEnv)->SetByteArrayRegion(jniEnv, array, index, 1, &value); + jthrowable exception = check_exception(); + return exception; +} + +jthrowable setShortArrayElement(jarray array, int index, jshort value) { + attach_thread(); + (*jniEnv)->SetShortArrayRegion(jniEnv, array, index, 1, &value); + jthrowable exception = check_exception(); + return exception; +} + +jthrowable setCharArrayElement(jarray array, int index, jchar value) { + attach_thread(); + (*jniEnv)->SetCharArrayRegion(jniEnv, array, index, 1, &value); + jthrowable exception = check_exception(); + return exception; +} + +jthrowable setIntArrayElement(jarray array, int index, jint value) { + attach_thread(); + (*jniEnv)->SetIntArrayRegion(jniEnv, array, index, 1, &value); + jthrowable exception = check_exception(); + return exception; +} + +jthrowable setLongArrayElement(jarray array, int index, jlong value) { + attach_thread(); + (*jniEnv)->SetLongArrayRegion(jniEnv, array, index, 1, &value); + jthrowable exception = check_exception(); + return exception; +} + +jthrowable setFloatArrayElement(jarray array, int index, jfloat value) { + attach_thread(); + (*jniEnv)->SetFloatArrayRegion(jniEnv, array, index, 1, &value); + jthrowable exception = check_exception(); + return exception; +} + +jthrowable setDoubleArrayElement(jarray array, int index, jdouble value) { + attach_thread(); + (*jniEnv)->SetDoubleArrayRegion(jniEnv, array, index, 1, &value); + jthrowable exception = check_exception(); + return exception; +} + JniExceptionDetails getExceptionDetails(jthrowable exception) { JniExceptionDetails details; details.message = (*jniEnv)->CallObjectMethod( @@ -552,6 +608,14 @@ JniAccessorsStruct accessors = { .newPrimitiveArray = newPrimitiveArray, .newObjectArray = newObjectArray, .getArrayElement = getArrayElement, + .setBooleanArrayElement = setBooleanArrayElement, + .setByteArrayElement = setByteArrayElement, + .setShortArrayElement = setShortArrayElement, + .setCharArrayElement = setCharArrayElement, + .setIntArrayElement = setIntArrayElement, + .setLongArrayElement = setLongArrayElement, + .setFloatArrayElement = setFloatArrayElement, + .setDoubleArrayElement = setDoubleArrayElement, .callMethod = callMethod, .callStaticMethod = callStaticMethod, .getField = getField, diff --git a/pkgs/jni/src/dartjni.h b/pkgs/jni/src/dartjni.h index 4433cbbe8..4ba8badb2 100644 --- a/pkgs/jni/src/dartjni.h +++ b/pkgs/jni/src/dartjni.h @@ -228,6 +228,14 @@ typedef struct JniAccessorsStruct { jclass elementClass, jobject initialElement); JniResult (*getArrayElement)(jarray array, int index, int type); + jthrowable (*setBooleanArrayElement)(jarray array, int index, jboolean value); + jthrowable (*setByteArrayElement)(jarray array, int index, jbyte value); + jthrowable (*setShortArrayElement)(jarray array, int index, jshort value); + jthrowable (*setCharArrayElement)(jarray array, int index, jchar value); + jthrowable (*setIntArrayElement)(jarray array, int index, jint value); + jthrowable (*setLongArrayElement)(jarray array, int index, jlong value); + jthrowable (*setFloatArrayElement)(jarray array, int index, jfloat value); + jthrowable (*setDoubleArrayElement)(jarray array, int index, jdouble value); JniResult (*callMethod)(jobject obj, jmethodID methodID, int callType, From b7608744037707d53e23bad629d31cc149342a80 Mon Sep 17 00:00:00 2001 From: Hossein Yousefi Date: Tue, 23 Apr 2024 19:17:38 +0200 Subject: [PATCH 09/15] Edit changelog --- pkgs/jni/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/pkgs/jni/CHANGELOG.md b/pkgs/jni/CHANGELOG.md index 5f2ce987b..5d61684ef 100644 --- a/pkgs/jni/CHANGELOG.md +++ b/pkgs/jni/CHANGELOG.md @@ -6,6 +6,7 @@ change the argument type of `operator []=` to accept `int`. - Added `getRange` method to `JArray` of primitive types that returns a `TypedData` list depending on the kind of the array. +- Improved the performance of `JArray`'s `setRange` and `operator []=`. ## 0.8.0 From 4780383fa15a4f04f0c5cda0ea7934c5bcfad5ff Mon Sep 17 00:00:00 2001 From: Hossein Yousefi Date: Wed, 24 Apr 2024 11:40:09 +0200 Subject: [PATCH 10/15] Address comments --- pkgs/jni/lib/src/jarray.dart | 4 ++++ pkgs/jni/test/jarray_test.dart | 43 ++++++++++++++-------------------- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/pkgs/jni/lib/src/jarray.dart b/pkgs/jni/lib/src/jarray.dart index 6dd4c87a4..ea368adf8 100644 --- a/pkgs/jni/lib/src/jarray.dart +++ b/pkgs/jni/lib/src/jarray.dart @@ -218,6 +218,10 @@ extension ByteArray on JArray { } } +/// `JArray` is a 16-bit integer array. +/// +/// Due to variable length encoding, the number of code units is not equal to +/// the number of characters. extension CharArray on JArray { int operator [](int index) { return _elementAt(index, JniCallType.charType).char; diff --git a/pkgs/jni/test/jarray_test.dart b/pkgs/jni/test/jarray_test.dart index 281e038ab..233ff5d9d 100644 --- a/pkgs/jni/test/jarray_test.dart +++ b/pkgs/jni/test/jarray_test.dart @@ -60,44 +60,35 @@ void run({required TestRunnerCallback testRunner}) { using((arena) { final array = JArray(jchar.type, 3)..releasedBy(arena); expect(array.length, 3); - array[0] = 'ح'.codeUnitAt(0); - array[1] = '2'.codeUnitAt(0); - array[2] = '3'.codeUnitAt(0); - expect(array[0], 'ح'.codeUnitAt(0)); - expect(array[1], '2'.codeUnitAt(0)); - expect(array[2], '3'.codeUnitAt(0)); + array[0] = 1; + array[1] = 2; + array[2] = 3 + 256 * 256 * 5; // Truncates the input. + expect(array[0], 1); + expect(array[1], 2); + expect(array[2], 3); final firstTwo = array.getRange(0, 2); expect(firstTwo.length, 2); expect(firstTwo.elementSizeInBytes, sizeOf()); - expect(firstTwo[0], 'ح'.codeUnitAt(0)); - expect(firstTwo[1], '2'.codeUnitAt(0)); + expect(firstTwo[0], 1); + expect(firstTwo[1], 2); expect(() { array.getRange(0, 4); }, throwsRangeError); expect(() { array.setRange(0, 4, []); }, throwsRangeError); - array.setRange( - 0, - 3, - [ - '4'.codeUnitAt(0), - '5'.codeUnitAt(0), - '6'.codeUnitAt(0), - '7'.codeUnitAt(0) - ], - 1); - expect(array[0], '5'.codeUnitAt(0)); - expect(array[1], '6'.codeUnitAt(0)); - expect(array[2], '7'.codeUnitAt(0)); + array.setRange(0, 3, [4, 5, 6, 7], 1); + expect(array[0], 5); + expect(array[1], 6); + expect(array[2], 7); expect(() { final _ = array[-1]; }, throwsRangeError); expect(() { - array[-1] = '4'.codeUnitAt(0); + array[-1] = 4; }, throwsRangeError); expect(() { - array[3] = '4'.codeUnitAt(0); + array[3] = 4; }, throwsRangeError); }); }); @@ -107,7 +98,7 @@ void run({required TestRunnerCallback testRunner}) { expect(array.length, 3); array[0] = 1; array[1] = 2; - array[2] = 3 + 256 * 5; // truncates the input; + array[2] = 3 + 256 * 5; // Truncates the input.; expect(array[0], 1); expect(array[1], 2); expect(array[2], 3); @@ -143,7 +134,7 @@ void run({required TestRunnerCallback testRunner}) { expect(array.length, 3); array[0] = 1; array[1] = 2; - array[2] = 3 + 256 * 256 * 5; // truncates the input + array[2] = 3 + 256 * 256 * 5; // Truncates the input. expect(array[0], 1); expect(array[1], 2); expect(array[2], 3); @@ -179,7 +170,7 @@ void run({required TestRunnerCallback testRunner}) { expect(array.length, 3); array[0] = 1; array[1] = 2; - array[2] = 3 + 256 * 256 * 256 * 256 * 5; // truncates the input + array[2] = 3 + 256 * 256 * 256 * 256 * 5; // Truncates the input. expect(array[0], 1); expect(array[1], 2); expect(array[2], 3); From ae2cff6de771aba9acc92841c80ea2930f3b0f0a Mon Sep 17 00:00:00 2001 From: Hossein Yousefi Date: Wed, 24 Apr 2024 11:41:32 +0200 Subject: [PATCH 11/15] ackchyually unsigned --- pkgs/jni/lib/src/jarray.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/jni/lib/src/jarray.dart b/pkgs/jni/lib/src/jarray.dart index ea368adf8..0b620c8f0 100644 --- a/pkgs/jni/lib/src/jarray.dart +++ b/pkgs/jni/lib/src/jarray.dart @@ -218,7 +218,7 @@ extension ByteArray on JArray { } } -/// `JArray` is a 16-bit integer array. +/// `JArray` is a 16-bit unsigned integer array. /// /// Due to variable length encoding, the number of code units is not equal to /// the number of characters. From 52880c6a1f7b730fd9e12bcb6c5baa055bd67cc2 Mon Sep 17 00:00:00 2001 From: Hossein Yousefi Date: Wed, 24 Apr 2024 11:49:11 +0200 Subject: [PATCH 12/15] Run apt-get clean and other commands to fix the CI --- .github/workflows/jnigen.yaml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/.github/workflows/jnigen.yaml b/.github/workflows/jnigen.yaml index 44c31fd74..f1f93eecb 100644 --- a/.github/workflows/jnigen.yaml +++ b/.github/workflows/jnigen.yaml @@ -90,7 +90,11 @@ jobs: ## So this is required to format generated bindings identically - name: install clang tools run: | + sudo apt-get clean + rm -rf /var/lib/apt/lists/* + sudo apt-get clean sudo apt-get update -y + sudo apt-get upgrade sudo apt-get install -y clang-format - name: Install dependencies run: dart pub get @@ -142,7 +146,11 @@ jobs: args: '--set-exit-if-changed' - name: install clang tools & CMake run: | + sudo apt-get clean + rm -rf /var/lib/apt/lists/* + sudo apt-get clean sudo apt-get update -y + sudo apt-get upgrade sudo apt-get install -y clang-format build-essential cmake - run: flutter pub get - name: Check formatting @@ -175,7 +183,11 @@ jobs: distribution: 'zulu' java-version: '11' - run: | + sudo apt-get clean + rm -rf /var/lib/apt/lists/* + sudo apt-get clean sudo apt-get update -y + sudo apt-get upgrade sudo apt-get install -y ninja-build libgtk-3-dev libclang-dev - run: dart pub get - run: dart run jni:setup @@ -325,7 +337,11 @@ jobs: distribution: 'zulu' java-version: '11' - run: | + sudo apt-get clean + rm -rf /var/lib/apt/lists/* + sudo apt-get clean sudo apt-get update -y + sudo apt-get upgrade sudo apt-get install -y ninja-build libgtk-3-dev - run: flutter config --enable-linux-desktop - run: flutter pub get @@ -388,7 +404,11 @@ jobs: distribution: 'zulu' java-version: '11' - run: | + sudo apt-get clean + rm -rf /var/lib/apt/lists/* + sudo apt-get clean sudo apt-get update -y + sudo apt-get upgrade sudo apt-get install -y ninja-build libgtk-3-dev clang-format - run: flutter config --enable-linux-desktop - run: dart pub get From 103cb387ef6905d9304a370447f5fad316680734 Mon Sep 17 00:00:00 2001 From: Hossein Yousefi Date: Wed, 24 Apr 2024 11:51:28 +0200 Subject: [PATCH 13/15] sudo --- .github/workflows/jnigen.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/jnigen.yaml b/.github/workflows/jnigen.yaml index f1f93eecb..98b0edced 100644 --- a/.github/workflows/jnigen.yaml +++ b/.github/workflows/jnigen.yaml @@ -91,7 +91,7 @@ jobs: - name: install clang tools run: | sudo apt-get clean - rm -rf /var/lib/apt/lists/* + sudo rm -rf /var/lib/apt/lists/* sudo apt-get clean sudo apt-get update -y sudo apt-get upgrade @@ -147,7 +147,7 @@ jobs: - name: install clang tools & CMake run: | sudo apt-get clean - rm -rf /var/lib/apt/lists/* + sudo rm -rf /var/lib/apt/lists/* sudo apt-get clean sudo apt-get update -y sudo apt-get upgrade @@ -184,7 +184,7 @@ jobs: java-version: '11' - run: | sudo apt-get clean - rm -rf /var/lib/apt/lists/* + sudo rm -rf /var/lib/apt/lists/* sudo apt-get clean sudo apt-get update -y sudo apt-get upgrade @@ -338,7 +338,7 @@ jobs: java-version: '11' - run: | sudo apt-get clean - rm -rf /var/lib/apt/lists/* + sudo rm -rf /var/lib/apt/lists/* sudo apt-get clean sudo apt-get update -y sudo apt-get upgrade @@ -405,7 +405,7 @@ jobs: java-version: '11' - run: | sudo apt-get clean - rm -rf /var/lib/apt/lists/* + sudo rm -rf /var/lib/apt/lists/* sudo apt-get clean sudo apt-get update -y sudo apt-get upgrade From f30f2d879162ff5f0cda13378d63c353a3bf4ac6 Mon Sep 17 00:00:00 2001 From: Hossein Yousefi Date: Wed, 24 Apr 2024 11:58:41 +0200 Subject: [PATCH 14/15] retry --- .github/workflows/jnigen.yaml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.github/workflows/jnigen.yaml b/.github/workflows/jnigen.yaml index 98b0edced..128145d0a 100644 --- a/.github/workflows/jnigen.yaml +++ b/.github/workflows/jnigen.yaml @@ -90,7 +90,6 @@ jobs: ## So this is required to format generated bindings identically - name: install clang tools run: | - sudo apt-get clean sudo rm -rf /var/lib/apt/lists/* sudo apt-get clean sudo apt-get update -y @@ -146,7 +145,6 @@ jobs: args: '--set-exit-if-changed' - name: install clang tools & CMake run: | - sudo apt-get clean sudo rm -rf /var/lib/apt/lists/* sudo apt-get clean sudo apt-get update -y @@ -183,7 +181,6 @@ jobs: distribution: 'zulu' java-version: '11' - run: | - sudo apt-get clean sudo rm -rf /var/lib/apt/lists/* sudo apt-get clean sudo apt-get update -y @@ -337,7 +334,6 @@ jobs: distribution: 'zulu' java-version: '11' - run: | - sudo apt-get clean sudo rm -rf /var/lib/apt/lists/* sudo apt-get clean sudo apt-get update -y @@ -404,7 +400,6 @@ jobs: distribution: 'zulu' java-version: '11' - run: | - sudo apt-get clean sudo rm -rf /var/lib/apt/lists/* sudo apt-get clean sudo apt-get update -y From 6f30e147b6b534610f49475533ae3ebb3e4b3628 Mon Sep 17 00:00:00 2001 From: Hossein Yousefi Date: Wed, 24 Apr 2024 12:02:42 +0200 Subject: [PATCH 15/15] revert the workflow changes --- .github/workflows/jnigen.yaml | 15 --------------- pkgs/jni/lib/src/jarray.dart | 2 +- 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/.github/workflows/jnigen.yaml b/.github/workflows/jnigen.yaml index 128145d0a..44c31fd74 100644 --- a/.github/workflows/jnigen.yaml +++ b/.github/workflows/jnigen.yaml @@ -90,10 +90,7 @@ jobs: ## So this is required to format generated bindings identically - name: install clang tools run: | - sudo rm -rf /var/lib/apt/lists/* - sudo apt-get clean sudo apt-get update -y - sudo apt-get upgrade sudo apt-get install -y clang-format - name: Install dependencies run: dart pub get @@ -145,10 +142,7 @@ jobs: args: '--set-exit-if-changed' - name: install clang tools & CMake run: | - sudo rm -rf /var/lib/apt/lists/* - sudo apt-get clean sudo apt-get update -y - sudo apt-get upgrade sudo apt-get install -y clang-format build-essential cmake - run: flutter pub get - name: Check formatting @@ -181,10 +175,7 @@ jobs: distribution: 'zulu' java-version: '11' - run: | - sudo rm -rf /var/lib/apt/lists/* - sudo apt-get clean sudo apt-get update -y - sudo apt-get upgrade sudo apt-get install -y ninja-build libgtk-3-dev libclang-dev - run: dart pub get - run: dart run jni:setup @@ -334,10 +325,7 @@ jobs: distribution: 'zulu' java-version: '11' - run: | - sudo rm -rf /var/lib/apt/lists/* - sudo apt-get clean sudo apt-get update -y - sudo apt-get upgrade sudo apt-get install -y ninja-build libgtk-3-dev - run: flutter config --enable-linux-desktop - run: flutter pub get @@ -400,10 +388,7 @@ jobs: distribution: 'zulu' java-version: '11' - run: | - sudo rm -rf /var/lib/apt/lists/* - sudo apt-get clean sudo apt-get update -y - sudo apt-get upgrade sudo apt-get install -y ninja-build libgtk-3-dev clang-format - run: flutter config --enable-linux-desktop - run: dart pub get diff --git a/pkgs/jni/lib/src/jarray.dart b/pkgs/jni/lib/src/jarray.dart index 0b620c8f0..ea368adf8 100644 --- a/pkgs/jni/lib/src/jarray.dart +++ b/pkgs/jni/lib/src/jarray.dart @@ -218,7 +218,7 @@ extension ByteArray on JArray { } } -/// `JArray` is a 16-bit unsigned integer array. +/// `JArray` is a 16-bit integer array. /// /// Due to variable length encoding, the number of code units is not equal to /// the number of characters.