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

Hard-code the numeric scalar types in CoerceValue and simplify enum marshalling to remove any usage of Convert.ChangeType. #1487

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/WinRT.Runtime/Marshalers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1720,11 +1720,11 @@ internal static class Marshaler

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 CopyIntEnum(object value, IntPtr dest) => *(int*)dest.ToPointer() = Convert.ToInt32(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 CopyUIntEnum(object value, IntPtr dest) => *(uint*)dest.ToPointer() = Convert.ToUInt32(value);

private static unsafe void CopyUIntEnumDirect(object value, IntPtr dest) => *(uint*)dest.ToPointer() = (uint)value;
}
Expand Down
47 changes: 41 additions & 6 deletions src/WinRT.Runtime/Projections/IPropertyValue.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using WinRT;
Expand Down Expand Up @@ -106,8 +107,8 @@ internal static class ManagedIPropertyValueImpl
private const int TYPE_E_TYPEMISMATCH = unchecked((int)0x80028CA0);
private const int DISP_E_OVERFLOW = unchecked((int)0x8002000A);
private static IPropertyValue.Vftbl AbiToProjectionVftable;
public static IntPtr AbiToProjectionVftablePtr;
public static IntPtr AbiToProjectionVftablePtr;

internal static readonly Guid IID = new(0x4BD682DD, 0x7554, 0x40E9, 0x9A, 0x9B, 0x82, 0x65, 0x4E, 0xDE, 0x7E, 0x62);

static unsafe ManagedIPropertyValueImpl()
Expand Down Expand Up @@ -325,12 +326,46 @@ private static T CoerceValue<T>(object value)
{
return (T)(object)guid.ToString("D", global::System.Globalization.CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(byte))
Sergio0694 marked this conversation as resolved.
Show resolved Hide resolved
{
return (T)(object)Convert.ToByte(value, global::System.Globalization.CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(short))
{
return (T)(object)Convert.ToInt16(value, global::System.Globalization.CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(ushort))
{
return (T)(object)Convert.ToUInt16(value, global::System.Globalization.CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(int))
{
return (T)(object)Convert.ToInt32(value, global::System.Globalization.CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(uint))
{
return (T)(object)Convert.ToUInt32(value, global::System.Globalization.CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(long))
{
return (T)(object)Convert.ToInt64(value, global::System.Globalization.CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(ulong))
{
return (T)(object)Convert.ToUInt64(value, global::System.Globalization.CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(float))
{
return (T)(object)Convert.ToSingle(value, global::System.Globalization.CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(double))
{
return (T)(object)Convert.ToDouble(value, global::System.Globalization.CultureInfo.InvariantCulture);
}
else
manodasanW marked this conversation as resolved.
Show resolved Hide resolved
{
if (NumericScalarTypes.TryGetValue(typeof(T), out _))
{
return (T)Convert.ChangeType(value, typeof(T), global::System.Globalization.CultureInfo.InvariantCulture);
}
Debug.Assert(!NumericScalarTypes.ContainsKey(typeof(T)));
throw new InvalidCastException("", TYPE_E_TYPEMISMATCH);
}
}
catch (FormatException)
Expand Down
46 changes: 40 additions & 6 deletions src/WinRT.Runtime/Projections/IPropertyValue.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ internal static class ManagedIPropertyValueImpl
private const int TYPE_E_TYPEMISMATCH = unchecked((int)0x80028CA0);
private const int DISP_E_OVERFLOW = unchecked((int)0x8002000A);
private static IPropertyValue.Vftbl AbiToProjectionVftable;
public static IntPtr AbiToProjectionVftablePtr;
public static IntPtr AbiToProjectionVftablePtr;

internal static readonly Guid IID = new(0x4BD682DD, 0x7554, 0x40E9, 0x9A, 0x9B, 0x82, 0x65, 0x4E, 0xDE, 0x7E, 0x62);

private static readonly Delegate[] DelegateCache = new Delegate[39];
Expand Down Expand Up @@ -327,12 +327,46 @@ private static T CoerceValue<T>(object value)
{
return (T)(object)guid.ToString("D", global::System.Globalization.CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(byte))
Sergio0694 marked this conversation as resolved.
Show resolved Hide resolved
{
return (T)(object)Convert.ToByte(value, global::System.Globalization.CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(short))
{
return (T)(object)Convert.ToInt16(value, global::System.Globalization.CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(ushort))
{
return (T)(object)Convert.ToUInt16(value, global::System.Globalization.CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(int))
{
return (T)(object)Convert.ToInt32(value, global::System.Globalization.CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(uint))
{
return (T)(object)Convert.ToUInt32(value, global::System.Globalization.CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(long))
{
return (T)(object)Convert.ToInt64(value, global::System.Globalization.CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(ulong))
{
return (T)(object)Convert.ToUInt64(value, global::System.Globalization.CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(float))
{
return (T)(object)Convert.ToSingle(value, global::System.Globalization.CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(double))
{
return (T)(object)Convert.ToDouble(value, global::System.Globalization.CultureInfo.InvariantCulture);
}
else
{
if (NumericScalarTypes.TryGetValue(typeof(T), out _))
{
return (T)Convert.ChangeType(value, typeof(T), global::System.Globalization.CultureInfo.InvariantCulture);
}
Debug.Assert(!NumericScalarTypes.ContainsKey(typeof(T)));
throw new InvalidCastException("", TYPE_E_TYPEMISMATCH);
}
}
catch (FormatException)
Expand Down
Loading