Skip to content

Commit

Permalink
Made iOS work w/ right pinvokes defined
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Pfister committed Jan 11, 2021
1 parent e6b89c0 commit 78c888a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 68 deletions.
40 changes: 6 additions & 34 deletions src/tests/FunctionalTests/iOS/Simulator/AOT/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,21 @@
using System.Threading.Tasks;
using System.Runtime.InteropServices;

// it's not part of the BCL but runtime needs it for native-to-managed callbacks in AOT
// To be replaced with NativeCallableAttribute
public class MonoPInvokeCallbackAttribute : Attribute
{
public MonoPInvokeCallbackAttribute(Type delegateType) { }
}

public static class Program
{
// Defined in main.m
// to be wired once https://github.com/dotnet/xharness/pull/46 is merged
[DllImport("__Internal")]
private extern static void ios_set_text(string value);
public extern static void mono_ios_append_output (string value);

[DllImport("__Internal")]
private extern static void ios_register_button_click(Action action);

private static Action buttonClickHandler = null;

private static int counter = 0;

// Called by native code, see main.m
[MonoPInvokeCallback(typeof(Action))]
private static void OnButtonClick()
{
ios_set_text("OnButtonClick! #" + counter++);
}

public extern static void mono_ios_set_summary (string value);

public static async Task<int> Main(string[] args)
{
// Register a managed callback (will be called by UIButton, see main.m)
// Also, keep the handler alive so GC won't collect it.
ios_register_button_click(buttonClickHandler = OnButtonClick);

const string msg = "Hello World!\n.NET 5.0";
for (int i = 0; i < msg.Length; i++)
{
// a kind of an animation
ios_set_text(msg.Substring(0, i + 1));
await Task.Delay(100);
}

mono_ios_set_summary($"Starting functional test");
Console.WriteLine("Done!");
await Task.Delay(5000);

return 42;
}
}
40 changes: 6 additions & 34 deletions src/tests/FunctionalTests/iOS/Simulator/Interpreter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,21 @@
using System.Threading.Tasks;
using System.Runtime.InteropServices;

// it's not part of the BCL but runtime needs it for native-to-managed callbacks in AOT
// To be replaced with NativeCallableAttribute
public class MonoPInvokeCallbackAttribute : Attribute
{
public MonoPInvokeCallbackAttribute(Type delegateType) { }
}

public static class Program
{
// Defined in main.m
// to be wired once https://github.com/dotnet/xharness/pull/46 is merged
[DllImport("__Internal")]
private extern static void ios_set_text(string value);
public extern static void mono_ios_append_output (string value);

[DllImport("__Internal")]
private extern static void ios_register_button_click(Action action);

private static Action buttonClickHandler = null;

private static int counter = 0;

// Called by native code, see main.m
[MonoPInvokeCallback(typeof(Action))]
private static void OnButtonClick()
{
ios_set_text("OnButtonClick! #" + counter++);
}

public extern static void mono_ios_set_summary (string value);

public static async Task<int> Main(string[] args)
{
// Register a managed callback (will be called by UIButton, see main.m)
// Also, keep the handler alive so GC won't collect it.
ios_register_button_click(buttonClickHandler = OnButtonClick);

const string msg = "Hello World!\n.NET 5.0";
for (int i = 0; i < msg.Length; i++)
{
// a kind of an animation
ios_set_text(msg.Substring(0, i + 1));
await Task.Delay(100);
}

mono_ios_set_summary($"Starting functional test");
Console.WriteLine("Done!");
await Task.Delay(5000);

return 42;
}
}

0 comments on commit 78c888a

Please sign in to comment.