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

Fix get type for nullable enums and fix WUX projections #1701

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/Tests/AuthoringConsumptionTest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,4 +720,6 @@ TEST(AuthoringTest, XamlMetadataProvider)
CustomXamlMetadataProvider provider;
EXPECT_NE(provider.GetXamlType(winrt::xaml_typename<Windows::Foundation::IReference<double>>()), nullptr);
EXPECT_NE(provider.GetXamlType(winrt::xaml_typename<Windows::Foundation::IReference<Windows::Foundation::TimeSpan>>()), nullptr);
EXPECT_NE(provider.GetXamlType(winrt::xaml_typename<Windows::Foundation::IReference<BasicEnum>>()), nullptr);
EXPECT_NE(provider.GetXamlType(winrt::xaml_typename<Windows::Foundation::IReference<FlagsEnum>>()), nullptr);
}
4 changes: 3 additions & 1 deletion src/Tests/AuthoringTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,9 @@ public sealed class CustomXamlMetadataProvider : IXamlMetadataProvider
public IXamlType GetXamlType(Type type)
{
if (type == typeof(Nullable<double>) ||
type == typeof(TimeSpan?))
type == typeof(TimeSpan?) ||
type == typeof(BasicEnum?) ||
type == typeof(FlagsEnum?))
{
return new XamlType(type);
}
Expand Down
3 changes: 2 additions & 1 deletion src/WinRT.Runtime/MatchingRefApiCompatBaseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,5 @@ TypesMustExist : Type 'Microsoft.UI.Xaml.Data.BindableCustomProperty' does not e
TypesMustExist : Type 'WinRT.GeneratedBindableCustomPropertyAttribute' does not exist in the reference but it does exist in the implementation.
TypesMustExist : Type 'Microsoft.UI.Xaml.Data.IBindableCustomPropertyImplementation' does not exist in the reference but it does exist in the implementation.
MembersMustExist : Member 'public void ABI.System.Uri.DisposeAbiArray(System.Object)' does not exist in the reference but it does exist in the implementation.
Total Issues: 273
TypesMustExist : Type 'WinRT.EnumTypeDetails<T>' does not exist in the reference but it does exist in the implementation.
Total Issues: 274
76 changes: 75 additions & 1 deletion src/WinRT.Runtime/Projections/Nullable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2285,7 +2285,32 @@ private static Func<IInspectable, object> GetValueFactoryInternal(global::System
if (type == typeof(global::System.Numerics.Vector2)) return typeof(global::System.Nullable<global::System.Numerics.Vector2>);
if (type == typeof(global::System.Numerics.Vector3)) return typeof(global::System.Nullable<global::System.Numerics.Vector3>);
if (type == typeof(global::System.Numerics.Vector4)) return typeof(global::System.Nullable<global::System.Numerics.Vector4>);


#if NET
var winrtExposedClassAttribute = type.GetCustomAttribute<WinRTExposedTypeAttribute>(false);
if (winrtExposedClassAttribute == null)
{
var authoringMetadaType = type.GetAuthoringMetadataType();
if (authoringMetadaType != null)
{
winrtExposedClassAttribute = authoringMetadaType.GetCustomAttribute<WinRTExposedTypeAttribute>(false);
}
}

if (winrtExposedClassAttribute != null && winrtExposedClassAttribute.WinRTExposedTypeDetails != null)
{
if (Activator.CreateInstance(winrtExposedClassAttribute.WinRTExposedTypeDetails) is IWinRTNullableTypeDetails nullableTypeDetails)
{
return nullableTypeDetails.GetNullableType();
}
}

if (!RuntimeFeature.IsDynamicCodeCompiled)
{
throw new NotSupportedException($"Failed to construct nullable with type '{type}'.");
}
#endif

return null;
}
}
Expand All @@ -2311,6 +2336,7 @@ namespace WinRT
internal interface IWinRTNullableTypeDetails
{
object GetNullableValue(IInspectable inspectable);
Type GetNullableType();
}

public sealed class StructTypeDetails<T, TAbi> : IWinRTExposedTypeDetails, IWinRTNullableTypeDetails where T: struct where TAbi : unmanaged
Expand Down Expand Up @@ -2364,6 +2390,8 @@ unsafe object IWinRTNullableTypeDetails.GetNullableValue(IInspectable inspectabl
Marshal.Release(nullablePtr);
}
}

Type IWinRTNullableTypeDetails.GetNullableType() => typeof(global::System.Nullable<T>);
Sergio0694 marked this conversation as resolved.
Show resolved Hide resolved
}

public abstract class DelegateTypeDetails<T> : IWinRTExposedTypeDetails, IWinRTNullableTypeDetails where T : global::System.Delegate
Expand Down Expand Up @@ -2418,6 +2446,52 @@ unsafe object IWinRTNullableTypeDetails.GetNullableValue(IInspectable inspectabl
Marshal.Release(nullablePtr);
}
}

// Delegates are handled separately.
Type IWinRTNullableTypeDetails.GetNullableType() => throw new NotImplementedException();
manodasanW marked this conversation as resolved.
Show resolved Hide resolved
}

public sealed class EnumTypeDetails<T> : IWinRTExposedTypeDetails, IWinRTNullableTypeDetails where T : unmanaged, Enum
{
[SkipLocalsInit]
manodasanW marked this conversation as resolved.
Show resolved Hide resolved
public ComWrappers.ComInterfaceEntry[] GetExposedInterfaces()
{
Span<ComWrappers.ComInterfaceEntry> entries = stackalloc ComWrappers.ComInterfaceEntry[2];
int count = 0;

if (FeatureSwitches.EnableIReferenceSupport)
{
entries[count++] = new ComWrappers.ComInterfaceEntry
{
IID = global::WinRT.Interop.IID.IID_IPropertyValue,
Vtable = ABI.Windows.Foundation.ManagedIPropertyValueImpl.AbiToProjectionVftablePtr
};

if (typeof(T).IsDefined(typeof(FlagsAttribute)))
{
entries[count++] = new ComWrappers.ComInterfaceEntry
{
IID = ABI.System.Nullable_FlagsEnum.GetIID(typeof(T)),
Vtable = ABI.System.Nullable_FlagsEnum.AbiToProjectionVftablePtr
};
}
else
{
entries[count++] = new ComWrappers.ComInterfaceEntry
{
IID = ABI.System.Nullable_IntEnum.GetIID(typeof(T)),
Vtable = ABI.System.Nullable_IntEnum.AbiToProjectionVftablePtr
};
}
}

return entries.Slice(0, count).ToArray();
}

Type IWinRTNullableTypeDetails.GetNullableType() => typeof(global::System.Nullable<T>);
manodasanW marked this conversation as resolved.
Show resolved Hide resolved

// Unboxing enums are handled separately.
object IWinRTNullableTypeDetails.GetNullableValue(IInspectable inspectable) => throw new NotImplementedException();
}
}
#endif
2 changes: 1 addition & 1 deletion src/WinRT.Runtime/TypeNameSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ static Type ResolveGenericType(Type resolvedType, Type[] genericTypes, string ru
{
if (type.IsValueType)
{
throw new NotSupportedException($"Cannot provide generic type from '{runtimeClassName}'.");
return null;
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/cswinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3612,6 +3612,7 @@ private % AsInternal(InterfaceTag<%> _) => % ?? Make_%();

return isFactory ||
get_category(type) == category::struct_type ||
get_category(type) == category::enum_type ||
(get_category(type) == category::delegate_type && distance(type.GenericParam()) == 0);
}

Expand All @@ -3625,6 +3626,11 @@ private % AsInternal(InterfaceTag<%> _) => % ?? Make_%();
bind<write_type_name>(type, typedef_name_type::Projected, false),
bind<write_type_name>(type, is_type_blittable(type) ? typedef_name_type::Projected : typedef_name_type::ABI, false));
}
else if (get_category(type) == category::enum_type)
{
w.write(R"([global::WinRT.WinRTExposedType(typeof(global::WinRT.EnumTypeDetails<%>))])",
bind<write_type_name>(type, typedef_name_type::Projected, false));
}
else
{
w.write(R"([global::WinRT.WinRTExposedType(typeof(%%WinRTTypeDetails))])",
Expand Down Expand Up @@ -9623,10 +9629,11 @@ return true;

auto enum_underlying_type = is_flags_enum(type) ? "uint" : "int";

w.write(R"(%%% enum % : %
w.write(R"(%%%% enum % : %
{
)",
bind<write_winrt_attribute>(type),
bind<write_winrt_exposed_type_attribute>(type, false),
bind<write_type_custom_attributes>(type, true),
(settings.internal || settings.embedded) ? (settings.public_enums ? "public" : "internal") : "public",
bind<write_type_name>(type, typedef_name_type::Projected, false), enum_underlying_type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public TimeSpan TimeSpan
}

[global::WinRT.WindowsRuntimeType("Microsoft.UI")]
#if NET
[global::WinRT.WinRTExposedType(typeof(global::WinRT.EnumTypeDetails<RepeatBehaviorType>))]
#endif
#if EMBED
internal
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ public double BottomLeft
}

[global::WinRT.WindowsRuntimeType("Microsoft.UI")]
#if NET
[global::WinRT.WinRTExposedType(typeof(global::WinRT.EnumTypeDetails<GridUnitType>))]
#endif
#if EMBED
internal
#else
Expand Down Expand Up @@ -406,6 +409,9 @@ public override int GetHashCode()
}

[global::WinRT.WindowsRuntimeType("Microsoft.UI")]
#if NET
[global::WinRT.WinRTExposedType(typeof(global::WinRT.EnumTypeDetails<DurationType>))]
#endif
#if EMBED
internal
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace Windows.UI.Xaml.Controls.Primitives

[global::WinRT.WindowsRuntimeType("Windows.UI.Xaml")]
[global::WinRT.WindowsRuntimeHelperType(typeof(global::ABI.Windows.UI.Xaml.Controls.Primitives.GeneratorPosition))]
#if NET
[global::WinRT.WinRTExposedType(typeof(global::WinRT.StructTypeDetails<GeneratorPosition, GeneratorPosition>))]
#endif
[StructLayout(LayoutKind.Sequential)]
#if EMBED
internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace Windows.UI.Xaml.Media.Animation

[global::WinRT.WindowsRuntimeType("Windows.UI.Xaml")]
[global::WinRT.WindowsRuntimeHelperType(typeof(global::ABI.Windows.UI.Xaml.Media.Animation.KeyTime))]
#if NET
[global::WinRT.WinRTExposedType(typeof(global::WinRT.StructTypeDetails<KeyTime, KeyTime>))]
#endif
[StructLayout(LayoutKind.Sequential)]
#if EMBED
internal
Expand Down Expand Up @@ -79,6 +82,9 @@ public TimeSpan TimeSpan
}

[global::WinRT.WindowsRuntimeType("Windows.UI.Xaml")]
#if NET
[global::WinRT.WinRTExposedType(typeof(global::WinRT.EnumTypeDetails<RepeatBehaviorType>))]
#endif
#if EMBED
internal
#else
Expand All @@ -92,7 +98,10 @@ enum RepeatBehaviorType
}

[global::WinRT.WindowsRuntimeType("Windows.UI.Xaml")]
[global::WinRT.WindowsRuntimeHelperType(typeof(global::ABI.Windows.UI.Xaml.Media.Animation.RepeatBehavior))]
[global::WinRT.WindowsRuntimeHelperType(typeof(global::ABI.Windows.UI.Xaml.Media.Animation.RepeatBehavior))]
#if NET
[global::WinRT.WinRTExposedType(typeof(global::WinRT.StructTypeDetails<RepeatBehavior, RepeatBehavior>))]
#endif
[StructLayout(LayoutKind.Sequential)]
#if EMBED
internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace Windows.UI.Xaml.Media.Media3D

[global::WinRT.WindowsRuntimeType("Windows.UI.Xaml")]
[global::WinRT.WindowsRuntimeHelperType(typeof(global::ABI.Windows.UI.Xaml.Media.Media3D.Matrix3D))]
#if NET
[global::WinRT.WinRTExposedType(typeof(global::WinRT.StructTypeDetails<Matrix3D, Matrix3D>))]
#endif
[StructLayout(LayoutKind.Sequential)]
#if EMBED
internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace Windows.UI.Xaml.Media

[global::WinRT.WindowsRuntimeType("Windows.UI.Xaml")]
[global::WinRT.WindowsRuntimeHelperType(typeof(global::ABI.Windows.UI.Xaml.Media.Matrix))]
#if NET
[global::WinRT.WinRTExposedType(typeof(global::WinRT.StructTypeDetails<Matrix, Matrix>))]
#endif
[StructLayout(LayoutKind.Sequential)]
#if EMBED
internal
Expand Down
18 changes: 18 additions & 0 deletions src/cswinrt/strings/additions/Windows.UI.Xaml/Windows.UI.Xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace Windows.UI.Xaml

[global::WinRT.WindowsRuntimeType("Windows.UI.Xaml")]
[global::WinRT.WindowsRuntimeHelperType(typeof(global::ABI.Windows.UI.Xaml.CornerRadius))]
#if NET
[global::WinRT.WinRTExposedType(typeof(global::WinRT.StructTypeDetails<CornerRadius, CornerRadius>))]
#endif
[StructLayout(LayoutKind.Sequential)]
#if EMBED
internal
Expand Down Expand Up @@ -152,6 +155,9 @@ public double BottomLeft
}

[global::WinRT.WindowsRuntimeType("Windows.UI.Xaml")]
#if NET
[global::WinRT.WinRTExposedType(typeof(global::WinRT.EnumTypeDetails<GridUnitType>))]
#endif
#if EMBED
internal
#else
Expand All @@ -166,6 +172,9 @@ enum GridUnitType

[global::WinRT.WindowsRuntimeType("Windows.UI.Xaml")]
[global::WinRT.WindowsRuntimeHelperType(typeof(global::ABI.Windows.UI.Xaml.GridLength))]
#if NET
[global::WinRT.WinRTExposedType(typeof(global::WinRT.StructTypeDetails<GridLength, GridLength>))]
#endif
[StructLayout(LayoutKind.Sequential)]
#if EMBED
internal
Expand Down Expand Up @@ -284,6 +293,9 @@ internal string ToString(global::System.Globalization.CultureInfo cultureInfo)

[global::WinRT.WindowsRuntimeType("Windows.UI.Xaml")]
[global::WinRT.WindowsRuntimeHelperType(typeof(global::ABI.Windows.UI.Xaml.Thickness))]
#if NET
[global::WinRT.WinRTExposedType(typeof(global::WinRT.StructTypeDetails<Thickness, Thickness>))]
#endif
[StructLayout(LayoutKind.Sequential)]
#if EMBED
internal
Expand Down Expand Up @@ -397,6 +409,9 @@ public override int GetHashCode()
}

[global::WinRT.WindowsRuntimeType("Windows.UI.Xaml")]
#if NET
[global::WinRT.WinRTExposedType(typeof(global::WinRT.EnumTypeDetails<DurationType>))]
#endif
#if EMBED
internal
#else
Expand All @@ -411,6 +426,9 @@ enum DurationType

[global::WinRT.WindowsRuntimeType("Windows.UI.Xaml")]
[global::WinRT.WindowsRuntimeHelperType(typeof(global::ABI.Windows.UI.Xaml.Duration))]
#if NET
[global::WinRT.WinRTExposedType(typeof(global::WinRT.StructTypeDetails<Duration, Duration>))]
#endif
[StructLayout(LayoutKind.Sequential)]
#if EMBED
internal
Expand Down