Skip to content

Commit

Permalink
[jnigen] added .filled ctor to JArray (#130)
Browse files Browse the repository at this point in the history
Closed #129.
  • Loading branch information
HosseinYousefi authored Nov 9, 2022
1 parent 1c95d14 commit 1ca777f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 17 deletions.
43 changes: 32 additions & 11 deletions pkgs/jni/lib/src/jarray.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,38 @@ class JArray<E> extends JObject {
/// Construct a new [JArray] with [reference] as its underlying reference.
JArray.fromRef(JArrayPtr reference) : super.fromRef(reference);

JArray(JType<E> typeClass, int length)
: super.fromRef(
(typeClass._type == JniCallType.objectType)
? _accessors
.newObjectArray(
length, typeClass._getClass().reference, nullptr)
.checkedRef
: _accessors
.newPrimitiveArray(length, typeClass._type)
.checkedRef,
);
/// Creates a [JArray] of the given length from the given [type].
///
/// The [length] must be a non-negative integer.
factory JArray(JType<E> type, int length) {
if (type._type == JniCallType.objectType) {
final clazz = type._getClass();
final array = JArray<E>.fromRef(
_accessors.newObjectArray(length, clazz.reference, nullptr).checkedRef,
);
clazz.delete();
return array;
}
return JArray.fromRef(
_accessors.newPrimitiveArray(length, type._type).checkedRef,
);
}

/// Creates a [JArray] of the given length with [fill] at each position.
///
/// The [length] must be a non-negative integer.
/// The [fill] must be a non-null [JObject].
static JArray<E> filled<E extends JObject>(int length, E fill) {
assert(!fill.isNull, "fill must not be null.");
final clazz = fill.getClass();
final array = JArray<E>.fromRef(
_accessors
.newObjectArray(length, clazz.reference, fill.reference)
.checkedRef,
);
clazz.delete();
return array;
}

int? _length;

Expand Down
29 changes: 23 additions & 6 deletions pkgs/jni/test/jarray_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ void main() {
array[0] = "حس".toJString()..deletedIn(arena);
array[1] = "\$".toJString()..deletedIn(arena);
array[2] = "33".toJString()..deletedIn(arena);
expect(array[0].toDartString(), "حس");
expect(array[1].toDartString(), "\$");
expect(array[2].toDartString(), "33");
expect(array[0].toDartString(deleteOriginal: true), "حس");
expect(array[1].toDartString(deleteOriginal: true), "\$");
expect(array[2].toDartString(deleteOriginal: true), "33");
array.setRange(
0,
3,
Expand All @@ -213,9 +213,9 @@ void main() {
],
1,
);
expect(array[0].toDartString(), "55");
expect(array[1].toDartString(), "66");
expect(array[2].toDartString(), "77");
expect(array[0].toDartString(deleteOriginal: true), "55");
expect(array[1].toDartString(deleteOriginal: true), "66");
expect(array[2].toDartString(deleteOriginal: true), "77");
expect(() {
final _ = array[-1];
}, throwsRangeError);
Expand Down Expand Up @@ -256,4 +256,21 @@ void main() {
expect(twoDimArray[2][2], 4);
});
});
test("JArray.filled", () {
using((arena) {
final string = "abc".toJString()..deletedIn(arena);
final array = JArray.filled(3, string)..deletedIn(arena);
expect(
() {
final _ = JArray.filled(3, JString.fromRef(nullptr))
..deletedIn(arena);
},
throwsA(isA<AssertionError>()),
);
expect(array.length, 3);
expect(array[0].toDartString(deleteOriginal: true), "abc");
expect(array[1].toDartString(deleteOriginal: true), "abc");
expect(array[2].toDartString(deleteOriginal: true), "abc");
});
});
}

0 comments on commit 1ca777f

Please sign in to comment.