Skip to content
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

Fix reflection-calling Set method on arrays #107529

Merged
merged 1 commit into from
Sep 9, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,32 @@ public static unsafe Array NewMultiDimArray(RuntimeTypeHandle typeHandleForArray
return Array.NewMultiDimArray(typeHandleForArrayType.ToMethodTable(), pImmutableLengths, lengths.Length);
}

public static unsafe void SetArrayValue(Array array, int[] indices, object value)
{
MethodTable* elementMT = array.ElementMethodTable;

if (elementMT->IsPointer || elementMT->IsFunctionPointer)
{
Debug.Assert(value.GetMethodTable()->ValueTypeSize == IntPtr.Size);
elementMT = value.GetMethodTable();
}

if (elementMT->IsValueType)
{
Debug.Assert(value.GetMethodTable()->IsValueType && elementMT->ValueTypeSize == value.GetMethodTable()->ValueTypeSize);
nint flattenedIndex = array.GetFlattenedIndex(indices);
ref byte element = ref Unsafe.AddByteOffset(ref MemoryMarshal.GetArrayDataReference(array), (nuint)flattenedIndex * array.ElementSize);
RuntimeImports.RhUnbox(value, ref element, elementMT);
}
else
{
RuntimeImports.RhCheckArrayStore(array, value);
nint flattenedIndex = array.GetFlattenedIndex(indices);
ref object element = ref Unsafe.Add(ref Unsafe.As<byte, object>(ref MemoryMarshal.GetArrayDataReference(array)), flattenedIndex);
element = value;
}
}

public static IntPtr GetAllocateObjectHelperForType(RuntimeTypeHandle type)
{
return RuntimeImports.RhGetRuntimeHelperForType(type.ToMethodTable(), RuntimeHelperKind.AllocateObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ private unsafe nint GetFlattenedIndex(int rawIndex)
return rawIndex;
}

private unsafe nint GetFlattenedIndex(ReadOnlySpan<int> indices)
internal unsafe nint GetFlattenedIndex(ReadOnlySpan<int> indices)
{
// Checked by the caller
Debug.Assert(indices.Length == Rank);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ internal sealed override IEnumerable<RuntimeMethodInfo> SyntheticMethods
for (int i = 0; i < rank; i++)
indices[i] = (int)(args[i]);
object value = args[rank];
array.SetValue(value, indices);
RuntimeAugments.SetArrayValue(array, indices, value);
return null;
}
);
Expand Down
Loading