Skip to content

Commit

Permalink
[.NET/CoreFoundation] Use [UnmanagedCallersOnly] instead of [MonoPInv…
Browse files Browse the repository at this point in the history
…okeCallback] for the CGPDFDictionaryApplyFunction function. (#12956)

I had to change the first argument from 'string' to 'IntPtr', because 'string'
isn't blittable. In order to diverge the code paths as little as possible, I
also made this change for the legacy Xamarin version.

Ref #10470.
  • Loading branch information
rolfbjarne authored Oct 8, 2021
1 parent 6d8ef62 commit 7a46df3
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/CoreGraphics/CGPDFDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,51 +154,77 @@ public bool GetArray (string key, out CGPDFArray array)
return r;
}

#if NET
[DllImport (Constants.CoreGraphicsLibrary)]
unsafe extern static void CGPDFDictionaryApplyFunction (/* CGPDFDictionaryRef */ IntPtr dic, delegate* unmanaged<IntPtr, IntPtr, IntPtr, void> function, /* void* */ IntPtr info);
#else
// CGPDFDictionaryApplierFunction
delegate void ApplierFunction (/* const char* */ string key, /* CGPDFObjectRef */ IntPtr value, /* void* */ IntPtr info);
delegate void ApplierFunction (/* const char* */ IntPtr key, /* CGPDFObjectRef */ IntPtr value, /* void* */ IntPtr info);

[DllImport (Constants.CoreGraphicsLibrary)]
extern static void CGPDFDictionaryApplyFunction (/* CGPDFDictionaryRef */ IntPtr dic, ApplierFunction function, /* void* */ IntPtr info);

static readonly ApplierFunction applyblock_handler = ApplyBridge;
#endif // NET

public delegate void ApplyCallback (string key, object value, object info);

#if NET
[UnmanagedCallersOnly]
#else
#if !MONOMAC
[MonoPInvokeCallback (typeof (ApplierFunction))]
#endif
static void ApplyBridge (string key, IntPtr pdfObject, IntPtr info)
#endif // NET
static void ApplyBridge (IntPtr key, IntPtr pdfObject, IntPtr info)
{
var data = (Tuple<ApplyCallback, object>) GCHandle.FromIntPtr (info).Target;
var callback = data.Item1;

callback (key, CGPDFObject.FromHandle (pdfObject), data.Item2);
callback (Marshal.PtrToStringUTF8 (key), CGPDFObject.FromHandle (pdfObject), data.Item2);
}

public void Apply (ApplyCallback callback, object info = null)
{
var data = new Tuple<ApplyCallback, object> (callback, info);
var gch = GCHandle.Alloc (data);
try {
#if NET
unsafe {
CGPDFDictionaryApplyFunction (Handle, &ApplyBridge, GCHandle.ToIntPtr (gch));
}
#else
CGPDFDictionaryApplyFunction (Handle, applyblock_handler, GCHandle.ToIntPtr (gch));
#endif
} finally {
gch.Free ();
}
}

#if NET
[UnmanagedCallersOnly]
#else
#if !MONOMAC
[MonoPInvokeCallback (typeof (ApplierFunction))]
#endif
static void ApplyBridge2 (string key, IntPtr pdfObject, IntPtr info)
#endif // NET
static void ApplyBridge2 (IntPtr key, IntPtr pdfObject, IntPtr info)
{
var callback = (Action<string,CGPDFObject>) GCHandle.FromIntPtr (info).Target;

callback (key, new CGPDFObject (pdfObject));
callback (Marshal.PtrToStringUTF8 (key), new CGPDFObject (pdfObject));
}

public void Apply (Action<string,CGPDFObject> callback)
{
GCHandle gch = GCHandle.Alloc (callback);
#if NET
unsafe {
CGPDFDictionaryApplyFunction (Handle, &ApplyBridge2, GCHandle.ToIntPtr (gch));
}
#else
CGPDFDictionaryApplyFunction (Handle, ApplyBridge2, GCHandle.ToIntPtr (gch));
#endif
gch.Free ();
}

Expand Down

7 comments on commit 7a46df3

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Tests failed catastrophically on Build (no summary found). 🔥

Result file $(TEST_SUMMARY_PATH) not found.

Pipeline on Agent
[.NET/CoreFoundation] Use [UnmanagedCallersOnly] instead of [MonoPInvokeCallback] for the CGPDFDictionaryApplyFunction function. (#12956)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ [CI Build] Tests failed on Build ❌

Tests failed on Build.

API diff

✅ API Diff from stable

View API diff

API & Generator diff

API Diff (from PR only) (no change)
Generator Diff (only version changes)

Packages generated

View packages

Test results

1 tests failed, 217 tests passed.

Failed tests

  • introspection/tvOS - simulator/Debug (tvOS 11.4) [dotnet]: TimedOut

Pipeline on Agent XAMBOT-1036.BigSur'
[.NET/CoreFoundation] Use [UnmanagedCallersOnly] instead of [MonoPInvokeCallback] for the CGPDFDictionaryApplyFunction function. (#12956)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Tests failed catastrophically on VSTS: device tests tvOS 🔥

Not enough free space in the host.

Pipeline on Agent
[.NET/CoreFoundation] Use [UnmanagedCallersOnly] instead of [MonoPInvokeCallback] for the CGPDFDictionaryApplyFunction function. (#12956)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Tests were not ran (VSTS: device tests tvOS). ⚠️

Results were skipped for this run due to provisioning problems Azure Devops. Please contact the bot administrator.

Pipeline on Agent
[.NET/CoreFoundation] Use [UnmanagedCallersOnly] instead of [MonoPInvokeCallback] for the CGPDFDictionaryApplyFunction function. (#12956)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Tests were not ran (VSTS: device tests iOS). ⚠️

Results were skipped for this run due to provisioning problems Azure Devops. Please contact the bot administrator.

Pipeline on Agent
[.NET/CoreFoundation] Use [UnmanagedCallersOnly] instead of [MonoPInvokeCallback] for the CGPDFDictionaryApplyFunction function. (#12956)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Tests passed on macOS M1 - Mac Big Sur (11.5) ✅

Tests passed

All tests on macOS X M1 - Mac Big Sur (11.5) passed.

Pipeline on Agent
[.NET/CoreFoundation] Use [UnmanagedCallersOnly] instead of [MonoPInvokeCallback] for the CGPDFDictionaryApplyFunction function. (#12956)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ Tests failed on macOS Mac Mojave (10.14) ❌

Tests failed on Mac Mojave (10.14).

Failed tests are:

  • introspection
  • linksdk

Pipeline on Agent
[.NET/CoreFoundation] Use [UnmanagedCallersOnly] instead of [MonoPInvokeCallback] for the CGPDFDictionaryApplyFunction function. (#12956)

Please sign in to comment.