Skip to content

Commit

Permalink
[src] Make all generated P/Invokes use blittable signatures. Fixes xa…
Browse files Browse the repository at this point in the history
…marin#18685.

This means:

* Change all bool and char arguments in P/Invokes to be byte and ushort, respectively.
* Change all out/ref arguments to be pointers instead.
* Update managed binding code accordingly.
* Update a struct (GKTriangle) to not use a MarshalAs field, but instead only use blittable fields.
* Update tests accordingly.

Fixes xamarin#18685.
  • Loading branch information
rolfbjarne committed Sep 18, 2023
1 parent 703ee68 commit 98d8d45
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 956 deletions.
2 changes: 1 addition & 1 deletion src/Contacts/CNContactFetchRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public CNContactFetchRequest (params INativeObject [] keysToFetch)
// NSObject.ConformsToProtocol won't work for *Wrapper types, like what returns ICNKeyDescriptor instances
static bool ConformsToProtocol (IntPtr handle, IntPtr protocol)
{
return Messaging.bool_objc_msgSend_IntPtr (handle, Selector.GetHandle ("conformsToProtocol:"), protocol);
return Messaging.bool_objc_msgSend_IntPtr (handle, Selector.GetHandle ("conformsToProtocol:"), protocol) != 0;
}

static NSArray Validate (params INativeObject [] keysToFetch)
Expand Down
4 changes: 2 additions & 2 deletions src/Foundation/NSLayoutConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ static public NSLayoutConstraint [] FromVisualFormat (string format, NSLayoutFor
views = new NSMutableDictionary ();
views [nskey] = (NSObject) value;
continue;
} else if (value is INativeObject && Messaging.bool_objc_msgSend_IntPtr (((INativeObject) value).Handle, Selector.GetHandle ("isKindOfClass:"), Class.GetHandle (typeof (View)))) {
} else if (value is INativeObject && Messaging.bool_objc_msgSend_IntPtr (((INativeObject) value).Handle, Selector.GetHandle ("isKindOfClass:"), Class.GetHandle (typeof (View))) != 0) {
if (views is null)
views = new NSMutableDictionary ();
views.LowlevelSetObject (((INativeObject) value).Handle, nskey.Handle);
continue;
}
#if !MONOMAC
// This requires UILayoutSupport class which is not exist on Mac
else if (value is INativeObject && Messaging.bool_objc_msgSend_IntPtr (((INativeObject) value).Handle, Selector.GetHandle ("conformsToProtocol:"), Protocol.GetHandle (typeof (UILayoutSupport).Name))) {
else if (value is INativeObject && Messaging.bool_objc_msgSend_IntPtr (((INativeObject) value).Handle, Selector.GetHandle ("conformsToProtocol:"), Protocol.GetHandle (typeof (UILayoutSupport).Name)) != 0) {
if (views is null)
views = new NSMutableDictionary ();
views.LowlevelSetObject (((INativeObject) value).Handle, nskey.Handle);
Expand Down
44 changes: 22 additions & 22 deletions src/Foundation/NSObject2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,15 +472,15 @@ public virtual bool ConformsToProtocol (NativeHandle protocol)

#if MONOMAC
if (is_wrapper) {
does = Messaging.bool_objc_msgSend_IntPtr (this.Handle, selConformsToProtocolHandle, protocol);
does = Messaging.bool_objc_msgSend_IntPtr (this.Handle, selConformsToProtocolHandle, protocol) != 0;
} else {
does = Messaging.bool_objc_msgSendSuper_IntPtr (this.SuperHandle, selConformsToProtocolHandle, protocol);
does = Messaging.bool_objc_msgSendSuper_IntPtr (this.SuperHandle, selConformsToProtocolHandle, protocol) != 0;
}
#else
if (is_wrapper) {
does = Messaging.bool_objc_msgSend_IntPtr (this.Handle, Selector.GetHandle (selConformsToProtocol), protocol);
does = Messaging.bool_objc_msgSend_IntPtr (this.Handle, Selector.GetHandle (selConformsToProtocol), protocol) != 0;
} else {
does = Messaging.bool_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle (selConformsToProtocol), protocol);
does = Messaging.bool_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle (selConformsToProtocol), protocol) != 0;
}
#endif

Expand Down Expand Up @@ -710,12 +710,12 @@ public void SetNativeField (string name, NSObject value)
private void InvokeOnMainThread (Selector sel, NSObject obj, bool wait)
{
#if NET
Messaging.void_objc_msgSend_NativeHandle_NativeHandle_bool (this.Handle, Selector.GetHandle (Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDone), sel.Handle, obj.GetHandle (), wait);
Messaging.void_objc_msgSend_NativeHandle_NativeHandle_bool (this.Handle, Selector.GetHandle (Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDone), sel.Handle, obj.GetHandle (), wait ? (byte) 1 : (byte) 0);
#else
#if MONOMAC
Messaging.void_objc_msgSend_IntPtr_IntPtr_bool (this.Handle, Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDoneHandle, sel.Handle, obj.GetHandle (), wait);
Messaging.void_objc_msgSend_IntPtr_IntPtr_bool (this.Handle, Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDoneHandle, sel.Handle, obj.GetHandle (), wait ? (byte) 1 : (byte) 0);
#else
Messaging.void_objc_msgSend_IntPtr_IntPtr_bool (this.Handle, Selector.GetHandle (Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDone), sel.Handle, obj.GetHandle (), wait);
Messaging.void_objc_msgSend_IntPtr_IntPtr_bool (this.Handle, Selector.GetHandle (Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDone), sel.Handle, obj.GetHandle (), wait ? (byte) 1 : (byte) 0);
#endif
#endif
}
Expand All @@ -735,14 +735,14 @@ public void BeginInvokeOnMainThread (Action action)
var d = new NSAsyncActionDispatcher (action);
#if NET
Messaging.void_objc_msgSend_NativeHandle_NativeHandle_bool (d.Handle, Selector.GetHandle (Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDone),
NSDispatcher.Selector.Handle, d.Handle, false);
NSDispatcher.Selector.Handle, d.Handle, 0);
#else
#if MONOMAC
Messaging.void_objc_msgSend_IntPtr_IntPtr_bool (d.Handle, Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDoneHandle,
NSDispatcher.Selector.Handle, d.Handle, false);
NSDispatcher.Selector.Handle, d.Handle, 0);
#else
Messaging.void_objc_msgSend_IntPtr_IntPtr_bool (d.Handle, Selector.GetHandle (Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDone),
Selector.GetHandle (NSDispatcher.SelectorName), d.Handle, false);
Selector.GetHandle (NSDispatcher.SelectorName), d.Handle, 0);
#endif
#endif
}
Expand All @@ -752,14 +752,14 @@ internal void BeginInvokeOnMainThread (System.Threading.SendOrPostCallback cb, o
var d = new NSAsyncSynchronizationContextDispatcher (cb, state);
#if NET
Messaging.void_objc_msgSend_NativeHandle_NativeHandle_bool (d.Handle, Selector.GetHandle (Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDone),
Selector.GetHandle (NSDispatcher.SelectorName), d.Handle, false);
Selector.GetHandle (NSDispatcher.SelectorName), d.Handle, 0);
#else
#if MONOMAC
Messaging.void_objc_msgSend_IntPtr_IntPtr_bool (d.Handle, Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDoneHandle,
NSDispatcher.Selector.Handle, d.Handle, false);
NSDispatcher.Selector.Handle, d.Handle, 0);
#else
Messaging.void_objc_msgSend_IntPtr_IntPtr_bool (d.Handle, Selector.GetHandle (Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDone),
Selector.GetHandle (NSDispatcher.SelectorName), d.Handle, false);
Selector.GetHandle (NSDispatcher.SelectorName), d.Handle, 0);
#endif
#endif
}
Expand All @@ -769,14 +769,14 @@ public void InvokeOnMainThread (Action action)
using (var d = new NSActionDispatcher (action)) {
#if NET
Messaging.void_objc_msgSend_NativeHandle_NativeHandle_bool (d.Handle, Selector.GetHandle (Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDone),
Selector.GetHandle (NSDispatcher.SelectorName), d.Handle, true);
Selector.GetHandle (NSDispatcher.SelectorName), d.Handle, 1);
#else
#if MONOMAC
Messaging.void_objc_msgSend_IntPtr_IntPtr_bool (d.Handle, Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDoneHandle,
NSDispatcher.Selector.Handle, d.Handle, true);
NSDispatcher.Selector.Handle, d.Handle, 1);
#else
Messaging.void_objc_msgSend_IntPtr_IntPtr_bool (d.Handle, Selector.GetHandle (Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDone),
Selector.GetHandle (NSDispatcher.SelectorName), d.Handle, true);
Selector.GetHandle (NSDispatcher.SelectorName), d.Handle, 1);
#endif
#endif
}
Expand All @@ -787,14 +787,14 @@ internal void InvokeOnMainThread (System.Threading.SendOrPostCallback cb, object
using (var d = new NSSynchronizationContextDispatcher (cb, state)) {
#if NET
Messaging.void_objc_msgSend_NativeHandle_NativeHandle_bool (d.Handle, Selector.GetHandle (Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDone),
Selector.GetHandle (NSDispatcher.SelectorName), d.Handle, true);
Selector.GetHandle (NSDispatcher.SelectorName), d.Handle, 1);
#else
#if MONOMAC
Messaging.void_objc_msgSend_IntPtr_IntPtr_bool (d.Handle, Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDoneHandle,
NSDispatcher.Selector.Handle, d.Handle, true);
NSDispatcher.Selector.Handle, d.Handle, 1);
#else
Messaging.void_objc_msgSend_IntPtr_IntPtr_bool (d.Handle, Selector.GetHandle (Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDone),
Selector.GetHandle (NSDispatcher.SelectorName), d.Handle, true);
Selector.GetHandle (NSDispatcher.SelectorName), d.Handle, 1);
#endif
#endif
}
Expand Down Expand Up @@ -1017,12 +1017,12 @@ static internal void Add (NSObject handle)
static void ScheduleDrain ()
{
#if NET
Messaging.void_objc_msgSend_NativeHandle_NativeHandle_bool (class_ptr, Selector.GetHandle (Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDone), Selector.GetHandle ("drain:"), NativeHandle.Zero, false);
Messaging.void_objc_msgSend_NativeHandle_NativeHandle_bool (class_ptr, Selector.GetHandle (Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDone), Selector.GetHandle ("drain:"), NativeHandle.Zero, 0);
#else
#if MONOMAC
Messaging.void_objc_msgSend_IntPtr_IntPtr_bool (class_ptr, Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDoneHandle, drainHandle, IntPtr.Zero, false);
Messaging.void_objc_msgSend_IntPtr_IntPtr_bool (class_ptr, Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDoneHandle, drainHandle, IntPtr.Zero, 0);
#else
Messaging.void_objc_msgSend_IntPtr_IntPtr_bool (class_ptr, Selector.GetHandle (Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDone), Selector.GetHandle ("drain:"), IntPtr.Zero, false);
Messaging.void_objc_msgSend_IntPtr_IntPtr_bool (class_ptr, Selector.GetHandle (Selector.PerformSelectorOnMainThreadWithObjectWaitUntilDone), Selector.GetHandle ("drain:"), IntPtr.Zero, 0);
#endif
#endif
}
Expand Down
8 changes: 4 additions & 4 deletions src/Foundation/NSSecureCoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ internal static bool SupportsSecureCoding (IntPtr ptr)
if (secure_coding == IntPtr.Zero)
return false;
#if MONOMAC
if (!Messaging.bool_objc_msgSend_IntPtr (ptr, Selector.GetHandle ("conformsToProtocol:"), secure_coding))
if (Messaging.bool_objc_msgSend_IntPtr (ptr, Selector.GetHandle ("conformsToProtocol:"), secure_coding) == 0)
return false;

return Messaging.bool_objc_msgSend (ptr, Selector.GetHandle ("supportsSecureCoding"));
return Messaging.bool_objc_msgSend (ptr, Selector.GetHandle ("supportsSecureCoding")) != 0;
#else
if (!Messaging.bool_objc_msgSend_IntPtr (ptr, selConformsToProtocolHandle, secure_coding))
if (Messaging.bool_objc_msgSend_IntPtr (ptr, selConformsToProtocolHandle, secure_coding) == 0)
return false;

return Messaging.bool_objc_msgSend (ptr, selSupportsSecureCodingHandle);
return Messaging.bool_objc_msgSend (ptr, selSupportsSecureCodingHandle) != 0;
#endif
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Foundation/NSString2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public partial class NSString : IComparable<NSString> {
public NSData Encode (NSStringEncoding enc, bool allowLossyConversion = false)
{
#if NET
return new NSData (Messaging.NativeHandle_objc_msgSend_NativeHandle_bool (Handle, Selector.GetHandle (selDataUsingEncodingAllow), (IntPtr) (int) enc, allowLossyConversion));
return new NSData (Messaging.NativeHandle_objc_msgSend_NativeHandle_bool (Handle, Selector.GetHandle (selDataUsingEncodingAllow), (IntPtr) (int) enc, allowLossyConversion ? (byte) 1 : (byte) 0));
#else
#if MONOMAC
return new NSData (Messaging.IntPtr_objc_msgSend_IntPtr_bool (Handle, selDataUsingEncodingAllowHandle, (IntPtr) (int) enc, allowLossyConversion));
return new NSData (Messaging.IntPtr_objc_msgSend_IntPtr_bool (Handle, selDataUsingEncodingAllowHandle, (IntPtr) (int) enc, allowLossyConversion ? (byte) 1 : (byte) 0));
#else
return new NSData (Messaging.IntPtr_objc_msgSend_IntPtr_bool (Handle, Selector.GetHandle (selDataUsingEncodingAllow), (IntPtr) (int) enc, allowLossyConversion));
return new NSData (Messaging.IntPtr_objc_msgSend_IntPtr_bool (Handle, Selector.GetHandle (selDataUsingEncodingAllow), (IntPtr) (int) enc, allowLossyConversion ? (byte) 1 : (byte) 0));
#endif
#endif
}
Expand Down
11 changes: 7 additions & 4 deletions src/GameplayKit/GKPrimitives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,21 @@ public struct GKQuad {
#endif
[StructLayout (LayoutKind.Sequential)]
public struct GKTriangle {
[MarshalAs (UnmanagedType.ByValArray, SizeConst = 3)]
Vector3 [] points;
Vector3 point1;
Vector3 point2;
Vector3 point3;
public Vector3 [] Points {
get {
return points ?? (points = new Vector3 [3]);
return new Vector3 [] { point1, point2, point3 };
}
set {
if (value is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (value));
if (value.Length != 3)
throw new ArgumentOutOfRangeException (nameof (value), "The length of the Value array must be 3");
points = value;
point1 = value [0];
point2 = value [1];
point3 = value [2];
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Metal/MTLCompat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public MTLSharedTextureHandle () { }
}

#if MONOMAC
public static partial class MTLDevice_Extensions {
public unsafe static partial class MTLDevice_Extensions {
[BindingImpl (BindingImplOptions.Optimizable)]
public static IMTLCounterSet[] GetIMTLCounterSets (this IMTLDevice This)
{
Expand All @@ -33,7 +33,7 @@ public static IMTLCounterSet[] GetIMTLCounterSets (this IMTLDevice This)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (descriptor));
var errorValue = NativeHandle.Zero;

var rv = global::ObjCRuntime.Messaging.IntPtr_objc_msgSend_IntPtr_ref_IntPtr (This.Handle, Selector.GetHandle ("newCounterSampleBufferWithDescriptor:error:"), descriptor.Handle, ref errorValue);
var rv = global::ObjCRuntime.Messaging.IntPtr_objc_msgSend_IntPtr_ref_IntPtr (This.Handle, Selector.GetHandle ("newCounterSampleBufferWithDescriptor:error:"), descriptor.Handle, &errorValue);
var ret = Runtime.GetINativeObject<IMTLCounterSampleBuffer> (rv, owns: false);
error = Runtime.GetNSObject<NSError> (errorValue);

Expand All @@ -48,7 +48,7 @@ public static void SampleCounters (this IMTLComputeCommandEncoder This, IMTLCoun
{
if (sampleBuffer is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (sampleBuffer));
global::ObjCRuntime.Messaging.void_objc_msgSend_IntPtr_UIntPtr_bool (This.Handle, Selector.GetHandle ("sampleCountersInBuffer:atSampleIndex:withBarrier:"), sampleBuffer.Handle, (UIntPtr) sampleIndex, barrier);
global::ObjCRuntime.Messaging.void_objc_msgSend_IntPtr_UIntPtr_bool (This.Handle, Selector.GetHandle ("sampleCountersInBuffer:atSampleIndex:withBarrier:"), sampleBuffer.Handle, (UIntPtr) sampleIndex, barrier ? (byte) 1 : (byte) 0);
}
}
#endif // MONOMAC
Expand Down
4 changes: 2 additions & 2 deletions src/Metal/MTLDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,13 @@ public static void ConvertSparsePixelRegions (this IMTLDevice This, MTLRegion []
#if !NET
[return: Release]
[BindingImpl (BindingImplOptions.GeneratedCode | BindingImplOptions.Optimizable)]
public static IMTLLibrary? CreateLibrary (this IMTLDevice This, global::CoreFoundation.DispatchData data, out NSError? error)
public unsafe static IMTLLibrary? CreateLibrary (this IMTLDevice This, global::CoreFoundation.DispatchData data, out NSError? error)
{
if (data is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (data));
var errorValue = NativeHandle.Zero;

var ret = Runtime.GetINativeObject<IMTLLibrary> (global::ObjCRuntime.Messaging.IntPtr_objc_msgSend_IntPtr_ref_IntPtr (This.Handle, Selector.GetHandle ("newLibraryWithData:error:"), data.Handle, ref errorValue), true);
var ret = Runtime.GetINativeObject<IMTLLibrary> (global::ObjCRuntime.Messaging.IntPtr_objc_msgSend_IntPtr_ref_IntPtr (This.Handle, Selector.GetHandle ("newLibraryWithData:error:"), data.Handle, &errorValue), true);
error = Runtime.GetNSObject<NSError> (errorValue);
return ret;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ObjCRuntime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,7 @@ static IntPtr CreateNSObject (IntPtr type_gchandle, IntPtr handle, NSObject.Flag
target_type = typeof (T);
else if (target_type.IsSubclassOf (typeof (T))) {
// do nothing, this is fine.
} else if (Messaging.bool_objc_msgSend_IntPtr (ptr, Selector.GetHandle ("isKindOfClass:"), Class.GetHandle (typeof (T)))) {
} else if (Messaging.bool_objc_msgSend_IntPtr (ptr, Selector.GetHandle ("isKindOfClass:"), Class.GetHandle (typeof (T))) != 0) {
// If the instance itself claims it's an instance of the provided (generic argument) type,
// then we believe the instance. See bug #20692 for a test case.
target_type = typeof (T);
Expand Down Expand Up @@ -1804,7 +1804,7 @@ static IntPtr CreateNSObject (IntPtr type_gchandle, IntPtr handle, NSObject.Flag
// nothing to do
} else if (dynamic_type.IsSubclassOf (target_type)) {
target_type = dynamic_type;
} else if (Messaging.bool_objc_msgSend_IntPtr (ptr, Selector.GetHandle ("isKindOfClass:"), Class.GetHandle (target_type))) {
} else if (Messaging.bool_objc_msgSend_IntPtr (ptr, Selector.GetHandle ("isKindOfClass:"), Class.GetHandle (target_type)) != 0) {
// nothing to do
} else {
target_type = dynamic_type;
Expand Down
8 changes: 4 additions & 4 deletions src/UIKit/UIToolbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public virtual void SetItems (UIBarButtonItem[] items, bool animated)

#if NET
if (IsDirectBinding) {
ObjCRuntime.Messaging.void_objc_msgSend_NativeHandle_bool (this.Handle, Selector.GetHandle ("setItems:animated:"), nsa_items.Handle, animated);
ObjCRuntime.Messaging.void_objc_msgSend_NativeHandle_bool (this.Handle, Selector.GetHandle ("setItems:animated:"), nsa_items.Handle, animated ? (byte) 1 : (byte) 0);
} else {
ObjCRuntime.Messaging.void_objc_msgSendSuper_NativeHandle_bool (this.SuperHandle, Selector.GetHandle ("setItems:animated:"), nsa_items.Handle, animated);
ObjCRuntime.Messaging.void_objc_msgSendSuper_NativeHandle_bool (this.SuperHandle, Selector.GetHandle ("setItems:animated:"), nsa_items.Handle, animated ? (byte) 1 : (byte) 0);
}
#else
if (IsDirectBinding) {
ObjCRuntime.Messaging.void_objc_msgSend_IntPtr_bool (this.Handle, Selector.GetHandle ("setItems:animated:"), nsa_items.Handle, animated);
ObjCRuntime.Messaging.void_objc_msgSend_IntPtr_bool (this.Handle, Selector.GetHandle ("setItems:animated:"), nsa_items.Handle, animated ? (byte) 1 : (byte) 0);
} else {
ObjCRuntime.Messaging.void_objc_msgSendSuper_IntPtr_bool (this.SuperHandle, Selector.GetHandle ("setItems:animated:"), nsa_items.Handle, animated);
ObjCRuntime.Messaging.void_objc_msgSendSuper_IntPtr_bool (this.SuperHandle, Selector.GetHandle ("setItems:animated:"), nsa_items.Handle, animated ? (byte) 1 : (byte) 0);
}
#endif
nsa_items.Dispose ();
Expand Down
Loading

0 comments on commit 98d8d45

Please sign in to comment.