Skip to content

Commit bde474e

Browse files
authored
Add ios pinvoke functional test (#47988)
Contributes to #43865.
1 parent a98fe6c commit bde474e

File tree

6 files changed

+67
-8
lines changed

6 files changed

+67
-8
lines changed

eng/testing/tests.mobile.targets

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<PropertyGroup>
9797
<Optimized>true</Optimized>
9898
<MainLibraryFileName Condition="'$(MainLibraryFileName)' == ''">AppleTestRunner.dll</MainLibraryFileName>
99+
<_MobileIntermediateOutputPath Condition="'$(RunAOTCompilation)' == 'true'">$(IntermediateOutputPath)mobile</_MobileIntermediateOutputPath>
99100
</PropertyGroup>
100101
<ItemGroup>
101102
<AotInputAssemblies Condition="'$(RunAOTCompilation)' == 'true'" Include="$(PublishDir)*.dll" Exclude="$(PublishDir)System.Runtime.WindowsRuntime.dll">
@@ -105,10 +106,13 @@
105106
<BundleAssemblies Condition="'$(RunAOTCompilation)' != 'true'" Include="$(PublishDir)*.dll" />
106107
</ItemGroup>
107108

109+
<MakeDir Directories="$(_MobileIntermediateOutputPath)"
110+
Condition="'$(RunAOTCompilation)' == 'true'"/>
108111
<RemoveDir Directories="$(BundleDir)" />
109112

110113
<MonoAOTCompiler Condition="'$(RunAOTCompilation)' == 'true'"
111114
CompilerBinaryPath="$(MicrosoftNetCoreAppRuntimePackNativeDir)cross\$(PackageRID)\mono-aot-cross"
115+
OutputDir="$(_MobileIntermediateOutputPath)"
112116
Mode="Full"
113117
OutputType="AsmOnly"
114118
Assemblies="@(AotInputAssemblies)"

src/tasks/AppleAppBuilder/Templates/main-console.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ - (void)viewDidLoad {
6464

6565
@end
6666

67+
// called from C#
68+
void
69+
invoke_external_native_api (void (*callback)(void))
70+
{
71+
if (callback)
72+
callback();
73+
}
74+
6775
// can be called from C# to update UI
6876
void
6977
mono_ios_set_summary (const char* value)

src/tests/FunctionalTests/iOS/Simulator/AOT/Program.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
public static class Program
1010
{
1111
[DllImport("__Internal")]
12-
public extern static void mono_ios_append_output (string value);
13-
14-
[DllImport("__Internal")]
15-
public extern static void mono_ios_set_summary (string value);
12+
public static extern void mono_ios_set_summary (string value);
1613

1714
public static async Task<int> Main(string[] args)
1815
{

src/tests/FunctionalTests/iOS/Simulator/Interpreter/Program.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
public static class Program
1010
{
1111
[DllImport("__Internal")]
12-
public extern static void mono_ios_append_output (string value);
13-
14-
[DllImport("__Internal")]
15-
public extern static void mono_ios_set_summary (string value);
12+
public static extern void mono_ios_set_summary (string value);
1613

1714
public static async Task<int> Main(string[] args)
1815
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using System.Runtime.InteropServices;
8+
9+
public static class Program
10+
{
11+
[DllImport("__Internal")]
12+
unsafe private static extern void invoke_external_native_api(delegate* unmanaged<void> callback);
13+
14+
private static int counter = 1;
15+
16+
[UnmanagedCallersOnly]
17+
private static void Callback()
18+
{
19+
counter = 42;
20+
}
21+
22+
[DllImport("__Internal")]
23+
public static extern void mono_ios_set_summary (string value);
24+
25+
public static async Task<int> Main(string[] args)
26+
{
27+
mono_ios_set_summary($"Starting functional test");
28+
unsafe {
29+
delegate* unmanaged<void> unmanagedPtr = &Callback;
30+
invoke_external_native_api(unmanagedPtr);
31+
}
32+
Console.WriteLine("Done!");
33+
await Task.Delay(5000);
34+
35+
return counter;
36+
}
37+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<MonoForceInterpreter>true</MonoForceInterpreter>
5+
<RunAOTCompilation>false</RunAOTCompilation>
6+
<TestRuntime>true</TestRuntime>
7+
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
8+
<MainLibraryFileName>iOS.Simulator.PInvoke.Test.dll</MainLibraryFileName>
9+
<IncludesTestRunner>false</IncludesTestRunner>
10+
<ExpectedExitCode>42</ExpectedExitCode>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<Compile Include="Program.cs" />
15+
</ItemGroup>
16+
</Project>

0 commit comments

Comments
 (0)