private static void ValidateSetMessageSendPendingExceptionImpl(MessageSendFunction msgSend) { if (!LibObjC.SupportedOnPlatform(msgSend)) { return; } IntPtr func = msgSend switch { MessageSendFunction.MsgSend => (IntPtr)(delegate * unmanaged <IntPtr, IntPtr, IntPtr>) & MsgSend, MessageSendFunction.MsgSendFpret => (IntPtr)(delegate * unmanaged <IntPtr, IntPtr, IntPtr>) & MsgSendFpret, MessageSendFunction.MsgSendStret => (IntPtr)(delegate * unmanaged <IntPtr *, IntPtr, IntPtr, void>) & MsgSendStret, MessageSendFunction.MsgSendSuper => (IntPtr)(delegate * unmanaged <IntPtr, IntPtr, IntPtr>) & MsgSendSuper, MessageSendFunction.MsgSendSuperStret => (IntPtr)(delegate * unmanaged <IntPtr *, IntPtr, IntPtr, void>) & MsgSendSuperStret, _ => throw new Exception($"Unknown {nameof(MessageSendFunction)}"), }; // Override message send function // // We are using the overriding mechanism to enable validating in the Libraries test suite. // Technically any Objective-C code that is entered via msgSend could call the managed SetMessageSendPendingException() // and it would be thrown when returning from the P/Invoke. This approach avoids us having to // create a pure Objective-C library for testing this behavior. ObjectiveCMarshal.SetMessageSendCallback(msgSend, func); // Call message send function through P/Invoke IntPtr inst = IntPtr.Zero; IntPtr sel = IntPtr.Zero; Exception ex = Assert.Throws <PendingException>(() => LibObjC.CallPInvoke(msgSend, inst, sel)); Assert.Equal(msgSend.ToString(), ex.Message); }
public static IntPtr CallPInvoke(MessageSendFunction msgSend, IntPtr inst, IntPtr sel) { switch (msgSend) { case MessageSendFunction.MsgSend: return(LibObjC.objc_msgSend(inst, sel)); case MessageSendFunction.MsgSendFpret: return(LibObjC.objc_msgSend_fpret(inst, sel)); case MessageSendFunction.MsgSendStret: { IntPtr ret; LibObjC.objc_msgSend_stret(out ret, inst, sel); return(ret); } case MessageSendFunction.MsgSendSuper: return(LibObjC.objc_msgSendSuper(inst, sel)); case MessageSendFunction.MsgSendSuperStret: { IntPtr ret; LibObjC.objc_msgSendSuper_stret(out ret, inst, sel); return(ret); } default: throw new ArgumentException($"Unknown {nameof(MessageSendFunction)}: {msgSend}"); } }