Skip to content

Commit

Permalink
Use span to pass type modifiers in RuntimeTypeHandle.GetTypeHelper (#…
Browse files Browse the repository at this point in the history
…69425)

* Use span to pass type modifiers

* Use switch statement

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
  • Loading branch information
huoyaoyuan and jkotas authored May 20, 2022
1 parent 6069680 commit 8626da3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
25 changes: 12 additions & 13 deletions src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ internal RuntimeType GetTypeChecked()
internal static extern bool IsInstanceOfType(RuntimeType type, [NotNullWhen(true)] object? o);

[RequiresUnreferencedCode("MakeGenericType cannot be statically analyzed. It's not possible to guarantee the availability of requirements of the generic type.")]
internal static Type GetTypeHelper(Type typeStart, Type[]? genericArgs, IntPtr pModifiers, int cModifiers)
internal static Type GetTypeHelper(Type typeStart, Type[]? genericArgs, IntPtr pModifiers, int cModifiers) // called by VM
=> GetTypeHelper(typeStart, genericArgs, new ReadOnlySpan<int>((void*)pModifiers, cModifiers));

[RequiresUnreferencedCode("MakeGenericType cannot be statically analyzed. It's not possible to guarantee the availability of requirements of the generic type.")]
internal static Type GetTypeHelper(Type typeStart, Type[]? genericArgs, ReadOnlySpan<int> modifiers)
{
Type type = typeStart;

Expand All @@ -49,20 +53,15 @@ internal static Type GetTypeHelper(Type typeStart, Type[]? genericArgs, IntPtr p
type = type.MakeGenericType(genericArgs);
}

if (cModifiers > 0)
for (int i = 0; i < modifiers.Length; i++)
{
int* arModifiers = (int*)pModifiers.ToPointer();
for (int i = 0; i < cModifiers; i++)
type = (CorElementType)modifiers[i] switch
{
if ((CorElementType)Marshal.ReadInt32((IntPtr)arModifiers, i * sizeof(int)) == CorElementType.ELEMENT_TYPE_PTR)
type = type.MakePointerType();
else if ((CorElementType)Marshal.ReadInt32((IntPtr)arModifiers, i * sizeof(int)) == CorElementType.ELEMENT_TYPE_BYREF)
type = type.MakeByRefType();
else if ((CorElementType)Marshal.ReadInt32((IntPtr)arModifiers, i * sizeof(int)) == CorElementType.ELEMENT_TYPE_SZARRAY)
type = type.MakeArrayType();
else
type = type.MakeArrayType(Marshal.ReadInt32((IntPtr)arModifiers, ++i * sizeof(int)));
}
CorElementType.ELEMENT_TYPE_PTR => type.MakePointerType(),
CorElementType.ELEMENT_TYPE_BYREF => type.MakeByRefType(),
CorElementType.ELEMENT_TYPE_SZARRAY => type.MakeArrayType(),
_ => type.MakeArrayType(modifiers[++i])
};
}

return type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,7 @@ public void Dispose()
}

int[]? modifiers = GetModifiers();

fixed (int* ptr = modifiers)
{
IntPtr intPtr = new IntPtr(ptr);
return RuntimeTypeHandle.GetTypeHelper(baseType, types!, intPtr, modifiers == null ? 0 : modifiers.Length);
}
return RuntimeTypeHandle.GetTypeHelper(baseType, types!, modifiers);
}

private static Assembly? ResolveAssembly(string asmName, Func<AssemblyName, Assembly?>? assemblyResolver, bool throwOnError, ref StackCrawlMark stackMark)
Expand Down

0 comments on commit 8626da3

Please sign in to comment.