From 3d3ea0b7ad97754a1fb8af1d803f970c4819811a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 14 Jan 2024 15:55:47 +0100 Subject: [PATCH] Add NonBlittableMarshallingStubs to remove instantiations --- src/WinRT.Runtime/FundamentalMarshalers.cs | 43 +++++++++++++++ src/WinRT.Runtime/Marshalers.cs | 64 +++++++++++----------- src/cswinrt/strings/WinRT.cs | 20 ------- 3 files changed, 75 insertions(+), 52 deletions(-) diff --git a/src/WinRT.Runtime/FundamentalMarshalers.cs b/src/WinRT.Runtime/FundamentalMarshalers.cs index 25a690ef1..9354f5996 100644 --- a/src/WinRT.Runtime/FundamentalMarshalers.cs +++ b/src/WinRT.Runtime/FundamentalMarshalers.cs @@ -5,6 +5,49 @@ namespace ABI.System { + /// + /// Non-generic marshalling stubs used from . + /// This avoids the generic instantiations for all additional stubs for these ABI types. + /// + internal static class NonBlittableMarshallingStubs + { + // This can be shared for all DisposeMarshaler and DisposeAbi methods for these ABI types. + // None of these has any special logic that needs to run when those two APIs are invoked. + public static readonly Action NoOpFunc = NoOp; + + public static object Boolean_CreateMarshaler(bool value) => Boolean.CreateMarshaler(value); + public static object Boolean_GetAbi(object value) => Boolean.GetAbi((bool)value); + public static bool Boolean_FromAbi(object abi) => Boolean.FromAbi((byte)abi); + public static void Boolean_CopyAbi(object value, IntPtr dest) => Boolean.CopyAbi((bool)value, dest); + public static object Boolean_FromManaged(bool value) => Boolean.FromManaged(value); + public static void Boolean_CopyManaged(object arg, IntPtr dest) => Boolean.CopyManaged((bool)arg, dest); + + public static object Char_CreateMarshaler(char value) => Char.CreateMarshaler(value); + public static object Char_GetAbi(object value) => Char.GetAbi((char)value); + public static char Char_FromAbi(object abi) => Char.FromAbi((ushort)abi); + public static void Char_CopyAbi(object value, IntPtr dest) => Char.CopyAbi((char)value, dest); + public static object Char_FromManaged(char value) => Char.FromManaged(value); + public static void Char_CopyManaged(char arg, IntPtr dest) => Char.CopyManaged((char)arg, dest); + + public static object TimeSpan_CreateMarshaler(global::System.TimeSpan value) => TimeSpan.CreateMarshaler(value); + public static object TimeSpan_GetAbi(object value) => TimeSpan.GetAbi((TimeSpan.Marshaler)value); + public static global::System.TimeSpan TimeSpan_FromAbi(object abi) => TimeSpan.FromAbi((TimeSpan)abi); + public static void TimeSpan_CopyAbi(object value, IntPtr dest) => TimeSpan.CopyAbi((TimeSpan.Marshaler)value, dest); + public static object TimeSpan_FromManaged(global::System.TimeSpan value) => TimeSpan.FromManaged(value); + public static void TimeSpan_CopyManaged(object arg, IntPtr dest) => TimeSpan.CopyManaged((global::System.TimeSpan)arg, dest); + + public static object DateTimeOffset_CreateMarshaler(global::System.DateTimeOffset value) => DateTimeOffset.CreateMarshaler(value); + public static object DateTimeOffset_GetAbi(object value) => DateTimeOffset.GetAbi((DateTimeOffset.Marshaler)value); + public static global::System.DateTimeOffset DateTimeOffset_FromAbi(object abi) => DateTimeOffset.FromAbi((DateTimeOffset)abi); + public static unsafe void DateTimeOffset_CopyAbi(object value, IntPtr dest) => DateTimeOffset.CopyAbi((DateTimeOffset.Marshaler)value, dest); + public static object DateTimeOffset_FromManaged(global::System.DateTimeOffset value) => DateTimeOffset.FromManaged(value); + public static void DateTimeOffset_CopyManaged(object arg, IntPtr dest) => DateTimeOffset.CopyManaged((global::System.DateTimeOffset)arg, dest); + + private static void NoOp(object obj) + { + } + } + internal struct Boolean { public static bool CreateMarshaler(bool value) => value; diff --git a/src/WinRT.Runtime/Marshalers.cs b/src/WinRT.Runtime/Marshalers.cs index 49c0eedc1..d4a301c1b 100644 --- a/src/WinRT.Runtime/Marshalers.cs +++ b/src/WinRT.Runtime/Marshalers.cs @@ -563,30 +563,30 @@ static MarshalGeneric() AbiType = typeof(byte); MarshalerType = typeof(bool); // MarshalByObjectReferenceValueSupported = false; (same as default value, we can always skip this field write) - CreateMarshaler = (Func)(object)((Func)global::ABI.System.Boolean.CreateMarshaler).WithObjectTResult(); + CreateMarshaler = (Func)(object)ABI.System.NonBlittableMarshallingStubs.Boolean_CreateMarshaler; CreateMarshaler2 = CreateMarshaler; - GetAbi = ((Func)global::ABI.System.Boolean.GetAbi).WithObjectParams(); - FromAbi = (Func)(object)((Func)global::ABI.System.Boolean.FromAbi).WithObjectT(); - CopyAbi = ((Action)global::ABI.System.Boolean.CopyAbi).WithObjectT1(); - FromManaged = (Func)(object)((Func)global::ABI.System.Boolean.FromManaged).WithObjectTResult(); - CopyManaged = (Action)(object)(Action)global::ABI.System.Boolean.CopyManaged; - DisposeMarshaler = ((Action)global::ABI.System.Boolean.DisposeMarshaler).WithObjectParams(); - DisposeAbi = ((Action)global::ABI.System.Boolean.DisposeAbi).WithObjectParams(); + GetAbi = ABI.System.NonBlittableMarshallingStubs.Boolean_GetAbi; + FromAbi = (Func)(object)ABI.System.NonBlittableMarshallingStubs.Boolean_FromAbi; + CopyAbi = ABI.System.NonBlittableMarshallingStubs.Boolean_CopyAbi; + FromManaged = (Func)(object)ABI.System.NonBlittableMarshallingStubs.Boolean_FromManaged; + CopyManaged = (Action)(object)ABI.System.NonBlittableMarshallingStubs.Boolean_CopyManaged; + DisposeMarshaler = ABI.System.NonBlittableMarshallingStubs.NoOpFunc; + DisposeAbi = ABI.System.NonBlittableMarshallingStubs.NoOpFunc; } else if (typeof(T) == typeof(char)) { HelperType = typeof(global::ABI.System.Char); AbiType = typeof(ushort); MarshalerType = typeof(char); - CreateMarshaler = (Func)(object)((Func)global::ABI.System.Char.CreateMarshaler).WithObjectTResult(); + CreateMarshaler = (Func)(object)ABI.System.NonBlittableMarshallingStubs.Char_CreateMarshaler; CreateMarshaler2 = CreateMarshaler; - GetAbi = ((Func)global::ABI.System.Char.GetAbi).WithObjectParams(); - FromAbi = (Func)(object)((Func)global::ABI.System.Char.FromAbi).WithObjectT(); - CopyAbi = ((Action)global::ABI.System.Char.CopyAbi).WithObjectT1(); - FromManaged = (Func)(object)((Func)global::ABI.System.Char.FromManaged).WithObjectTResult(); - CopyManaged = (Action)(object)(Action)global::ABI.System.Char.CopyManaged; - DisposeMarshaler = ((Action)global::ABI.System.Char.DisposeMarshaler).WithObjectParams(); - DisposeAbi = ((Action)global::ABI.System.Char.DisposeAbi).WithObjectParams(); + GetAbi = ABI.System.NonBlittableMarshallingStubs.Char_GetAbi; + FromAbi = (Func)(object)ABI.System.NonBlittableMarshallingStubs.Char_FromAbi; + CopyAbi = ABI.System.NonBlittableMarshallingStubs.Char_CopyAbi; + FromManaged = (Func)(object)ABI.System.NonBlittableMarshallingStubs.Char_FromManaged; + CopyManaged = (Action)(object)ABI.System.NonBlittableMarshallingStubs.Char_CopyManaged; + DisposeMarshaler = ABI.System.NonBlittableMarshallingStubs.NoOpFunc; + DisposeAbi = ABI.System.NonBlittableMarshallingStubs.NoOpFunc; } else if (typeof(T) == typeof(TimeSpan)) { @@ -594,15 +594,15 @@ static MarshalGeneric() HelperType = typeof(global::ABI.System.TimeSpan); AbiType = typeof(global::ABI.System.TimeSpan); MarshalerType = typeof(global::ABI.System.TimeSpan.Marshaler); - CreateMarshaler = (Func)(object)((Func)global::ABI.System.TimeSpan.CreateMarshaler).WithObjectTResult(); + CreateMarshaler = (Func)(object)ABI.System.NonBlittableMarshallingStubs.TimeSpan_CreateMarshaler; CreateMarshaler2 = CreateMarshaler; - GetAbi = ((Func)global::ABI.System.TimeSpan.GetAbi).WithObjectParams(); - FromAbi = (Func)(object)((Func)global::ABI.System.TimeSpan.FromAbi).WithObjectT(); - CopyAbi = ((Action)global::ABI.System.TimeSpan.CopyAbi).WithObjectT1(); - FromManaged = (Func)(object)((Func)global::ABI.System.TimeSpan.FromManaged).WithObjectTResult(); - CopyManaged = (Action)(object)(Action)global::ABI.System.TimeSpan.CopyManaged; - DisposeMarshaler = ((Action)global::ABI.System.TimeSpan.DisposeMarshaler).WithObjectParams(); - DisposeAbi = ((Action)global::ABI.System.TimeSpan.DisposeAbi).WithObjectParams(); + GetAbi = ABI.System.NonBlittableMarshallingStubs.TimeSpan_GetAbi; + FromAbi = (Func)(object)ABI.System.NonBlittableMarshallingStubs.TimeSpan_FromAbi; + CopyAbi = ABI.System.NonBlittableMarshallingStubs.TimeSpan_CopyAbi; + FromManaged = (Func)(object)ABI.System.NonBlittableMarshallingStubs.TimeSpan_FromManaged; + CopyManaged = (Action)(object)ABI.System.NonBlittableMarshallingStubs.TimeSpan_CopyManaged; + DisposeMarshaler = ABI.System.NonBlittableMarshallingStubs.NoOpFunc; + DisposeAbi = ABI.System.NonBlittableMarshallingStubs.NoOpFunc; } else if (typeof(T) == typeof(DateTimeOffset)) { @@ -611,15 +611,15 @@ static MarshalGeneric() HelperType = typeof(global::ABI.System.DateTimeOffset); AbiType = typeof(global::ABI.System.DateTimeOffset); MarshalerType = typeof(global::ABI.System.DateTimeOffset.Marshaler); - CreateMarshaler = (Func)(object)((Func)global::ABI.System.DateTimeOffset.CreateMarshaler).WithObjectTResult(); + CreateMarshaler = (Func)(object)ABI.System.NonBlittableMarshallingStubs.DateTimeOffset_CreateMarshaler; CreateMarshaler2 = CreateMarshaler; - GetAbi = ((Func)global::ABI.System.DateTimeOffset.GetAbi).WithObjectParams(); - FromAbi = (Func)(object)((Func)global::ABI.System.DateTimeOffset.FromAbi).WithObjectT(); - CopyAbi = ((Action)global::ABI.System.DateTimeOffset.CopyAbi).WithObjectT1(); - FromManaged = (Func)(object)((Func)global::ABI.System.DateTimeOffset.FromManaged).WithObjectTResult(); - CopyManaged = (Action)(object)(Action)global::ABI.System.DateTimeOffset.CopyManaged; - DisposeMarshaler = ((Action)global::ABI.System.DateTimeOffset.DisposeMarshaler).WithObjectParams(); - DisposeAbi = ((Action)global::ABI.System.DateTimeOffset.DisposeAbi).WithObjectParams(); + GetAbi = ABI.System.NonBlittableMarshallingStubs.DateTimeOffset_GetAbi; + FromAbi = (Func)(object)ABI.System.NonBlittableMarshallingStubs.DateTimeOffset_FromAbi; + CopyAbi = ABI.System.NonBlittableMarshallingStubs.DateTimeOffset_CopyAbi; + FromManaged = (Func)(object)ABI.System.NonBlittableMarshallingStubs.DateTimeOffset_FromManaged; + CopyManaged = (Action)(object)ABI.System.NonBlittableMarshallingStubs.DateTimeOffset_CopyManaged; + DisposeMarshaler = ABI.System.NonBlittableMarshallingStubs.NoOpFunc; + DisposeAbi = ABI.System.NonBlittableMarshallingStubs.NoOpFunc; } else if (typeof(T).IsValueType) { diff --git a/src/cswinrt/strings/WinRT.cs b/src/cswinrt/strings/WinRT.cs index cea4c1edd..c4f1f05d3 100644 --- a/src/cswinrt/strings/WinRT.cs +++ b/src/cswinrt/strings/WinRT.cs @@ -57,21 +57,11 @@ public static Func WithObjectTResult(this Func WithObjectParams(this Func func) - { - return func.InvokeWithObjectParams; - } - public static Action WithObjectParams(this Action action) { return action.InvokeWithObjectParams; } - public static Action WithObjectT1(this Action action) - { - return action.InvokeWithObjectT1; - } - private static object InvokeWithMarshaler2Support(this Func func, object arg) { if (arg is ObjectReferenceValue objectReferenceValue) @@ -104,20 +94,10 @@ private static TResult InvokeWithObjectT(this Func func, return func.Invoke((T)arg); } - private static object InvokeWithObjectParams(this Func func, object arg) - { - return func.Invoke((T)arg); - } - private static void InvokeWithObjectParams(this Action func, object arg) { func.Invoke((T)arg); } - - private static void InvokeWithObjectT1(this Action action, object arg1, T2 arg2) - { - action.Invoke((T1)arg1, arg2); - } } internal sealed class Platform