Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Foundation] Make P/Invokes have blittable signatures. #20539

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/Foundation/NSObject2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ internal Flags FlagsInternal {
#endif // !NET || !__MACOS__

[DllImport ("__Internal")]
static extern void xamarin_release_managed_ref (IntPtr handle, [MarshalAs (UnmanagedType.I1)] bool user_type);
static extern void xamarin_release_managed_ref (IntPtr handle, byte user_type);

static void RegisterToggleReference (NSObject obj, IntPtr handle, bool isCustomType)
{
Expand Down Expand Up @@ -404,8 +404,7 @@ private void InitializeObject (bool alloced)
}

[DllImport ("__Internal")]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool xamarin_set_gchandle_with_flags_safe (IntPtr handle, IntPtr gchandle, XamarinGCHandleFlags flags);
static extern byte xamarin_set_gchandle_with_flags_safe (IntPtr handle, IntPtr gchandle, XamarinGCHandleFlags flags);

void CreateManagedRef (bool retain)
{
Expand All @@ -415,7 +414,7 @@ void CreateManagedRef (bool retain)
var flags = XamarinGCHandleFlags.HasManagedRef | XamarinGCHandleFlags.InitialSet | XamarinGCHandleFlags.WeakGCHandle;
var gchandle = GCHandle.Alloc (this, GCHandleType.WeakTrackResurrection);
var h = GCHandle.ToIntPtr (gchandle);
if (!xamarin_set_gchandle_with_flags_safe (handle, h, flags)) {
if (xamarin_set_gchandle_with_flags_safe (handle, h, flags) == 0) {
// A GCHandle already existed: this shouldn't happen, but let's handle it anyway.
Runtime.NSLog ($"Tried to create a managed reference from an object that already has a managed reference (type: {GetType ()})");
gchandle.Free ();
Expand All @@ -435,7 +434,7 @@ void ReleaseManagedRef ()
/* If we're a wrapper type, we need to unregister here, since we won't enter the release trampoline */
Runtime.NativeObjectHasDied (handle, this);
}
xamarin_release_managed_ref (handle, user_type);
xamarin_release_managed_ref (handle, user_type.AsByte ());
FreeData ();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Foundation/NSSearchPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ namespace Foundation {
public static class NSSearchPath {
public static string [] GetDirectories (NSSearchPathDirectory directory, NSSearchPathDomain domainMask, bool expandTilde = true)
{
return CFArray.StringArrayFromHandle (NSSearchPathForDirectoriesInDomains ((nuint) (ulong) directory, (nuint) (ulong) domainMask, expandTilde));
return CFArray.StringArrayFromHandle (NSSearchPathForDirectoriesInDomains ((nuint) (ulong) directory, (nuint) (ulong) domainMask, expandTilde.AsByte ()));
}

[DllImport (Constants.FoundationLibrary)]
static extern IntPtr NSSearchPathForDirectoriesInDomains (nuint directory, nuint domainMask, [MarshalAs (UnmanagedType.I1)] bool expandTilde);
static extern IntPtr NSSearchPathForDirectoriesInDomains (nuint directory, nuint domainMask, byte expandTilde);
}
}
4 changes: 2 additions & 2 deletions src/Foundation/NSThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static double Priority {
}

[DllImport ("__Internal")]
static extern IntPtr xamarin_init_nsthread (IntPtr handle, [MarshalAs (UnmanagedType.I1)] bool is_direct_binding, IntPtr target, IntPtr selector, IntPtr argument);
static extern IntPtr xamarin_init_nsthread (IntPtr handle, byte is_direct_binding, IntPtr target, IntPtr selector, IntPtr argument);

IntPtr InitNSThread (NSObject target, Selector selector, NSObject argument)
{
Expand All @@ -46,7 +46,7 @@ IntPtr InitNSThread (NSObject target, Selector selector, NSObject argument)
if (selector is null)
throw new ArgumentNullException ("selector");

return xamarin_init_nsthread (IsDirectBinding ? this.Handle : this.SuperHandle, IsDirectBinding, target.Handle, selector.Handle, argument is null ? IntPtr.Zero : argument.Handle);
return xamarin_init_nsthread (IsDirectBinding ? this.Handle : this.SuperHandle, IsDirectBinding.AsByte (), target.Handle, selector.Handle, argument is null ? IntPtr.Zero : argument.Handle);
}

[Export ("initWithTarget:selector:object:")]
Expand Down
4 changes: 0 additions & 4 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ public partial class BlittablePInvokes {
"Security.SslStatus Security.SslContext::SSLRead(System.IntPtr,System.Byte*,System.IntPtr,System.IntPtr&)",
"Security.SslStatus Security.SslContext::SSLSetSessionOption(System.IntPtr,Security.SslSessionOption,System.Boolean)",
"Security.SslStatus Security.SslContext::SSLWrite(System.IntPtr,System.Byte*,System.IntPtr,System.IntPtr&)",
"System.Boolean Foundation.NSObject::xamarin_set_gchandle_with_flags_safe(System.IntPtr,System.IntPtr,Foundation.NSObject/XamarinGCHandleFlags)",
"System.Boolean Network.NWAdvertiseDescriptor::nw_advertise_descriptor_get_no_auto_rename(System.IntPtr)",
"System.Boolean Network.NWBrowserDescriptor::nw_browse_descriptor_get_include_txt_record(System.IntPtr)",
"System.Boolean Network.NWConnectionGroup::nw_connection_group_reinsert_extracted_connection(System.IntPtr,System.IntPtr)",
Expand Down Expand Up @@ -219,8 +218,6 @@ public partial class BlittablePInvokes {
"System.Int32 Security.SecCertificate::SecCertificateCopyEmailAddresses(System.IntPtr,System.IntPtr&)",
"System.Int32 Security.SslContext::SSLCopyALPNProtocols(System.IntPtr,System.IntPtr&)",
"System.Int32 Security.SslContext::SSLSetSessionTicketsEnabled(System.IntPtr,System.Boolean)",
"System.IntPtr Foundation.NSSearchPath::NSSearchPathForDirectoriesInDomains(System.UIntPtr,System.UIntPtr,System.Boolean)",
"System.IntPtr Foundation.NSThread::xamarin_init_nsthread(System.IntPtr,System.Boolean,System.IntPtr,System.IntPtr,System.IntPtr)",
"System.IntPtr ObjCRuntime.Selector::GetHandle(System.String)",
"System.IntPtr Security.SecAccessControl::SecAccessControlCreateWithFlags(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.IntPtr Security.SecCertificate::SecCertificateCopySerialNumberData(System.IntPtr,System.IntPtr&)",
Expand All @@ -232,7 +229,6 @@ public partial class BlittablePInvokes {
"System.IntPtr Security.SecKey::SecKeyCreateSignature(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.IntPtr Security.SecKey::SecKeyCreateWithData(System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.IntPtr Security.SecPolicy::SecPolicyCreateSSL(System.Boolean,System.IntPtr)",
"System.Void Foundation.NSObject::xamarin_release_managed_ref(System.IntPtr,System.Boolean)",
"System.Void Network.NWAdvertiseDescriptor::nw_advertise_descriptor_set_no_auto_rename(System.IntPtr,System.Boolean)",
"System.Void Network.NWBrowserDescriptor::nw_browse_descriptor_set_include_txt_record(System.IntPtr,System.Boolean)",
"System.Void Network.NWConnection::nw_connection_send(System.IntPtr,System.IntPtr,System.IntPtr,System.Boolean,ObjCRuntime.BlockLiteral*)",
Expand Down
Loading