-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JIT: Add support for
SwiftIndirectResult
in Swift calling convention (
#103570) Add a test for pinvoke/reverse pinvokes involving `SwiftIndirectResult`, and add the necessary support to make the tests pass in RyuJIT.
- Loading branch information
1 parent
aa0a7e9
commit 35437dd
Showing
11 changed files
with
219 additions
and
34 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
21 changes: 21 additions & 0 deletions
21
src/tests/Interop/Swift/SwiftIndirectResult/CMakeLists.txt
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,21 @@ | ||
project(SwiftIndirectResult) | ||
include ("${CLR_INTEROP_TEST_ROOT}/Interop.cmake") | ||
|
||
set(SOURCE SwiftIndirectResult) | ||
|
||
if (NOT SWIFT_COMPILER_TARGET AND CLR_CMAKE_TARGET_OSX) | ||
set(SWIFT_PLATFORM "macosx") | ||
set(SWIFT_PLATFORM_SUFFIX "") | ||
set(SWIFT_DEPLOYMENT_TARGET ${CMAKE_OSX_DEPLOYMENT_TARGET}) | ||
set(SWIFT_COMPILER_TARGET "${CMAKE_OSX_ARCHITECTURES}-apple-${SWIFT_PLATFORM}${SWIFT_DEPLOYMENT_TARGET}${SWIFT_PLATFORM_SUFFIX}") | ||
endif() | ||
|
||
add_custom_target(${SOURCE} ALL | ||
COMMAND xcrun swiftc -target ${SWIFT_COMPILER_TARGET} -enable-library-evolution -emit-library ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE}.swift -o ${CMAKE_CURRENT_BINARY_DIR}/lib${SOURCE}.dylib | ||
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE}.swift | ||
COMMENT "Generating ${SOURCE} library" | ||
) | ||
|
||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/lib${SOURCE}.dylib | ||
DESTINATION bin | ||
) |
53 changes: 53 additions & 0 deletions
53
src/tests/Interop/Swift/SwiftIndirectResult/SwiftIndirectResult.cs
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,53 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.InteropServices.Swift; | ||
using Xunit; | ||
|
||
public unsafe class SwiftIndirectResultTests | ||
{ | ||
private struct NonFrozenStruct | ||
{ | ||
public int A; | ||
public int B; | ||
public int C; | ||
} | ||
|
||
private const string SwiftLib = "libSwiftIndirectResult.dylib"; | ||
|
||
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })] | ||
[DllImport(SwiftLib, EntryPoint = "$s19SwiftIndirectResult21ReturnNonFrozenStruct1a1b1cAA0efG0Vs5Int32V_A2ItF")] | ||
public static extern void ReturnNonFrozenStruct(SwiftIndirectResult result, int a, int b, int c); | ||
|
||
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })] | ||
[DllImport(SwiftLib, EntryPoint = "$s19SwiftIndirectResult26SumReturnedNonFrozenStruct1fs5Int32VAA0fgH0VyXE_tF")] | ||
public static extern int SumReturnedNonFrozenStruct(delegate* unmanaged[Swift]<SwiftIndirectResult, SwiftSelf, void> func, void* funcContext); | ||
|
||
[Fact] | ||
public static void TestReturnNonFrozenStruct() | ||
{ | ||
// In normal circumstances this instance would have unknown/dynamically determined size. | ||
NonFrozenStruct instance; | ||
ReturnNonFrozenStruct(new SwiftIndirectResult(&instance), 10, 20, 30); | ||
Assert.Equal(10, instance.A); | ||
Assert.Equal(20, instance.B); | ||
Assert.Equal(30, instance.C); | ||
} | ||
|
||
[UnmanagedCallersOnly(CallConvs = new Type[] { typeof(CallConvSwift) })] | ||
private static void ReversePInvokeReturnNonFrozenStruct(SwiftIndirectResult result, SwiftSelf self) | ||
{ | ||
// In normal circumstances this would require using dynamically sized memcpy and members to create the struct. | ||
*(NonFrozenStruct*)result.Value = new NonFrozenStruct { A = 10, B = 20, C = 30 }; | ||
} | ||
|
||
[Fact] | ||
public static void TestSumReturnedNonFrozenStruct() | ||
{ | ||
int result = SumReturnedNonFrozenStruct(&ReversePInvokeReturnNonFrozenStruct, null); | ||
Assert.Equal(60, result); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/tests/Interop/Swift/SwiftIndirectResult/SwiftIndirectResult.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,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<!-- Needed for CLRTestTargetUnsupported, CMakeProjectReference --> | ||
<RequiresProcessIsolation>true</RequiresProcessIsolation> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<!-- Swift interop is supported on Apple platforms only --> | ||
<CLRTestTargetUnsupported Condition="'$(TargetsOSX)' != 'true' and '$(TargetsAppleMobile)' != 'true'">true</CLRTestTargetUnsupported> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="$(MSBuildProjectName).cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="$(TestLibraryProjectPath)" /> | ||
<CMakeProjectReference Include="CMakeLists.txt" /> | ||
</ItemGroup> | ||
</Project> |
18 changes: 18 additions & 0 deletions
18
src/tests/Interop/Swift/SwiftIndirectResult/SwiftIndirectResult.swift
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 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
public struct NonFrozenStruct | ||
{ | ||
let a : Int32; | ||
let b : Int32; | ||
let c : Int32; | ||
} | ||
|
||
public func ReturnNonFrozenStruct(a: Int32, b: Int32, c: Int32) -> NonFrozenStruct { | ||
return NonFrozenStruct(a: a, b: b, c: c) | ||
} | ||
|
||
public func SumReturnedNonFrozenStruct(f: () -> NonFrozenStruct) -> Int32 { | ||
let s = f() | ||
return s.a + s.b + s.c | ||
} |
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