Skip to content

Commit

Permalink
Cache enum stubs and further avoid display classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Jan 18, 2024
1 parent d062331 commit 47e4332
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/WinRT.Runtime/Marshalers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1672,16 +1672,24 @@ public static T FromAbi<T>(IntPtr nativeDelegate)

internal static class Marshaler
{
internal static Action<object> EmptyFunc = (object box) => { };
internal static Func<object, object> ReturnParameterFunc = (object box) => box;
internal static readonly Action<object> EmptyFunc = Empty;
internal static readonly Func<object, object> ReturnParameterFunc = ReturnParameter;
internal static readonly Action<object, IntPtr> CopyIntEnumFunc = CopyIntEnum;
internal static readonly Action<object, IntPtr> CopyIntEnumDirectFunc = CopyIntEnumDirect;
internal static readonly Action<object, IntPtr> CopyUIntEnumFunc = CopyUIntEnum;
internal static readonly Action<object, IntPtr> CopyUIntEnumDirectFunc = CopyUIntEnumDirect;

internal static unsafe void CopyIntEnum(object value, IntPtr dest) => *(int*)dest.ToPointer() = (int)Convert.ChangeType(value, typeof(int));
private static void Empty(object arg) { }

internal static unsafe void CopyIntEnumDirect(object value, IntPtr dest) => *(int*)dest.ToPointer() = (int)value;
private static object ReturnParameter(object arg) => arg;

internal static unsafe void CopyUIntEnum(object value, IntPtr dest) => *(uint*)dest.ToPointer() = (uint)Convert.ChangeType(value, typeof(uint));
private static unsafe void CopyIntEnum(object value, IntPtr dest) => *(int*)dest.ToPointer() = (int)Convert.ChangeType(value, typeof(int));

internal static unsafe void CopyUIntEnumDirect(object value, IntPtr dest) => *(uint*)dest.ToPointer() = (uint)value;
private static unsafe void CopyIntEnumDirect(object value, IntPtr dest) => *(int*)dest.ToPointer() = (int)value;

private static unsafe void CopyUIntEnum(object value, IntPtr dest) => *(uint*)dest.ToPointer() = (uint)Convert.ChangeType(value, typeof(uint));

private static unsafe void CopyUIntEnumDirect(object value, IntPtr dest) => *(uint*)dest.ToPointer() = (uint)value;
}

#if EMBED
Expand Down Expand Up @@ -1842,13 +1850,13 @@ static Marshaler()
// For marshaling non-blittable enum arrays via MarshalNonBlittable
if (typeof(T).GetEnumUnderlyingType() == typeof(int))
{
CopyAbi = Marshaler.CopyIntEnum;
CopyManaged = ((Action<object, IntPtr>)Marshaler.CopyIntEnumDirect).WithTypedEnumT1<T>();
CopyAbi = Marshaler.CopyIntEnumFunc;
CopyManaged = Marshaler.CopyIntEnumDirectFunc.WithTypedEnumT1<T>();
}
else
{
CopyAbi = Marshaler.CopyUIntEnum;
CopyManaged = ((Action<object, IntPtr>)Marshaler.CopyUIntEnumDirect).WithTypedEnumT1<T>();
CopyAbi = Marshaler.CopyUIntEnumFunc;
CopyManaged = Marshaler.CopyUIntEnumDirectFunc.WithTypedEnumT1<T>();
}
}
CreateMarshalerArray = (T[] array) => MarshalBlittable<T>.CreateMarshalerArray(array);
Expand Down

0 comments on commit 47e4332

Please sign in to comment.