Skip to content
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
12 changes: 6 additions & 6 deletions build/AzurePipelineTemplates/CsWinRT-FunctionalTest-Steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,20 @@ steps:
TargetFolder: $(StagingFolder)\FunctionalTests\${{ functionalTest }}\

- task: MSBuild@1
displayName: Publish ${{ functionalTest }} for AOT (net8.0)
displayName: Publish ${{ functionalTest }} for AOT (net9.0)
condition: and(succeeded(), and(eq(variables['BuildConfiguration'], 'release'), eq(variables['BuildPlatform'], 'x64')))
inputs:
solution: $(Build.SourcesDirectory)\src\Tests\FunctionalTests\${{ functionalTest }}\${{ functionalTest }}.csproj
msbuildArguments: /t:publish /p:CIBuildReason=CI,RuntimeIdentifier=win-$(BuildPlatform),TargetFramework=net8.0,solutiondir=$(Build.SourcesDirectory)\src\,VersionNumber=$(VersionNumber),VersionString=$(Build.BuildNumber),AssemblyVersionNumber=$(WinRT.Runtime.AssemblyVersion),GenerateTestProjection=true,AllowedReferenceRelatedFileExtensions=".xml;.pri;.dll.config;.exe.config"
msbuildArguments: /t:publish /p:CIBuildReason=CI,RuntimeIdentifier=win-$(BuildPlatform),TargetFramework=net9.0,solutiondir=$(Build.SourcesDirectory)\src\,VersionNumber=$(VersionNumber),VersionString=$(Build.BuildNumber),AssemblyVersionNumber=$(WinRT.Runtime.AssemblyVersion),GenerateTestProjection=true,AllowedReferenceRelatedFileExtensions=".xml;.pri;.dll.config;.exe.config"
platform: $(BuildPlatform)
configuration: $(BuildConfiguration)

- task: CmdLine@2
displayName: Run ${{ functionalTest }} for AOT (net8.0)
displayName: Run ${{ functionalTest }} for AOT (net9.0)
condition: and(succeeded(), and(eq(variables['BuildConfiguration'], 'release'), eq(variables['BuildPlatform'], 'x64')))
continueOnError: True
inputs:
workingDirectory: $(Build.SourcesDirectory)\src\Tests\FunctionalTests\${{ functionalTest }}\bin\$(BuildConfiguration)\net8.0\win-$(BuildPlatform)\publish
workingDirectory: $(Build.SourcesDirectory)\src\Tests\FunctionalTests\${{ functionalTest }}\bin\$(BuildConfiguration)\net9.0\win-$(BuildPlatform)\publish
script: |
dir
echo Running ${{ functionalTest }}.exe
Expand All @@ -86,8 +86,8 @@ steps:
exit /b 0

- task: CopyFiles@2
displayName: Copy ${{ functionalTest }} for AOT (net8.0)
displayName: Copy ${{ functionalTest }} for AOT (net9.0)
condition: and(eq(variables['_PublishFunctionalTests'], 'true'), and(eq(variables['BuildConfiguration'], 'release'), eq(variables['BuildPlatform'], 'x64')))
inputs:
SourceFolder: $(Build.SourcesDirectory)\src\Tests\FunctionalTests\${{ functionalTest }}\bin\$(BuildConfiguration)\net8.0\win-$(BuildPlatform)\publish
SourceFolder: $(Build.SourcesDirectory)\src\Tests\FunctionalTests\${{ functionalTest }}\bin\$(BuildConfiguration)\net9.0\win-$(BuildPlatform)\publish
TargetFolder: $(StagingFolder)\FunctionalTests\${{ functionalTest }}\
39 changes: 37 additions & 2 deletions src/Authoring/WinRT.SourceGenerator/AotOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,8 @@ genericParameter is INamedTypeSymbol genericParameterIface &&
genericParameters.Add(new GenericParameter(
ToFullyQualifiedString(genericParameter),
GeneratorHelper.GetAbiType(genericParameter, mapper),
isNullable ? TypeKind.Interface : genericParameter.TypeKind));
isNullable ? TypeKind.Interface : genericParameter.TypeKind,
ComputeTypeFlags(genericParameter, compilation)));
}

genericInterfacesToAddToVtable.Add(new GenericInterface(
Expand Down Expand Up @@ -774,6 +775,25 @@ void CheckForInterfaceToUseForRuntimeClassName(INamedTypeSymbol iface)
}
}

private static TypeFlags ComputeTypeFlags(ITypeSymbol symbol, Compilation compilation)
{
TypeFlags typeFlags = TypeFlags.None;

// Check for exception types
if (symbol.TypeKind is TypeKind.Class)
{
var exceptionType = compilation.GetTypeByMetadataName("System.Exception")!;

if (SymbolEqualityComparer.Default.Equals(symbol, exceptionType) ||
symbol.InheritsFromType(exceptionType))
{
typeFlags |= TypeFlags.Exception;
}
}

return typeFlags;
}

private static bool TryGetCompatibleWindowsRuntimeTypesForVariantType(INamedTypeSymbol type, TypeMapper mapper, Stack<INamedTypeSymbol> typeStack, Func<ISymbol, TypeMapper, bool> isWinRTType, INamedTypeSymbol objectType, out IList<INamedTypeSymbol> compatibleTypes)
{
compatibleTypes = null;
Expand Down Expand Up @@ -1919,7 +1939,8 @@ namespace {{bindableCustomProperties.Namespace}}
internal readonly record struct GenericParameter(
string ProjectedType,
string AbiType,
TypeKind TypeKind);
TypeKind TypeKind,
TypeFlags TypeFlags);

internal readonly record struct GenericInterface(
string Interface,
Expand Down Expand Up @@ -1969,6 +1990,20 @@ internal readonly record struct CsWinRTAotOptimizerProperties(
bool IsCsWinRTCcwLookupTableGeneratorEnabled,
bool IsCsWinRTAotOptimizerInAutoMode);

/// <summary>
/// Additional flags for discovered types.
/// </summary>
[Flags]
internal enum TypeFlags
{
None = 0x0,

/// <summary>
/// The type derives from <see cref="System.Exception"/>.
/// </summary>
Exception = 0x1 << 0
}

/// <summary>
/// A model describing a type info in a type hierarchy.
/// </summary>
Expand Down
23 changes: 23 additions & 0 deletions src/Authoring/WinRT.SourceGenerator/Extensions/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,27 @@ public static bool IsAccessibleFromCompilationAssembly(this ISymbol symbol, Comp
{
return compilation.IsSymbolAccessibleWithin(symbol, compilation.Assembly);
}

/// <summary>
/// Checks whether or not a given <see cref="ITypeSymbol"/> inherits from a specified type.
/// </summary>
/// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param>
/// <param name="baseTypeSymbol">The <see cref="ITypeSymbol"/> instane to check for inheritance from.</param>
/// <returns>Whether or not <paramref name="typeSymbol"/> inherits from <paramref name="baseTypeSymbol"/>.</returns>
public static bool InheritsFromType(this ITypeSymbol typeSymbol, ITypeSymbol baseTypeSymbol)
{
INamedTypeSymbol? currentBaseTypeSymbol = typeSymbol.BaseType;

while (currentBaseTypeSymbol is not null)
{
if (SymbolEqualityComparer.Default.Equals(currentBaseTypeSymbol, baseTypeSymbol))
{
return true;
}

currentBaseTypeSymbol = currentBaseTypeSymbol.BaseType;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@ public static string GetInstantiation(string genericInterface, EquatableArray<Ge
}
else if (genericInterface == "System.Collections.Generic.IList`1")
{
return GetIListInstantiation(genericParameters[0].ProjectedType, genericParameters[0].AbiType, genericParameters[0].TypeKind);
return GetIListInstantiation(
genericParameters[0].ProjectedType,
genericParameters[0].AbiType,
genericParameters[0].TypeKind,
genericParameters[0].TypeFlags);
}
else if (genericInterface == "System.Collections.Generic.IReadOnlyList`1")
{
return GetIReadOnlyListInstantiation(genericParameters[0].ProjectedType, genericParameters[0].AbiType, genericParameters[0].TypeKind);
return GetIReadOnlyListInstantiation(
genericParameters[0].ProjectedType,
genericParameters[0].AbiType,
genericParameters[0].TypeKind,
genericParameters[0].TypeFlags);
}
else if (genericInterface == "System.Collections.Generic.IDictionary`2")
{
Expand All @@ -46,7 +54,11 @@ public static string GetInstantiation(string genericInterface, EquatableArray<Ge
}
else if (genericInterface == "System.Collections.Generic.IEnumerator`1")
{
return GetIEnumeratorInstantiation(genericParameters[0].ProjectedType, genericParameters[0].AbiType, genericParameters[0].TypeKind);
return GetIEnumeratorInstantiation(
genericParameters[0].ProjectedType,
genericParameters[0].AbiType,
genericParameters[0].TypeKind,
genericParameters[0].TypeFlags);
}
else if (genericInterface == "System.Collections.Generic.KeyValuePair`2")
{
Expand Down Expand Up @@ -222,7 +234,7 @@ private static unsafe int Do_Abi_First_0(IntPtr thisPtr, IntPtr* __return_value_
return iEnumerableInstantiation;
}

private static string GetIEnumeratorInstantiation(string genericType, string abiType, TypeKind typeKind)
private static string GetIEnumeratorInstantiation(string genericType, string abiType, TypeKind typeKind, TypeFlags typeFlags)
{
string iEnumeratorInstantiation = $$"""
internal static class IEnumerator_{{GeneratorHelper.EscapeTypeNameForIdentifier(genericType)}}
Expand Down Expand Up @@ -271,7 +283,7 @@ private static unsafe int Do_Abi_GetMany_3(IntPtr thisPtr, int __itemsSize, IntP
try
{
____return_value__ = global::ABI.System.Collections.Generic.IEnumeratorMethods<{{genericType}}>.Abi_GetMany_3(thisPtr, ref __items);
{{GeneratorHelper.GetCopyManagedArrayMarshaler(genericType, abiType, typeKind)}}.CopyManagedArray(__items, items);
{{GeneratorHelper.GetCopyManagedArrayMarshaler(genericType, abiType, typeKind, typeFlags)}}.CopyManagedArray(__items, items);
*__return_value__ = ____return_value__;
}
catch (global::System.Exception __exception__)
Expand Down Expand Up @@ -326,7 +338,7 @@ private static unsafe int Do_Abi_get_HasCurrent_1(IntPtr thisPtr, byte* __return
return iEnumeratorInstantiation;
}

private static string GetIListInstantiation(string genericType, string abiType, TypeKind typeKind)
private static string GetIListInstantiation(string genericType, string abiType, TypeKind typeKind, TypeFlags typeFlags)
{
string iListInstantiation = $$"""
internal static class IList_{{GeneratorHelper.EscapeTypeNameForIdentifier(genericType)}}
Expand Down Expand Up @@ -513,7 +525,7 @@ private static unsafe int Do_Abi_GetMany_10(IntPtr thisPtr, uint startIndex, int
try
{
____return_value__ = global::ABI.System.Collections.Generic.IListMethods<{{genericType}}>.Abi_GetMany_10(thisPtr, startIndex, ref __items);
{{GeneratorHelper.GetCopyManagedArrayMarshaler(genericType, abiType, typeKind)}}.CopyManagedArray(__items, items);
{{GeneratorHelper.GetCopyManagedArrayMarshaler(genericType, abiType, typeKind, typeFlags)}}.CopyManagedArray(__items, items);
*__return_value__ = ____return_value__;
}
catch (global::System.Exception __exception__)
Expand Down Expand Up @@ -563,7 +575,7 @@ private static unsafe int Do_Abi_get_Size_1(IntPtr thisPtr, uint* __return_value
return iListInstantiation;
}

private static string GetIReadOnlyListInstantiation(string genericType, string abiType, TypeKind typeKind)
private static string GetIReadOnlyListInstantiation(string genericType, string abiType, TypeKind typeKind, TypeFlags typeFlags)
{
string iReadOnlylistInstantiation = $$"""
internal static class IReadOnlyList_{{GeneratorHelper.EscapeTypeNameForIdentifier(genericType)}}
Expand Down Expand Up @@ -633,7 +645,7 @@ private static unsafe int Do_Abi_GetMany_3(IntPtr thisPtr, uint startIndex, int
try
{
____return_value__ = global::ABI.System.Collections.Generic.IReadOnlyListMethods<{{genericType}}>.Abi_GetMany_3(thisPtr, startIndex, ref __items);
{{GeneratorHelper.GetCopyManagedArrayMarshaler(genericType, abiType, typeKind)}}.CopyManagedArray(__items, items);
{{GeneratorHelper.GetCopyManagedArrayMarshaler(genericType, abiType, typeKind, typeFlags)}}.CopyManagedArray(__items, items);
*__return_value__ = ____return_value__;
}
catch (global::System.Exception __exception__)
Expand Down
15 changes: 12 additions & 3 deletions src/Authoring/WinRT.SourceGenerator/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,12 +1064,21 @@ public static string GetFromManagedMarshaler(string type, string abiType, TypeKi
}
}

public static string GetCopyManagedArrayMarshaler(string type, string abiType, TypeKind kind)
public static string GetCopyManagedArrayMarshaler(string type, string abiType, TypeKind kind, TypeFlags typeFlags)
{
if (kind == TypeKind.Class || kind == TypeKind.Delegate)
{
// TODO: Classes and delegates are missing CopyManagedArray.
return $$"""Marshaler<{{type}}>""";
if (type is "System.String") return "global::WinRT.MarshalString";
if (type is "System.Type") return "global::ABI.System.Type";
if (type is "System.Object") return "global::WinRT.MarshalInspectable<object>";

if (typeFlags.HasFlag(TypeFlags.Exception))
{
return "global::WinRT.MarshalNonBlittable<Exception>";
}

// Handles all other classes and delegate types
return $"global::WinRT.MarshalGenericHelper<{type}>";
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<LibBuildTFMsNoNetStandard Condition="'$(CIBuildReason)' == 'CI'">net8.0;net9.0</LibBuildTFMsNoNetStandard>
<TestsBuildTFMs>net8.0-windows10.0.19041.0</TestsBuildTFMs>
<TestsBuildTFMs Condition="'$(CIBuildReason)' == 'CI'">net8.0-windows10.0.19041.0</TestsBuildTFMs>
<FunctionalTestsBuildTFMs>net8.0</FunctionalTestsBuildTFMs>
<FunctionalTestsBuildTFMs>net8.0;net9.0</FunctionalTestsBuildTFMs>
<WindowsAppSDKVerifyWinrtRuntimeVersion>false</WindowsAppSDKVerifyWinrtRuntimeVersion>
<CsWinRTMessageImportance>high</CsWinRTMessageImportance>
</PropertyGroup>
Expand Down
79 changes: 79 additions & 0 deletions src/Tests/FunctionalTests/Collections/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using test_component_derived.Nested;
using TestComponentCSharp;
using Windows.Foundation;

var instance = new Class();

Expand Down Expand Up @@ -40,6 +41,84 @@
return 101;
}

string[] stringArr = new string[] { "apples", "oranges", "pears" };
string[] stringArr2 = new string[stringArr.Length];
string[] outStringArr;
string[] retStringArr = instance2.Array12(stringArr, stringArr2, out outStringArr);
if (!AllEqual(stringArr, stringArr2, outStringArr, retStringArr))
{
return 101;
}

TestComponent.Blittable[] blittableArr = new TestComponent.Blittable[] {
new TestComponent.Blittable(1, 2, 3, 4, -5, -6, -7, 8.0f, 9.0, typeof(TestComponent.ITests).GUID),
new TestComponent.Blittable(10, 20, 30, 40, -50, -60, -70, 80.0f, 90.0, typeof(IStringable).GUID)
};
TestComponent.Blittable[] blittableArr2 = new TestComponent.Blittable[blittableArr.Length];
TestComponent.Blittable[] outBlittableArr;
TestComponent.Blittable[] retBlittableArr = instance2.Array13(blittableArr, blittableArr2, out outBlittableArr);
if (!AllEqual(blittableArr, blittableArr2, outBlittableArr, retBlittableArr))
{
return 101;
}

#if NET9_0_OR_GREATER

TestComponent.NonBlittable[] nonBlittableArr = new TestComponent.NonBlittable[] {
new TestComponent.NonBlittable(false, 'X', "First", (long?)PropertyValue.CreateInt64(123)),
new TestComponent.NonBlittable(true, 'Y', "Second", (long?)PropertyValue.CreateInt64(456)),
new TestComponent.NonBlittable(false, 'Z', "Third", (long?)PropertyValue.CreateInt64(789))
};
TestComponent.NonBlittable[] nonBlittableArr2 = new TestComponent.NonBlittable[nonBlittableArr.Length];
TestComponent.NonBlittable[] outNonBlittableArr;
TestComponent.NonBlittable[] retNonBlittableArr = instance2.Array14(nonBlittableArr, nonBlittableArr2, out outNonBlittableArr);
if (!AllEqual(nonBlittableArr, nonBlittableArr2, outNonBlittableArr, retNonBlittableArr))
{
return 101;
}

TestComponent.Nested[] nestedArr = new TestComponent.Nested[]{
new TestComponent.Nested(
new TestComponent.Blittable(1, 2, 3, 4, -5, -6, -7, 8.0f, 9.0, typeof(TestComponent.ITests).GUID),
new TestComponent.NonBlittable(false, 'X', "First", (long?)PropertyValue.CreateInt64(123))),
new TestComponent.Nested(
new TestComponent.Blittable(10, 20, 30, 40, -50, -60, -70, 80.0f, 90.0, typeof(IStringable).GUID),
new TestComponent.NonBlittable(true, 'Y', "Second", (long?)PropertyValue.CreateInt64(456))),
new TestComponent.Nested(
new TestComponent.Blittable(1, 2, 3, 4, -5, -6, -7, 8.0f, 9.0, typeof(WinRT.IInspectable).GUID),
new TestComponent.NonBlittable(false, 'Z', "Third", (long?)PropertyValue.CreateInt64(789)))
};
TestComponent.Nested[] nestedArr2 = new TestComponent.Nested[nestedArr.Length];
TestComponent.Nested[] outNestedArr;
TestComponent.Nested[] retNestedArr = instance2.Array15(nestedArr, nestedArr2, out outNestedArr);
if (!AllEqual(nestedArr, nestedArr2, outNestedArr, retNestedArr))
{
return 101;
}

EnumValue[] enumArr = new EnumValue[] { EnumValue.One, EnumValue.Two };
instance.EnumsProperty = enumArr;
EnumValue[] retEnumArr = instance.EnumsProperty;
if (!AllEqual(enumArr, retEnumArr))
{
return 101;
}

#endif

IStringable[] stringableArr = new IStringable[] {
Windows.Data.Json.JsonValue.CreateNumberValue(3),
Windows.Data.Json.JsonValue.CreateNumberValue(4),
Windows.Data.Json.JsonValue.CreateNumberValue(5.0)
};
IStringable[] stringableArr2 = new IStringable[stringableArr.Length];
IStringable[] outStringableArr;
IStringable[] retStringableArr = instance2.Array16(stringableArr, stringableArr2, out outStringableArr);
if (!AllEqual(stringableArr, stringableArr2, outStringableArr, retStringableArr))
{
return 101;
}

var hierarchyDAsObjectList = HierarchyC.CreateDerivedHierarchyDAsObjectList();
foreach (var hierarchyDAsObject in hierarchyDAsObjectList)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Authoring\WinRT.SourceGenerator.Roslyn4080\WinRT.SourceGenerator.Roslyn4080.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" SetPlatform="Platform=x64"/>
<ProjectReference Include="..\..\..\Authoring\WinRT.SourceGenerator.Roslyn4120\WinRT.SourceGenerator.Roslyn4120.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" SetPlatform="Platform=x64" />
<ProjectReference Include="..\..\..\Projections\Test\Test.csproj" />
<ProjectReference Include="..\..\..\Projections\Windows\Windows.csproj" />
</ItemGroup>
Expand Down
18 changes: 12 additions & 6 deletions src/Tests/FunctionalTests/JsonValueFunctionCalls/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
// Class function call
result += (int)(a[1] as Windows.Data.Json.JsonValue).GetNumber();

var enumVal = TestComponentCSharp.Class.BoxedEnum;
if (enumVal is TestComponentCSharp.EnumValue val && val == TestComponentCSharp.EnumValue.Two)
{
result += 1;
}
CheckBoxedEnum();

return result == 17 ? 100 : 101;
return result == 17 ? 100 : 101;

[WinRT.DynamicWindowsRuntimeCast(typeof(TestComponentCSharp.EnumValue))]
void CheckBoxedEnum()
{
var enumVal = TestComponentCSharp.Class.BoxedEnum;
if (enumVal is TestComponentCSharp.EnumValue val && val == TestComponentCSharp.EnumValue.Two)
{
result += 1;
}
}
Loading
Loading