Skip to content

Commit

Permalink
Fix array marshalers to handle null (#983)
Browse files Browse the repository at this point in the history
  • Loading branch information
manodasanW authored Sep 9, 2021
1 parent 26943b2 commit 7acab7a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
21 changes: 19 additions & 2 deletions src/Tests/UnitTest/TestComponentCSharp_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,19 @@ public void TestEnums()
TestObject.EnumsProperty = expectedEnums;
Assert.Equal(expectedEnums, TestObject.EnumsProperty);
TestObject.CallForEnums(() => expectedEnums);
Assert.Equal(expectedEnums, TestObject.EnumsProperty);
Assert.Equal(expectedEnums, TestObject.EnumsProperty);

TestObject.EnumsProperty = null;
Assert.Equal(null, TestObject.EnumsProperty);

var expectedEnumStructs = new EnumStruct[] { new EnumStruct(EnumValue.One), new EnumStruct(EnumValue.Two) };
TestObject.EnumStructsProperty = expectedEnumStructs;
Assert.Equal(expectedEnumStructs, TestObject.EnumStructsProperty);
TestObject.CallForEnumStructs(() => expectedEnumStructs);
Assert.Equal(expectedEnumStructs, TestObject.EnumStructsProperty);
Assert.Equal(expectedEnumStructs, TestObject.EnumStructsProperty);

TestObject.EnumStructsProperty = null;
Assert.Equal(null, TestObject.EnumStructsProperty);

// Flags
var expectedFlag = FlagValue.All;
Expand Down Expand Up @@ -1167,6 +1173,17 @@ public void TestComposedNonBlittableStruct()
Assert.True(val.bools.x);
Assert.False(val.bools.y);
Assert.True(val.bools.z);
}

[Fact]
public void TestBlittableArrays()
{
int[] arr = new[] { 2, 4, 6, 8 };
TestObject.SetInts(arr);
Assert.True(TestObject.GetInts().SequenceEqual(arr));

TestObject.SetInts(null);
Assert.Null(TestObject.GetInts());
}

#if NETCOREAPP2_0
Expand Down
22 changes: 22 additions & 0 deletions src/Tests/UnitTest/TestComponent_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,17 @@ public void Array_String()
Assert.True(AllEqual(a, b, c, d));
}

[Fact]
public void Array_NullStringArray()
{
string[] a = null;
string[] b = null;
string[] c;
string[] d = Tests.Array12(a, b, out c);
Assert.Null(c);
Assert.Null(d);
}

[Fact]
public void Array_Blittable()
{
Expand Down Expand Up @@ -437,6 +448,17 @@ public void Array_Stringable()
Assert.True(AllEqual(a, b, c, d));
}

[Fact]
public void Array_NullInterfaces()
{
IStringable[] a = null;
IStringable[] b = null;
IStringable[] c;
IStringable[] d = Tests.Array16(a, b, out c);
Assert.Null(c);
Assert.Null(d);
}

private T[] Array_Call<T>(T[] a, T[] b, out T[] c)
{
Assert.True(a.Length == b.Length);
Expand Down
20 changes: 18 additions & 2 deletions src/WinRT.Runtime/Marshalers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ public static unsafe string[] FromAbiArray(object box)
return null;
}
var abi = ((int length, IntPtr data))box;
if (abi.data == IntPtr.Zero)
{
return null;
}
string[] array = new string[abi.length];
var data = (IntPtr*)abi.data.ToPointer();
for (int i = 0; i < abi.length; i++)
Expand Down Expand Up @@ -277,7 +281,7 @@ public struct MarshalBlittable<T>
{
public struct MarshalerArray
{
public MarshalerArray(Array array) => _gchandle = GCHandle.Alloc(array, GCHandleType.Pinned);
public MarshalerArray(Array array) => _gchandle = array is null ? default : GCHandle.Alloc(array, GCHandleType.Pinned);
public void Dispose() => _gchandle.Dispose();

public GCHandle _gchandle;
Expand All @@ -288,7 +292,7 @@ public struct MarshalerArray
public static (int length, IntPtr data) GetAbiArray(object box)
{
var m = (MarshalerArray)box;
return (((Array)m._gchandle.Target).Length, m._gchandle.AddrOfPinnedObject());
return m._gchandle.IsAllocated ? (((Array)m._gchandle.Target).Length, m._gchandle.AddrOfPinnedObject()) : (0, IntPtr.Zero);
}

public static unsafe T[] FromAbiArray(object box)
Expand All @@ -298,6 +302,10 @@ public static unsafe T[] FromAbiArray(object box)
return null;
}
var abi = ((int length, IntPtr data))box;
if (abi.data == IntPtr.Zero)
{
return null;
}
var abiSpan = new ReadOnlySpan<T>(abi.data.ToPointer(), abi.length);
return abiSpan.ToArray();
}
Expand Down Expand Up @@ -583,6 +591,10 @@ public void Dispose()
return null;
}
var abi = ((int length, IntPtr data))box;
if (abi.data == IntPtr.Zero)
{
return null;
}
var array = new T[abi.length];
var data = (byte*)abi.data.ToPointer();
var abi_element_size = Marshal.SizeOf(AbiType);
Expand Down Expand Up @@ -763,6 +775,10 @@ public static unsafe T[] FromAbiArray(object box, Func<IntPtr, T> fromAbi)
return null;
}
var abi = ((int length, IntPtr data))box;
if (abi.data == IntPtr.Zero)
{
return null;
}
var array = new T[abi.length];
var data = (IntPtr*)abi.data.ToPointer();
for (int i = 0; i < abi.length; i++)
Expand Down

0 comments on commit 7acab7a

Please sign in to comment.