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

Reduce enum stubs codegen with NativeAOT #1441

Merged
merged 3 commits into from
Jan 24, 2024
Merged
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
24 changes: 17 additions & 7 deletions src/WinRT.Runtime/Marshalers.cs
Original file line number Diff line number Diff line change
@@ -1708,11 +1708,21 @@ public static T FromAbi<T>(IntPtr nativeDelegate)

internal static class Marshaler
{
internal static Func<object, object> ReturnParameterFunc = (object box) => box;
internal static unsafe Action<object, IntPtr> CopyIntEnumFunc =
(object value, IntPtr dest) => *(int*)dest.ToPointer() = (int)Convert.ChangeType(value, typeof(int));
internal static unsafe Action<object, IntPtr> CopyUIntEnumFunc =
(object value, IntPtr dest) => *(uint*)dest.ToPointer() = (uint)Convert.ChangeType(value, typeof(uint));
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;

private static object ReturnParameter(object arg) => arg;

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

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
@@ -1901,12 +1911,12 @@ static Marshaler()
if (typeof(T).GetEnumUnderlyingType() == typeof(int))
{
CopyAbi = Marshaler.CopyIntEnumFunc;
CopyManaged = (T value, IntPtr dest) => Marshaler.CopyIntEnumFunc(value, dest);
CopyManaged = Marshaler.CopyIntEnumDirectFunc.WithTypedT1<T>();
}
else
{
CopyAbi = Marshaler.CopyUIntEnumFunc;
CopyManaged = (T value, IntPtr dest) => Marshaler.CopyUIntEnumFunc(value, dest);
CopyManaged = Marshaler.CopyUIntEnumDirectFunc.WithTypedT1<T>();
}
}
CreateMarshalerArray = (T[] array) => MarshalBlittable<T>.CreateMarshalerArray(array);
10 changes: 10 additions & 0 deletions src/cswinrt/strings/WinRT.cs
Original file line number Diff line number Diff line change
@@ -47,6 +47,11 @@ public static Action<object> WithMarshaler2Support(this Action<IObjectReference>
return action.InvokeWithMarshaler2Support;
}

public static Action<T, IntPtr> WithTypedT1<T>(this Action<object, IntPtr> action)
{
return action.InvokeWithTypedT1;
}

public static Func<object, TResult> WithObjectT<T, TResult>(this Func<T, TResult> function)
{
return function.InvokeWithObjectT;
@@ -98,6 +103,11 @@ private static void InvokeWithObjectParams<T>(this Action<T> func, object arg)
{
func.Invoke((T)arg);
}

private static void InvokeWithTypedT1<T>(this Action<object, IntPtr> action, T arg1, IntPtr arg2)
{
action.Invoke(arg1, arg2);
}
}

internal sealed class Platform