Skip to content

Commit

Permalink
[CoreServices] Make P/Invokes have blittable signatures. (#20408)
Browse files Browse the repository at this point in the history
Contributes towards #15684.
  • Loading branch information
rolfbjarne authored Apr 10, 2024
1 parent 71b9431 commit f6d5b85
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
30 changes: 14 additions & 16 deletions src/CoreServices/FSEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,11 @@ public static ulong GetLastEventIdForDeviceBeforeTime (ulong device, double time
}

[DllImport (Constants.CoreServicesLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool FSEventsPurgeEventsForDeviceUpToEventId (ulong device, ulong eventId);
static extern byte FSEventsPurgeEventsForDeviceUpToEventId (ulong device, ulong eventId);

public static bool PurgeEventsForDeviceUpToEventId (ulong device, ulong eventId)
{
return FSEventsPurgeEventsForDeviceUpToEventId (device, eventId);
return FSEventsPurgeEventsForDeviceUpToEventId (device, eventId) != 0;
}
}

Expand All @@ -144,7 +143,7 @@ struct FSEventStreamContext {
#if NET
internal unsafe delegate* unmanaged<IntPtr, void> Release; /* CFAllocatorReleaseCallBack __nullable */
#else
internal FSEventStream.ReleaseContextCallback Release; /* CFAllocatorReleaseCallBack __nullable */
internal IntPtr Release; /* CFAllocatorReleaseCallBack __nullable */
#endif
IntPtr CopyDescription; /* CFAllocatorCopyDescriptionCallBack __nullable */
}
Expand Down Expand Up @@ -272,19 +271,19 @@ unsafe static extern IntPtr FSEventStreamCreate (IntPtr allocator,
#if NET
delegate* unmanaged<IntPtr, IntPtr, nint, IntPtr, IntPtr, IntPtr, void> callback,
#else
FSEventStreamCallback callback,
IntPtr callback,
#endif
ref FSEventStreamContext context, IntPtr pathsToWatch,
FSEventStreamContext* context, IntPtr pathsToWatch,
ulong sinceWhen, double latency, FSEventStreamCreateFlags flags);

[DllImport (Constants.CoreServicesLibrary)]
unsafe static extern IntPtr FSEventStreamCreateRelativeToDevice (IntPtr allocator,
#if NET
delegate* unmanaged<IntPtr, IntPtr, nint, IntPtr, IntPtr, IntPtr, void> callback,
#else
FSEventStreamCallback callback,
IntPtr callback,
#endif
ref FSEventStreamContext context, ulong deviceToWatch, IntPtr pathsToWatchRelativeToDevice,
FSEventStreamContext* context, ulong deviceToWatch, IntPtr pathsToWatchRelativeToDevice,
ulong sinceWhen, double latency, FSEventStreamCreateFlags flags);

public FSEventStream (FSEventStreamCreateOptions options)
Expand Down Expand Up @@ -314,7 +313,7 @@ public FSEventStream (FSEventStreamCreateOptions options)
context.Release = &FreeGCHandle;
}
#else
context.Release = releaseContextCallback;
context.Release = Marshal.GetFunctionPointerForDelegate (releaseContextCallback);
#endif

var allocator = options.Allocator.GetHandle ();
Expand All @@ -330,9 +329,9 @@ public FSEventStream (FSEventStreamCreateOptions options)
#if NET
&EventsCallback,
#else
eventsCallback,
Marshal.GetFunctionPointerForDelegate (eventsCallback),
#endif
ref context,
&context,
options.DeviceToWatch.Value,
pathsToWatch.Handle, sinceWhenId, latency, flags);
} else {
Expand All @@ -341,9 +340,9 @@ public FSEventStream (FSEventStreamCreateOptions options)
#if NET
&EventsCallback,
#else
eventsCallback,
Marshal.GetFunctionPointerForDelegate (eventsCallback),
#endif
ref context,
&context,
pathsToWatch.Handle, sinceWhenId, latency, flags);
}
}
Expand Down Expand Up @@ -482,12 +481,11 @@ public void Show ()
}

[DllImport (Constants.CoreServicesLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool FSEventStreamStart (IntPtr handle);
static extern byte FSEventStreamStart (IntPtr handle);

public bool Start ()
{
return FSEventStreamStart (GetCheckedHandle ());
return FSEventStreamStart (GetCheckedHandle ()) != 0;
}

[DllImport (Constants.CoreServicesLibrary)]
Expand Down
8 changes: 5 additions & 3 deletions src/CoreServices/LaunchServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ public static NSUrl [] GetApplicationUrlsForUrl (NSUrl url, LSRoles roles = LSRo
}

[DllImport (Constants.CoreServicesLibrary)]
static extern LSResult LSCanURLAcceptURL (IntPtr inItemUrl, IntPtr inTargetUrl,
LSRoles inRole, LSAcceptanceFlags inFlags, out byte outAcceptsItem);
unsafe static extern LSResult LSCanURLAcceptURL (IntPtr inItemUrl, IntPtr inTargetUrl,
LSRoles inRole, LSAcceptanceFlags inFlags, byte* outAcceptsItem);

// NOTE: intentionally inverting the status results (return bool, with an out
// LSResult vs return LSResult with an out bool) to make the API nicer to use
Expand All @@ -183,7 +183,9 @@ public static bool CanUrlAcceptUrl (NSUrl itemUrl, NSUrl targetUrl,
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (targetUrl));

byte acceptsItem;
result = LSCanURLAcceptURL (itemUrl.Handle, targetUrl.Handle, roles, acceptanceFlags, out acceptsItem);
unsafe {
result = LSCanURLAcceptURL (itemUrl.Handle, targetUrl.Handle, roles, acceptanceFlags, &acceptsItem);
}
return acceptsItem != 0;
}

Expand Down
5 changes: 0 additions & 5 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public partial class BlittablePInvokes {
"AVFoundation.AVSampleCursorSyncInfo ObjCRuntime.Messaging::AVSampleCursorSyncInfo_objc_msgSendSuper_stret(System.IntPtr,System.IntPtr)",
"AVFoundation.AVSampleCursorSyncInfo ObjCRuntime.Messaging::AVSampleCursorSyncInfo_objc_msgSendSuper(System.IntPtr,System.IntPtr)",
"CoreGraphics.CGSize CoreText.CTFramesetter::CTFramesetterSuggestFrameSizeWithConstraints(System.IntPtr,Foundation.NSRange,System.IntPtr,CoreGraphics.CGSize,Foundation.NSRange&)",
"CoreServices.LSResult CoreServices.LaunchServices::LSCanURLAcceptURL(System.IntPtr,System.IntPtr,CoreServices.LSRoles,CoreServices.LSAcceptanceFlags,System.Byte&)",
"CoreVideo.CVReturn CoreVideo.CVDisplayLink::CVDisplayLinkCreateWithActiveCGDisplays(System.IntPtr&)",
"CoreVideo.CVReturn CoreVideo.CVDisplayLink::CVDisplayLinkCreateWithCGDisplay(System.UInt32,System.IntPtr&)",
"CoreVideo.CVReturn CoreVideo.CVDisplayLink::CVDisplayLinkCreateWithCGDisplays(System.UInt32[],System.IntPtr,System.IntPtr&)",
Expand Down Expand Up @@ -160,8 +159,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 CoreServices.FSEvent::FSEventsPurgeEventsForDeviceUpToEventId(System.UInt64,System.UInt64)",
"System.Boolean CoreServices.FSEventStream::FSEventStreamStart(System.IntPtr)",
"System.Boolean CoreText.CTFont::CTFontGetGlyphsForCharacters(System.IntPtr,System.Char[],System.UInt16[],System.IntPtr)",
"System.Boolean CoreText.CTFontManager::CTFontManagerIsSupportedFont(System.IntPtr)",
"System.Boolean CoreText.CTFontManager::CTFontManagerRegisterFontsForURL(System.IntPtr,CoreText.CTFontManagerScope,System.IntPtr&)",
Expand Down Expand Up @@ -273,8 +270,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 CoreServices.FSEventStream::FSEventStreamCreate(System.IntPtr,method System.Void *(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr),CoreServices.FSEventStreamContext&,System.IntPtr,System.UInt64,System.Double,CoreServices.FSEventStreamCreateFlags)",
"System.IntPtr CoreServices.FSEventStream::FSEventStreamCreateRelativeToDevice(System.IntPtr,method System.Void *(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr),CoreServices.FSEventStreamContext&,System.UInt64,System.IntPtr,System.UInt64,System.Double,CoreServices.FSEventStreamCreateFlags)",
"System.IntPtr CoreText.CTFont::CTFontCopyLocalizedName(System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.IntPtr CoreText.CTFont::CTFontCreateCopyWithAttributes(System.IntPtr,System.Runtime.InteropServices.NFloat,CoreGraphics.CGAffineTransform&,System.IntPtr)",
"System.IntPtr CoreText.CTFont::CTFontCreateCopyWithFamily(System.IntPtr,System.Runtime.InteropServices.NFloat,CoreGraphics.CGAffineTransform&,System.IntPtr)",
Expand Down

8 comments on commit f6d5b85

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.