Skip to content
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 @@ -75,7 +75,7 @@ internal unsafe ref struct QCallAssembly
internal QCallAssembly(ref System.Reflection.RuntimeAssembly assembly)
{
_ptr = Unsafe.AsPointer(ref assembly);
_assembly = assembly.GetUnderlyingNativeHandle();
_assembly = assembly?.GetUnderlyingNativeHandle() ?? IntPtr.Zero;
}
}

Expand All @@ -88,10 +88,7 @@ internal unsafe ref struct QCallTypeHandle
internal QCallTypeHandle(ref System.RuntimeType type)
{
_ptr = Unsafe.AsPointer(ref type);
if (type != null)
_handle = type.GetUnderlyingNativeHandle();
else
_handle = IntPtr.Zero;
_handle = type?.GetUnderlyingNativeHandle() ?? IntPtr.Zero;
}

internal QCallTypeHandle(ref System.RuntimeTypeHandle rth)
Expand Down
8 changes: 4 additions & 4 deletions src/mono/System.Private.CoreLib/src/System/Delegate.Mono.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static Delegate CreateDelegate(Type type, object? firstArgument, MethodIn
return null;
}

Delegate? d = CreateDelegate_internal(type, firstArgument, method, throwOnBindFailure);
Delegate? d = CreateDelegate_internal(new QCallTypeHandle(ref rtType), firstArgument, method, throwOnBindFailure);
if (d != null)
{
d.original_method_info = method;
Expand Down Expand Up @@ -180,7 +180,7 @@ public static Delegate CreateDelegate(Type type, object? firstArgument, MethodIn
return null;
}

return CreateDelegate_internal(type, target, info, throwOnBindFailure);
return CreateDelegate_internal(new QCallTypeHandle(ref rtType), target, info, throwOnBindFailure);
}

public static Delegate? CreateDelegate(Type type, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type target, string method, bool ignoreCase, bool throwOnBindFailure)
Expand Down Expand Up @@ -211,7 +211,7 @@ public static Delegate CreateDelegate(Type type, object? firstArgument, MethodIn
return null;
}

return CreateDelegate_internal(type, null, info, throwOnBindFailure);
return CreateDelegate_internal(new QCallTypeHandle(ref rtType), null, info, throwOnBindFailure);
}

// GetCandidateMethod is annotated as DynamicallyAccessedMemberTypes.All because it will bind to non-public methods
Expand Down Expand Up @@ -578,7 +578,7 @@ private static bool InternalEqualTypes(object source, object value)
private protected static extern MulticastDelegate AllocDelegateLike_internal(Delegate d);

[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern Delegate? CreateDelegate_internal(Type type, object? target, MethodInfo info, bool throwOnBindFailure);
private static extern Delegate? CreateDelegate_internal(QCallTypeHandle type, object? target, MethodInfo info, bool throwOnBindFailure);

[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern MethodInfo GetVirtualMethod_internal();
Expand Down
30 changes: 25 additions & 5 deletions src/mono/System.Private.CoreLib/src/System/Enum.Mono.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,44 @@ namespace System
public partial class Enum
{
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern bool GetEnumValuesAndNames(RuntimeType enumType, out ulong[] values, out string[] names);
private static extern bool GetEnumValuesAndNames(QCallTypeHandle enumType, out ulong[] values, out string[] names);

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern object InternalBoxEnum(RuntimeType enumType, long value);
private static extern void InternalBoxEnum(QCallTypeHandle enumType, ObjectHandleOnStack res, long value);

[MethodImpl(MethodImplOptions.InternalCall)]
private extern CorElementType InternalGetCorElementType();
private static extern CorElementType InternalGetCorElementType(QCallTypeHandle enumType);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern RuntimeType InternalGetUnderlyingType(RuntimeType enumType);
private static extern void InternalGetUnderlyingType(QCallTypeHandle enumType, ObjectHandleOnStack res);

private static object InternalBoxEnum(RuntimeType enumType, long value)
{
object? res = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Wondering if it'd be better to use

object res = null!;

for cases where you expect res to never be null and user object? res = null where it can be.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

res is null initially, so object! looks like a better type for it.

InternalBoxEnum(new QCallTypeHandle(ref enumType), ObjectHandleOnStack.Create(ref res), value);
return res!;
}

private CorElementType InternalGetCorElementType()
{
RuntimeType this_type = (RuntimeType)GetType();
return InternalGetCorElementType(new QCallTypeHandle(ref this_type));
}

internal static RuntimeType InternalGetUnderlyingType(RuntimeType enumType)
{
RuntimeType? res = null;
InternalGetUnderlyingType(new QCallTypeHandle(ref enumType), ObjectHandleOnStack.Create(ref res));
return res!;
}

private static EnumInfo GetEnumInfo(RuntimeType enumType, bool getNames = true)
{
EnumInfo? entry = enumType.Cache.EnumInfo;

if (entry == null || (getNames && entry.Names == null))
{
if (!GetEnumValuesAndNames(enumType, out ulong[]? values, out string[]? names))
if (!GetEnumValuesAndNames(new QCallTypeHandle(ref enumType), out ulong[]? values, out string[]? names))
Array.Sort(values, names, Collections.Generic.Comparer<ulong>.Default);

bool hasFlagsAttribute = enumType.IsDefined(typeof(FlagsAttribute), inherit: false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static IntPtr AllocateTypeAssociatedMemory(Type type, int size)
internal static bool ObjectHasReferences(object obj)
{
// TODO: Missing intrinsic in interpreter
return RuntimeTypeHandle.HasReferences(obj.GetType() as RuntimeType);
return RuntimeTypeHandle.HasReferences((obj.GetType() as RuntimeType)!);
}

public static object GetUninitializedObject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ public partial class Marshal
public static extern void StructureToPtr(object structure, IntPtr ptr, bool fDeleteOld);

[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern bool IsPinnableType(Type type);
private static extern bool IsPinnableType(QCallTypeHandle type);

internal static bool IsPinnable(object? obj)
{
if (obj == null || obj is string)
return true;
return IsPinnableType(obj.GetType());
var type = (obj.GetType() as RuntimeType)!;
return IsPinnableType(new QCallTypeHandle(ref type));
//Type type = obj.GetType ();
//return !type.IsValueType || RuntimeTypeHandle.HasReferences (type as RuntimeType);
}
Expand Down Expand Up @@ -73,16 +74,30 @@ private static void PtrToStructureHelper(IntPtr ptr, object? structure, bool all
}

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern Delegate GetDelegateForFunctionPointerInternal(IntPtr ptr, Type t);
private static extern void GetDelegateForFunctionPointerInternal(QCallTypeHandle t, IntPtr ptr, ObjectHandleOnStack res);

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern IntPtr GetFunctionPointerForDelegateInternal(Delegate d);

private static Delegate GetDelegateForFunctionPointerInternal(IntPtr ptr, Type t)
{
RuntimeType rttype = (RuntimeType)t;
Delegate? res = null;
GetDelegateForFunctionPointerInternal(new QCallTypeHandle(ref rttype), ptr, ObjectHandleOnStack.Create(ref res));
return res!;
}

[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern void PrelinkInternal(MethodInfo m);

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern int SizeOfHelper(Type t, bool throwIfNotMarshalable);
private static extern int SizeOfHelper(QCallTypeHandle t, bool throwIfNotMarshalable);

private static int SizeOfHelper(Type t, bool throwIfNotMarshalable)
{
RuntimeType rttype = (RuntimeType)t;
return SizeOfHelper(new QCallTypeHandle(ref rttype), throwIfNotMarshalable);
}

public static IntPtr GetExceptionPointers()
{
Expand Down
Loading