Skip to content

Commit

Permalink
[NSObject] Unify some code between iOS and macOS. (#15302)
Browse files Browse the repository at this point in the history
* Add these methods to shared code so they're available on all platforms
  (they're already available on mobile platforms), since there's no reason to
  exclude them on macOS:
    * NSObject.Init
    * NSObject.Alloc
    * NSObject.InvokeInBackground
* Remove unused usings.
* Move identical code in platform-specific files to shared code.
  • Loading branch information
rolfbjarne authored Jun 27, 2022
1 parent 1fa2efb commit a55557f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 46 deletions.
38 changes: 1 addition & 37 deletions src/Foundation/NSObject.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,15 @@

using System;
using System.Reflection;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

using UIKit;
using ObjCRuntime;

namespace Foundation {
public partial class NSObject : INativeObject
#if !COREBUILD
, IDisposable
#endif
{
public partial class NSObject {
#if !COREBUILD

#if !NET && !WATCH
[Obsolete ("Use 'PlatformAssembly' for easier code sharing across platforms.")]
public readonly static Assembly MonoTouchAssembly = typeof (NSObject).Assembly;
#endif

public static NSObject Alloc (Class kls) {
var h = Messaging.IntPtr_objc_msgSend (kls.Handle, Selector.GetHandle (Selector.Alloc));
return new NSObject (h, true);
}

public void Init () {
if (handle == IntPtr.Zero)
throw new Exception ("you have not allocated the native object");

handle = Messaging.IntPtr_objc_msgSend (handle, Selector.GetHandle ("init"));
}

public static void InvokeInBackground (Action action)
{
// using the parameterized Thread.Start to avoid capturing
// the 'action' parameter (it'll needlessly create an extra
// object).
new System.Threading.Thread ((v) =>
{
((Action) v) ();
})
{
IsBackground = true,
}.Start (action);
}
#endif // !COREBUILD
}
}
Expand Down
8 changes: 1 addition & 7 deletions src/Foundation/NSObject.mac.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,11 @@

using System;
using System.Reflection;
using System.Collections.Generic;
using System.Runtime.InteropServices;

using ObjCRuntime;

namespace Foundation {
public partial class NSObject : INativeObject
#if !COREBUILD
, IDisposable
#endif
{
public partial class NSObject {
#if !COREBUILD

// note: the linker will remove the unrequired `dlopen` calls
Expand Down
33 changes: 31 additions & 2 deletions src/Foundation/NSObject2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ public class NSObjectFlag {
[SupportedOSPlatform ("tvos")]
#endif
[StructLayout (LayoutKind.Sequential)]
public partial class NSObject
public partial class NSObject : INativeObject
#if !COREBUILD
: IEquatable<NSObject>
, IEquatable<NSObject>
, IDisposable
#endif
{
#if !COREBUILD
Expand Down Expand Up @@ -1073,6 +1074,34 @@ public IDisposable AddObserver (NSString key, NSKeyValueObservingOptions options
AddObserver (o, key, options, o.Handle);
return o;
}

public static NSObject Alloc (Class kls)
{
var h = Messaging.IntPtr_objc_msgSend (kls.Handle, Selector.GetHandle (Selector.Alloc));
return new NSObject (h, true);
}

public void Init ()
{
if (handle == IntPtr.Zero)
throw new Exception ("you have not allocated the native object");

handle = Messaging.IntPtr_objc_msgSend (handle, Selector.GetHandle ("init"));
}

public static void InvokeInBackground (Action action)
{
// using the parameterized Thread.Start to avoid capturing
// the 'action' parameter (it'll needlessly create an extra
// object).
new System.Threading.Thread ((v) =>
{
((Action) v) ();
})
{
IsBackground = true,
}.Start (action);
}
#endif // !COREBUILD
}

Expand Down

6 comments on commit a55557f

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 [CI Build] Build failed 🔥

Build failed for the job 'Detect API changes'

Pipeline on Agent
Hash: [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Unable to find the contents for the comment: D:\a\1\s\change-detection\results\gh-comment.md does not exist :fire

Pipeline on Agent
Hash: a55557fd54d3c0be748191d6078effa38120545f [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ [CI Build] Tests on macOS M1 - Mac Big Sur (11.5) failed ❌

Tests on macOS M1 - Mac Big Sur (11.5) failed for unknown reasons.

Pipeline on Agent
Hash: a55557fd54d3c0be748191d6078effa38120545f [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ [CI Build] Tests on macOS Mac Catalina (10.15) failed ❌

Failed tests are:

  • monotouch-test

Pipeline on Agent
Hash: a55557fd54d3c0be748191d6078effa38120545f [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📚 [CI Build] Artifacts 📚

Artifacts were not provided.

Pipeline on Agent XAMMINI-058.Monterey'
Hash: a55557fd54d3c0be748191d6078effa38120545f [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 [CI Build] Test results 🔥

Test results

❌ Tests failed on VSTS: simulator tests

0 tests crashed, 24 tests failed, 123 tests passed.

Failures

❌ introspection tests

1 tests failed, 12 tests passed.
  • introspection/watchOS 32-bits - simulator/Debug (watchOS 6.0): Crashed Known issue: HE0038)

Html Report (VSDrops) Download

❌ linker tests

20 tests failed, 7 tests passed.
  • dont link/Mac [dotnet]/Debug [dotnet]: TimedOut
  • dont link/Mac [dotnet]/Release [dotnet]: TimedOut
  • dont link/Mac Catalyst [dotnet]/Debug [dotnet]: TimedOut
  • dont link/Mac Catalyst [dotnet]/Release [dotnet]: TimedOut
  • link sdk/Mac [dotnet]/Debug [dotnet]: TimedOut
  • link sdk/Mac [dotnet]/Release [dotnet]: TimedOut
  • link sdk/Mac Catalyst [dotnet]/Debug [dotnet]: TimedOut
  • link sdk/Mac Catalyst [dotnet]/Release [dotnet]: TimedOut
  • link all/Mac [dotnet]/Debug [dotnet]: TimedOut
  • link all/Mac [dotnet]/Release [dotnet]: TimedOut
  • link all/Mac Catalyst [dotnet]/Debug [dotnet]: TimedOut
  • link all/Mac Catalyst [dotnet]/Release [dotnet]: TimedOut
  • trimmode copy/Mac [dotnet]/Debug [dotnet]: TimedOut
  • trimmode copy/Mac [dotnet]/Release [dotnet]: TimedOut
  • trimmode copy/Mac Catalyst [dotnet]/Debug [dotnet]: TimedOut
  • trimmode copy/Mac Catalyst [dotnet]/Release [dotnet]: TimedOut
  • trimmode link/Mac [dotnet]/Debug [dotnet]: TimedOut
  • trimmode link/Mac [dotnet]/Release [dotnet]: TimedOut
  • trimmode link/Mac Catalyst [dotnet]/Debug [dotnet]: TimedOut
  • trimmode link/Mac Catalyst [dotnet]/Release [dotnet]: TimedOut

Html Report (VSDrops) Download

❌ msbuild tests

2 tests failed, 0 tests passed.
  • MSBuild tests/Tasks: TimedOut (Execution timed out after 60 minutes.)
  • MSBuild tests/Integration: TimedOut (Execution timed out after 120 minutes.)

Html Report (VSDrops) Download

❌ mtouch tests

1 tests failed, 0 tests passed.
  • MTouch tests/NUnit: Failed (Execution failed with exit code 9)

Html Report (VSDrops) Download

Successes

✅ bcl: All 69 tests passed. Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests: All 1 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 7 tests passed. Html Report (VSDrops) Download
✅ framework: All 3 tests passed. Html Report (VSDrops) Download
✅ generator: All 2 tests passed. Html Report (VSDrops) Download
✅ interdependent_binding_projects: All 2 tests passed. Html Report (VSDrops) Download
✅ install_source: All 1 tests passed. Html Report (VSDrops) Download
✅ mac_binding_project: All 1 tests passed. Html Report (VSDrops) Download
✅ mmp: All 2 tests passed. Html Report (VSDrops) Download
✅ mononative: All 4 tests passed. Html Report (VSDrops) Download
✅ monotouch: All 3 tests passed. Html Report (VSDrops) Download
✅ xammac: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 3 tests passed. Html Report (VSDrops) Download
✅ xtro: All 2 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: [CI build]

Please sign in to comment.