Author: miguel
Date: 2007-02-10 22:40:33 -0500 (Sat, 10 Feb 2007)
New Revision: 72601

Added:
   trunk/mono/mono/tests/handleref.cs
Modified:
   trunk/mono/mono/metadata/ChangeLog
   trunk/mono/mono/metadata/class-internals.h
   trunk/mono/mono/metadata/domain.c
   trunk/mono/mono/metadata/marshal.c
   trunk/mono/mono/metadata/metadata.c
   trunk/mono/mono/metadata/metadata.h
   trunk/mono/mono/tests/Makefile.am
Log:
Add support for HandleRef to the Marshaller

2007-02-10  Miguel de Icaza  <[EMAIL PROTECTED]>

        * class-internals.h: Add MonoHandleRef definition, and
        handleref_class to mono_defaults. 

        * metadata.c (mono_type_to_unmanaged): If we find HandleRefs in a
        structure, use new conversion MONO_MARSHAL_CONV_HANDLEREF.

        * marshal.c (emit_ptr_to_object_conv): Add support for HandleRefs
        (do nothing on this stage)
        (emit_object_to_ptr_conv): Extract the handle from the HandleRef.  
        (emit_marshal_handleref): New method, used for argument handling
        of HandleRefs. 

Tests are a simplified version of SafeHandle tests

Fixes: 80515



Modified: trunk/mono/mono/metadata/ChangeLog
===================================================================
--- trunk/mono/mono/metadata/ChangeLog  2007-02-11 03:36:56 UTC (rev 72600)
+++ trunk/mono/mono/metadata/ChangeLog  2007-02-11 03:40:33 UTC (rev 72601)
@@ -1,3 +1,17 @@
+2007-02-10  Miguel de Icaza  <[EMAIL PROTECTED]>
+
+       * class-internals.h: Add MonoHandleRef definition, and
+       handleref_class to mono_defaults. 
+
+       * metadata.c (mono_type_to_unmanaged): If we find HandleRefs in a
+       structure, use new conversion MONO_MARSHAL_CONV_HANDLEREF.
+
+       * marshal.c (emit_ptr_to_object_conv): Add support for HandleRefs
+       (do nothing on this stage)
+       (emit_object_to_ptr_conv): Extract the handle from the HandleRef.  
+       (emit_marshal_handleref): New method, used for argument handling
+       of HandleRefs. 
+
 2007-02-08  Jonathan Chambers  <[EMAIL PROTECTED]>
 
        * class.c (mono_class_setup_parent): Lazily init com types.

Modified: trunk/mono/mono/metadata/class-internals.h
===================================================================
--- trunk/mono/mono/metadata/class-internals.h  2007-02-11 03:36:56 UTC (rev 
72600)
+++ trunk/mono/mono/metadata/class-internals.h  2007-02-11 03:40:33 UTC (rev 
72601)
@@ -528,6 +528,14 @@
        void       *handle;
 } MonoSafeHandle;
 
+/*
+ * Keep in sync with HandleRef.cs
+ */
+typedef struct {
+       MonoObject *wrapper;
+       void       *handle;
+} MonoHandleRef;
+
 extern MonoStats mono_stats MONO_INTERNAL;
 
 typedef gpointer (*MonoTrampoline)       (MonoMethod *method);
@@ -692,6 +700,7 @@
        MonoClass *iunknown_class;
        MonoClass *idispatch_class;
        MonoClass *safehandle_class;
+       MonoClass *handleref_class;
 } MonoDefaults;
 
 extern MonoDefaults mono_defaults MONO_INTERNAL;

Modified: trunk/mono/mono/metadata/domain.c
===================================================================
--- trunk/mono/mono/metadata/domain.c   2007-02-11 03:36:56 UTC (rev 72600)
+++ trunk/mono/mono/metadata/domain.c   2007-02-11 03:40:33 UTC (rev 72601)
@@ -815,6 +815,9 @@
        mono_defaults.safehandle_class = mono_class_from_name (
                mono_defaults.corlib, "System.Runtime.InteropServices", 
"SafeHandle");
 
+       mono_defaults.handleref_class = mono_class_from_name (
+               mono_defaults.corlib, "System.Runtime.InteropServices", 
"HandleRef");
+       
        /* these are initialized lazily when COM features are used */
        mono_defaults.variant_class = NULL;
        mono_defaults.com_object_class = NULL;

Modified: trunk/mono/mono/metadata/marshal.c
===================================================================
--- trunk/mono/mono/metadata/marshal.c  2007-02-11 03:36:56 UTC (rev 72600)
+++ trunk/mono/mono/metadata/marshal.c  2007-02-11 03:40:33 UTC (rev 72601)
@@ -2025,6 +2025,14 @@
                break;
        }
                
+       case MONO_MARSHAL_CONV_HANDLEREF: {
+               /*
+                * Passing HandleRefs in a struct that is ref()ed does not 
+                * copy the values back to the HandleRef
+                */
+               break;
+       }
+               
        case MONO_MARSHAL_CONV_STR_BSTR:
        case MONO_MARSHAL_CONV_STR_ANSIBSTR:
        case MONO_MARSHAL_CONV_STR_TBSTR:
@@ -2415,6 +2423,16 @@
                mono_mb_emit_byte (mb, CEE_STIND_I);
                break;
        }
+
+       case MONO_MARSHAL_CONV_HANDLEREF: {
+               mono_mb_emit_ldloc (mb, 1);
+               mono_mb_emit_ldloc (mb, 0);
+               mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoHandleRef, 
handle));
+               mono_mb_emit_byte (mb, CEE_LDIND_I);
+               mono_mb_emit_byte (mb, CEE_STIND_I);
+               break;
+       }
+               
        default: {
                char *msg = g_strdup_printf ("marshalling conversion %d not 
implemented", conv);
                MonoException *exc = mono_get_exception_not_implemented (msg);
@@ -6442,6 +6460,66 @@
 }
 
 static int
+emit_marshal_handleref (EmitMarshalContext *m, int argnum, MonoType *t, 
+                       MonoMarshalSpec *spec, int conv_arg, 
+                       MonoType **conv_arg_type, MarshalAction action)
+{
+       MonoMethodBuilder *mb = m->mb;
+
+       switch (action){
+       case MARSHAL_ACTION_CONV_IN: {
+               MonoType *intptr_type;
+
+               intptr_type = &mono_defaults.int_class->byval_arg;
+               conv_arg = mono_mb_add_local (mb, intptr_type);
+               *conv_arg_type = intptr_type;
+
+               if (t->byref){
+                       mono_mb_emit_exception_marshal_directive (mb,
+                               "HandleRefs can not be returned from unmanaged 
code (or passed by ref)");
+                       break;
+               } 
+               mono_mb_emit_ldarg (mb, argnum);
+               mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoHandleRef, 
handle));
+               mono_mb_emit_byte (mb, CEE_LDIND_I);
+               mono_mb_emit_stloc (mb, conv_arg);
+               break;
+       }
+
+       case MARSHAL_ACTION_PUSH:
+               mono_mb_emit_ldloc (mb, conv_arg);
+               break;
+
+       case MARSHAL_ACTION_CONV_OUT: {
+               /* no resource release required */
+               break;
+       }
+               
+       case MARSHAL_ACTION_CONV_RESULT: {
+               mono_mb_emit_exception_marshal_directive (mb,
+                       "HandleRefs can not be returned from unmanaged code (or 
passed by ref)");
+               break;
+       }
+               
+       case MARSHAL_ACTION_MANAGED_CONV_IN:
+               fprintf (stderr, "mono/marshal: SafeHandles missing 
MANAGED_CONV_IN\n");
+               break;
+               
+       case MARSHAL_ACTION_MANAGED_CONV_OUT:
+               fprintf (stderr, "mono/marshal: SafeHandles missing 
MANAGED_CONV_OUT\n");
+               break;
+
+       case MARSHAL_ACTION_MANAGED_CONV_RESULT:
+               fprintf (stderr, "mono/marshal: SafeHandles missing 
MANAGED_CONV_RESULT\n");
+               break;
+       default:
+               fprintf (stderr, "Unhandled case for MarshalAction: %d\n", 
action);
+       }
+
+       return conv_arg;
+}
+
+static int
 emit_marshal_object (EmitMarshalContext *m, int argnum, MonoType *t,
                     MonoMarshalSpec *spec, 
                     int conv_arg, MonoType **conv_arg_type, 
@@ -7879,9 +7957,9 @@
 
 static int
 emit_marshal_boolean (EmitMarshalContext *m, int argnum, MonoType *t,
-                                         MonoMarshalSpec *spec, 
-                                         int conv_arg, MonoType 
**conv_arg_type, 
-                                         MarshalAction action)
+                     MonoMarshalSpec *spec, 
+                     int conv_arg, MonoType **conv_arg_type, 
+                     MarshalAction action)
 {
        MonoMethodBuilder *mb = m->mb;
 
@@ -7949,8 +8027,8 @@
 
 static int
 emit_marshal_ptr (EmitMarshalContext *m, int argnum, MonoType *t, 
-                                 MonoMarshalSpec *spec, int conv_arg, 
-                                 MonoType **conv_arg_type, MarshalAction 
action)
+                 MonoMarshalSpec *spec, int conv_arg, 
+                 MonoType **conv_arg_type, MarshalAction action)
 {
        MonoMethodBuilder *mb = m->mb;
 
@@ -7980,8 +8058,8 @@
 
 static int
 emit_marshal_char (EmitMarshalContext *m, int argnum, MonoType *t, 
-                                  MonoMarshalSpec *spec, int conv_arg, 
-                                  MonoType **conv_arg_type, MarshalAction 
action)
+                  MonoMarshalSpec *spec, int conv_arg, 
+                  MonoType **conv_arg_type, MarshalAction action)
 {
        MonoMethodBuilder *mb = m->mb;
 
@@ -8007,8 +8085,8 @@
 
 static int
 emit_marshal_scalar (EmitMarshalContext *m, int argnum, MonoType *t, 
-                                        MonoMarshalSpec *spec, int conv_arg, 
-                                        MonoType **conv_arg_type, 
MarshalAction action)
+                    MonoMarshalSpec *spec, int conv_arg, 
+                    MonoType **conv_arg_type, MarshalAction action)
 {
        MonoMethodBuilder *mb = m->mb;
 
@@ -8031,8 +8109,8 @@
 
 static int
 emit_marshal (EmitMarshalContext *m, int argnum, MonoType *t, 
-                         MonoMarshalSpec *spec, int conv_arg, 
-                         MonoType **conv_arg_type, MarshalAction action)
+             MonoMarshalSpec *spec, int conv_arg, 
+             MonoType **conv_arg_type, MarshalAction action)
 {
        /* Ensure that we have marshalling info for this param */
        mono_marshal_load_type_info (mono_class_from_mono_type (t));
@@ -8045,6 +8123,9 @@
                        
        switch (t->type) {
        case MONO_TYPE_VALUETYPE:
+               if (t->data.klass == mono_defaults.handleref_class)
+                       return emit_marshal_handleref (m, argnum, t, spec, 
conv_arg, conv_arg_type, action);
+               
                return emit_marshal_vtype (m, argnum, t, spec, conv_arg, 
conv_arg_type, action);
        case MONO_TYPE_STRING:
                return emit_marshal_string (m, argnum, t, spec, conv_arg, 
conv_arg_type, action);

Modified: trunk/mono/mono/metadata/metadata.c
===================================================================
--- trunk/mono/mono/metadata/metadata.c 2007-02-11 03:36:56 UTC (rev 72600)
+++ trunk/mono/mono/metadata/metadata.c 2007-02-11 03:40:33 UTC (rev 72601)
@@ -4225,6 +4225,9 @@
                        t = type->data.klass->enum_basetype->type;
                        goto handle_enum;
                }
+               if (type->data.klass == mono_defaults.handleref_class)
+                       *conv = MONO_MARSHAL_CONV_HANDLEREF;
+                       return MONO_NATIVE_INT;
                return MONO_NATIVE_STRUCT;
        case MONO_TYPE_SZARRAY: 
        case MONO_TYPE_ARRAY: 

Modified: trunk/mono/mono/metadata/metadata.h
===================================================================
--- trunk/mono/mono/metadata/metadata.h 2007-02-11 03:36:56 UTC (rev 72600)
+++ trunk/mono/mono/metadata/metadata.h 2007-02-11 03:40:33 UTC (rev 72601)
@@ -177,7 +177,8 @@
        MONO_MARSHAL_CONV_FTN_DEL,
        MONO_MARSHAL_FREE_ARRAY,
        MONO_MARSHAL_CONV_BSTR_STR,
-       MONO_MARSHAL_CONV_SAFEHANDLE
+       MONO_MARSHAL_CONV_SAFEHANDLE,
+       MONO_MARSHAL_CONV_HANDLEREF
 } MonoMarshalConv;
 
 typedef struct {

Modified: trunk/mono/mono/tests/Makefile.am
===================================================================
--- trunk/mono/mono/tests/Makefile.am   2007-02-11 03:36:56 UTC (rev 72600)
+++ trunk/mono/mono/tests/Makefile.am   2007-02-11 03:40:33 UTC (rev 72601)
@@ -226,7 +226,8 @@
        subthread-exit.cs       \
        desweak.cs              \
        cominterop.cs           \
-       exists.cs
+       exists.cs               \
+       handleref.cs
 
 if X86
 # bug #71274

Added: trunk/mono/mono/tests/handleref.cs
===================================================================
--- trunk/mono/mono/tests/handleref.cs  2007-02-11 03:36:56 UTC (rev 72600)
+++ trunk/mono/mono/tests/handleref.cs  2007-02-11 03:40:33 UTC (rev 72601)
@@ -0,0 +1,109 @@
+using System;
+using System.Runtime.InteropServices;
+
+public class Tests {
+
+       //
+       // This is not permitted, should throw an exception
+       //
+       [DllImport ("libtest")]
+       public static extern void mono_safe_handle_ref (ref HandleRef handle);
+
+       [DllImport ("libtest", EntryPoint="mono_xr")]
+       public static extern HandleRef mono_xr_as_handle (HandleRef r);
+
+       [DllImport ("libtest")]
+       public static extern int mono_xr (HandleRef sh);
+
+       //
+       // Mono should throw exceptions on ref HandleRefs
+       //
+       public static int test_0_ref_handleref ()
+       {
+               object o = new object ();
+               HandleRef s = new HandleRef (o, (IntPtr) 0xeadcafe);
+               try {
+                       mono_safe_handle_ref (ref s);
+               } catch (MarshalDirectiveException){
+                       return 0;
+               }
+               // failed
+               return 1;
+       }
+
+       //
+       // Mono should throw excentions on return HandleRefs
+       //
+       public static int test_0_handleref_return ()
+       {
+               object o = new object ();
+               HandleRef s = new HandleRef (o, (IntPtr) 0xeadcafe);
+               try {
+                       HandleRef ret = mono_xr_as_handle (s);
+               } catch (MarshalDirectiveException){
+                       return 0;
+               }
+               // failed
+               return 1;
+       }
+       
+       public static int test_0_marshal_handleref_argument ()
+       {
+               object o = new object ();
+               Console.WriteLine ("BEFORE");
+               HandleRef s = new HandleRef (o, (IntPtr) 0xeadcafe);
+               if (mono_xr (s) != (0xeadcafe + 1234))
+                       return 1;
+               Console.WriteLine ("AFTER");
+               return 0;
+       }
+
+        [StructLayout (LayoutKind.Sequential)]
+       public struct StructTest {
+               public int a;
+               public HandleRef handle1;
+               public HandleRef handle2;
+               public int b;
+       }
+
+       [DllImport ("libtest")]
+       public static extern int mono_safe_handle_struct_ref (ref StructTest 
test);
+
+       [DllImport ("libtest")]
+       public static extern int mono_safe_handle_struct (StructTest test);
+
+       static StructTest x = new StructTest ();
+
+       public static int test_0_marshal_safehandle_field ()
+       {
+               x.a = 1234;
+               x.b = 8743;
+               object o = new object ();
+               x.handle1 = new HandleRef (o, (IntPtr) 0x7080feed);
+               x.handle2 = new HandleRef (o, (IntPtr) 0x1234abcd);
+
+               if (mono_safe_handle_struct (x) != 0xf00f)
+                       return 1;
+
+               return 0;
+       }
+
+       public static int test_0_marshal_safehandle_field_ref ()
+       {
+               x.a = 1234;
+               x.b = 8743;
+               object o = new object ();
+               x.handle1 = new HandleRef (o, (IntPtr) 0x7080feed);
+               x.handle2 = new HandleRef (o, (IntPtr) 0x1234abcd);
+               
+               if (mono_safe_handle_struct_ref (ref x) != 0xf00d)
+                       return 1;
+
+               return 0;
+       }
+       
+       static int Main ()
+       {
+               return TestDriver.RunTests (typeof (Tests));
+       }
+}


Property changes on: trunk/mono/mono/tests/handleref.cs
___________________________________________________________________
Name: svn:eol-style
   + native

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to