From 0f85419078ef68902fef7f76e5868c642c19cdcc Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Thu, 14 Dec 2023 18:59:50 +0100 Subject: [PATCH] [Foundation] Make P/Invokes in NSDecimal.cs have blittable signatures. Contributes towards #15684. --- src/Foundation/NSDecimal.cs | 102 +++++++++++++----- .../BlittablePInvokes.KnownFailures.cs | 10 -- 2 files changed, 73 insertions(+), 39 deletions(-) diff --git a/src/Foundation/NSDecimal.cs b/src/Foundation/NSDecimal.cs index ffb45b0f7858..666dbe1a6941 100644 --- a/src/Foundation/NSDecimal.cs +++ b/src/Foundation/NSDecimal.cs @@ -28,6 +28,7 @@ // using System; using System.Globalization; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Versioning; @@ -53,74 +54,117 @@ public struct NSDecimal #if !COREBUILD [DllImport (Constants.FoundationLibrary)] - static extern nint NSDecimalCompare (ref NSDecimal left, ref NSDecimal right); - public static NSComparisonResult Compare (ref NSDecimal left, ref NSDecimal right) + unsafe static extern nint NSDecimalCompare (NSDecimal* left, NSDecimal* right); + public unsafe static NSComparisonResult Compare (ref NSDecimal left, ref NSDecimal right) { - return (NSComparisonResult) (long) NSDecimalCompare (ref left, ref right); + return (NSComparisonResult) (long) NSDecimalCompare ( + (NSDecimal *) Unsafe.AsPointer (ref left), + (NSDecimal *) Unsafe.AsPointer (ref right)); } [DllImport (Constants.FoundationLibrary)] - static extern void NSDecimalRound (out NSDecimal result, ref NSDecimal number, nint scale, nuint mode); - public static void Round (out NSDecimal result, ref NSDecimal number, nint scale, NSRoundingMode mode) + unsafe static extern void NSDecimalRound (NSDecimal* result, NSDecimal* number, nint scale, nuint mode); + public unsafe static void Round (out NSDecimal result, ref NSDecimal number, nint scale, NSRoundingMode mode) { - NSDecimalRound (out result, ref number, scale, (nuint) (ulong) mode); + result = default (NSDecimal); + NSDecimalRound ( + (NSDecimal *) Unsafe.AsPointer (ref result), + (NSDecimal *) Unsafe.AsPointer (ref number), + scale, + (nuint) (ulong) mode); } [DllImport (Constants.FoundationLibrary)] - static extern nuint NSDecimalNormalize (ref NSDecimal number1, ref NSDecimal number2); - public static NSCalculationError Normalize (ref NSDecimal number1, ref NSDecimal number2) + unsafe static extern nuint NSDecimalNormalize (NSDecimal* number1, NSDecimal* number2); + public unsafe static NSCalculationError Normalize (ref NSDecimal number1, ref NSDecimal number2) { - return (NSCalculationError) (ulong) NSDecimalNormalize (ref number1, ref number2); + return (NSCalculationError) (ulong) NSDecimalNormalize ( + (NSDecimal *) Unsafe.AsPointer (ref number1), + (NSDecimal *) Unsafe.AsPointer (ref number2)); } [DllImport (Constants.FoundationLibrary)] - static extern nuint NSDecimalAdd (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, nuint mode); - public static NSCalculationError Add (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, NSRoundingMode mode) + static unsafe extern nuint NSDecimalAdd (NSDecimal* result, NSDecimal* left, NSDecimal* right, nuint mode); + public unsafe static NSCalculationError Add (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, NSRoundingMode mode) { - return (NSCalculationError) (ulong) NSDecimalAdd (out result, ref left, ref right, (nuint) (ulong) mode); + result = default (NSDecimal); + return (NSCalculationError) (ulong) NSDecimalAdd ( + (NSDecimal *) Unsafe.AsPointer (ref result), + (NSDecimal *) Unsafe.AsPointer (ref left), + (NSDecimal *) Unsafe.AsPointer (ref right), + (nuint) (ulong) mode); } [DllImport (Constants.FoundationLibrary)] - static extern nuint NSDecimalSubtract (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, nuint mode); - public static NSCalculationError Subtract (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, NSRoundingMode mode) + unsafe static extern nuint NSDecimalSubtract (NSDecimal* result, NSDecimal* left, NSDecimal* right, nuint mode); + public unsafe static NSCalculationError Subtract (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, NSRoundingMode mode) { - return (NSCalculationError) (ulong) NSDecimalSubtract (out result, ref left, ref right, (nuint) (ulong) mode); + result = default (NSDecimal); + return (NSCalculationError) (ulong) NSDecimalSubtract ( + (NSDecimal *) Unsafe.AsPointer (ref result), + (NSDecimal *) Unsafe.AsPointer (ref left), + (NSDecimal *) Unsafe.AsPointer (ref right), + (nuint) (ulong) mode); } [DllImport (Constants.FoundationLibrary)] - static extern nuint NSDecimalMultiply (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, nuint mode); - public static NSCalculationError Multiply (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, NSRoundingMode mode) + static unsafe extern nuint NSDecimalMultiply (NSDecimal* result, NSDecimal* left, NSDecimal* right, nuint mode); + public unsafe static NSCalculationError Multiply (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, NSRoundingMode mode) { - return (NSCalculationError) (ulong) NSDecimalMultiply (out result, ref left, ref right, (nuint) (ulong) mode); + result = default (NSDecimal); + return (NSCalculationError) (ulong) NSDecimalMultiply ( + (NSDecimal *) Unsafe.AsPointer (ref result), + (NSDecimal *) Unsafe.AsPointer (ref left), + (NSDecimal *) Unsafe.AsPointer (ref right), + (nuint) (ulong) mode); } [DllImport (Constants.FoundationLibrary)] - static extern nuint NSDecimalDivide (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, nuint mode); - public static NSCalculationError Divide (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, NSRoundingMode mode) + unsafe static extern nuint NSDecimalDivide (NSDecimal* result, NSDecimal* left, NSDecimal* right, nuint mode); + public unsafe static NSCalculationError Divide (out NSDecimal result, ref NSDecimal left, ref NSDecimal right, NSRoundingMode mode) { - return (NSCalculationError) (ulong) NSDecimalDivide (out result, ref left, ref right, (nuint) (ulong) mode); + result = default (NSDecimal); + return (NSCalculationError) (ulong) NSDecimalDivide ( + (NSDecimal *) Unsafe.AsPointer (ref result), + (NSDecimal *) Unsafe.AsPointer (ref left), + (NSDecimal *) Unsafe.AsPointer (ref right), + (nuint) (ulong) mode); } [DllImport (Constants.FoundationLibrary)] - static extern nuint NSDecimalPower (out NSDecimal result, ref NSDecimal number, nint power, nuint mode); - public static NSCalculationError Power (out NSDecimal result, ref NSDecimal number, nint power, NSRoundingMode mode) + unsafe static extern nuint NSDecimalPower (NSDecimal* result, NSDecimal* number, nint power, nuint mode); + public unsafe static NSCalculationError Power (out NSDecimal result, ref NSDecimal number, nint power, NSRoundingMode mode) { - return (NSCalculationError) (ulong) NSDecimalPower (out result, ref number, power, (nuint) (ulong) mode); + result = default (NSDecimal); + return (NSCalculationError) (ulong) NSDecimalPower ( + (NSDecimal *) Unsafe.AsPointer (ref result), + (NSDecimal *) Unsafe.AsPointer (ref number), + power, + (nuint) (ulong) mode); } [DllImport (Constants.FoundationLibrary)] - static extern nuint NSDecimalMultiplyByPowerOf10 (out NSDecimal result, ref NSDecimal number, short power10, nuint mode); - public static NSCalculationError MultiplyByPowerOf10 (out NSDecimal result, ref NSDecimal number, short power10, NSRoundingMode mode) + unsafe static extern nuint NSDecimalMultiplyByPowerOf10 (NSDecimal* result, NSDecimal* number, short power10, nuint mode); + public unsafe static NSCalculationError MultiplyByPowerOf10 (out NSDecimal result, ref NSDecimal number, short power10, NSRoundingMode mode) { - return (NSCalculationError) (ulong) NSDecimalMultiplyByPowerOf10 (out result, ref number, power10, (nuint) (ulong) mode); + result = default (NSDecimal); + return (NSCalculationError) (ulong) NSDecimalMultiplyByPowerOf10 ( + (NSDecimal *) Unsafe.AsPointer (ref result), + (NSDecimal *) Unsafe.AsPointer (ref number), + power10, + (nuint) (ulong) mode); } [DllImport (Constants.FoundationLibrary)] - static extern IntPtr NSDecimalString (ref NSDecimal value, /* _Nullable */ IntPtr locale); + unsafe static extern IntPtr NSDecimalString (NSDecimal* value, /* _Nullable */ IntPtr locale); public override string ToString () { - return new NSString (NSDecimalString (ref this, NSLocale.CurrentLocale.Handle)); + unsafe { + fixed (NSDecimal* self = &this) { + return new NSString (NSDecimalString (self, NSLocale.CurrentLocale.Handle)); + } + } } public static NSDecimal operator + (NSDecimal left, NSDecimal right) diff --git a/tests/cecil-tests/BlittablePInvokes.KnownFailures.cs b/tests/cecil-tests/BlittablePInvokes.KnownFailures.cs index 25cee30bd483..cc4301dfecc1 100644 --- a/tests/cecil-tests/BlittablePInvokes.KnownFailures.cs +++ b/tests/cecil-tests/BlittablePInvokes.KnownFailures.cs @@ -721,8 +721,6 @@ public partial class BlittablePInvokes { "System.IntPtr CoreText.CTRunDelegate::CTRunDelegateCreate(CoreText.CTRunDelegateCallbacks&,System.IntPtr)", "System.IntPtr CoreVideo.CVBuffer::CVBufferCopyAttachment(System.IntPtr,System.IntPtr,CoreVideo.CVAttachmentMode&)", "System.IntPtr CoreVideo.CVBuffer::CVBufferGetAttachment(System.IntPtr,System.IntPtr,CoreVideo.CVAttachmentMode&)", - "System.IntPtr Foundation.NSDecimal::NSDecimalCompare(Foundation.NSDecimal&,Foundation.NSDecimal&)", - "System.IntPtr Foundation.NSDecimal::NSDecimalString(Foundation.NSDecimal&,System.IntPtr)", "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 GameController.GCExtendedGamepadSnapshotData::NSDataFromGCExtendedGamepadSnapshotData(GameController.GCExtendedGamepadSnapshotData&)", @@ -760,13 +758,6 @@ public partial class BlittablePInvokes { "System.Runtime.InteropServices.NFloat MediaAccessibility.MACaptionAppearance::MACaptionAppearanceGetRelativeCharacterSize(System.IntPtr,System.IntPtr&)", "System.Runtime.InteropServices.NFloat MediaAccessibility.MACaptionAppearance::MACaptionAppearanceGetWindowOpacity(System.IntPtr,System.IntPtr&)", "System.Runtime.InteropServices.NFloat MediaAccessibility.MACaptionAppearance::MACaptionAppearanceGetWindowRoundedCornerRadius(System.IntPtr,System.IntPtr&)", - "System.UIntPtr Foundation.NSDecimal::NSDecimalAdd(Foundation.NSDecimal&,Foundation.NSDecimal&,Foundation.NSDecimal&,System.UIntPtr)", - "System.UIntPtr Foundation.NSDecimal::NSDecimalDivide(Foundation.NSDecimal&,Foundation.NSDecimal&,Foundation.NSDecimal&,System.UIntPtr)", - "System.UIntPtr Foundation.NSDecimal::NSDecimalMultiply(Foundation.NSDecimal&,Foundation.NSDecimal&,Foundation.NSDecimal&,System.UIntPtr)", - "System.UIntPtr Foundation.NSDecimal::NSDecimalMultiplyByPowerOf10(Foundation.NSDecimal&,Foundation.NSDecimal&,System.Int16,System.UIntPtr)", - "System.UIntPtr Foundation.NSDecimal::NSDecimalNormalize(Foundation.NSDecimal&,Foundation.NSDecimal&)", - "System.UIntPtr Foundation.NSDecimal::NSDecimalPower(Foundation.NSDecimal&,Foundation.NSDecimal&,System.IntPtr,System.UIntPtr)", - "System.UIntPtr Foundation.NSDecimal::NSDecimalSubtract(Foundation.NSDecimal&,Foundation.NSDecimal&,Foundation.NSDecimal&,System.UIntPtr)", "System.Void AppKit.NSCell::NSDrawNinePartImage(CoreGraphics.CGRect,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.Runtime.InteropServices.NFloat,System.Boolean)", "System.Void AppKit.NSCell::NSDrawThreePartImage(CoreGraphics.CGRect,System.IntPtr,System.IntPtr,System.IntPtr,System.Boolean,System.IntPtr,System.Runtime.InteropServices.NFloat,System.Boolean)", "System.Void CoreFoundation.CFBundle::CFBundleGetPackageInfo(System.IntPtr,System.UInt32&,System.UInt32&)", @@ -794,7 +785,6 @@ public partial class BlittablePInvokes { "System.Void CoreText.CTFontManager::CTFontManagerRegisterFontsWithAssetNames(System.IntPtr,System.IntPtr,CoreText.CTFontManagerScope,System.Boolean,ObjCRuntime.BlockLiteral*)", "System.Void CoreText.CTFontManager::CTFontManagerRegisterFontURLs(System.IntPtr,CoreText.CTFontManagerScope,System.Boolean,ObjCRuntime.BlockLiteral*)", "System.Void CoreVideo.CVPixelBuffer::CVPixelBufferGetExtendedPixels(System.IntPtr,System.UIntPtr&,System.UIntPtr&,System.UIntPtr&,System.UIntPtr&)", - "System.Void Foundation.NSDecimal::NSDecimalRound(Foundation.NSDecimal&,Foundation.NSDecimal&,System.IntPtr,System.UIntPtr)", "System.Void Foundation.NSObject::xamarin_release_managed_ref(System.IntPtr,System.Boolean)", "System.Void ImageIO.CGImageSource::CGImageSourceUpdateData(System.IntPtr,System.IntPtr,System.Boolean)", "System.Void ImageIO.CGImageSource::CGImageSourceUpdateDataProvider(System.IntPtr,System.IntPtr,System.Boolean)",