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 boxing of types #1646

Merged
merged 5 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 8 additions & 1 deletion src/Tests/FunctionalTests/Collections/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using test_component_derived.Nested;
using TestComponentCSharp;
Expand Down Expand Up @@ -68,6 +69,12 @@
return 101;
}

var types = Class.ListOfTypes;
if (types.Count != 2 || types[0] != typeof(Class))
{
return 101;
}

return 100;

static bool SequencesEqual<T>(IEnumerable<T> x, params IEnumerable<T>[] list) => list.All((y) => x.SequenceEqual(y));
Expand Down
15 changes: 15 additions & 0 deletions src/Tests/TestComponentCSharp/Class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,11 @@ namespace winrt::TestComponentCSharp::implementation
return winrt::unbox_value<TestComponentCSharp::ProvideInt>(obj);
}

Windows::UI::Xaml::Interop::TypeName Class::UnboxType(WF::IInspectable const& obj)
{
return winrt::unbox_value<Windows::UI::Xaml::Interop::TypeName>(obj);
}

com_array<int32_t> Class::UnboxInt32Array(WF::IInspectable const& obj)
{
return obj.as<IReferenceArray<int32_t>>().Value();
Expand Down Expand Up @@ -1633,6 +1638,16 @@ namespace winrt::TestComponentCSharp::implementation
return winrt::xaml_typename<winrt::TestComponentCSharp::Class>();
}

WF::IInspectable Class::BoxedType()
{
return winrt::box_value(winrt::xaml_typename<winrt::TestComponentCSharp::Class>());
}

IVector<Windows::UI::Xaml::Interop::TypeName> Class::ListOfTypes()
{
return single_threaded_vector<Windows::UI::Xaml::Interop::TypeName>({ winrt::xaml_typename<winrt::TestComponentCSharp::Class>(), winrt::xaml_typename<IReference<int32_t>>() });
}

bool Class::VerifyTypeIsInt32Type(TypeName const& type_name)
{
return winrt::xaml_typename<int32_t>() == type_name;
Expand Down
4 changes: 4 additions & 0 deletions src/Tests/TestComponentCSharp/Class.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ namespace winrt::TestComponentCSharp::implementation
static hstring UnboxString(IInspectable const& obj);
static EnumValue UnboxEnum(IInspectable const& obj);
static TestComponentCSharp::ProvideInt UnboxDelegate(IInspectable const& obj);
static Windows::UI::Xaml::Interop::TypeName UnboxType(IInspectable const& obj);
static com_array<int32_t> UnboxInt32Array(IInspectable const& obj);
static com_array<bool> UnboxBooleanArray(IInspectable const& obj);
static com_array<hstring> UnboxStringArray(IInspectable const& obj);
Expand All @@ -384,6 +385,9 @@ namespace winrt::TestComponentCSharp::implementation
static Windows::UI::Xaml::Interop::TypeName Int32Type();
static Windows::UI::Xaml::Interop::TypeName ReferenceInt32Type();
static Windows::UI::Xaml::Interop::TypeName ThisClassType();
static Windows::Foundation::IInspectable BoxedType();
static Windows::Foundation::Collections::IVector<Windows::UI::Xaml::Interop::TypeName> ListOfTypes();

static bool VerifyTypeIsInt32Type(Windows::UI::Xaml::Interop::TypeName const& type_name);
static bool VerifyTypeIsReferenceInt32Type(Windows::UI::Xaml::Interop::TypeName const& type_name);
static bool VerifyTypeIsThisClassType(Windows::UI::Xaml::Interop::TypeName const& type_name);
Expand Down
3 changes: 3 additions & 0 deletions src/Tests/TestComponentCSharp/TestComponentCSharp.idl
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ namespace TestComponentCSharp
static String UnboxString(Object obj);
static ProvideInt UnboxDelegate(Object obj);
static EnumValue UnboxEnum(Object obj);
static Windows.UI.Xaml.Interop.TypeName UnboxType(Object obj);
static Int32[] UnboxInt32Array(Object obj);
static Boolean[] UnboxBooleanArray(Object obj);
static String[] UnboxStringArray(Object obj);
Expand All @@ -420,6 +421,8 @@ namespace TestComponentCSharp
static Windows.UI.Xaml.Interop.TypeName Int32Type { get; };
static Windows.UI.Xaml.Interop.TypeName ThisClassType { get; };
static Windows.UI.Xaml.Interop.TypeName ReferenceInt32Type { get; };
static Object BoxedType{ get; };
static Windows.Foundation.Collections.IVector<Windows.UI.Xaml.Interop.TypeName> ListOfTypes{ get; };

static Boolean VerifyTypeIsInt32Type(Windows.UI.Xaml.Interop.TypeName type);
static Boolean VerifyTypeIsThisClassType(Windows.UI.Xaml.Interop.TypeName type);
Expand Down
14 changes: 14 additions & 0 deletions src/Tests/UnitTest/TestComponentCSharp_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2574,6 +2574,11 @@ public void TestValueBoxing()

EnumValue enumValue = EnumValue.Two;
Assert.Equal(enumValue, Class.UnboxEnum(enumValue));

var type = typeof(EnumValue);
Assert.Equal(type, Class.UnboxType(type));

Assert.Equal(typeof(Class), Class.BoxedType);
}

[Fact]
Expand All @@ -2599,6 +2604,15 @@ public void TestArrayUnboxing()
Assert.Equal(i, (IEnumerable<int>)obj);
}

[Fact]
public void TestListOfTypes()
{
var types = Class.ListOfTypes;
Assert.Equal(2, types.Count);
Assert.Equal(typeof(Class), types[0]);
Assert.Equal(typeof(int?), types[1]);
}

[Fact]
public void PrimitiveTypeInfo()
{
Expand Down
4 changes: 1 addition & 3 deletions src/WinRT.Runtime/Projections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ static Projections()
// They're also very cheap anyway and commonly used, so this keeps things simpler.
RegisterCustomAbiTypeMappingNoLock(typeof(bool), typeof(ABI.System.Boolean), "Boolean");
RegisterCustomAbiTypeMappingNoLock(typeof(char), typeof(ABI.System.Char), "Char");

// Also always register Type, since it's "free" (no associated ABI type to root)
CustomTypeToAbiTypeNameMappings.Add(typeof(Type), "Windows.UI.Xaml.Interop.TypeName");
RegisterCustomAbiTypeMappingNoLock(typeof(Type), typeof(ABI.System.Type), "Windows.UI.Xaml.Interop.TypeName");
manodasanW marked this conversation as resolved.
Show resolved Hide resolved

#if NET
// If default mappings are disabled, we avoid rooting everything by default.
Expand Down