Skip to content

Commit

Permalink
null oblivious indexers
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 committed Oct 5, 2024
1 parent 0abfc38 commit 953b9b4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Il2CppInterop.Runtime.InteropTypes.Arrays;

public class Il2CppReferenceArray<T> : Il2CppArrayBase<T?> where T : Il2CppObjectBase
public class Il2CppReferenceArray<T> : Il2CppArrayBase<T> where T : Il2CppObjectBase
{
private static readonly int ourElementTypeSize;
private static readonly bool ourElementIsValueType;
Expand Down Expand Up @@ -33,26 +33,26 @@ public Il2CppReferenceArray(long size) : base(AllocateArray(size))
{
}

public Il2CppReferenceArray(T[] arr) : base(AllocateArray(arr.Length))
public Il2CppReferenceArray(T?[] arr) : base(AllocateArray(arr.Length))
{
for (var i = 0; i < arr.Length; i++)
this[i] = arr[i];
}

public override T? this[int index]
#nullable disable
public override T this[int index]
{
get => WrapElement(GetElementPointer(index));
set => StoreValue(GetElementPointer(index), value?.Pointer ?? IntPtr.Zero);
}

#nullable enable
private IntPtr GetElementPointer(int index)
{
ThrowIfIndexOutOfRange(index);
return IntPtr.Add(ArrayStartPointer, index * ourElementTypeSize);
}

[return: NotNullIfNotNull(nameof(arr))]
public static implicit operator Il2CppReferenceArray<T>?(T[]? arr)
public static implicit operator Il2CppReferenceArray<T>?(T?[]? arr)
{
if (arr == null) return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Il2CppInterop.Runtime.InteropTypes.Arrays;

public class Il2CppStringArray : Il2CppArrayBase<string?>
public class Il2CppStringArray : Il2CppArrayBase<string>
{
static Il2CppStringArray()
{
Expand All @@ -23,13 +23,13 @@ public Il2CppStringArray(string?[] arr) : base(AllocateArray(arr.Length))
for (var i = 0; i < arr.Length; i++)
this[i] = arr[i];
}

public override unsafe string? this[int index]
#nullable disable
public override unsafe string this[int index]
{
get => IL2CPP.Il2CppStringToManaged(*GetElementPointer(index));
set => *GetElementPointer(index) = IL2CPP.ManagedStringToIl2Cpp(value);
}

#nullable enable
private unsafe IntPtr* GetElementPointer(int index)
{
ThrowIfIndexOutOfRange(index);
Expand Down

0 comments on commit 953b9b4

Please sign in to comment.