-
Notifications
You must be signed in to change notification settings - Fork 56
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
[jnigen] Add getRange
method to JArray<JPrimitive>
and change the dart equivalent type for JArray<jchar>
to int
#1095
Merged
Merged
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7297b56
remove unused dartjni.h functions, change jarray<char> to return int …
HosseinYousefi d7ea673
Add getRange method to `JArray<JPrimitive>`
HosseinYousefi aa789a6
Edit changelog
HosseinYousefi e469521
Use `AllocatorAlloc` extension to simplify
HosseinYousefi c80b41e
Test utf16 characters as well
HosseinYousefi 688dde6
Make `JArray.setRange` efficient
HosseinYousefi 9433da9
Minor argument rename
HosseinYousefi 41bd217
Improve JArray's operator []= performance
HosseinYousefi b760874
Edit changelog
HosseinYousefi 4780383
Address comments
HosseinYousefi ae2cff6
ackchyually unsigned
HosseinYousefi 52880c6
Run apt-get clean and other commands to fix the CI
HosseinYousefi 103cb38
sudo
HosseinYousefi f30f2d8
retry
HosseinYousefi 6f30e14
revert the workflow changes
HosseinYousefi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -131,16 +132,26 @@ class JArray<E> extends JObject { | |
|
||
extension NativeArray<E extends JPrimitive> on JArray<E> { | ||
void _allocate<T extends NativeType>( | ||
int size, | ||
int rangeLength, | ||
void Function(Pointer<T> ptr) use, | ||
) { | ||
using((arena) { | ||
final ptr = arena.allocate<T>(size); | ||
final ptr = arena.allocate<T>(rangeLength); | ||
use(ptr); | ||
}, malloc); | ||
} | ||
} | ||
|
||
extension on Allocator { | ||
Pointer<NativeFinalizerFunction>? get _nativeFree { | ||
return switch (this) { | ||
malloc => malloc.nativeFree, | ||
calloc => calloc.nativeFree, | ||
_ => null, | ||
}; | ||
} | ||
} | ||
|
||
extension BoolArray on JArray<jboolean> { | ||
bool operator [](int index) { | ||
return _elementAt(index, JniCallType.booleanType).boolean; | ||
|
@@ -154,16 +165,26 @@ extension BoolArray on JArray<jboolean> { | |
}); | ||
} | ||
|
||
Uint8List getRange(int start, int end, {Allocator allocator = malloc}) { | ||
RangeError.checkValidRange(start, end, length); | ||
final rangeLength = end - start; | ||
final buffer = allocator | ||
.allocate<JBooleanMarker>(sizeOf<JBooleanMarker>() * rangeLength); | ||
Jni.env | ||
.GetBooleanArrayRegion(reference.pointer, start, rangeLength, buffer); | ||
return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); | ||
} | ||
|
||
void setRange(int start, int end, Iterable<bool> iterable, | ||
[int skipCount = 0]) { | ||
RangeError.checkValidRange(start, end, length); | ||
final size = end - start; | ||
final it = iterable.skip(skipCount).take(size); | ||
_allocate<JBooleanMarker>(sizeOf<JBooleanMarker>() * size, (ptr) { | ||
final rangeLength = end - start; | ||
final it = iterable.skip(skipCount).take(rangeLength); | ||
_allocate<JBooleanMarker>(sizeOf<JBooleanMarker>() * 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); | ||
}); | ||
} | ||
} | ||
|
@@ -181,45 +202,57 @@ extension ByteArray on JArray<jbyte> { | |
}); | ||
} | ||
|
||
Int8List getRange(int start, int end, {Allocator allocator = malloc}) { | ||
RangeError.checkValidRange(start, end, length); | ||
final rangeLength = end - start; | ||
final buffer = allocator<JByteMarker>(rangeLength); | ||
Jni.env.GetByteArrayRegion(reference.pointer, start, rangeLength, buffer); | ||
return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); | ||
} | ||
|
||
void setRange(int start, int end, Iterable<int> iterable, | ||
[int skipCount = 0]) { | ||
RangeError.checkValidRange(start, end, length); | ||
final size = end - start; | ||
final it = iterable.skip(skipCount).take(size); | ||
_allocate<JByteMarker>(sizeOf<JByteMarker>() * size, (ptr) { | ||
it.forEachIndexed((index, element) { | ||
ptr[index] = element; | ||
}); | ||
Jni.env.SetByteArrayRegion(reference.pointer, start, size, ptr); | ||
final rangeLength = end - start; | ||
_allocate<JByteMarker>(sizeOf<JByteMarker>() * rangeLength, (ptr) { | ||
ptr | ||
.asTypedList(rangeLength) | ||
.setRange(0, rangeLength, iterable, skipCount); | ||
Jni.env.SetByteArrayRegion(reference.pointer, start, rangeLength, ptr); | ||
}); | ||
} | ||
} | ||
|
||
extension CharArray on JArray<jchar> { | ||
String operator [](int index) { | ||
return String.fromCharCode( | ||
_elementAt(index, JniCallType.charType).char, | ||
); | ||
int operator [](int index) { | ||
HosseinYousefi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return _elementAt(index, JniCallType.charType).char; | ||
} | ||
|
||
void operator []=(int index, String value) { | ||
void operator []=(int index, int value) { | ||
RangeError.checkValidIndex(index, this); | ||
_allocate<JCharMarker>(sizeOf<JCharMarker>(), (ptr) { | ||
HosseinYousefi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ptr.value = value.codeUnits.first; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was broken. It didn't take into account characters with more than one code unit. |
||
ptr.value = value; | ||
Jni.env.SetCharArrayRegion(reference.pointer, index, 1, ptr); | ||
}); | ||
} | ||
|
||
void setRange(int start, int end, Iterable<String> iterable, | ||
Uint16List getRange(int start, int end, {Allocator allocator = malloc}) { | ||
RangeError.checkValidRange(start, end, length); | ||
final rangeLength = end - start; | ||
final buffer = allocator<JCharMarker>(rangeLength); | ||
Jni.env.GetCharArrayRegion(reference.pointer, start, rangeLength, buffer); | ||
return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); | ||
} | ||
|
||
void setRange(int start, int end, Iterable<int> iterable, | ||
[int skipCount = 0]) { | ||
RangeError.checkValidRange(start, end, length); | ||
final size = end - start; | ||
final it = iterable.skip(skipCount).take(size); | ||
_allocate<JCharMarker>(sizeOf<JCharMarker>() * size, (ptr) { | ||
it.forEachIndexed((index, element) { | ||
ptr[index] = element.codeUnits.first; | ||
}); | ||
Jni.env.SetCharArrayRegion(reference.pointer, start, size, ptr); | ||
final rangeLength = end - start; | ||
_allocate<JCharMarker>(sizeOf<JCharMarker>() * rangeLength, (ptr) { | ||
ptr | ||
.asTypedList(rangeLength) | ||
.setRange(0, rangeLength, iterable, skipCount); | ||
Jni.env.SetCharArrayRegion(reference.pointer, start, rangeLength, ptr); | ||
}); | ||
} | ||
} | ||
|
@@ -237,16 +270,23 @@ extension ShortArray on JArray<jshort> { | |
}); | ||
} | ||
|
||
Int16List getRange(int start, int end, {Allocator allocator = malloc}) { | ||
RangeError.checkValidRange(start, end, length); | ||
final rangeLength = end - start; | ||
final buffer = allocator<JShortMarker>(rangeLength); | ||
Jni.env.GetShortArrayRegion(reference.pointer, start, rangeLength, buffer); | ||
return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); | ||
} | ||
|
||
void setRange(int start, int end, Iterable<int> iterable, | ||
[int skipCount = 0]) { | ||
RangeError.checkValidRange(start, end, length); | ||
final size = end - start; | ||
final it = iterable.skip(skipCount).take(size); | ||
_allocate<JShortMarker>(sizeOf<JShortMarker>() * size, (ptr) { | ||
it.forEachIndexed((index, element) { | ||
ptr[index] = element; | ||
}); | ||
Jni.env.SetShortArrayRegion(reference.pointer, start, size, ptr); | ||
final rangeLength = end - start; | ||
_allocate<JShortMarker>(sizeOf<JShortMarker>() * rangeLength, (ptr) { | ||
ptr | ||
.asTypedList(rangeLength) | ||
.setRange(0, rangeLength, iterable, skipCount); | ||
HosseinYousefi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Jni.env.SetShortArrayRegion(reference.pointer, start, rangeLength, ptr); | ||
}); | ||
} | ||
} | ||
|
@@ -264,16 +304,23 @@ extension IntArray on JArray<jint> { | |
}); | ||
} | ||
|
||
Int32List getRange(int start, int end, {Allocator allocator = malloc}) { | ||
RangeError.checkValidRange(start, end, length); | ||
final rangeLength = end - start; | ||
final buffer = allocator<JIntMarker>(rangeLength); | ||
Jni.env.GetIntArrayRegion(reference.pointer, start, rangeLength, buffer); | ||
return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); | ||
} | ||
|
||
void setRange(int start, int end, Iterable<int> iterable, | ||
[int skipCount = 0]) { | ||
RangeError.checkValidRange(start, end, length); | ||
final size = end - start; | ||
final it = iterable.skip(skipCount).take(size); | ||
_allocate<JIntMarker>(sizeOf<JIntMarker>() * size, (ptr) { | ||
it.forEachIndexed((index, element) { | ||
ptr[index] = element; | ||
}); | ||
Jni.env.SetIntArrayRegion(reference.pointer, start, size, ptr); | ||
final rangeLength = end - start; | ||
_allocate<JIntMarker>(sizeOf<JIntMarker>() * rangeLength, (ptr) { | ||
ptr | ||
.asTypedList(rangeLength) | ||
.setRange(0, rangeLength, iterable, skipCount); | ||
Jni.env.SetIntArrayRegion(reference.pointer, start, rangeLength, ptr); | ||
}); | ||
} | ||
} | ||
|
@@ -291,16 +338,23 @@ extension LongArray on JArray<jlong> { | |
}); | ||
} | ||
|
||
Int64List getRange(int start, int end, {Allocator allocator = malloc}) { | ||
RangeError.checkValidRange(start, end, length); | ||
final rangeLength = end - start; | ||
final buffer = allocator<JLongMarker>(rangeLength); | ||
Jni.env.GetLongArrayRegion(reference.pointer, start, rangeLength, buffer); | ||
return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); | ||
} | ||
|
||
void setRange(int start, int end, Iterable<int> iterable, | ||
[int skipCount = 0]) { | ||
RangeError.checkValidRange(start, end, length); | ||
final size = end - start; | ||
final it = iterable.skip(skipCount).take(size); | ||
_allocate<JLongMarker>(sizeOf<JLongMarker>() * size, (ptr) { | ||
it.forEachIndexed((index, element) { | ||
ptr[index] = element; | ||
}); | ||
Jni.env.SetLongArrayRegion(reference.pointer, start, size, ptr); | ||
final rangeLength = end - start; | ||
_allocate<JLongMarker>(sizeOf<JLongMarker>() * rangeLength, (ptr) { | ||
ptr | ||
.asTypedList(rangeLength) | ||
.setRange(0, rangeLength, iterable, skipCount); | ||
Jni.env.SetLongArrayRegion(reference.pointer, start, rangeLength, ptr); | ||
}); | ||
} | ||
} | ||
|
@@ -318,16 +372,23 @@ extension FloatArray on JArray<jfloat> { | |
}); | ||
} | ||
|
||
Float32List getRange(int start, int end, {Allocator allocator = malloc}) { | ||
RangeError.checkValidRange(start, end, length); | ||
final rangeLength = end - start; | ||
final buffer = allocator<JFloatMarker>(rangeLength); | ||
Jni.env.GetFloatArrayRegion(reference.pointer, start, rangeLength, buffer); | ||
return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); | ||
} | ||
|
||
void setRange(int start, int end, Iterable<double> iterable, | ||
[int skipCount = 0]) { | ||
RangeError.checkValidRange(start, end, length); | ||
final size = end - start; | ||
final it = iterable.skip(skipCount).take(size); | ||
_allocate<JFloatMarker>(sizeOf<JFloatMarker>() * size, (ptr) { | ||
it.forEachIndexed((index, element) { | ||
ptr[index] = element; | ||
}); | ||
Jni.env.SetFloatArrayRegion(reference.pointer, start, size, ptr); | ||
final rangeLength = end - start; | ||
_allocate<JFloatMarker>(sizeOf<JFloatMarker>() * rangeLength, (ptr) { | ||
ptr | ||
.asTypedList(rangeLength) | ||
.setRange(0, rangeLength, iterable, skipCount); | ||
Jni.env.SetFloatArrayRegion(reference.pointer, start, rangeLength, ptr); | ||
}); | ||
} | ||
} | ||
|
@@ -345,16 +406,23 @@ extension DoubleArray on JArray<jdouble> { | |
}); | ||
} | ||
|
||
Float64List getRange(int start, int end, {Allocator allocator = malloc}) { | ||
RangeError.checkValidRange(start, end, length); | ||
final rangeLength = end - start; | ||
final buffer = allocator<JDoubleMarker>(rangeLength); | ||
Jni.env.GetDoubleArrayRegion(reference.pointer, start, rangeLength, buffer); | ||
return buffer.asTypedList(rangeLength, finalizer: allocator._nativeFree); | ||
} | ||
|
||
void setRange(int start, int end, Iterable<double> iterable, | ||
[int skipCount = 0]) { | ||
RangeError.checkValidRange(start, end, length); | ||
final size = end - start; | ||
final it = iterable.skip(skipCount).take(size); | ||
_allocate<JDoubleMarker>(sizeOf<JDoubleMarker>() * size, (ptr) { | ||
it.forEachIndexed((index, element) { | ||
ptr[index] = element; | ||
}); | ||
Jni.env.SetDoubleArrayRegion(reference.pointer, start, size, ptr); | ||
final rangeLength = end - start; | ||
_allocate<JDoubleMarker>(sizeOf<JDoubleMarker>() * rangeLength, (ptr) { | ||
ptr | ||
.asTypedList(rangeLength) | ||
.setRange(0, rangeLength, iterable, skipCount); | ||
Jni.env.SetDoubleArrayRegion(reference.pointer, start, rangeLength, ptr); | ||
}); | ||
} | ||
} | ||
|
@@ -373,8 +441,8 @@ extension ObjectArray<T extends JObject> on JArray<T> { | |
|
||
void setRange(int start, int end, Iterable<T> 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; | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was broken, it didn't take into account characters consisting of more than one code unit.