-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First iteration of making CsWinRT compatible with IL trimming (#1224)
* Initial attempt at IL trimming compat by using attribute lookup instead of type lookup for helper types. * Remove commented out helper type for custom projected types. * Make Helper type lookup trim friendly * Mark Type FromABI trim unfriendly for now to see impact * Make test projections trimmable * Add more annotations to support trimming and handle trimmed types. * Add functional tests * Update testwinrt * Remove attributes from runtime class name as it is not an qualified type name and attempt to address issue where Xaml metadata providers asks for trimmed types. * Experiment with symbols * Fix WinUI type lookup from base class being given as a type. * Fix interfaces being trimmed affecting CCW / vtables on managed objects * Propgate more attributes. * Fix build * Fix issue with eventhandler getting trimmed * Enable trimming of WinUI projection * Update attribute to reflect that we handle trimmed types. * Add more tests * Update testwinrt and add supress for MakeGenericType in nullable * Run functional tests in pipeline * Move publishing to build * Fix tests * Fix build break * Fix build * Fix build * Revert file with no changes. * Remove accidental commit * PR feedback * Fix base type lookup not being recursive * Fix casing * Try removing NET6_0
- Loading branch information
1 parent
1ad15ea
commit d6fb9c4
Showing
73 changed files
with
1,653 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Platforms>x86;x64</Platforms> | ||
<RuntimeIdentifiers>win10-x86;win10-x64</RuntimeIdentifiers> | ||
<PublishProfileFullPath>$(MSBuildProjectDirectory)\..\PublishProfiles\win10-$(Platform).pubxml</PublishProfileFullPath> | ||
<SimulateCsWinRTNugetReference>true</SimulateCsWinRTNugetReference> | ||
<CsWinRTEnabled>false</CsWinRTEnabled> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Projections\Test\Test.csproj" /> | ||
<ProjectReference Include="..\..\..\Projections\Windows\Windows.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using TestComponentCSharp; | ||
|
||
var instance = new Class(); | ||
|
||
instance.IntProperty = 12; | ||
var async_get_int = instance.GetIntAsync(); | ||
int async_int = 0; | ||
async_get_int.Completed = (info, status) => async_int = info.GetResults(); | ||
async_get_int.GetResults(); | ||
|
||
if (async_int != 12) | ||
{ | ||
return 101; | ||
} | ||
|
||
instance.StringProperty = "foo"; | ||
var async_get_string = instance.GetStringAsync(); | ||
string async_string = ""; | ||
async_get_string.Completed = (info, status) => async_string = info.GetResults(); | ||
int async_progress; | ||
async_get_string.Progress = (info, progress) => async_progress = progress; | ||
async_get_string.GetResults(); | ||
|
||
if (async_string != "foo") | ||
{ | ||
return 102; | ||
} | ||
|
||
var task = InvokeAddAsync(instance, 20, 10); | ||
if (task.Wait(25)) | ||
{ | ||
return 103; | ||
} | ||
|
||
instance.CompleteAsync(); | ||
if (!task.Wait(1000)) | ||
{ | ||
return 104; | ||
} | ||
|
||
if (task.Status != TaskStatus.RanToCompletion || task.Result != 30) | ||
{ | ||
return 105; | ||
} | ||
|
||
var ports = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync( | ||
Windows.Devices.SerialCommunication.SerialDevice.GetDeviceSelector(), | ||
new string[] { "System.ItemNameDisplay" }); | ||
foreach (var port in ports) | ||
{ | ||
object o = port.Properties["System.ItemNameDisplay"]; | ||
if (o is null) | ||
{ | ||
return 106; | ||
} | ||
} | ||
|
||
return 100; | ||
|
||
static async Task<int> InvokeAddAsync(Class instance, int lhs, int rhs) | ||
{ | ||
return await instance.AddAsync(lhs, rhs); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Platforms>x86;x64</Platforms> | ||
<RuntimeIdentifiers>win10-x86;win10-x64</RuntimeIdentifiers> | ||
<PublishProfileFullPath>$(MSBuildProjectDirectory)\..\PublishProfiles\win10-$(Platform).pubxml</PublishProfileFullPath> | ||
<SimulateCsWinRTNugetReference>true</SimulateCsWinRTNugetReference> | ||
<CsWinRTEnabled>false</CsWinRTEnabled> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Projections\Test\Test.csproj" /> | ||
<ProjectReference Include="..\..\..\Projections\Windows\Windows.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using TestComponentCSharp; | ||
using WinRT.Interop; | ||
using WinRT; | ||
|
||
var managedProperties = new ManagedProperties(42); | ||
var instance = new Class(); | ||
|
||
// Ensure we can use the IProperties interface from the native side. | ||
instance.CopyProperties(managedProperties); | ||
if (managedProperties.ReadWriteProperty != instance.ReadWriteProperty) | ||
{ | ||
return 101; | ||
} | ||
|
||
// Check for the default interfaces provided by WinRT.Runtime | ||
Guid IID_IMarshal = new Guid("00000003-0000-0000-c000-000000000046"); | ||
IObjectReference ccw = MarshalInterface<IProperties1>.CreateMarshaler(managedProperties); | ||
ccw.TryAs<IUnknownVftbl>(IID_IMarshal, out var marshalCCW); | ||
if (marshalCCW == null) | ||
{ | ||
return 102; | ||
} | ||
|
||
// Check for managed implemented interface to ensure not trimmed. | ||
Guid IID_IUriHandler = new Guid("FF4B4334-2104-537D-812E-67E3856AC7A2"); | ||
ccw.TryAs<IUnknownVftbl>(IID_IUriHandler, out var uriHandlerCCW); | ||
if (uriHandlerCCW == null) | ||
{ | ||
return 103; | ||
} | ||
|
||
// Ensure that interfaces on the vtable / object don't get trimmed even if unused. | ||
Guid IID_IWarning1 = new Guid("4DB3FA26-4BB1-50EA-8362-98F49651E516"); | ||
Guid IID_IWarningClassOverrides = new Guid("E5635CE4-D483-55AA-86D5-080DC07F0A09"); | ||
|
||
var managedWarningClass = new ManagedWarningClass(); | ||
ccw = MarshalInterface<IUriHandler>.CreateMarshaler(managedWarningClass); | ||
ccw.TryAs<IUnknownVftbl>(IID_IWarning1, out var warningCCW); | ||
if (warningCCW == null) | ||
{ | ||
return 104; | ||
} | ||
|
||
ccw.TryAs<IUnknownVftbl>(IID_IWarningClassOverrides, out var warningOverrideCCW); | ||
if (warningOverrideCCW == null) | ||
{ | ||
return 105; | ||
} | ||
|
||
return 100; | ||
|
||
sealed class ManagedProperties : IProperties1, IUriHandler | ||
{ | ||
private readonly int _value; | ||
|
||
public ManagedProperties(int value) | ||
{ | ||
_value = value; | ||
} | ||
|
||
public int ReadWriteProperty => _value; | ||
|
||
public void AddUriHandler(ProvideUri provideUri) | ||
{ | ||
_ = provideUri(); | ||
} | ||
|
||
void IUriHandler.AddUriHandler(ProvideUri provideUri) => AddUriHandler(provideUri); | ||
} | ||
|
||
sealed class ManagedWarningClass : WarningClass, IUriHandler | ||
{ | ||
public void AddUriHandler(ProvideUri provideUri) | ||
{ | ||
_ = provideUri(); | ||
} | ||
|
||
void IUriHandler.AddUriHandler(ProvideUri provideUri) => AddUriHandler(provideUri); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Tests/FunctionalTests/ClassActivation/ClassActivation.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Platforms>x86;x64</Platforms> | ||
<RuntimeIdentifiers>win10-x86;win10-x64</RuntimeIdentifiers> | ||
<PublishProfileFullPath>$(MSBuildProjectDirectory)\..\PublishProfiles\win10-$(Platform).pubxml</PublishProfileFullPath> | ||
<SimulateCsWinRTNugetReference>true</SimulateCsWinRTNugetReference> | ||
<CsWinRTEnabled>false</CsWinRTEnabled> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Projections\Test\Test.csproj" /> | ||
<ProjectReference Include="..\..\..\Projections\Windows\Windows.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using TestComponentCSharp; | ||
|
||
// new RCW / Factory activation | ||
var instance = new Class(); | ||
|
||
var expectedEnum = EnumValue.Two; | ||
instance.EnumProperty = expectedEnum; | ||
|
||
// Custom type marshaling | ||
var expectedUri = new Uri("http://expected"); | ||
instance.UriProperty = expectedUri; | ||
|
||
var instance2 = new Class(32); | ||
|
||
return instance.EnumProperty == expectedEnum && | ||
instance.UriProperty == expectedUri && | ||
instance2.IntProperty == 32 ? 100 : 101; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Platforms>x86;x64</Platforms> | ||
<RuntimeIdentifiers>win10-x86;win10-x64</RuntimeIdentifiers> | ||
<PublishProfileFullPath>$(MSBuildProjectDirectory)\..\PublishProfiles\win10-$(Platform).pubxml</PublishProfileFullPath> | ||
<SimulateCsWinRTNugetReference>true</SimulateCsWinRTNugetReference> | ||
<CsWinRTEnabled>false</CsWinRTEnabled> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Projections\Test\Test.csproj" /> | ||
<ProjectReference Include="..\..\..\Projections\Windows\Windows.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.