Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Mono.Android/Android.Runtime/JNIEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,11 @@ public static void CopyArray (IntPtr src, string[] dest)
_GetByteArrayRegion (source, index, 1, r);
return r [0];
} },
{ typeof (sbyte), (type, source, index) => {
var r = new sbyte [1];
_GetByteArrayRegion (source, index, 1, (byte[]) (object) r);
return r [0];
Comment on lines +660 to +663
} },
{ typeof (char), (type, source, index) => {
var r = new char [1];
_GetCharArrayRegion (source, index, 1, r);
Expand Down Expand Up @@ -926,6 +931,7 @@ static Dictionary<Type, Action<Array, IntPtr>> CreateCopyManagedToNativeArray ()
return new Dictionary<Type, Action<Array, IntPtr>> () {
{ typeof (bool), (source, dest) => CopyArray ((bool[]) source, dest) },
{ typeof (byte), (source, dest) => CopyArray ((byte[]) source, dest) },
{ typeof (sbyte), (source, dest) => CopyArray ((byte[]) (object) (sbyte[]) source, dest) },
{ typeof (char), (source, dest) => CopyArray ((char[]) source, dest) },
{ typeof (short), (source, dest) => CopyArray ((short[]) source, dest) },
{ typeof (int), (source, dest) => CopyArray ((int[]) source, dest) },
Expand Down Expand Up @@ -1045,6 +1051,11 @@ public static void CopyArray<T> (T[] src, IntPtr dest)
CopyArray (source, r);
return r;
} },
{ typeof (sbyte), (type, source, len) => {
var r = new sbyte [len];
CopyArray (source, (byte[]) (object) r);
return r;
} },
{ typeof (char), (type, source, len) => {
var r = new char [len];
CopyArray (source, r);
Expand Down Expand Up @@ -1379,6 +1390,7 @@ static Dictionary<Type, Func<Array, IntPtr>> CreateCreateManagedToNativeArray ()
return new Dictionary<Type, Func<Array, IntPtr>> () {
{ typeof (bool), (source) => NewArray ((bool[]) source) },
{ typeof (byte), (source) => NewArray ((byte[]) source) },
{ typeof (sbyte), (source) => NewArray ((byte[]) (object) (sbyte[]) source) },
{ typeof (char), (source) => NewArray ((char[]) source) },
{ typeof (short), (source) => NewArray ((short[]) source) },
{ typeof (int), (source) => NewArray ((int[]) source) },
Expand Down Expand Up @@ -1513,6 +1525,10 @@ static IntPtr NewArray (Array value, Type elementType, IntPtr elementClass)
var _value = new[]{(byte) value!};
_SetByteArrayRegion (dest, index, _value.Length, _value);
} },
{ typeof (sbyte), (dest, index, value) => {
var _value = new[]{(sbyte) value!};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 💡 Nullable — This new entry introduces another null-forgiving operator even though value is declared object?. Please pattern-match it to an sbyte (and throw a contextual exception for null or the wrong type) before creating _value; that preserves the invariant instead of suppressing nullable analysis.

Rule: Never use the null-forgiving operator

_SetByteArrayRegion (dest, index, _value.Length, (byte[]) (object) _value);
} },
{ typeof (char), (dest, index, value) => {
var _value = new[]{(char) value!};
_SetCharArrayRegion (dest, index, _value.Length, _value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,109 @@ public void GetArray_ByteArrayArray ()
}
}

[TestCase (false)]
[TestCase (true)]
public void NewAndGetArray_SByteJaggedArrays (bool generic)
{
AssertArrayRoundTrip (
new sbyte[]{sbyte.MinValue, -1, 0, sbyte.MaxValue},
new sbyte[]{1, 2, -2, -3},
generic);
Comment on lines +179 to +186
AssertArrayRoundTrip (new [] {
new sbyte[]{sbyte.MinValue, -1},
new sbyte[]{0, sbyte.MaxValue},
}, new [] {
new sbyte[]{1, -2},
new sbyte[]{3, -4},
}, generic);
AssertArrayRoundTrip (new [] {
new [] {
new sbyte[]{sbyte.MinValue, -1},
new sbyte[]{0, sbyte.MaxValue},
},
new [] {
new sbyte[]{sbyte.MaxValue, 0},
new sbyte[]{-1, sbyte.MinValue},
},
}, new [] {
new [] {
new sbyte[]{1, -2},
new sbyte[]{3, -4},
},
new [] {
new sbyte[]{5, -6},
new sbyte[]{7, -8},
},
}, generic);
}

[TestCase (false)]
[TestCase (true)]
public void NewAndGetArray_ByteJaggedArraysPreserveHighBits (bool generic)
{
AssertArrayRoundTrip (
new byte[]{0, 127, 128, byte.MaxValue},
new byte[]{1, 126, 129, 254},
generic);
AssertArrayRoundTrip (new [] {
new byte[]{0, 127},
new byte[]{128, byte.MaxValue},
}, new [] {
new byte[]{1, 126},
new byte[]{129, 254},
}, generic);
AssertArrayRoundTrip (new [] {
new [] {
new byte[]{0, 127},
new byte[]{128, byte.MaxValue},
},
new [] {
new byte[]{byte.MaxValue, 128},
new byte[]{127, 0},
},
}, new [] {
new [] {
new byte[]{1, 126},
new byte[]{129, 254},
},
new [] {
new byte[]{253, 130},
new byte[]{125, 2},
},
}, generic);
}

static void AssertArrayRoundTrip<T> (T[] created, T[] copied, bool generic)
{
int rank = 1;
Type elementType = typeof (T);
while (elementType.IsArray) {
rank++;
elementType = elementType.GetElementType ();
}

IntPtr array = generic ? JNIEnv.NewArray<T> (created) : JNIEnv.NewArray ((Array) created);

try {
Assert.AreEqual (new string ('[', rank) + "B", JNIEnv.GetClassNameFromInstance (array));
Array actual = generic
? JNIEnv.GetArray<T> (array)
: JNIEnv.GetArray (array, JniHandleOwnership.DoNotTransfer, typeof (T));
Assert.AreEqual (created, actual);

if (generic)
JNIEnv.CopyArray<T> (copied, array);
else
JNIEnv.CopyArray (copied, typeof (T), array);
actual = generic
? JNIEnv.GetArray<T> (array)
: JNIEnv.GetArray (array, JniHandleOwnership.DoNotTransfer, typeof (T));
Assert.AreEqual (copied, actual);
} finally {
JNIEnv.DeleteLocalRef (array);
}
}

[Test]
public void GetArray_JavaLangByteArrayToSystemByteArray ()
{
Expand Down
Loading