Skip to content

Commit

Permalink
[SearchKit] Make P/Invokes have blittable signatures. (#20493)
Browse files Browse the repository at this point in the history
Contributes towards #15684.
  • Loading branch information
rolfbjarne committed Apr 24, 2024
1 parent cb34348 commit 6c19986
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 37 deletions.
51 changes: 23 additions & 28 deletions src/SearchKit/SearchKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#nullable enable

using System;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using CoreFoundation;
using ObjCRuntime;
Expand Down Expand Up @@ -56,8 +57,7 @@ internal SKSearch (NativeHandle handle, bool owns)
}

[DllImport (Constants.SearchKitLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool SKSearchFindMatches (IntPtr handle, nint maxCount, IntPtr ids, IntPtr scores, double time, out nint foundCount);
unsafe extern static byte SKSearchFindMatches (IntPtr handle, nint maxCount, IntPtr ids, IntPtr scores, double time, nint* foundCount);

public bool FindMatches (nint maxCount, ref nint [] ids, double waitTime, out nint foundCount)
{
Expand All @@ -68,9 +68,10 @@ public bool FindMatches (nint maxCount, ref nint [] ids, double waitTime, out ni
if (ids.Length != maxCount)
throw new ArgumentException ("ids should have as many elements as maxCount");

foundCount = default;
unsafe {
fixed (nint* p = ids) {
return SKSearchFindMatches (Handle, maxCount, (IntPtr) p, IntPtr.Zero, waitTime, out foundCount);
return SKSearchFindMatches (Handle, maxCount, (IntPtr) p, IntPtr.Zero, waitTime, (nint*) Unsafe.AsPointer<nint> (ref foundCount)) != 0;
}
}
}
Expand All @@ -90,13 +91,14 @@ public bool FindMatches (nint maxCount, ref nint [] ids, ref float []? scores, d
if (scores.Length != maxCount)
throw new ArgumentException ("scores should have as many elements as maxCount");
}
foundCount = default;
unsafe {
fixed (nint* p = ids) {
if (scores is null)
return SKSearchFindMatches (Handle, maxCount, (IntPtr) p, IntPtr.Zero, waitTime, out foundCount);
return SKSearchFindMatches (Handle, maxCount, (IntPtr) p, IntPtr.Zero, waitTime, (nint*) Unsafe.AsPointer<nint> (ref foundCount)) != 0;
else {
fixed (float* s = scores) {
return SKSearchFindMatches (Handle, maxCount, (IntPtr) p, (IntPtr) s, waitTime, out foundCount);
return SKSearchFindMatches (Handle, maxCount, (IntPtr) p, (IntPtr) s, waitTime, (nint*) Unsafe.AsPointer<nint> (ref foundCount)) != 0;
}
}
}
Expand Down Expand Up @@ -199,7 +201,7 @@ public class SKIndex : NativeObject
[DllImport (Constants.SearchKitLibrary)]
extern static IntPtr SKIndexCreateWithMutableData (IntPtr url, IntPtr str, SKIndexType type, IntPtr dict);
[DllImport (Constants.SearchKitLibrary)]
extern static IntPtr SKIndexOpenWithURL (IntPtr url, IntPtr str, [MarshalAs (UnmanagedType.I1)] bool writeAccess);
extern static IntPtr SKIndexOpenWithURL (IntPtr url, IntPtr str, byte writeAccess);
[DllImport (Constants.SearchKitLibrary)]
extern static IntPtr SKIndexOpenWithMutableData (IntPtr mutableData, IntPtr str);
[DllImport (Constants.SearchKitLibrary)]
Expand Down Expand Up @@ -237,7 +239,7 @@ public class SKIndex : NativeObject
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (indexName));
var indexNameHandle = CFString.CreateNative (indexName);
try {
var handle = SKIndexOpenWithURL (url.Handle, indexNameHandle, writeAccess);
var handle = SKIndexOpenWithURL (url.Handle, indexNameHandle, writeAccess.AsByte ());
if (handle == IntPtr.Zero)
return null;
return new SKIndex (handle, true);
Expand Down Expand Up @@ -321,32 +323,30 @@ protected override void Dispose (bool disposing)
}

[DllImport (Constants.SearchKitLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool SKIndexAddDocumentWithText (IntPtr h, IntPtr doc, IntPtr str, [MarshalAs (UnmanagedType.I1)] bool canreplace);
extern static byte SKIndexAddDocumentWithText (IntPtr h, IntPtr doc, IntPtr str, byte canreplace);

public bool AddDocumentWithText (SKDocument document, string text, bool canReplace)
{
if (document is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (document));
var textHandle = CFString.CreateNative (text);
try {
return SKIndexAddDocumentWithText (Handle, document.Handle, textHandle, canReplace);
return SKIndexAddDocumentWithText (Handle, document.Handle, textHandle, canReplace.AsByte ()) != 0;
} finally {
CFString.ReleaseNative (textHandle);
}
}

[DllImport (Constants.SearchKitLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool SKIndexAddDocument (IntPtr h, IntPtr doc, IntPtr mimeHintStr, [MarshalAs (UnmanagedType.I1)] bool canReplace);
extern static byte SKIndexAddDocument (IntPtr h, IntPtr doc, IntPtr mimeHintStr, byte canReplace);

public bool AddDocument (SKDocument document, string mimeHint, bool canReplace)
{
if (document is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (document));
var mimeHintHandle = CFString.CreateNative (mimeHint);
try {
return SKIndexAddDocument (Handle, document.Handle, mimeHintHandle, canReplace);
return SKIndexAddDocument (Handle, document.Handle, mimeHintHandle, canReplace.AsByte ()) != 0;
} finally {
CFString.ReleaseNative (mimeHintHandle);
}
Expand All @@ -356,18 +356,16 @@ public bool AddDocument (SKDocument document, string mimeHint, bool canReplace)
public extern static void LoadDefaultExtractorPlugIns ();

[DllImport (Constants.SearchKitLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool SKIndexFlush (IntPtr h);
extern static byte SKIndexFlush (IntPtr h);
public bool Flush ()
{
return SKIndexFlush (Handle);
return SKIndexFlush (Handle) != 0;
}
[DllImport (Constants.SearchKitLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool SKIndexCompact (IntPtr h);
extern static byte SKIndexCompact (IntPtr h);
public bool Compact ()
{
return SKIndexCompact (Handle);
return SKIndexCompact (Handle) != 0;
}

[DllImport (Constants.SearchKitLibrary)]
Expand Down Expand Up @@ -404,33 +402,30 @@ public SKTextAnalysis AnalysisProperties {
}

[DllImport (Constants.SearchKitLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool SKIndexMoveDocument (IntPtr h, IntPtr document, IntPtr newParent);
extern static byte SKIndexMoveDocument (IntPtr h, IntPtr document, IntPtr newParent);
public bool MoveDocument (SKDocument document, SKDocument newParent)
{
if (document is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (document));
if (newParent is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (newParent));
return SKIndexMoveDocument (Handle, document.Handle, newParent.Handle);
return SKIndexMoveDocument (Handle, document.Handle, newParent.Handle) != 0;
}


[DllImport (Constants.SearchKitLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool SKIndexRemoveDocument (IntPtr h, IntPtr doc);
extern static byte SKIndexRemoveDocument (IntPtr h, IntPtr doc);

public bool RemoveDocument (SKDocument document)
{
if (document is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (document));
return SKIndexRemoveDocument (Handle, document.Handle);
return SKIndexRemoveDocument (Handle, document.Handle) != 0;
}


[DllImport (Constants.SearchKitLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool SKIndexRenameDocument (IntPtr h, IntPtr doc, IntPtr newName);
extern static byte SKIndexRenameDocument (IntPtr h, IntPtr doc, IntPtr newName);
public bool RenameDocument (SKDocument document, string newName)
{
if (document is null)
Expand All @@ -439,7 +434,7 @@ public bool RenameDocument (SKDocument document, string newName)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (newName));
var newNameHandle = CFString.CreateNative (newName);
try {
return SKIndexRenameDocument (Handle, document.Handle, newNameHandle);
return SKIndexRenameDocument (Handle, document.Handle, newNameHandle) != 0;
} finally {
CFString.ReleaseNative (newNameHandle);
}
Expand Down
9 changes: 0 additions & 9 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,6 @@ public partial class BlittablePInvokes {
"System.Boolean ObjCRuntime.Class::class_addProperty(System.IntPtr,System.IntPtr,System.IntPtr*,System.Int32)",
"System.Boolean ObjCRuntime.Class::class_addProtocol(System.IntPtr,System.IntPtr)",
"System.Boolean ObjCRuntime.Selector::sel_isMapped(System.IntPtr)",
"System.Boolean SearchKit.SKIndex::SKIndexAddDocument(System.IntPtr,System.IntPtr,System.IntPtr,System.Boolean)",
"System.Boolean SearchKit.SKIndex::SKIndexAddDocumentWithText(System.IntPtr,System.IntPtr,System.IntPtr,System.Boolean)",
"System.Boolean SearchKit.SKIndex::SKIndexCompact(System.IntPtr)",
"System.Boolean SearchKit.SKIndex::SKIndexFlush(System.IntPtr)",
"System.Boolean SearchKit.SKIndex::SKIndexMoveDocument(System.IntPtr,System.IntPtr,System.IntPtr)",
"System.Boolean SearchKit.SKIndex::SKIndexRemoveDocument(System.IntPtr,System.IntPtr)",
"System.Boolean SearchKit.SKIndex::SKIndexRenameDocument(System.IntPtr,System.IntPtr,System.IntPtr)",
"System.Boolean SearchKit.SKSearch::SKSearchFindMatches(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.Double,System.IntPtr&)",
"System.Boolean Security.SecIdentity2::sec_identity_access_certificates(System.IntPtr,ObjCRuntime.BlockLiteral*)",
"System.Boolean Security.SecKey::SecKeyIsAlgorithmSupported(System.IntPtr,System.IntPtr,System.IntPtr)",
"System.Boolean Security.SecKey::SecKeyVerifySignature(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr&)",
Expand Down Expand Up @@ -256,7 +248,6 @@ public partial class BlittablePInvokes {
"System.IntPtr GameController.GCMicroGamepadSnapshotData::NSDataFromGCMicroGamepadSnapshotData(GameController.GCMicroGamepadSnapshotData&)",
"System.IntPtr GameController.GCMicroGamepadSnapShotDataV100::NSDataFromGCMicroGamepadSnapShotDataV100(GameController.GCMicroGamepadSnapShotDataV100&)",
"System.IntPtr ObjCRuntime.Selector::GetHandle(System.String)",
"System.IntPtr SearchKit.SKIndex::SKIndexOpenWithURL(System.IntPtr,System.IntPtr,System.Boolean)",
"System.IntPtr Security.SecAccessControl::SecAccessControlCreateWithFlags(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.IntPtr Security.SecCertificate::SecCertificateCopySerialNumberData(System.IntPtr,System.IntPtr&)",
"System.IntPtr Security.SecKey::SecKeyCopyExternalRepresentation(System.IntPtr,System.IntPtr&)",
Expand Down

8 comments on commit 6c19986

@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.