Skip to content

Commit

Permalink
Add NonBlittableMarshallingStubs to remove instantiations
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Jan 14, 2024
1 parent 71637fd commit 3d3ea0b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 52 deletions.
43 changes: 43 additions & 0 deletions src/WinRT.Runtime/FundamentalMarshalers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,49 @@

namespace ABI.System
{
/// <summary>
/// Non-generic marshalling stubs used from <see cref="global::WinRT.MarshalGeneric{T}"/>.
/// This avoids the generic instantiations for all additional stubs for these ABI types.
/// </summary>
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<object> 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;
Expand Down
64 changes: 32 additions & 32 deletions src/WinRT.Runtime/Marshalers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -563,46 +563,46 @@ static MarshalGeneric()
AbiType = typeof(byte);
MarshalerType = typeof(bool);
// MarshalByObjectReferenceValueSupported = false; (same as default value, we can always skip this field write)
CreateMarshaler = (Func<T, object>)(object)((Func<bool, bool>)global::ABI.System.Boolean.CreateMarshaler).WithObjectTResult();
CreateMarshaler = (Func<T, object>)(object)ABI.System.NonBlittableMarshallingStubs.Boolean_CreateMarshaler;
CreateMarshaler2 = CreateMarshaler;
GetAbi = ((Func<bool, byte>)global::ABI.System.Boolean.GetAbi).WithObjectParams();
FromAbi = (Func<object, T>)(object)((Func<byte, bool>)global::ABI.System.Boolean.FromAbi).WithObjectT();
CopyAbi = ((Action<bool, IntPtr>)global::ABI.System.Boolean.CopyAbi).WithObjectT1();
FromManaged = (Func<T, object>)(object)((Func<bool, byte>)global::ABI.System.Boolean.FromManaged).WithObjectTResult();
CopyManaged = (Action<T, IntPtr>)(object)(Action<bool, IntPtr>)global::ABI.System.Boolean.CopyManaged;
DisposeMarshaler = ((Action<bool>)global::ABI.System.Boolean.DisposeMarshaler).WithObjectParams();
DisposeAbi = ((Action<byte>)global::ABI.System.Boolean.DisposeAbi).WithObjectParams();
GetAbi = ABI.System.NonBlittableMarshallingStubs.Boolean_GetAbi;
FromAbi = (Func<object, T>)(object)ABI.System.NonBlittableMarshallingStubs.Boolean_FromAbi;
CopyAbi = ABI.System.NonBlittableMarshallingStubs.Boolean_CopyAbi;
FromManaged = (Func<T, object>)(object)ABI.System.NonBlittableMarshallingStubs.Boolean_FromManaged;
CopyManaged = (Action<T, IntPtr>)(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<T, object>)(object)((Func<char, char>)global::ABI.System.Char.CreateMarshaler).WithObjectTResult();
CreateMarshaler = (Func<T, object>)(object)ABI.System.NonBlittableMarshallingStubs.Char_CreateMarshaler;
CreateMarshaler2 = CreateMarshaler;
GetAbi = ((Func<char, ushort>)global::ABI.System.Char.GetAbi).WithObjectParams();
FromAbi = (Func<object, T>)(object)((Func<ushort, char>)global::ABI.System.Char.FromAbi).WithObjectT();
CopyAbi = ((Action<char, IntPtr>)global::ABI.System.Char.CopyAbi).WithObjectT1();
FromManaged = (Func<T, object>)(object)((Func<char, ushort>)global::ABI.System.Char.FromManaged).WithObjectTResult();
CopyManaged = (Action<T, IntPtr>)(object)(Action<char, IntPtr>)global::ABI.System.Char.CopyManaged;
DisposeMarshaler = ((Action<char>)global::ABI.System.Char.DisposeMarshaler).WithObjectParams();
DisposeAbi = ((Action<ushort>)global::ABI.System.Char.DisposeAbi).WithObjectParams();
GetAbi = ABI.System.NonBlittableMarshallingStubs.Char_GetAbi;
FromAbi = (Func<object, T>)(object)ABI.System.NonBlittableMarshallingStubs.Char_FromAbi;
CopyAbi = ABI.System.NonBlittableMarshallingStubs.Char_CopyAbi;
FromManaged = (Func<T, object>)(object)ABI.System.NonBlittableMarshallingStubs.Char_FromManaged;
CopyManaged = (Action<T, IntPtr>)(object)ABI.System.NonBlittableMarshallingStubs.Char_CopyManaged;
DisposeMarshaler = ABI.System.NonBlittableMarshallingStubs.NoOpFunc;
DisposeAbi = ABI.System.NonBlittableMarshallingStubs.NoOpFunc;
}
else if (typeof(T) == typeof(TimeSpan))
{
// Another well known projected type that we might will be constructed
HelperType = typeof(global::ABI.System.TimeSpan);
AbiType = typeof(global::ABI.System.TimeSpan);
MarshalerType = typeof(global::ABI.System.TimeSpan.Marshaler);
CreateMarshaler = (Func<T, object>)(object)((Func<global::System.TimeSpan, global::ABI.System.TimeSpan.Marshaler>)global::ABI.System.TimeSpan.CreateMarshaler).WithObjectTResult();
CreateMarshaler = (Func<T, object>)(object)ABI.System.NonBlittableMarshallingStubs.TimeSpan_CreateMarshaler;
CreateMarshaler2 = CreateMarshaler;
GetAbi = ((Func<global::ABI.System.TimeSpan.Marshaler, global::ABI.System.TimeSpan>)global::ABI.System.TimeSpan.GetAbi).WithObjectParams();
FromAbi = (Func<object, T>)(object)((Func<global::ABI.System.TimeSpan, global::System.TimeSpan>)global::ABI.System.TimeSpan.FromAbi).WithObjectT();
CopyAbi = ((Action<global::ABI.System.TimeSpan.Marshaler, IntPtr>)global::ABI.System.TimeSpan.CopyAbi).WithObjectT1();
FromManaged = (Func<T, object>)(object)((Func<global::System.TimeSpan, global::ABI.System.TimeSpan>)global::ABI.System.TimeSpan.FromManaged).WithObjectTResult();
CopyManaged = (Action<T, IntPtr>)(object)(Action<global::System.TimeSpan, IntPtr>)global::ABI.System.TimeSpan.CopyManaged;
DisposeMarshaler = ((Action<global::ABI.System.TimeSpan.Marshaler>)global::ABI.System.TimeSpan.DisposeMarshaler).WithObjectParams();
DisposeAbi = ((Action<global::ABI.System.TimeSpan>)global::ABI.System.TimeSpan.DisposeAbi).WithObjectParams();
GetAbi = ABI.System.NonBlittableMarshallingStubs.TimeSpan_GetAbi;
FromAbi = (Func<object, T>)(object)ABI.System.NonBlittableMarshallingStubs.TimeSpan_FromAbi;
CopyAbi = ABI.System.NonBlittableMarshallingStubs.TimeSpan_CopyAbi;
FromManaged = (Func<T, object>)(object)ABI.System.NonBlittableMarshallingStubs.TimeSpan_FromManaged;
CopyManaged = (Action<T, IntPtr>)(object)ABI.System.NonBlittableMarshallingStubs.TimeSpan_CopyManaged;
DisposeMarshaler = ABI.System.NonBlittableMarshallingStubs.NoOpFunc;
DisposeAbi = ABI.System.NonBlittableMarshallingStubs.NoOpFunc;
}
else if (typeof(T) == typeof(DateTimeOffset))
{
Expand All @@ -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<T, object>)(object)((Func<global::System.DateTimeOffset, global::ABI.System.DateTimeOffset.Marshaler>)global::ABI.System.DateTimeOffset.CreateMarshaler).WithObjectTResult();
CreateMarshaler = (Func<T, object>)(object)ABI.System.NonBlittableMarshallingStubs.DateTimeOffset_CreateMarshaler;
CreateMarshaler2 = CreateMarshaler;
GetAbi = ((Func<global::ABI.System.DateTimeOffset.Marshaler, global::ABI.System.DateTimeOffset>)global::ABI.System.DateTimeOffset.GetAbi).WithObjectParams();
FromAbi = (Func<object, T>)(object)((Func<global::ABI.System.DateTimeOffset, global::System.DateTimeOffset>)global::ABI.System.DateTimeOffset.FromAbi).WithObjectT();
CopyAbi = ((Action<global::ABI.System.DateTimeOffset.Marshaler, IntPtr>)global::ABI.System.DateTimeOffset.CopyAbi).WithObjectT1();
FromManaged = (Func<T, object>)(object)((Func<global::System.DateTimeOffset, global::ABI.System.DateTimeOffset>)global::ABI.System.DateTimeOffset.FromManaged).WithObjectTResult();
CopyManaged = (Action<T, IntPtr>)(object)(Action<global::System.DateTimeOffset, IntPtr>)global::ABI.System.DateTimeOffset.CopyManaged;
DisposeMarshaler = ((Action<global::ABI.System.DateTimeOffset.Marshaler>)global::ABI.System.DateTimeOffset.DisposeMarshaler).WithObjectParams();
DisposeAbi = ((Action<global::ABI.System.DateTimeOffset>)global::ABI.System.DateTimeOffset.DisposeAbi).WithObjectParams();
GetAbi = ABI.System.NonBlittableMarshallingStubs.DateTimeOffset_GetAbi;
FromAbi = (Func<object, T>)(object)ABI.System.NonBlittableMarshallingStubs.DateTimeOffset_FromAbi;
CopyAbi = ABI.System.NonBlittableMarshallingStubs.DateTimeOffset_CopyAbi;
FromManaged = (Func<T, object>)(object)ABI.System.NonBlittableMarshallingStubs.DateTimeOffset_FromManaged;
CopyManaged = (Action<T, IntPtr>)(object)ABI.System.NonBlittableMarshallingStubs.DateTimeOffset_CopyManaged;
DisposeMarshaler = ABI.System.NonBlittableMarshallingStubs.NoOpFunc;
DisposeAbi = ABI.System.NonBlittableMarshallingStubs.NoOpFunc;
}
else if (typeof(T).IsValueType)
{
Expand Down
20 changes: 0 additions & 20 deletions src/cswinrt/strings/WinRT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,11 @@ public static Func<T, object> WithObjectTResult<T, TResult>(this Func<T, TResult
return function.InvokeWithObjectTResult;
}

public static Func<object, object> WithObjectParams<T, TResult>(this Func<T, TResult> func)
{
return func.InvokeWithObjectParams;
}

public static Action<object> WithObjectParams<T>(this Action<T> action)
{
return action.InvokeWithObjectParams;
}

public static Action<object, T2> WithObjectT1<T1, T2>(this Action<T1, T2> action)
{
return action.InvokeWithObjectT1;
}

private static object InvokeWithMarshaler2Support(this Func<IObjectReference, IntPtr> func, object arg)
{
if (arg is ObjectReferenceValue objectReferenceValue)
Expand Down Expand Up @@ -104,20 +94,10 @@ private static TResult InvokeWithObjectT<T, TResult>(this Func<T, TResult> func,
return func.Invoke((T)arg);
}

private static object InvokeWithObjectParams<T, TResult>(this Func<T, TResult> func, object arg)
{
return func.Invoke((T)arg);
}

private static void InvokeWithObjectParams<T>(this Action<T> func, object arg)
{
func.Invoke((T)arg);
}

private static void InvokeWithObjectT1<T1, T2>(this Action<T1, T2> action, object arg1, T2 arg2)
{
action.Invoke((T1)arg1, arg2);
}
}

internal sealed class Platform
Expand Down

0 comments on commit 3d3ea0b

Please sign in to comment.