[runtime] Implement native-to-managed marshalling of byref delegate arguments. Fixes...
authorZoltan Varga <vargaz@gmail.com>
Wed, 4 Nov 2015 21:27:29 +0000 (16:27 -0500)
committerZoltan Varga <vargaz@gmail.com>
Wed, 4 Nov 2015 21:27:35 +0000 (16:27 -0500)
mono/metadata/marshal.c
mono/tests/libtest.c
mono/tests/pinvoke3.cs

index 78ed91e9e727d9df2c1cae8952b91f5dc8001f58..fa1807cd77bb9f06da8f5b4254b632a1c374cc1f 100644 (file)
@@ -5625,10 +5625,11 @@ emit_marshal_object (EmitMarshalContext *m, int argnum, MonoType *t,
                conv_arg = mono_mb_add_local (mb, &klass->byval_arg);
 
                if (klass->delegate) {
-                       g_assert (!t->byref);
                        mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
                        mono_mb_emit_op (mb, CEE_MONO_CLASSCONST, klass);
                        mono_mb_emit_ldarg (mb, argnum);
+                       if (t->byref)
+                               mono_mb_emit_byte (mb, CEE_LDIND_I);
                        mono_mb_emit_icall (mb, conv_to_icall (MONO_MARSHAL_CONV_FTN_DEL));
                        mono_mb_emit_stloc (mb, conv_arg);
                        break;
@@ -5701,6 +5702,16 @@ emit_marshal_object (EmitMarshalContext *m, int argnum, MonoType *t,
                break;
 
        case MARSHAL_ACTION_MANAGED_CONV_OUT:
+               if (klass->delegate) {
+                       if (t->byref) {
+                               mono_mb_emit_ldarg (mb, argnum);
+                               mono_mb_emit_ldloc (mb, conv_arg);
+                               mono_mb_emit_icall (mb, conv_to_icall (MONO_MARSHAL_CONV_DEL_FTN));
+                               mono_mb_emit_byte (mb, CEE_STIND_I);
+                               break;
+                       }
+               }
+
                if (t->byref) {
                        /* Check for null */
                        mono_mb_emit_ldloc (mb, conv_arg);
index aa58c852c36607f97f196a5d5ced44333e5dfe48..7a6f7d1e17225cc62eeed3efcd415b61ed187eb9 100644 (file)
@@ -876,6 +876,18 @@ mono_test_marshal_return_delegate (SimpleDelegate delegate)
        return delegate;
 }
 
+typedef int DelegateByrefDelegate (void *);
+
+LIBTEST_API int STDCALL
+mono_test_marshal_delegate_ref_delegate (DelegateByrefDelegate del)
+{
+       int (*ptr) (int i);
+
+       del (&ptr);
+
+       return ptr (54);
+}
+
 static int STDCALL
 return_plus_one (int i)
 {
index ae64d16e090d7af29dd027197a083e92bb2d7845..4b9a4b7f49a300186eb95ef72be806e21d897bfe 100644 (file)
@@ -188,6 +188,9 @@ public class Tests {
        [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate_delegate")]
        public static extern int mono_test_marshal_return_delegate_delegate (ReturnDelegateDelegate d);
 
+       [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate_ref_delegate")]
+       public static extern int mono_test_marshal_delegate_ref_delegate (DelegateByrefDelegate del);
+
        public delegate int TestDelegate (int a, ref SimpleStruct ss, int b);
 
        public delegate SimpleStruct SimpleDelegate2 (SimpleStruct ss);
@@ -210,6 +213,8 @@ public class Tests {
 
        public delegate return_int_delegate ReturnDelegateDelegate ();
 
+       public delegate int DelegateByrefDelegate (ref return_int_delegate del);
+
        public static int Main () {
                return TestDriver.RunTests (typeof (Tests));
        }
@@ -333,6 +338,20 @@ public class Tests {
                return mono_test_marshal_return_delegate_delegate (new ReturnDelegateDelegate (return_delegate));
        }
 
+       public static int return_plus_1 (int i) {
+               return i + 1;
+       }
+
+       public static int ref_delegate_delegate (ref return_int_delegate del) {
+               del = return_plus_1;
+               return 0;
+       }
+
+       public static int test_55_marshal_delegate_ref_delegate () {
+               var del = new DelegateByrefDelegate (ref_delegate_delegate);
+               return mono_test_marshal_delegate_ref_delegate (del);
+       }
+
        /* Passing and returning strings */
 
        public delegate String ReturnStringDelegate (String s);