[SearchKit] Make P/Invokes have blittable signatures. (#20493)

Contributes towards #15684.
This commit is contained in:
Rolf Bjarne Kvinge 2024-04-24 13:19:08 +02:00 коммит произвёл GitHub
Родитель cb34348455
Коммит 6c19986225
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 23 добавлений и 37 удалений

Просмотреть файл

@ -21,6 +21,7 @@
#nullable enable #nullable enable
using System; using System;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning; using System.Runtime.Versioning;
using CoreFoundation; using CoreFoundation;
using ObjCRuntime; using ObjCRuntime;
@ -56,8 +57,7 @@ namespace SearchKit {
} }
[DllImport (Constants.SearchKitLibrary)] [DllImport (Constants.SearchKitLibrary)]
[return: MarshalAs (UnmanagedType.I1)] unsafe extern static byte SKSearchFindMatches (IntPtr handle, nint maxCount, IntPtr ids, IntPtr scores, double time, nint* foundCount);
extern static bool SKSearchFindMatches (IntPtr handle, nint maxCount, IntPtr ids, IntPtr scores, double time, out nint foundCount);
public bool FindMatches (nint maxCount, ref nint [] ids, double waitTime, out nint foundCount) public bool FindMatches (nint maxCount, ref nint [] ids, double waitTime, out nint foundCount)
{ {
@ -68,9 +68,10 @@ namespace SearchKit {
if (ids.Length != maxCount) if (ids.Length != maxCount)
throw new ArgumentException ("ids should have as many elements as maxCount"); throw new ArgumentException ("ids should have as many elements as maxCount");
foundCount = default;
unsafe { unsafe {
fixed (nint* p = ids) { 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;
} }
} }
} }
@ -90,13 +91,14 @@ namespace SearchKit {
if (scores.Length != maxCount) if (scores.Length != maxCount)
throw new ArgumentException ("scores should have as many elements as maxCount"); throw new ArgumentException ("scores should have as many elements as maxCount");
} }
foundCount = default;
unsafe { unsafe {
fixed (nint* p = ids) { fixed (nint* p = ids) {
if (scores is null) 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 { else {
fixed (float* s = scores) { 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;
} }
} }
} }
@ -199,7 +201,7 @@ namespace SearchKit {
[DllImport (Constants.SearchKitLibrary)] [DllImport (Constants.SearchKitLibrary)]
extern static IntPtr SKIndexCreateWithMutableData (IntPtr url, IntPtr str, SKIndexType type, IntPtr dict); extern static IntPtr SKIndexCreateWithMutableData (IntPtr url, IntPtr str, SKIndexType type, IntPtr dict);
[DllImport (Constants.SearchKitLibrary)] [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)] [DllImport (Constants.SearchKitLibrary)]
extern static IntPtr SKIndexOpenWithMutableData (IntPtr mutableData, IntPtr str); extern static IntPtr SKIndexOpenWithMutableData (IntPtr mutableData, IntPtr str);
[DllImport (Constants.SearchKitLibrary)] [DllImport (Constants.SearchKitLibrary)]
@ -237,7 +239,7 @@ namespace SearchKit {
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (indexName)); ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (indexName));
var indexNameHandle = CFString.CreateNative (indexName); var indexNameHandle = CFString.CreateNative (indexName);
try { try {
var handle = SKIndexOpenWithURL (url.Handle, indexNameHandle, writeAccess); var handle = SKIndexOpenWithURL (url.Handle, indexNameHandle, writeAccess.AsByte ());
if (handle == IntPtr.Zero) if (handle == IntPtr.Zero)
return null; return null;
return new SKIndex (handle, true); return new SKIndex (handle, true);
@ -321,8 +323,7 @@ namespace SearchKit {
} }
[DllImport (Constants.SearchKitLibrary)] [DllImport (Constants.SearchKitLibrary)]
[return: MarshalAs (UnmanagedType.I1)] extern static byte SKIndexAddDocumentWithText (IntPtr h, IntPtr doc, IntPtr str, byte canreplace);
extern static bool SKIndexAddDocumentWithText (IntPtr h, IntPtr doc, IntPtr str, [MarshalAs (UnmanagedType.I1)] bool canreplace);
public bool AddDocumentWithText (SKDocument document, string text, bool canReplace) public bool AddDocumentWithText (SKDocument document, string text, bool canReplace)
{ {
@ -330,15 +331,14 @@ namespace SearchKit {
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (document)); ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (document));
var textHandle = CFString.CreateNative (text); var textHandle = CFString.CreateNative (text);
try { try {
return SKIndexAddDocumentWithText (Handle, document.Handle, textHandle, canReplace); return SKIndexAddDocumentWithText (Handle, document.Handle, textHandle, canReplace.AsByte ()) != 0;
} finally { } finally {
CFString.ReleaseNative (textHandle); CFString.ReleaseNative (textHandle);
} }
} }
[DllImport (Constants.SearchKitLibrary)] [DllImport (Constants.SearchKitLibrary)]
[return: MarshalAs (UnmanagedType.I1)] extern static byte SKIndexAddDocument (IntPtr h, IntPtr doc, IntPtr mimeHintStr, byte canReplace);
extern static bool SKIndexAddDocument (IntPtr h, IntPtr doc, IntPtr mimeHintStr, [MarshalAs (UnmanagedType.I1)] bool canReplace);
public bool AddDocument (SKDocument document, string mimeHint, bool canReplace) public bool AddDocument (SKDocument document, string mimeHint, bool canReplace)
{ {
@ -346,7 +346,7 @@ namespace SearchKit {
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (document)); ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (document));
var mimeHintHandle = CFString.CreateNative (mimeHint); var mimeHintHandle = CFString.CreateNative (mimeHint);
try { try {
return SKIndexAddDocument (Handle, document.Handle, mimeHintHandle, canReplace); return SKIndexAddDocument (Handle, document.Handle, mimeHintHandle, canReplace.AsByte ()) != 0;
} finally { } finally {
CFString.ReleaseNative (mimeHintHandle); CFString.ReleaseNative (mimeHintHandle);
} }
@ -356,18 +356,16 @@ namespace SearchKit {
public extern static void LoadDefaultExtractorPlugIns (); public extern static void LoadDefaultExtractorPlugIns ();
[DllImport (Constants.SearchKitLibrary)] [DllImport (Constants.SearchKitLibrary)]
[return: MarshalAs (UnmanagedType.I1)] extern static byte SKIndexFlush (IntPtr h);
extern static bool SKIndexFlush (IntPtr h);
public bool Flush () public bool Flush ()
{ {
return SKIndexFlush (Handle); return SKIndexFlush (Handle) != 0;
} }
[DllImport (Constants.SearchKitLibrary)] [DllImport (Constants.SearchKitLibrary)]
[return: MarshalAs (UnmanagedType.I1)] extern static byte SKIndexCompact (IntPtr h);
extern static bool SKIndexCompact (IntPtr h);
public bool Compact () public bool Compact ()
{ {
return SKIndexCompact (Handle); return SKIndexCompact (Handle) != 0;
} }
[DllImport (Constants.SearchKitLibrary)] [DllImport (Constants.SearchKitLibrary)]
@ -404,33 +402,30 @@ namespace SearchKit {
} }
[DllImport (Constants.SearchKitLibrary)] [DllImport (Constants.SearchKitLibrary)]
[return: MarshalAs (UnmanagedType.I1)] extern static byte SKIndexMoveDocument (IntPtr h, IntPtr document, IntPtr newParent);
extern static bool SKIndexMoveDocument (IntPtr h, IntPtr document, IntPtr newParent);
public bool MoveDocument (SKDocument document, SKDocument newParent) public bool MoveDocument (SKDocument document, SKDocument newParent)
{ {
if (document is null) if (document is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (document)); ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (document));
if (newParent is null) if (newParent is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (newParent)); ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (newParent));
return SKIndexMoveDocument (Handle, document.Handle, newParent.Handle); return SKIndexMoveDocument (Handle, document.Handle, newParent.Handle) != 0;
} }
[DllImport (Constants.SearchKitLibrary)] [DllImport (Constants.SearchKitLibrary)]
[return: MarshalAs (UnmanagedType.I1)] extern static byte SKIndexRemoveDocument (IntPtr h, IntPtr doc);
extern static bool SKIndexRemoveDocument (IntPtr h, IntPtr doc);
public bool RemoveDocument (SKDocument document) public bool RemoveDocument (SKDocument document)
{ {
if (document is null) if (document is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (document)); ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (document));
return SKIndexRemoveDocument (Handle, document.Handle); return SKIndexRemoveDocument (Handle, document.Handle) != 0;
} }
[DllImport (Constants.SearchKitLibrary)] [DllImport (Constants.SearchKitLibrary)]
[return: MarshalAs (UnmanagedType.I1)] extern static byte SKIndexRenameDocument (IntPtr h, IntPtr doc, IntPtr newName);
extern static bool SKIndexRenameDocument (IntPtr h, IntPtr doc, IntPtr newName);
public bool RenameDocument (SKDocument document, string newName) public bool RenameDocument (SKDocument document, string newName)
{ {
if (document is null) if (document is null)
@ -439,7 +434,7 @@ namespace SearchKit {
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (newName)); ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (newName));
var newNameHandle = CFString.CreateNative (newName); var newNameHandle = CFString.CreateNative (newName);
try { try {
return SKIndexRenameDocument (Handle, document.Handle, newNameHandle); return SKIndexRenameDocument (Handle, document.Handle, newNameHandle) != 0;
} finally { } finally {
CFString.ReleaseNative (newNameHandle); CFString.ReleaseNative (newNameHandle);
} }

Просмотреть файл

@ -215,14 +215,6 @@ namespace Cecil.Tests {
"System.Boolean ObjCRuntime.Class::class_addProperty(System.IntPtr,System.IntPtr,System.IntPtr*,System.Int32)", "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.Class::class_addProtocol(System.IntPtr,System.IntPtr)",
"System.Boolean ObjCRuntime.Selector::sel_isMapped(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.SecIdentity2::sec_identity_access_certificates(System.IntPtr,ObjCRuntime.BlockLiteral*)",
"System.Boolean Security.SecKey::SecKeyIsAlgorithmSupported(System.IntPtr,System.IntPtr,System.IntPtr)", "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&)", "System.Boolean Security.SecKey::SecKeyVerifySignature(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr&)",
@ -256,7 +248,6 @@ namespace Cecil.Tests {
"System.IntPtr GameController.GCMicroGamepadSnapshotData::NSDataFromGCMicroGamepadSnapshotData(GameController.GCMicroGamepadSnapshotData&)", "System.IntPtr GameController.GCMicroGamepadSnapshotData::NSDataFromGCMicroGamepadSnapshotData(GameController.GCMicroGamepadSnapshotData&)",
"System.IntPtr GameController.GCMicroGamepadSnapShotDataV100::NSDataFromGCMicroGamepadSnapShotDataV100(GameController.GCMicroGamepadSnapShotDataV100&)", "System.IntPtr GameController.GCMicroGamepadSnapShotDataV100::NSDataFromGCMicroGamepadSnapShotDataV100(GameController.GCMicroGamepadSnapShotDataV100&)",
"System.IntPtr ObjCRuntime.Selector::GetHandle(System.String)", "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.SecAccessControl::SecAccessControlCreateWithFlags(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.IntPtr Security.SecCertificate::SecCertificateCopySerialNumberData(System.IntPtr,System.IntPtr&)", "System.IntPtr Security.SecCertificate::SecCertificateCopySerialNumberData(System.IntPtr,System.IntPtr&)",
"System.IntPtr Security.SecKey::SecKeyCopyExternalRepresentation(System.IntPtr,System.IntPtr&)", "System.IntPtr Security.SecKey::SecKeyCopyExternalRepresentation(System.IntPtr,System.IntPtr&)",