2 * marshal.c: Routines for marshaling complex types in P/Invoke methods.
5 * Paolo Molaro (lupus@ximian.com)
7 * (C) 2002 Ximian, Inc. http://www.ximian.com
14 #include "metadata/marshal.h"
15 #include "metadata/tabledefs.h"
16 #include "metadata/exception.h"
17 #include "metadata/appdomain.h"
18 #include "mono/metadata/debug-helpers.h"
19 #include "mono/metadata/threadpool.h"
20 #include "mono/metadata/threads.h"
21 #include "mono/metadata/monitor.h"
22 #include "mono/metadata/metadata-internals.h"
23 #include "mono/metadata/domain-internals.h"
24 #include "mono/metadata/gc-internal.h"
25 #include "mono/metadata/threads-types.h"
26 #include "mono/metadata/string-icalls.h"
27 #include <mono/os/gc_wrapper.h>
31 /* #define DEBUG_RUNTIME_CODE */
33 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
37 MONO_MARSHAL_NONE
, /* No marshalling needed */
38 MONO_MARSHAL_COPY
, /* Can be copied by value to the new domain */
39 MONO_MARSHAL_COPY_OUT
, /* out parameter that needs to be copied back to the original instance */
40 MONO_MARSHAL_SERIALIZE
/* Value needs to be serialized into the new domain */
41 } MonoXDomainMarshalType
;
44 #include "mono/cil/opcode.def"
49 struct _MonoMethodBuilder
{
55 guint32 code_size
, pos
;
59 struct _MonoRemotingMethods
{
61 MonoMethod
*invoke_with_check
;
62 MonoMethod
*xdomain_invoke
;
63 MonoMethod
*xdomain_dispatch
;
66 typedef struct _MonoRemotingMethods MonoRemotingMethods
;
68 #ifdef DEBUG_RUNTIME_CODE
70 indenter (MonoDisHelper
*dh
, MonoMethod
*method
, guint32 ip_offset
)
72 return g_strdup (" ");
75 static MonoDisHelper marshal_dh
= {
86 * This mutex protects the various marshalling related caches in MonoImage
87 * and a few other data structures static to this file.
88 * Note that when this lock is held it is not possible to take other runtime
89 * locks like the loader lock.
91 #define mono_marshal_lock() EnterCriticalSection (&marshal_mutex)
92 #define mono_marshal_unlock() LeaveCriticalSection (&marshal_mutex)
93 static CRITICAL_SECTION marshal_mutex
;
95 /* This mutex protects the various cominterop related caches in MonoImage */
96 #define mono_cominterop_lock() EnterCriticalSection (&cominterop_mutex)
97 #define mono_cominterop_unlock() LeaveCriticalSection (&cominterop_mutex)
98 static CRITICAL_SECTION cominterop_mutex
;
100 /* Maps wrapper methods to the methods they wrap */
101 static GHashTable
*wrapper_hash
;
103 static guint32 last_error_tls_id
;
105 static guint32 load_type_info_tls_id
;
108 delegate_hash_table_add (MonoDelegate
*d
);
111 emit_struct_conv (MonoMethodBuilder
*mb
, MonoClass
*klass
, gboolean to_object
);
114 mono_struct_delete_old (MonoClass
*klass
, char *ptr
);
117 mono_marshal_string_to_utf16 (MonoString
*s
);
120 mono_string_to_lpstr (MonoString
*string_obj
);
123 mono_string_from_bstr (gpointer bstr
);
126 mono_free_bstr (gpointer bstr
);
129 mono_byvalarray_to_array (MonoArray
*arr
, gpointer native_arr
, MonoClass
*eltype
, guint32 elnum
);
132 mono_array_to_byvalarray (gpointer native_arr
, MonoArray
*arr
, MonoClass
*eltype
, guint32 elnum
);
135 mono_remoting_wrapper (MonoMethod
*method
, gpointer
*params
);
137 static MonoAsyncResult
*
138 mono_delegate_begin_invoke (MonoDelegate
*delegate
, gpointer
*params
);
141 mono_delegate_end_invoke (MonoDelegate
*delegate
, gpointer
*params
);
144 mono_marshal_xdomain_copy_value (MonoObject
*val
);
147 mono_marshal_xdomain_copy_out_value (MonoObject
*src
, MonoObject
*dst
);
150 mono_marshal_set_domain_by_id (gint32 id
, MonoBoolean push
);
153 mono_marshal_check_domain_image (gint32 domain_id
, MonoImage
*image
);
156 mono_upgrade_remote_class_wrapper (MonoReflectionType
*rtype
, MonoTransparentProxy
*tproxy
);
158 static MonoReflectionType
*
159 type_from_handle (MonoType
*handle
);
162 mono_marshal_set_last_error_windows (int error
);
165 mono_marshal_emit_native_wrapper (MonoImage
*image
, MonoMethodBuilder
*mb
, MonoMethodSignature
*sig
, MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
**mspecs
, gpointer func
);
167 static void init_safe_handle (void);
169 /* MonoMethod pointers to SafeHandle::DangerousAddRef and ::DangerousRelease */
170 static MonoMethod
*sh_dangerous_add_ref
;
171 static MonoMethod
*sh_dangerous_release
;
177 sh_dangerous_add_ref
= mono_class_get_method_from_name (
178 mono_defaults
.safehandle_class
, "DangerousAddRef", 1);
179 sh_dangerous_release
= mono_class_get_method_from_name (
180 mono_defaults
.safehandle_class
, "DangerousRelease", 0);
184 register_icall (gpointer func
, const char *name
, const char *sigstr
, gboolean save
)
186 MonoMethodSignature
*sig
= mono_create_icall_signature (sigstr
);
188 mono_register_jit_icall (func
, name
, sig
, save
);
191 static MonoMethodSignature
*
192 signature_dup (MonoImage
*image
, MonoMethodSignature
*sig
)
194 MonoMethodSignature
*res
;
197 sigsize
= sizeof (MonoMethodSignature
) + ((sig
->param_count
- MONO_ZERO_LEN_ARRAY
) * sizeof (MonoType
*));
199 res
= mono_mempool_alloc (image
->mempool
, sigsize
);
200 mono_loader_unlock ();
201 memcpy (res
, sig
, sigsize
);
206 static MonoMethodSignature
*
207 signature_no_pinvoke (MonoMethod
*method
)
209 MonoMethodSignature
*sig
= mono_method_signature (method
);
211 sig
= signature_dup (method
->klass
->image
, sig
);
212 sig
->pinvoke
= FALSE
;
218 /* Begin COM Interop related stuff until seperate file */
221 /* STDCALL on windows, CDECL everywhere else to work with XPCOM and MainWin COM */
222 #ifdef PLATFORM_WIN32
223 #define STDCALL __stdcall
228 /* Upon creation of a CCW, only allocate a weak handle and set the
229 * reference count to 0. If the unmanaged client code decides to addref and
230 * hold onto the CCW, I then allocate a strong handle. Once the reference count
231 * goes back to 0, convert back to a weak handle.
236 GHashTable
* vtable_hash
;
239 /* This type is the actual pointer passed to unmanaged code
240 * to represent a COM interface.
248 static int STDCALL
cominterop_ccw_addref (MonoCCWInterface
* ccwe
);
250 static int STDCALL
cominterop_ccw_release (MonoCCWInterface
* ccwe
);
252 static int STDCALL
cominterop_ccw_queryinterface (MonoCCWInterface
* ccwe
, guint8
* riid
, gpointer
* ppv
);
255 static int STDCALL
cominterop_ccw_get_type_info_count (MonoCCWInterface
* ccwe
, guint32
*pctinfo
);
257 static int STDCALL
cominterop_ccw_get_type_info (MonoCCWInterface
* ccwe
, guint32 iTInfo
, guint32 lcid
, gpointer
*ppTInfo
);
259 static int STDCALL
cominterop_ccw_get_ids_of_names (MonoCCWInterface
* ccwe
, gpointer riid
,
260 gunichar2
** rgszNames
, guint32 cNames
,
261 guint32 lcid
, gint32
*rgDispId
);
263 static int STDCALL
cominterop_ccw_invoke (MonoCCWInterface
* ccwe
, guint32 dispIdMember
,
264 gpointer riid
, guint32 lcid
,
265 guint16 wFlags
, gpointer pDispParams
,
266 gpointer pVarResult
, gpointer pExcepInfo
,
270 cominterop_get_managed_wrapper_adjusted (MonoMethod
*method
);
273 cominterop_get_ccw (MonoObject
* object
, MonoClass
* itf
);
276 cominterop_get_ccw_object (MonoCCWInterface
* ccw_entry
, gboolean verify
);
279 * cominterop_method_signature:
282 * Returns: the corresponding unmanaged method signature for a managed COM
285 static MonoMethodSignature
*
286 cominterop_method_signature (MonoMethod
* method
)
288 MonoMethodSignature
*res
;
289 MonoImage
*image
= method
->klass
->image
;
290 MonoMethodSignature
*sig
= mono_method_signature (method
);
291 gboolean preserve_sig
= method
->iflags
& METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG
;
294 int param_count
= sig
->param_count
+ 1; // convert this arg into IntPtr arg
296 if (!preserve_sig
&&!MONO_TYPE_IS_VOID (sig
->ret
))
299 sigsize
= sizeof (MonoMethodSignature
) + ((param_count
- MONO_ZERO_LEN_ARRAY
) * sizeof (MonoType
*));
301 res
= mono_mempool_alloc (image
->mempool
, sigsize
);
302 mono_loader_unlock ();
303 memcpy (res
, sig
, sigsize
);
305 // now move args forward one
306 for (i
= sig
->param_count
-1; i
>= 0; i
--)
307 res
->params
[i
+1] = sig
->params
[i
];
309 // first arg is interface pointer
310 res
->params
[0] = &mono_defaults
.int_class
->byval_arg
;
316 // last arg is return type
317 if (!MONO_TYPE_IS_VOID (sig
->ret
)) {
318 res
->params
[param_count
-1] = mono_metadata_type_dup_mp (image
, sig
->ret
);
319 res
->params
[param_count
-1]->byref
= 1;
320 res
->params
[param_count
-1]->attrs
= PARAM_ATTRIBUTE_OUT
;
323 // return type is always int32 (HRESULT)
324 res
->ret
= &mono_defaults
.int32_class
->byval_arg
;
328 res
->pinvoke
= FALSE
;
334 res
->param_count
= param_count
;
336 // STDCALL on windows, CDECL everywhere else to work with XPCOM and MainWin COM
337 #ifdef PLATFORM_WIN32
338 res
->call_convention
= MONO_CALL_STDCALL
;
340 res
->call_convention
= MONO_CALL_C
;
347 * cominterop_get_function_pointer:
348 * @itf: a pointer to the COM interface
349 * @slot: the vtable slot of the method pointer to return
351 * Returns: the unmanaged vtable function pointer from the interface
354 cominterop_get_function_pointer (gpointer itf
, int slot
)
357 func
= *((*(gpointer
**)itf
)+slot
);
362 * cominterop_object_is_com_object:
363 * @obj: a pointer to the object
365 * Returns: a value indicating if the object is a
366 * Runtime Callable Wrapper (RCW) for a COM object
369 cominterop_object_is_rcw (MonoObject
*obj
)
371 MonoClass
*klass
= NULL
;
372 MonoRealProxy
* real_proxy
= NULL
;
375 klass
= mono_object_class (obj
);
376 if (klass
!= mono_defaults
.transparent_proxy_class
)
379 real_proxy
= ((MonoTransparentProxy
*)obj
)->rp
;
383 klass
= mono_object_class (real_proxy
);
384 return (klass
&& klass
== mono_defaults
.com_interop_proxy_class
);
388 cominterop_get_com_slot_begin (MonoClass
* klass
)
390 static MonoClass
*interface_type_attribute
= NULL
;
391 MonoCustomAttrInfo
*cinfo
= NULL
;
392 MonoInterfaceTypeAttribute
* itf_attr
= NULL
;
394 if (!interface_type_attribute
)
395 interface_type_attribute
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.InteropServices", "InterfaceTypeAttribute");
396 cinfo
= mono_custom_attrs_from_class (klass
);
398 itf_attr
= (MonoInterfaceTypeAttribute
*)mono_custom_attrs_get_attr (cinfo
, interface_type_attribute
);
400 mono_custom_attrs_free (cinfo
);
403 if (itf_attr
&& itf_attr
->intType
== 1)
404 return 3; /* 3 methods in IUnknown*/
406 return 7; /* 7 methods in IDispatch*/
410 * cominterop_get_com_slot_for_method:
413 * Returns: the method's slot in the COM interface vtable
416 cominterop_get_com_slot_for_method (MonoMethod
* method
)
418 guint32 slot
= method
->slot
;
420 MonoClass
*ic
= NULL
;
423 ifaces
= mono_class_get_implemented_interfaces (method
->klass
);
426 for (i
= 0; i
< ifaces
->len
; ++i
) {
427 ic
= g_ptr_array_index (ifaces
, i
);
428 offset
= method
->klass
->interface_offsets
[ic
->interface_id
];
429 if (method
->slot
>= offset
&& method
->slot
< offset
+ ic
->method
.count
) {
434 g_ptr_array_free (ifaces
, TRUE
);
441 g_assert (MONO_CLASS_IS_INTERFACE (ic
));
443 return slot
+ cominterop_get_com_slot_begin (ic
);
447 * cominterop_get_method_interface:
448 * @method: method being called
450 * Returns: the Type on which the method is defined on
452 static MonoReflectionType
*
453 cominterop_get_method_interface (MonoMethod
* method
)
457 MonoClass
*ic
= NULL
;
459 MonoReflectionType
* rt
= NULL
;
461 ifaces
= mono_class_get_implemented_interfaces (method
->klass
);
464 for (i
= 0; i
< ifaces
->len
; ++i
) {
465 ic
= g_ptr_array_index (ifaces
, i
);
466 offset
= method
->klass
->interface_offsets
[ic
->interface_id
];
467 if (method
->slot
>= offset
&& method
->slot
< offset
+ ic
->method
.count
)
471 g_ptr_array_free (ifaces
, TRUE
);
478 g_assert (MONO_CLASS_IS_INTERFACE (ic
));
480 t
= mono_class_get_type (ic
);
481 rt
= mono_type_get_object (mono_domain_get(), t
);
487 cominterop_get_hresult_for_exception (MonoException
* exc
)
494 mono_marshal_init (void)
496 static gboolean module_initialized
= FALSE
;
498 if (!module_initialized
) {
499 module_initialized
= TRUE
;
500 InitializeCriticalSection (&marshal_mutex
);
501 InitializeCriticalSection (&cominterop_mutex
);
502 wrapper_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
503 last_error_tls_id
= TlsAlloc ();
504 load_type_info_tls_id
= TlsAlloc ();
506 register_icall (mono_marshal_string_to_utf16
, "mono_marshal_string_to_utf16", "ptr obj", FALSE
);
507 register_icall (mono_string_to_utf16
, "mono_string_to_utf16", "ptr obj", FALSE
);
508 register_icall (mono_string_from_utf16
, "mono_string_from_utf16", "obj ptr", FALSE
);
509 register_icall (mono_string_new_wrapper
, "mono_string_new_wrapper", "obj ptr", FALSE
);
510 register_icall (mono_string_to_utf8
, "mono_string_to_utf8", "ptr obj", FALSE
);
511 register_icall (mono_string_to_lpstr
, "mono_string_to_lpstr", "ptr obj", FALSE
);
512 register_icall (mono_string_to_bstr
, "mono_string_to_bstr", "ptr obj", FALSE
);
513 register_icall (mono_string_from_bstr
, "mono_string_from_bstr", "obj ptr", FALSE
);
514 register_icall (mono_free_bstr
, "mono_free_bstr", "void ptr", FALSE
);
515 register_icall (mono_string_to_ansibstr
, "mono_string_to_ansibstr", "ptr object", FALSE
);
516 register_icall (mono_string_builder_to_utf8
, "mono_string_builder_to_utf8", "ptr object", FALSE
);
517 register_icall (mono_string_builder_to_utf16
, "mono_string_builder_to_utf16", "ptr object", FALSE
);
518 register_icall (mono_array_to_savearray
, "mono_array_to_savearray", "ptr object", FALSE
);
519 register_icall (mono_array_to_lparray
, "mono_array_to_lparray", "ptr object", FALSE
);
520 register_icall (mono_byvalarray_to_array
, "mono_byvalarray_to_array", "void object ptr ptr int32", FALSE
);
521 register_icall (mono_array_to_byvalarray
, "mono_array_to_byvalarray", "void ptr object ptr int32", FALSE
);
522 register_icall (mono_delegate_to_ftnptr
, "mono_delegate_to_ftnptr", "ptr object", FALSE
);
523 register_icall (mono_ftnptr_to_delegate
, "mono_ftnptr_to_delegate", "object ptr ptr", FALSE
);
524 register_icall (mono_marshal_asany
, "mono_marshal_asany", "ptr object int32 int32", FALSE
);
525 register_icall (mono_marshal_free_asany
, "mono_marshal_free_asany", "void object ptr int32 int32", FALSE
);
526 register_icall (mono_marshal_alloc
, "mono_marshal_alloc", "ptr int32", FALSE
);
527 register_icall (mono_marshal_free
, "mono_marshal_free", "void ptr", FALSE
);
528 register_icall (mono_marshal_set_last_error
, "mono_marshal_set_last_error", "void", FALSE
);
529 register_icall (mono_marshal_set_last_error_windows
, "mono_marshal_set_last_error_windows", "void int32", FALSE
);
530 register_icall (mono_string_utf8_to_builder
, "mono_string_utf8_to_builder", "void ptr ptr", FALSE
);
531 register_icall (mono_string_utf16_to_builder
, "mono_string_utf16_to_builder", "void ptr ptr", FALSE
);
532 register_icall (mono_marshal_free_array
, "mono_marshal_free_array", "void ptr int32", FALSE
);
533 register_icall (mono_string_to_byvalstr
, "mono_string_to_byvalstr", "void ptr ptr int32", FALSE
);
534 register_icall (mono_string_to_byvalwstr
, "mono_string_to_byvalwstr", "void ptr ptr int32", FALSE
);
535 register_icall (g_free
, "g_free", "void ptr", FALSE
);
536 register_icall (mono_object_isinst
, "mono_object_isinst", "object object ptr", FALSE
);
537 register_icall (mono_struct_delete_old
, "mono_struct_delete_old", "void ptr ptr", FALSE
);
538 register_icall (mono_remoting_wrapper
, "mono_remoting_wrapper", "object ptr ptr", FALSE
);
539 register_icall (mono_delegate_begin_invoke
, "mono_delegate_begin_invoke", "object object ptr", FALSE
);
540 register_icall (mono_delegate_end_invoke
, "mono_delegate_end_invoke", "object object ptr", FALSE
);
541 register_icall (mono_marshal_xdomain_copy_value
, "mono_marshal_xdomain_copy_value", "object object", FALSE
);
542 register_icall (mono_marshal_xdomain_copy_out_value
, "mono_marshal_xdomain_copy_out_value", "void object object", FALSE
);
543 register_icall (mono_marshal_set_domain_by_id
, "mono_marshal_set_domain_by_id", "int32 int32 int32", FALSE
);
544 register_icall (mono_marshal_check_domain_image
, "mono_marshal_check_domain_image", "int32 int32 ptr", FALSE
);
545 register_icall (mono_compile_method
, "mono_compile_method", "ptr ptr", FALSE
);
546 register_icall (mono_context_get
, "mono_context_get", "object", FALSE
);
547 register_icall (mono_context_set
, "mono_context_set", "void object", FALSE
);
548 register_icall (mono_upgrade_remote_class_wrapper
, "mono_upgrade_remote_class_wrapper", "void object object", FALSE
);
549 register_icall (type_from_handle
, "type_from_handle", "object ptr", FALSE
);
550 register_icall (mono_gc_wbarrier_generic_store
, "wb_generic", "void ptr object", FALSE
);
551 register_icall (cominterop_get_method_interface
, "cominterop_get_method_interface", "object ptr", FALSE
);
552 register_icall (cominterop_get_function_pointer
, "cominterop_get_function_pointer", "ptr ptr int32", FALSE
);
553 register_icall (cominterop_object_is_rcw
, "cominterop_object_is_rcw", "int32 object", FALSE
);
554 register_icall (cominterop_get_ccw
, "cominterop_get_ccw", "ptr object ptr", FALSE
);
555 register_icall (cominterop_get_ccw_object
, "cominterop_get_ccw_object", "object ptr int32", FALSE
);
556 register_icall (cominterop_get_hresult_for_exception
, "cominterop_get_hresult_for_exception", "int32 object", FALSE
);
561 mono_marshal_cleanup (void)
563 g_hash_table_destroy (wrapper_hash
);
564 TlsFree (load_type_info_tls_id
);
565 TlsFree (last_error_tls_id
);
566 DeleteCriticalSection (&marshal_mutex
);
567 DeleteCriticalSection (&cominterop_mutex
);
570 MonoClass
*byte_array_class
;
571 static MonoMethod
*method_rs_serialize
, *method_rs_deserialize
, *method_exc_fixexc
, *method_rs_appdomain_target
;
572 static MonoMethod
*method_set_context
, *method_get_context
;
573 static MonoMethod
*method_set_call_context
, *method_needs_context_sink
, *method_rs_serialize_exc
;
576 mono_remoting_marshal_init (void)
580 static gboolean module_initialized
= FALSE
;
582 if (!module_initialized
) {
583 klass
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.Remoting", "RemotingServices");
584 method_rs_serialize
= mono_class_get_method_from_name (klass
, "SerializeCallData", -1);
585 method_rs_deserialize
= mono_class_get_method_from_name (klass
, "DeserializeCallData", -1);
586 method_rs_serialize_exc
= mono_class_get_method_from_name (klass
, "SerializeExceptionData", -1);
588 klass
= mono_defaults
.real_proxy_class
;
589 method_rs_appdomain_target
= mono_class_get_method_from_name (klass
, "GetAppDomainTarget", -1);
591 klass
= mono_defaults
.exception_class
;
592 method_exc_fixexc
= mono_class_get_method_from_name (klass
, "FixRemotingException", -1);
594 klass
= mono_defaults
.thread_class
;
595 method_get_context
= mono_class_get_method_from_name (klass
, "get_CurrentContext", -1);
597 klass
= mono_defaults
.appdomain_class
;
598 method_set_context
= mono_class_get_method_from_name (klass
, "InternalSetContext", -1);
599 byte_array_class
= mono_array_class_get (mono_defaults
.byte_class
, 1);
601 klass
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.Remoting.Messaging", "CallContext");
602 method_set_call_context
= mono_class_get_method_from_name (klass
, "SetCurrentCallContext", -1);
604 klass
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.Remoting.Contexts", "Context");
605 method_needs_context_sink
= mono_class_get_method_from_name (klass
, "get_NeedsContextSink", -1);
607 module_initialized
= TRUE
;
612 mono_delegate_to_ftnptr (MonoDelegate
*delegate
)
614 MonoMethod
*method
, *wrapper
;
620 if (delegate
->delegate_trampoline
)
621 return delegate
->delegate_trampoline
;
623 klass
= ((MonoObject
*)delegate
)->vtable
->klass
;
624 g_assert (klass
->delegate
);
626 method
= delegate
->method_info
->method
;
628 wrapper
= mono_marshal_get_managed_wrapper (method
, klass
, delegate
->target
);
630 delegate
->delegate_trampoline
= mono_compile_method (wrapper
);
632 // Add the delegate to the delegate hash table
633 delegate_hash_table_add (delegate
);
635 /* when the object is collected, collect the dynamic method, too */
636 mono_object_register_finalizer ((MonoObject
*)delegate
);
638 return delegate
->delegate_trampoline
;
642 * this hash table maps from a delegate trampoline object to a weak reference
643 * of the delegate. As an optimizations with a non-moving GC we store the
644 * object pointer itself, otherwise we use a GC handle.
646 static GHashTable
*delegate_hash_table
;
649 delegate_hash_table_new (void) {
650 return g_hash_table_new (NULL
, NULL
);
654 delegate_hash_table_remove (MonoDelegate
*d
)
656 #ifdef HAVE_MOVING_COLLECTOR
659 mono_marshal_lock ();
660 if (delegate_hash_table
== NULL
)
661 delegate_hash_table
= delegate_hash_table_new ();
662 #ifdef HAVE_MOVING_COLLECTOR
663 gchandle
= GPOINTER_TO_UINT (g_hash_table_lookup (delegate_hash_table
, d
->delegate_trampoline
));
665 g_hash_table_remove (delegate_hash_table
, d
->delegate_trampoline
);
666 mono_marshal_unlock ();
667 #ifdef HAVE_MOVING_COLLECTOR
668 mono_gchandle_free (gchandle
);
673 delegate_hash_table_add (MonoDelegate
*d
)
675 #ifdef HAVE_MOVING_COLLECTOR
676 guint32 gchandle
= mono_gchandle_new_weakref ((MonoObject
*)d
, FALSE
);
678 mono_marshal_lock ();
679 if (delegate_hash_table
== NULL
)
680 delegate_hash_table
= delegate_hash_table_new ();
681 #ifdef HAVE_MOVING_COLLECTOR
682 g_hash_table_insert (delegate_hash_table
, d
->delegate_trampoline
, GUINT_TO_POINTER (gchandle
));
684 g_hash_table_insert (delegate_hash_table
, d
->delegate_trampoline
, d
);
686 mono_marshal_unlock ();
690 mono_ftnptr_to_delegate (MonoClass
*klass
, gpointer ftn
)
692 #ifdef HAVE_MOVING_COLLECTOR
697 mono_marshal_lock ();
698 if (delegate_hash_table
== NULL
)
699 delegate_hash_table
= delegate_hash_table_new ();
701 #ifdef HAVE_MOVING_COLLECTOR
702 gchandle
= GPOINTER_TO_UINT (g_hash_table_lookup (delegate_hash_table
, ftn
));
703 mono_marshal_unlock ();
705 d
= (MonoDelegate
*)mono_gchandle_get_target (gchandle
);
709 d
= g_hash_table_lookup (delegate_hash_table
, ftn
);
710 mono_marshal_unlock ();
713 /* This is a native function, so construct a delegate for it */
714 static MonoClass
*UnmanagedFunctionPointerAttribute
;
715 MonoMethodSignature
*sig
;
717 MonoMarshalSpec
**mspecs
;
718 MonoCustomAttrInfo
*cinfo
;
719 MonoReflectionUnmanagedFunctionPointerAttribute
*attr
;
720 MonoMethod
*invoke
= mono_get_delegate_invoke (klass
);
721 MonoMethodPInvoke piinfo
;
724 memset (&piinfo
, 0, sizeof (piinfo
));
725 if (!UnmanagedFunctionPointerAttribute
)
726 UnmanagedFunctionPointerAttribute
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.InteropServices", "UnmanagedFunctionPointerAttribute");
728 /* The attribute is only available in Net 2.0 */
729 if (UnmanagedFunctionPointerAttribute
) {
731 * The pinvoke attributes are stored in a real custom attribute so we have to
734 cinfo
= mono_custom_attrs_from_class (klass
);
736 attr
= (MonoReflectionUnmanagedFunctionPointerAttribute
*)mono_custom_attrs_get_attr (cinfo
, UnmanagedFunctionPointerAttribute
);
738 piinfo
.piflags
= (attr
->call_conv
<< 8) | (attr
->charset
? (attr
->charset
- 1) * 2 : 1) | attr
->set_last_error
;
741 mono_custom_attrs_free (cinfo
);
745 mspecs
= g_new0 (MonoMarshalSpec
*, mono_method_signature (invoke
)->param_count
+ 1);
746 mono_method_get_marshal_info (invoke
, mspecs
);
747 /* Freed below so don't alloc from mempool */
748 sig
= mono_metadata_signature_dup (mono_method_signature (invoke
));
751 wrapper
= mono_marshal_get_native_func_wrapper (klass
->image
, sig
, &piinfo
, mspecs
, ftn
);
753 for (i
= mono_method_signature (invoke
)->param_count
; i
>= 0; i
--)
755 mono_metadata_free_marshal_spec (mspecs
[i
]);
759 d
= (MonoDelegate
*)mono_object_new (mono_domain_get (), klass
);
760 mono_delegate_ctor ((MonoObject
*)d
, NULL
, mono_compile_method (wrapper
));
763 if (d
->object
.vtable
->domain
!= mono_domain_get ())
764 mono_raise_exception (mono_get_exception_not_supported ("Delegates cannot be marshalled from native code into a domain other than their home domain"));
770 mono_delegate_free_ftnptr (MonoDelegate
*delegate
)
775 delegate_hash_table_remove (delegate
);
777 ptr
= (gpointer
)InterlockedExchangePointer (&delegate
->delegate_trampoline
, NULL
);
779 if (!delegate
->target
) {
780 /* The wrapper method is shared between delegates -> no need to free it */
785 ji
= mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (ptr
));
788 mono_runtime_free_method (mono_object_domain (delegate
), ji
->method
);
793 mono_array_to_savearray (MonoArray
*array
)
798 g_assert_not_reached ();
803 mono_array_to_lparray (MonoArray
*array
)
808 /* fixme: maybe we need to make a copy */
809 return array
->vector
;
813 mono_byvalarray_to_array (MonoArray
*arr
, gpointer native_arr
, MonoClass
*elclass
, guint32 elnum
)
815 g_assert (arr
->obj
.vtable
->klass
->element_class
== mono_defaults
.char_class
);
817 if (elclass
== mono_defaults
.byte_class
) {
818 GError
*error
= NULL
;
822 ut
= g_utf8_to_utf16 (native_arr
, elnum
, NULL
, &items_written
, &error
);
825 memcpy (mono_array_addr (arr
, guint16
, 0), ut
, items_written
* sizeof (guint16
));
829 g_error_free (error
);
832 g_assert_not_reached ();
836 mono_array_to_byvalarray (gpointer native_arr
, MonoArray
*arr
, MonoClass
*elclass
, guint32 elnum
)
838 g_assert (arr
->obj
.vtable
->klass
->element_class
== mono_defaults
.char_class
);
840 if (elclass
== mono_defaults
.byte_class
) {
842 GError
*error
= NULL
;
844 as
= g_utf16_to_utf8 (mono_array_addr (arr
, gunichar2
, 0), mono_array_length (arr
), NULL
, NULL
, &error
);
846 MonoException
*exc
= mono_get_exception_argument ("string", error
->message
);
847 g_error_free (error
);
848 mono_raise_exception (exc
);
851 memcpy (native_arr
, as
, MIN (strlen (as
), elnum
));
854 g_assert_not_reached ();
859 mono_string_utf8_to_builder (MonoStringBuilder
*sb
, char *text
)
861 GError
*error
= NULL
;
871 ut
= g_utf8_to_utf16 (text
, l
, NULL
, &items_written
, &error
);
873 if (items_written
> mono_stringbuilder_capacity (sb
))
874 items_written
= mono_stringbuilder_capacity (sb
);
877 if (! sb
->str
|| sb
->str
== sb
->cached_str
) {
878 MONO_OBJECT_SETREF (sb
, str
, mono_string_new_size (mono_domain_get (), items_written
));
879 sb
->cached_str
= NULL
;
882 memcpy (mono_string_chars (sb
->str
), ut
, items_written
* 2);
883 sb
->length
= items_written
;
885 g_error_free (error
);
891 * FIXME: This routine does not seem to do what it seems to do
892 * the @text is never copied into the string builder
895 mono_string_utf16_to_builder (MonoStringBuilder
*sb
, gunichar2
*text
)
902 g_assert (mono_string_chars (sb
->str
) == text
);
904 for (len
= 0; text
[len
] != 0; ++len
)
911 * mono_string_builder_to_utf8:
912 * @sb: the string builder
914 * Converts to utf8 the contents of the MonoStringBuilder.
916 * Returns: a utf8 string with the contents of the StringBuilder.
918 * The return value must be released with g_free.
921 mono_string_builder_to_utf8 (MonoStringBuilder
*sb
)
923 GError
*error
= NULL
;
930 if ((sb
->str
== sb
->cached_str
) && (sb
->str
->length
== 0)) {
932 * The sb could have been allocated with the default capacity and be empty.
933 * we need to alloc a buffer of the default capacity in this case.
935 MONO_OBJECT_SETREF (sb
, str
, mono_string_new_size (mono_domain_get (), 16));
936 sb
->cached_str
= NULL
;
939 res
= mono_marshal_alloc (mono_stringbuilder_capacity (sb
) + 1);
941 tmp
= g_utf16_to_utf8 (mono_string_chars (sb
->str
), sb
->length
, NULL
, res
, &error
);
943 g_error_free (error
);
944 mono_marshal_free (res
);
945 mono_raise_exception (mono_get_exception_execution_engine ("Failed to convert StringBuilder from utf16 to utf8"));
947 memcpy (res
, tmp
, sb
->length
+ 1);
955 * mono_string_builder_to_utf16:
956 * @sb: the string builder
958 * Converts to utf16 the contents of the MonoStringBuilder.
960 * Returns: a utf16 string with the contents of the StringBuilder.
962 * The return value must not be freed.
965 mono_string_builder_to_utf16 (MonoStringBuilder
*sb
)
973 * The stringbuilder might not have ownership of this string. If this is
974 * the case, we must duplicate the string, so that we don't munge immutable
977 if (sb
->str
== sb
->cached_str
) {
979 * The sb could have been allocated with the default capacity and be empty.
980 * we need to alloc a buffer of the default capacity in this case.
982 if (sb
->str
->length
== 0)
983 MONO_OBJECT_SETREF (sb
, str
, mono_string_new_size (mono_domain_get (), 16));
985 MONO_OBJECT_SETREF (sb
, str
, mono_string_new_utf16 (mono_domain_get (), mono_string_chars (sb
->str
), mono_stringbuilder_capacity (sb
)));
986 sb
->cached_str
= NULL
;
989 return mono_string_chars (sb
->str
);
993 mono_string_to_lpstr (MonoString
*s
)
995 #ifdef PLATFORM_WIN32
998 GError
*error
= NULL
;
1004 as
= CoTaskMemAlloc (1);
1009 tmp
= g_utf16_to_utf8 (mono_string_chars (s
), s
->length
, NULL
, &len
, &error
);
1011 MonoException
*exc
= mono_get_exception_argument ("string", error
->message
);
1012 g_error_free (error
);
1013 mono_raise_exception(exc
);
1016 as
= CoTaskMemAlloc (len
+ 1);
1017 memcpy (as
, tmp
, len
+ 1);
1022 return mono_string_to_utf8 (s
);
1027 mono_string_to_ansibstr (MonoString
*string_obj
)
1029 g_error ("UnmanagedMarshal.BStr is not implemented.");
1034 mono_string_to_bstr (MonoString
*string_obj
)
1036 #ifdef PLATFORM_WIN32
1039 return SysAllocStringLen (mono_string_chars (string_obj
), mono_string_length (string_obj
));
1041 int slen
= mono_string_length (string_obj
);
1042 char *ret
= g_malloc (slen
* 2 + 4 + 2);
1045 memcpy (ret
+ 4, mono_string_chars (string_obj
), slen
* 2);
1046 * ((guint32
*) ret
) = slen
* 2;
1047 ret
[4 + slen
* 2] = 0;
1048 ret
[5 + slen
* 2] = 0;
1055 mono_string_from_bstr (gpointer bstr
)
1057 #ifdef PLATFORM_WIN32
1060 return mono_string_new_utf16 (mono_domain_get (), bstr
, SysStringLen (bstr
));
1062 return mono_string_new_utf16 (mono_domain_get (), bstr
, *(guint32
*)((char *)bstr
- 4));
1067 mono_free_bstr (gpointer bstr
)
1069 #ifdef PLATFORM_WIN32
1070 SysFreeString ((BSTR
)bstr
);
1072 g_free (((char *)bstr
) - 4);
1077 * mono_string_to_byvalstr:
1078 * @dst: Where to store the null-terminated utf8 decoded string.
1079 * @src: the MonoString to copy.
1080 * @size: the maximum number of bytes to copy.
1082 * Copies the MonoString pointed to by @src as a utf8 string
1083 * into @dst, it copies at most @size bytes into the destination.
1086 mono_string_to_byvalstr (gpointer dst
, MonoString
*src
, int size
)
1091 g_assert (dst
!= NULL
);
1092 g_assert (size
> 0);
1094 memset (dst
, 0, size
);
1098 s
= mono_string_to_utf8 (src
);
1099 len
= MIN (size
, strlen (s
));
1102 memcpy (dst
, s
, len
);
1107 * mono_string_to_byvalwstr:
1108 * @dst: Where to store the null-terminated utf16 decoded string.
1109 * @src: the MonoString to copy.
1110 * @size: the maximum number of bytes to copy.
1112 * Copies the MonoString pointed to by @src as a utf16 string into
1113 * @dst, it copies at most @size bytes into the destination (including
1114 * a terminating 16-bit zero terminator).
1117 mono_string_to_byvalwstr (gpointer dst
, MonoString
*src
, int size
)
1121 g_assert (dst
!= NULL
);
1122 g_assert (size
> 1);
1125 memset (dst
, 0, size
* 2);
1129 len
= MIN (size
, (mono_string_length (src
)));
1130 memcpy (dst
, mono_string_chars (src
), size
* 2);
1131 if (size
<= mono_string_length (src
))
1133 *((gunichar2
*) dst
+ len
) = 0;
1137 mono_mb_free (MonoMethodBuilder
*mb
)
1139 g_list_free (mb
->locals_list
);
1141 g_free (mb
->method
);
1149 mono_mb_new (MonoClass
*klass
, const char *name
, MonoWrapperType type
)
1151 MonoMethodBuilder
*mb
;
1154 g_assert (klass
!= NULL
);
1155 g_assert (name
!= NULL
);
1157 mb
= g_new0 (MonoMethodBuilder
, 1);
1159 mb
->method
= m
= (MonoMethod
*)g_new0 (MonoMethodWrapper
, 1);
1163 m
->wrapper_type
= type
;
1165 mb
->name
= g_strdup (name
);
1167 mb
->code
= g_malloc (mb
->code_size
);
1173 mono_mb_add_local (MonoMethodBuilder
*mb
, MonoType
*type
)
1177 g_assert (mb
!= NULL
);
1178 g_assert (type
!= NULL
);
1181 mb
->locals_list
= g_list_append (mb
->locals_list
, type
);
1188 * mono_mb_create_method:
1190 * Create a MonoMethod from this method builder.
1191 * Returns: the newly created method.
1193 * LOCKING: Takes the loader lock.
1196 mono_mb_create_method (MonoMethodBuilder
*mb
, MonoMethodSignature
*signature
, int max_stack
)
1198 MonoMethodHeader
*header
;
1199 MonoMethodWrapper
*mw
;
1205 g_assert (mb
!= NULL
);
1207 mp
= mb
->method
->klass
->image
->mempool
;
1209 mono_loader_lock ();
1211 method
= mb
->method
;
1213 method
->name
= mb
->name
;
1214 method
->dynamic
= TRUE
;
1216 ((MonoMethodNormal
*)method
)->header
= header
= (MonoMethodHeader
*)
1217 g_malloc0 (sizeof (MonoMethodHeader
) + mb
->locals
* sizeof (MonoType
*));
1219 header
->code
= mb
->code
;
1221 /* Realloc the method info into a mempool */
1223 method
= mono_mempool_alloc (mp
, sizeof (MonoMethodWrapper
));
1224 memcpy (method
, mb
->method
, sizeof (MonoMethodWrapper
));
1226 method
->name
= mono_mempool_strdup (mp
, mb
->name
);
1228 ((MonoMethodNormal
*)method
)->header
= header
= (MonoMethodHeader
*)
1229 mono_mempool_alloc0 (mp
, sizeof (MonoMethodHeader
) + mb
->locals
* sizeof (MonoType
*));
1231 header
->code
= mono_mempool_alloc (mp
, mb
->pos
);
1232 memcpy ((char*)header
->code
, mb
->code
, mb
->pos
);
1238 header
->max_stack
= max_stack
;
1240 for (i
= 0, l
= mb
->locals_list
; l
; l
= l
->next
, i
++) {
1241 header
->locals
[i
] = (MonoType
*)l
->data
;
1244 method
->signature
= signature
;
1246 header
->code_size
= mb
->pos
;
1247 header
->num_locals
= mb
->locals
;
1248 header
->init_locals
= TRUE
;
1250 mw
= (MonoMethodWrapper
*) mb
->method
;
1251 i
= g_list_length (mw
->method_data
);
1255 l
= g_list_reverse (mw
->method_data
);
1256 if (method
->dynamic
)
1257 data
= g_malloc (sizeof (gpointer
) * (i
+ 1));
1259 data
= mono_mempool_alloc (mp
, sizeof (gpointer
) * (i
+ 1));
1260 /* store the size in the first element */
1261 data
[0] = GUINT_TO_POINTER (i
);
1263 for (tmp
= l
; tmp
; tmp
= tmp
->next
) {
1264 data
[i
++] = tmp
->data
;
1268 ((MonoMethodWrapper
*)method
)->method_data
= data
;
1271 static int total_code = 0;
1272 static int total_alloc = 0;
1273 total_code += mb->pos;
1274 total_alloc += mb->code_size;
1275 g_print ("code size: %d of %d (allocated: %d)\n", mb->pos, total_code, total_alloc);
1278 #ifdef DEBUG_RUNTIME_CODE
1279 printf ("RUNTIME CODE FOR %s\n", mono_method_full_name (method
, TRUE
));
1280 printf ("%s\n", mono_disasm_code (&marshal_dh
, method
, mb
->code
, mb
->code
+ mb
->pos
));
1283 mono_loader_unlock ();
1288 mono_mb_add_data (MonoMethodBuilder
*mb
, gpointer data
)
1290 MonoMethodWrapper
*mw
;
1292 g_assert (mb
!= NULL
);
1294 mw
= (MonoMethodWrapper
*)mb
->method
;
1296 /* one O(n) is enough */
1297 mw
->method_data
= g_list_prepend (mw
->method_data
, data
);
1299 return g_list_length (mw
->method_data
);
1303 mono_mb_patch_addr (MonoMethodBuilder
*mb
, int pos
, int value
)
1305 mb
->code
[pos
] = value
& 0xff;
1306 mb
->code
[pos
+ 1] = (value
>> 8) & 0xff;
1307 mb
->code
[pos
+ 2] = (value
>> 16) & 0xff;
1308 mb
->code
[pos
+ 3] = (value
>> 24) & 0xff;
1312 mono_mb_patch_addr_s (MonoMethodBuilder
*mb
, int pos
, gint8 value
)
1314 *((gint8
*)(&mb
->code
[pos
])) = value
;
1318 mono_mb_emit_byte (MonoMethodBuilder
*mb
, guint8 op
)
1320 if (mb
->pos
>= mb
->code_size
) {
1321 mb
->code_size
+= mb
->code_size
>> 1;
1322 mb
->code
= g_realloc (mb
->code
, mb
->code_size
);
1325 mb
->code
[mb
->pos
++] = op
;
1329 mono_mb_emit_ldflda (MonoMethodBuilder
*mb
, gint32 offset
)
1331 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1332 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
1335 mono_mb_emit_icon (mb
, offset
);
1336 mono_mb_emit_byte (mb
, CEE_ADD
);
1341 mono_mb_emit_proxy_check (MonoMethodBuilder
*mb
, int branch_code
)
1344 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoObject
, vtable
));
1345 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1346 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoVTable
, klass
));
1347 mono_mb_emit_byte (mb
, CEE_ADD
);
1348 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1349 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1350 mono_mb_emit_byte (mb
, CEE_MONO_CLASSCONST
);
1351 mono_mb_emit_i4 (mb
, mono_mb_add_data (mb
, mono_defaults
.transparent_proxy_class
));
1352 mono_mb_emit_byte (mb
, branch_code
);
1354 mono_mb_emit_i4 (mb
, 0);
1359 mono_mb_emit_xdomain_check (MonoMethodBuilder
*mb
, int branch_code
)
1362 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
1363 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1364 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoRealProxy
, target_domain_id
));
1365 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
1366 mono_mb_emit_icon (mb
, -1);
1367 mono_mb_emit_byte (mb
, branch_code
);
1369 mono_mb_emit_i4 (mb
, 0);
1374 mono_mb_emit_i4 (MonoMethodBuilder
*mb
, gint32 data
)
1376 if ((mb
->pos
+ 4) >= mb
->code_size
) {
1377 mb
->code_size
+= mb
->code_size
>> 1;
1378 mb
->code
= g_realloc (mb
->code
, mb
->code_size
);
1381 mono_mb_patch_addr (mb
, mb
->pos
, data
);
1386 mono_mb_emit_i2 (MonoMethodBuilder
*mb
, gint16 data
)
1388 if ((mb
->pos
+ 2) >= mb
->code_size
) {
1389 mb
->code_size
+= mb
->code_size
>> 1;
1390 mb
->code
= g_realloc (mb
->code
, mb
->code_size
);
1393 mb
->code
[mb
->pos
] = data
& 0xff;
1394 mb
->code
[mb
->pos
+ 1] = (data
>> 8) & 0xff;
1399 mono_mb_emit_op (MonoMethodBuilder
*mb
, guint8 op
, gpointer data
)
1401 mono_mb_emit_byte (mb
, op
);
1402 mono_mb_emit_i4 (mb
, mono_mb_add_data (mb
, data
));
1406 mono_mb_emit_ldstr (MonoMethodBuilder
*mb
, char *str
)
1408 mono_mb_emit_op (mb
, CEE_LDSTR
, str
);
1412 mono_mb_emit_ldarg (MonoMethodBuilder
*mb
, guint argnum
)
1415 mono_mb_emit_byte (mb
, CEE_LDARG_0
+ argnum
);
1416 } else if (argnum
< 256) {
1417 mono_mb_emit_byte (mb
, CEE_LDARG_S
);
1418 mono_mb_emit_byte (mb
, argnum
);
1420 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
1421 mono_mb_emit_byte (mb
, CEE_LDARG
);
1422 mono_mb_emit_i2 (mb
, argnum
);
1427 mono_mb_emit_ldarg_addr (MonoMethodBuilder
*mb
, guint argnum
)
1430 mono_mb_emit_byte (mb
, CEE_LDARGA_S
);
1431 mono_mb_emit_byte (mb
, argnum
);
1433 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
1434 mono_mb_emit_byte (mb
, CEE_LDARGA
);
1435 mono_mb_emit_i2 (mb
, argnum
);
1440 mono_mb_emit_ldloc_addr (MonoMethodBuilder
*mb
, guint locnum
)
1443 mono_mb_emit_byte (mb
, CEE_LDLOCA_S
);
1444 mono_mb_emit_byte (mb
, locnum
);
1446 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
1447 mono_mb_emit_byte (mb
, CEE_LDLOCA
);
1448 mono_mb_emit_i2 (mb
, locnum
);
1453 mono_mb_emit_ldloc (MonoMethodBuilder
*mb
, guint num
)
1456 mono_mb_emit_byte (mb
, CEE_LDLOC_0
+ num
);
1457 } else if (num
< 256) {
1458 mono_mb_emit_byte (mb
, CEE_LDLOC_S
);
1459 mono_mb_emit_byte (mb
, num
);
1461 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
1462 mono_mb_emit_byte (mb
, CEE_LDLOC
);
1463 mono_mb_emit_i2 (mb
, num
);
1468 mono_mb_emit_stloc (MonoMethodBuilder
*mb
, guint num
)
1471 mono_mb_emit_byte (mb
, CEE_STLOC_0
+ num
);
1472 } else if (num
< 256) {
1473 mono_mb_emit_byte (mb
, CEE_STLOC_S
);
1474 mono_mb_emit_byte (mb
, num
);
1476 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
1477 mono_mb_emit_byte (mb
, CEE_STLOC
);
1478 mono_mb_emit_i2 (mb
, num
);
1483 mono_mb_emit_icon (MonoMethodBuilder
*mb
, gint32 value
)
1485 if (value
>= -1 && value
< 8) {
1486 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
+ value
);
1487 } else if (value
>= -128 && value
<= 127) {
1488 mono_mb_emit_byte (mb
, CEE_LDC_I4_S
);
1489 mono_mb_emit_byte (mb
, value
);
1491 mono_mb_emit_byte (mb
, CEE_LDC_I4
);
1492 mono_mb_emit_i4 (mb
, value
);
1497 mono_mb_emit_branch (MonoMethodBuilder
*mb
, guint8 op
)
1500 mono_mb_emit_byte (mb
, op
);
1502 mono_mb_emit_i4 (mb
, 0);
1507 mono_mb_emit_short_branch (MonoMethodBuilder
*mb
, guint8 op
)
1510 mono_mb_emit_byte (mb
, op
);
1512 mono_mb_emit_byte (mb
, 0);
1518 mono_mb_patch_branch (MonoMethodBuilder
*mb
, guint32 pos
)
1520 mono_mb_patch_addr (mb
, pos
, mb
->pos
- (pos
+ 4));
1524 mono_mb_patch_short_branch (MonoMethodBuilder
*mb
, guint32 pos
)
1526 mono_mb_patch_addr_s (mb
, pos
, mb
->pos
- (pos
+ 1));
1530 mono_mb_emit_ptr (MonoMethodBuilder
*mb
, gpointer ptr
)
1532 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1533 mono_mb_emit_op (mb
, CEE_MONO_LDPTR
, ptr
);
1537 mono_mb_emit_calli (MonoMethodBuilder
*mb
, MonoMethodSignature
*sig
)
1539 mono_mb_emit_op (mb
, CEE_CALLI
, sig
);
1543 mono_mb_emit_managed_call (MonoMethodBuilder
*mb
, MonoMethod
*method
, MonoMethodSignature
*opt_sig
)
1545 mono_mb_emit_op (mb
, CEE_CALL
, method
);
1549 mono_mb_emit_native_call (MonoMethodBuilder
*mb
, MonoMethodSignature
*sig
, gpointer func
)
1551 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1552 mono_mb_emit_byte (mb
, CEE_MONO_SAVE_LMF
);
1553 mono_mb_emit_ptr (mb
, func
);
1554 mono_mb_emit_calli (mb
, sig
);
1555 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1556 mono_mb_emit_byte (mb
, CEE_MONO_RESTORE_LMF
);
1560 mono_mb_emit_icall (MonoMethodBuilder
*mb
, gpointer func
)
1562 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1563 mono_mb_emit_op (mb
, CEE_MONO_ICALL
, func
);
1567 mono_mb_emit_cominterop_call (MonoMethodBuilder
*mb
, MonoMethodSignature
*sig
, MonoMethod
* method
)
1569 // get function pointer from 1st arg, the COM interface pointer
1570 mono_mb_emit_ldarg (mb
, 0);
1571 mono_mb_emit_icon (mb
, cominterop_get_com_slot_for_method (method
));
1572 mono_mb_emit_icall (mb
, cominterop_get_function_pointer
);
1574 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1575 mono_mb_emit_byte (mb
, CEE_MONO_SAVE_LMF
);
1576 mono_mb_emit_calli (mb
, sig
);
1577 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1578 mono_mb_emit_byte (mb
, CEE_MONO_RESTORE_LMF
);
1582 mono_mb_emit_exception_full (MonoMethodBuilder
*mb
, const char *exc_nspace
, const char *exc_name
, const char *msg
)
1584 MonoMethod
*ctor
= NULL
;
1586 MonoClass
*mme
= mono_class_from_name (mono_defaults
.corlib
, exc_nspace
, exc_name
);
1587 mono_class_init (mme
);
1588 ctor
= mono_class_get_method_from_name (mme
, ".ctor", 0);
1590 mono_mb_emit_op (mb
, CEE_NEWOBJ
, ctor
);
1592 mono_mb_emit_byte (mb
, CEE_DUP
);
1593 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoException
, message
));
1594 mono_mb_emit_ldstr (mb
, (char*)msg
);
1595 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1597 mono_mb_emit_byte (mb
, CEE_THROW
);
1601 mono_mb_emit_exception (MonoMethodBuilder
*mb
, const char *exc_name
, const char *msg
)
1603 mono_mb_emit_exception_full (mb
, "System", exc_name
, msg
);
1607 mono_mb_emit_exception_marshal_directive (MonoMethodBuilder
*mb
, const char *msg
)
1609 mono_mb_emit_exception_full (mb
, "System.Runtime.InteropServices", "MarshalDirectiveException", msg
);
1613 mono_mb_emit_add_to_local (MonoMethodBuilder
*mb
, guint16 local
, gint32 incr
)
1615 mono_mb_emit_ldloc (mb
, local
);
1616 mono_mb_emit_icon (mb
, incr
);
1617 mono_mb_emit_byte (mb
, CEE_ADD
);
1618 mono_mb_emit_stloc (mb
, local
);
1622 mono_type_to_ldind (MonoType
*type
)
1628 switch (type
->type
) {
1630 return CEE_LDIND_I1
;
1632 case MONO_TYPE_BOOLEAN
:
1633 return CEE_LDIND_U1
;
1635 return CEE_LDIND_I2
;
1637 case MONO_TYPE_CHAR
:
1638 return CEE_LDIND_U2
;
1640 return CEE_LDIND_I4
;
1642 return CEE_LDIND_U4
;
1646 case MONO_TYPE_FNPTR
:
1648 case MONO_TYPE_CLASS
:
1649 case MONO_TYPE_STRING
:
1650 case MONO_TYPE_OBJECT
:
1651 case MONO_TYPE_SZARRAY
:
1652 case MONO_TYPE_ARRAY
:
1653 return CEE_LDIND_REF
;
1656 return CEE_LDIND_I8
;
1658 return CEE_LDIND_R4
;
1660 return CEE_LDIND_R8
;
1661 case MONO_TYPE_VALUETYPE
:
1662 if (type
->data
.klass
->enumtype
) {
1663 type
= type
->data
.klass
->enum_basetype
;
1667 case MONO_TYPE_TYPEDBYREF
:
1669 case MONO_TYPE_GENERICINST
:
1670 type
= &type
->data
.generic_class
->container_class
->byval_arg
;
1673 g_error ("unknown type 0x%02x in type_to_ldind", type
->type
);
1679 mono_type_to_stind (MonoType
*type
)
1685 switch (type
->type
) {
1688 case MONO_TYPE_BOOLEAN
:
1689 return CEE_STIND_I1
;
1692 case MONO_TYPE_CHAR
:
1693 return CEE_STIND_I2
;
1696 return CEE_STIND_I4
;
1700 case MONO_TYPE_FNPTR
:
1702 case MONO_TYPE_CLASS
:
1703 case MONO_TYPE_STRING
:
1704 case MONO_TYPE_OBJECT
:
1705 case MONO_TYPE_SZARRAY
:
1706 case MONO_TYPE_ARRAY
:
1707 return CEE_STIND_REF
;
1710 return CEE_STIND_I8
;
1712 return CEE_STIND_R4
;
1714 return CEE_STIND_R8
;
1715 case MONO_TYPE_VALUETYPE
:
1716 if (type
->data
.klass
->enumtype
) {
1717 type
= type
->data
.klass
->enum_basetype
;
1721 case MONO_TYPE_TYPEDBYREF
:
1723 case MONO_TYPE_GENERICINST
:
1724 type
= &type
->data
.generic_class
->container_class
->byval_arg
;
1727 g_error ("unknown type 0x%02x in type_to_stind", type
->type
);
1733 emit_ptr_to_object_conv (MonoMethodBuilder
*mb
, MonoType
*type
, MonoMarshalConv conv
, MonoMarshalSpec
*mspec
)
1736 case MONO_MARSHAL_CONV_BOOL_I4
:
1737 mono_mb_emit_ldloc (mb
, 1);
1738 mono_mb_emit_ldloc (mb
, 0);
1739 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
1740 mono_mb_emit_byte (mb
, CEE_BRFALSE_S
);
1741 mono_mb_emit_byte (mb
, 3);
1742 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
1743 mono_mb_emit_byte (mb
, CEE_BR_S
);
1744 mono_mb_emit_byte (mb
, 1);
1745 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
1746 mono_mb_emit_byte (mb
, CEE_STIND_I1
);
1748 case MONO_MARSHAL_CONV_BOOL_VARIANTBOOL
:
1749 mono_mb_emit_ldloc (mb
, 1);
1750 mono_mb_emit_ldloc (mb
, 0);
1751 mono_mb_emit_byte (mb
, CEE_LDIND_I2
);
1752 mono_mb_emit_byte (mb
, CEE_BRFALSE_S
);
1753 mono_mb_emit_byte (mb
, 3);
1754 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
1755 mono_mb_emit_byte (mb
, CEE_BR_S
);
1756 mono_mb_emit_byte (mb
, 1);
1757 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
1758 mono_mb_emit_byte (mb
, CEE_STIND_I1
);
1760 case MONO_MARSHAL_CONV_ARRAY_BYVALARRAY
: {
1761 MonoClass
*eklass
= NULL
;
1764 if (type
->type
== MONO_TYPE_SZARRAY
) {
1765 eklass
= type
->data
.klass
;
1767 g_assert_not_reached ();
1770 esize
= mono_class_native_size (eklass
, NULL
);
1772 /* create a new array */
1773 mono_mb_emit_ldloc (mb
, 1);
1774 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
);
1775 mono_mb_emit_op (mb
, CEE_NEWARR
, eklass
);
1776 mono_mb_emit_byte (mb
, CEE_STIND_I
);
1778 if (eklass
->blittable
) {
1779 /* copy the elements */
1780 mono_mb_emit_ldloc (mb
, 1);
1781 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1782 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoArray
, vector
));
1783 mono_mb_emit_byte (mb
, CEE_ADD
);
1784 mono_mb_emit_ldloc (mb
, 0);
1785 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
* esize
);
1786 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
1787 mono_mb_emit_byte (mb
, CEE_CPBLK
);
1790 int array_var
, src_var
, dst_var
, index_var
;
1791 guint32 label2
, label3
;
1793 array_var
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
1794 src_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1795 dst_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1798 mono_mb_emit_ldloc (mb
, 1);
1799 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1800 mono_mb_emit_stloc (mb
, array_var
);
1802 /* save the old src pointer */
1803 mono_mb_emit_ldloc (mb
, 0);
1804 mono_mb_emit_stloc (mb
, src_var
);
1805 /* save the old dst pointer */
1806 mono_mb_emit_ldloc (mb
, 1);
1807 mono_mb_emit_stloc (mb
, dst_var
);
1809 /* Emit marshalling loop */
1810 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1811 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
1812 mono_mb_emit_stloc (mb
, index_var
);
1816 mono_mb_emit_ldloc (mb
, index_var
);
1817 mono_mb_emit_ldloc (mb
, array_var
);
1818 mono_mb_emit_byte (mb
, CEE_LDLEN
);
1819 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
1821 /* src is already set */
1824 mono_mb_emit_ldloc (mb
, array_var
);
1825 mono_mb_emit_ldloc (mb
, index_var
);
1826 mono_mb_emit_op (mb
, CEE_LDELEMA
, eklass
);
1827 mono_mb_emit_stloc (mb
, 1);
1829 /* Do the conversion */
1830 emit_struct_conv (mb
, eklass
, TRUE
);
1833 mono_mb_emit_add_to_local (mb
, index_var
, 1);
1835 mono_mb_emit_byte (mb
, CEE_BR
);
1836 mono_mb_emit_i4 (mb
, label2
- (mb
->pos
+ 4));
1838 mono_mb_patch_branch (mb
, label3
);
1840 /* restore the old src pointer */
1841 mono_mb_emit_ldloc (mb
, src_var
);
1842 mono_mb_emit_stloc (mb
, 0);
1843 /* restore the old dst pointer */
1844 mono_mb_emit_ldloc (mb
, dst_var
);
1845 mono_mb_emit_stloc (mb
, 1);
1849 case MONO_MARSHAL_CONV_ARRAY_BYVALCHARARRAY
: {
1850 MonoClass
*eclass
= mono_defaults
.char_class
;
1852 /* create a new array */
1853 mono_mb_emit_ldloc (mb
, 1);
1854 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
);
1855 mono_mb_emit_op (mb
, CEE_NEWARR
, eclass
);
1856 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1858 mono_mb_emit_ldloc (mb
, 1);
1859 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1860 mono_mb_emit_ldloc (mb
, 0);
1861 mono_mb_emit_ptr (mb
, mono_defaults
.byte_class
);
1862 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
);
1863 mono_mb_emit_icall (mb
, mono_byvalarray_to_array
);
1866 case MONO_MARSHAL_CONV_STR_BYVALSTR
:
1867 mono_mb_emit_ldloc (mb
, 1);
1868 mono_mb_emit_ldloc (mb
, 0);
1869 mono_mb_emit_icall (mb
, mono_string_new_wrapper
);
1870 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1872 case MONO_MARSHAL_CONV_STR_BYVALWSTR
:
1873 mono_mb_emit_ldloc (mb
, 1);
1874 mono_mb_emit_ldloc (mb
, 0);
1875 mono_mb_emit_icall (mb
, mono_string_from_utf16
);
1876 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1878 case MONO_MARSHAL_CONV_STR_LPTSTR
:
1879 case MONO_MARSHAL_CONV_STR_LPSTR
:
1880 mono_mb_emit_ldloc (mb
, 1);
1881 mono_mb_emit_ldloc (mb
, 0);
1882 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1883 mono_mb_emit_icall (mb
, mono_string_new_wrapper
);
1884 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1886 case MONO_MARSHAL_CONV_STR_LPWSTR
:
1887 mono_mb_emit_ldloc (mb
, 1);
1888 mono_mb_emit_ldloc (mb
, 0);
1889 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1890 mono_mb_emit_icall (mb
, mono_string_from_utf16
);
1891 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1893 case MONO_MARSHAL_CONV_OBJECT_STRUCT
: {
1894 MonoClass
*klass
= mono_class_from_mono_type (type
);
1895 int src_var
, dst_var
;
1897 src_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1898 dst_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1900 /* *dst = new object */
1901 mono_mb_emit_ldloc (mb
, 1);
1902 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1903 mono_mb_emit_op (mb
, CEE_MONO_NEWOBJ
, klass
);
1904 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1906 /* save the old src pointer */
1907 mono_mb_emit_ldloc (mb
, 0);
1908 mono_mb_emit_stloc (mb
, src_var
);
1909 /* save the old dst pointer */
1910 mono_mb_emit_ldloc (mb
, 1);
1911 mono_mb_emit_stloc (mb
, dst_var
);
1913 /* dst = pointer to newly created object data */
1914 mono_mb_emit_ldloc (mb
, 1);
1915 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1916 mono_mb_emit_icon (mb
, sizeof (MonoObject
));
1917 mono_mb_emit_byte (mb
, CEE_ADD
);
1918 mono_mb_emit_stloc (mb
, 1);
1920 emit_struct_conv (mb
, klass
, TRUE
);
1922 /* restore the old src pointer */
1923 mono_mb_emit_ldloc (mb
, src_var
);
1924 mono_mb_emit_stloc (mb
, 0);
1925 /* restore the old dst pointer */
1926 mono_mb_emit_ldloc (mb
, dst_var
);
1927 mono_mb_emit_stloc (mb
, 1);
1930 case MONO_MARSHAL_CONV_DEL_FTN
: {
1931 MonoClass
*klass
= mono_class_from_mono_type (type
);
1933 mono_mb_emit_ldloc (mb
, 1);
1934 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1935 mono_mb_emit_op (mb
, CEE_MONO_CLASSCONST
, klass
);
1936 mono_mb_emit_ldloc (mb
, 0);
1937 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1938 mono_mb_emit_icall (mb
, mono_ftnptr_to_delegate
);
1939 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1942 case MONO_MARSHAL_CONV_ARRAY_LPARRAY
:
1943 g_error ("Structure field of type %s can't be marshalled as LPArray", mono_class_from_mono_type (type
)->name
);
1945 case MONO_MARSHAL_CONV_OBJECT_INTERFACE
:
1946 case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN
:
1947 case MONO_MARSHAL_CONV_OBJECT_IDISPATCH
: {
1948 static MonoClass
* com_interop_proxy_class
= NULL
;
1949 static MonoMethod
* com_interop_proxy_get_proxy
= NULL
;
1950 static MonoMethod
* get_transparent_proxy
= NULL
;
1952 guint32 pos_null
= 0, pos_ccw
= 0, pos_end
= 0;
1953 MonoClass
*klass
= NULL
;
1955 /* COM types are initialized lazily */
1956 mono_init_com_types ();
1958 klass
= mono_class_from_mono_type (type
);
1960 mono_mb_emit_ldloc (mb
, 1);
1961 mono_mb_emit_byte (mb
, CEE_LDNULL
);
1962 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1964 mono_mb_emit_ldloc (mb
, 0);
1965 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1966 pos_null
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
1968 /* load dst to store later */
1969 mono_mb_emit_ldloc (mb
, 1);
1971 mono_mb_emit_ldloc (mb
, 0);
1972 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1973 mono_mb_emit_icon (mb
, TRUE
);
1974 mono_mb_emit_icall (mb
, cominterop_get_ccw_object
);
1975 pos_ccw
= mono_mb_emit_short_branch (mb
, CEE_BRTRUE_S
);
1977 if (!com_interop_proxy_class
)
1978 com_interop_proxy_class
= mono_class_from_name (mono_defaults
.corlib
, "Mono.Interop", "ComInteropProxy");
1979 if (!com_interop_proxy_get_proxy
)
1980 com_interop_proxy_get_proxy
= mono_class_get_method_from_name_flags (com_interop_proxy_class
, "GetProxy", 2, METHOD_ATTRIBUTE_PRIVATE
);
1981 if (!get_transparent_proxy
)
1982 get_transparent_proxy
= mono_class_get_method_from_name (mono_defaults
.real_proxy_class
, "GetTransparentProxy", 0);
1984 real_proxy
= mono_mb_add_local (mb
, &com_interop_proxy_class
->byval_arg
);
1986 mono_mb_emit_ldloc (mb
, 0);
1987 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1988 mono_mb_emit_ptr (mb
, &mono_defaults
.com_object_class
->byval_arg
);
1989 mono_mb_emit_icall (mb
, type_from_handle
);
1990 mono_mb_emit_managed_call (mb
, com_interop_proxy_get_proxy
, NULL
);
1991 mono_mb_emit_managed_call (mb
, get_transparent_proxy
, NULL
);
1992 if (conv
== MONO_MARSHAL_CONV_OBJECT_INTERFACE
) {
1994 mono_mb_emit_op (mb
, CEE_CASTCLASS
, klass
);
1996 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1997 pos_end
= mono_mb_emit_short_branch (mb
, CEE_BR_S
);
1999 /* is already managed object */
2000 mono_mb_patch_short_branch (mb
, pos_ccw
);
2001 mono_mb_emit_ldloc (mb
, 0);
2002 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
2003 mono_mb_emit_icon (mb
, TRUE
);
2004 mono_mb_emit_icall (mb
, cominterop_get_ccw_object
);
2006 if (conv
== MONO_MARSHAL_CONV_OBJECT_INTERFACE
) {
2008 mono_mb_emit_op (mb
, CEE_CASTCLASS
, klass
);
2010 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
2012 mono_mb_patch_short_branch (mb
, pos_end
);
2014 mono_mb_patch_short_branch (mb
, pos_null
);
2018 case MONO_MARSHAL_CONV_SAFEHANDLE
: {
2020 * Passing SafeHandles as ref does not allow the unmanaged code
2021 * to change the SafeHandle value. If the value is changed,
2022 * we should issue a diagnostic exception (NotSupportedException)
2023 * that informs the user that changes to handles in unmanaged code
2026 * Since we currently have no access to the original
2027 * SafeHandle that was used during the marshalling,
2028 * for now we just ignore this, and ignore/discard any
2029 * changes that might have happened to the handle.
2034 case MONO_MARSHAL_CONV_HANDLEREF
: {
2036 * Passing HandleRefs in a struct that is ref()ed does not
2037 * copy the values back to the HandleRef
2042 case MONO_MARSHAL_CONV_STR_BSTR
:
2043 case MONO_MARSHAL_CONV_STR_ANSIBSTR
:
2044 case MONO_MARSHAL_CONV_STR_TBSTR
:
2045 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY
:
2047 g_warning ("marshaling conversion %d not implemented", conv
);
2048 g_assert_not_reached ();
2053 conv_to_icall (MonoMarshalConv conv
)
2056 case MONO_MARSHAL_CONV_STR_LPWSTR
:
2057 return mono_marshal_string_to_utf16
;
2058 case MONO_MARSHAL_CONV_LPWSTR_STR
:
2059 return mono_string_from_utf16
;
2060 case MONO_MARSHAL_CONV_LPSTR_STR
:
2061 return mono_string_new_wrapper
;
2062 case MONO_MARSHAL_CONV_STR_LPTSTR
:
2063 case MONO_MARSHAL_CONV_STR_LPSTR
:
2064 return mono_string_to_lpstr
;
2065 case MONO_MARSHAL_CONV_STR_BSTR
:
2066 return mono_string_to_bstr
;
2067 case MONO_MARSHAL_CONV_BSTR_STR
:
2068 return mono_string_from_bstr
;
2069 case MONO_MARSHAL_CONV_STR_TBSTR
:
2070 case MONO_MARSHAL_CONV_STR_ANSIBSTR
:
2071 return mono_string_to_ansibstr
;
2072 case MONO_MARSHAL_CONV_SB_LPSTR
:
2073 case MONO_MARSHAL_CONV_SB_LPTSTR
:
2074 return mono_string_builder_to_utf8
;
2075 case MONO_MARSHAL_CONV_SB_LPWSTR
:
2076 return mono_string_builder_to_utf16
;
2077 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY
:
2078 return mono_array_to_savearray
;
2079 case MONO_MARSHAL_CONV_ARRAY_LPARRAY
:
2080 return mono_array_to_lparray
;
2081 case MONO_MARSHAL_CONV_DEL_FTN
:
2082 return mono_delegate_to_ftnptr
;
2083 case MONO_MARSHAL_CONV_FTN_DEL
:
2084 return mono_ftnptr_to_delegate
;
2085 case MONO_MARSHAL_CONV_LPSTR_SB
:
2086 case MONO_MARSHAL_CONV_LPTSTR_SB
:
2087 return mono_string_utf8_to_builder
;
2088 case MONO_MARSHAL_CONV_LPWSTR_SB
:
2089 return mono_string_utf16_to_builder
;
2090 case MONO_MARSHAL_FREE_ARRAY
:
2091 return mono_marshal_free_array
;
2092 case MONO_MARSHAL_CONV_STR_BYVALSTR
:
2093 return mono_string_to_byvalstr
;
2094 case MONO_MARSHAL_CONV_STR_BYVALWSTR
:
2095 return mono_string_to_byvalwstr
;
2097 g_assert_not_reached ();
2104 emit_object_to_ptr_conv (MonoMethodBuilder
*mb
, MonoType
*type
, MonoMarshalConv conv
, MonoMarshalSpec
*mspec
)
2109 case MONO_MARSHAL_CONV_BOOL_I4
:
2110 mono_mb_emit_ldloc (mb
, 1);
2111 mono_mb_emit_ldloc (mb
, 0);
2112 mono_mb_emit_byte (mb
, CEE_LDIND_U1
);
2113 mono_mb_emit_byte (mb
, CEE_STIND_I4
);
2115 case MONO_MARSHAL_CONV_BOOL_VARIANTBOOL
:
2116 mono_mb_emit_ldloc (mb
, 1);
2117 mono_mb_emit_ldloc (mb
, 0);
2118 mono_mb_emit_byte (mb
, CEE_LDIND_U1
);
2119 mono_mb_emit_byte (mb
, CEE_NEG
);
2120 mono_mb_emit_byte (mb
, CEE_STIND_I2
);
2122 case MONO_MARSHAL_CONV_STR_LPWSTR
:
2123 case MONO_MARSHAL_CONV_STR_LPSTR
:
2124 case MONO_MARSHAL_CONV_STR_LPTSTR
:
2125 case MONO_MARSHAL_CONV_STR_BSTR
:
2126 case MONO_MARSHAL_CONV_STR_ANSIBSTR
:
2127 case MONO_MARSHAL_CONV_STR_TBSTR
: {
2130 /* free space if free == true */
2131 mono_mb_emit_ldloc (mb
, 2);
2132 pos
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
2133 mono_mb_emit_ldloc (mb
, 1);
2134 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
2135 mono_mb_emit_icall (mb
, g_free
);
2136 mono_mb_patch_short_branch (mb
, pos
);
2138 mono_mb_emit_ldloc (mb
, 1);
2139 mono_mb_emit_ldloc (mb
, 0);
2140 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
2141 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
2142 mono_mb_emit_byte (mb
, CEE_STIND_I
);
2145 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY
:
2146 case MONO_MARSHAL_CONV_ARRAY_LPARRAY
:
2147 case MONO_MARSHAL_CONV_DEL_FTN
:
2148 mono_mb_emit_ldloc (mb
, 1);
2149 mono_mb_emit_ldloc (mb
, 0);
2150 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
2151 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
2152 mono_mb_emit_byte (mb
, CEE_STIND_I
);
2154 case MONO_MARSHAL_CONV_STR_BYVALSTR
:
2155 case MONO_MARSHAL_CONV_STR_BYVALWSTR
: {
2158 mono_mb_emit_ldloc (mb
, 1); /* dst */
2159 mono_mb_emit_ldloc (mb
, 0);
2160 mono_mb_emit_byte (mb
, CEE_LDIND_REF
); /* src String */
2161 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
);
2162 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
2165 case MONO_MARSHAL_CONV_ARRAY_BYVALARRAY
: {
2166 MonoClass
*eklass
= NULL
;
2169 if (type
->type
== MONO_TYPE_SZARRAY
) {
2170 eklass
= type
->data
.klass
;
2172 g_assert_not_reached ();
2175 if (eklass
->valuetype
)
2176 esize
= mono_class_native_size (eklass
, NULL
);
2178 esize
= sizeof (gpointer
);
2180 mono_mb_emit_ldloc (mb
, 0);
2181 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
2182 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
2184 if (eklass
->blittable
) {
2185 mono_mb_emit_ldloc (mb
, 1);
2186 mono_mb_emit_ldloc (mb
, 0);
2187 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
2188 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoArray
, vector
));
2189 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
* esize
);
2190 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
2191 mono_mb_emit_byte (mb
, CEE_CPBLK
);
2193 int array_var
, src_var
, dst_var
, index_var
;
2194 guint32 label2
, label3
;
2196 array_var
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
2197 src_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
2198 dst_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
2201 mono_mb_emit_ldloc (mb
, 0);
2202 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
2203 mono_mb_emit_stloc (mb
, array_var
);
2205 /* save the old src pointer */
2206 mono_mb_emit_ldloc (mb
, 0);
2207 mono_mb_emit_stloc (mb
, src_var
);
2208 /* save the old dst pointer */
2209 mono_mb_emit_ldloc (mb
, 1);
2210 mono_mb_emit_stloc (mb
, dst_var
);
2212 /* Emit marshalling loop */
2213 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
2214 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
2215 mono_mb_emit_stloc (mb
, index_var
);
2219 mono_mb_emit_ldloc (mb
, index_var
);
2220 mono_mb_emit_ldloc (mb
, array_var
);
2221 mono_mb_emit_byte (mb
, CEE_LDLEN
);
2222 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
2225 mono_mb_emit_ldloc (mb
, array_var
);
2226 mono_mb_emit_ldloc (mb
, index_var
);
2227 mono_mb_emit_op (mb
, CEE_LDELEMA
, eklass
);
2228 mono_mb_emit_stloc (mb
, 0);
2230 /* dst is already set */
2232 /* Do the conversion */
2233 emit_struct_conv (mb
, eklass
, FALSE
);
2236 mono_mb_emit_add_to_local (mb
, index_var
, 1);
2238 mono_mb_emit_byte (mb
, CEE_BR
);
2239 mono_mb_emit_i4 (mb
, label2
- (mb
->pos
+ 4));
2241 mono_mb_patch_branch (mb
, label3
);
2243 /* restore the old src pointer */
2244 mono_mb_emit_ldloc (mb
, src_var
);
2245 mono_mb_emit_stloc (mb
, 0);
2246 /* restore the old dst pointer */
2247 mono_mb_emit_ldloc (mb
, dst_var
);
2248 mono_mb_emit_stloc (mb
, 1);
2251 mono_mb_patch_branch (mb
, pos
);
2254 case MONO_MARSHAL_CONV_ARRAY_BYVALCHARARRAY
: {
2255 mono_mb_emit_ldloc (mb
, 0);
2256 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
2257 pos
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
2259 mono_mb_emit_ldloc (mb
, 1);
2260 mono_mb_emit_ldloc (mb
, 0);
2261 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
2262 mono_mb_emit_ptr (mb
, mono_defaults
.byte_class
);
2263 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
);
2264 mono_mb_emit_icall (mb
, mono_array_to_byvalarray
);
2265 mono_mb_patch_short_branch (mb
, pos
);
2268 case MONO_MARSHAL_CONV_OBJECT_STRUCT
: {
2269 int src_var
, dst_var
;
2271 src_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
2272 dst_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
2274 mono_mb_emit_ldloc (mb
, 0);
2275 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
2276 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
2278 /* save the old src pointer */
2279 mono_mb_emit_ldloc (mb
, 0);
2280 mono_mb_emit_stloc (mb
, src_var
);
2281 /* save the old dst pointer */
2282 mono_mb_emit_ldloc (mb
, 1);
2283 mono_mb_emit_stloc (mb
, dst_var
);
2285 /* src = pointer to object data */
2286 mono_mb_emit_ldloc (mb
, 0);
2287 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
2288 mono_mb_emit_icon (mb
, sizeof (MonoObject
));
2289 mono_mb_emit_byte (mb
, CEE_ADD
);
2290 mono_mb_emit_stloc (mb
, 0);
2292 emit_struct_conv (mb
, mono_class_from_mono_type (type
), FALSE
);
2294 /* restore the old src pointer */
2295 mono_mb_emit_ldloc (mb
, src_var
);
2296 mono_mb_emit_stloc (mb
, 0);
2297 /* restore the old dst pointer */
2298 mono_mb_emit_ldloc (mb
, dst_var
);
2299 mono_mb_emit_stloc (mb
, 1);
2301 mono_mb_patch_branch (mb
, pos
);
2304 case MONO_MARSHAL_CONV_OBJECT_INTERFACE
:
2305 case MONO_MARSHAL_CONV_OBJECT_IDISPATCH
:
2306 case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN
: {
2307 guint32 pos_null
= 0, pos_rcw
= 0, pos_end
= 0;
2309 /* COM types are initialized lazily */
2310 mono_init_com_types ();
2313 mono_mb_emit_ldloc (mb
, 1);
2314 mono_mb_emit_icon (mb
, 0);
2315 mono_mb_emit_byte (mb
, CEE_CONV_U
);
2316 mono_mb_emit_byte (mb
, CEE_STIND_I
);
2318 mono_mb_emit_ldloc (mb
, 0);
2319 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
2321 // if null just break, dst was already inited to 0
2322 pos_null
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
2324 mono_mb_emit_ldloc (mb
, 0);
2325 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
2326 mono_mb_emit_icall (mb
, cominterop_object_is_rcw
);
2327 pos_rcw
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
2329 // load dst to store later
2330 mono_mb_emit_ldloc (mb
, 1);
2333 mono_mb_emit_ldloc (mb
, 0);
2334 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
2335 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
2336 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
2338 /* load the RCW from the ComInteropProxy*/
2339 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoComInteropProxy
, com_object
));
2340 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
2342 if (conv
== MONO_MARSHAL_CONV_OBJECT_INTERFACE
) {
2343 static MonoMethod
* GetInterface
= NULL
;
2346 GetInterface
= mono_class_get_method_from_name (mono_defaults
.com_object_class
, "GetInterface", 1);
2347 mono_mb_emit_ptr (mb
, type
);
2348 mono_mb_emit_icall (mb
, type_from_handle
);
2349 mono_mb_emit_managed_call (mb
, GetInterface
, NULL
);
2351 else if (conv
== MONO_MARSHAL_CONV_OBJECT_IUNKNOWN
) {
2352 static MonoProperty
* iunknown
= NULL
;
2355 iunknown
= mono_class_get_property_from_name (mono_defaults
.com_object_class
, "IUnknown");
2356 mono_mb_emit_managed_call (mb
, iunknown
->get
, NULL
);
2358 else if (conv
== MONO_MARSHAL_CONV_OBJECT_IDISPATCH
) {
2359 static MonoProperty
* idispatch
= NULL
;
2362 idispatch
= mono_class_get_property_from_name (mono_defaults
.com_object_class
, "IDispatch");
2363 mono_mb_emit_managed_call (mb
, idispatch
->get
, NULL
);
2366 g_assert_not_reached ();
2368 mono_mb_emit_byte (mb
, CEE_STIND_I
);
2369 pos_end
= mono_mb_emit_short_branch (mb
, CEE_BR_S
);
2372 mono_mb_patch_short_branch (mb
, pos_rcw
);
2373 /* load dst to store later */
2374 mono_mb_emit_ldloc (mb
, 1);
2376 mono_mb_emit_ldloc (mb
, 0);
2377 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
2379 if (conv
== MONO_MARSHAL_CONV_OBJECT_INTERFACE
)
2380 mono_mb_emit_ptr (mb
, mono_type_get_class (type
));
2381 else if (conv
== MONO_MARSHAL_CONV_OBJECT_IUNKNOWN
)
2382 mono_mb_emit_ptr (mb
, mono_defaults
.iunknown_class
);
2383 else if (conv
== MONO_MARSHAL_CONV_OBJECT_IDISPATCH
)
2384 mono_mb_emit_ptr (mb
, mono_defaults
.idispatch_class
);
2386 g_assert_not_reached ();
2387 mono_mb_emit_icall (mb
, cominterop_get_ccw
);
2388 mono_mb_emit_byte (mb
, CEE_STIND_I
);
2390 mono_mb_patch_short_branch (mb
, pos_end
);
2391 mono_mb_patch_short_branch (mb
, pos_null
);
2395 case MONO_MARSHAL_CONV_SAFEHANDLE
: {
2396 int dar_release_slot
, pos
;
2398 dar_release_slot
= mono_mb_add_local (mb
, &mono_defaults
.boolean_class
->byval_arg
);
2401 * The following is ifdefed-out, because I have no way of doing the
2402 * DangerousRelease when destroying the structure
2405 /* set release = false */
2406 mono_mb_emit_icon (mb
, 0);
2407 mono_mb_emit_stloc (mb
, dar_release_slot
);
2408 if (!sh_dangerous_add_ref
)
2409 init_safe_handle ();
2411 /* safehandle.DangerousAddRef (ref release) */
2412 mono_mb_emit_ldloc (mb
, 0); /* the source */
2413 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
2414 mono_mb_emit_ldloc_addr (mb
, dar_release_slot
);
2415 mono_mb_emit_managed_call (mb
, sh_dangerous_add_ref
, NULL
);
2417 mono_mb_emit_ldloc (mb
, 0);
2418 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
2419 pos
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
2420 mono_mb_emit_exception (mb
, "ArgumentNullException", NULL
);
2421 mono_mb_patch_branch (mb
, pos
);
2423 /* Pull the handle field from SafeHandle */
2424 mono_mb_emit_ldloc (mb
, 1);
2425 mono_mb_emit_ldloc (mb
, 0);
2426 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
2427 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoSafeHandle
, handle
));
2428 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
2429 mono_mb_emit_byte (mb
, CEE_STIND_I
);
2433 case MONO_MARSHAL_CONV_HANDLEREF
: {
2434 mono_mb_emit_ldloc (mb
, 1);
2435 mono_mb_emit_ldloc (mb
, 0);
2436 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoHandleRef
, handle
));
2437 mono_mb_emit_byte (mb
, CEE_ADD
);
2438 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
2439 mono_mb_emit_byte (mb
, CEE_STIND_I
);
2444 char *msg
= g_strdup_printf ("marshalling conversion %d not implemented", conv
);
2445 MonoException
*exc
= mono_get_exception_not_implemented (msg
);
2448 mono_raise_exception (exc
);
2454 emit_struct_conv (MonoMethodBuilder
*mb
, MonoClass
*klass
, gboolean to_object
)
2456 MonoMarshalType
*info
;
2460 emit_struct_conv(mb
, klass
->parent
, to_object
);
2462 info
= mono_marshal_load_type_info (klass
);
2464 if (info
->native_size
== 0)
2467 if (klass
->blittable
) {
2468 int msize
= mono_class_value_size (klass
, NULL
);
2469 g_assert (msize
== info
->native_size
);
2470 mono_mb_emit_ldloc (mb
, 1);
2471 mono_mb_emit_ldloc (mb
, 0);
2472 mono_mb_emit_icon (mb
, msize
);
2473 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
2474 mono_mb_emit_byte (mb
, CEE_CPBLK
);
2476 mono_mb_emit_add_to_local (mb
, 0, msize
);
2477 mono_mb_emit_add_to_local (mb
, 1, msize
);
2481 for (i
= 0; i
< info
->num_fields
; i
++) {
2482 MonoMarshalNative ntype
;
2483 MonoMarshalConv conv
;
2484 MonoType
*ftype
= info
->fields
[i
].field
->type
;
2487 gboolean last_field
= i
< (info
->num_fields
-1) ? 0 : 1;
2489 if (ftype
->attrs
& FIELD_ATTRIBUTE_STATIC
)
2492 ntype
= mono_type_to_unmanaged (ftype
, info
->fields
[i
].mspec
, TRUE
, klass
->unicode
, &conv
);
2495 msize
= klass
->instance_size
- info
->fields
[i
].field
->offset
;
2496 usize
= info
->native_size
- info
->fields
[i
].offset
;
2498 msize
= info
->fields
[i
+ 1].field
->offset
- info
->fields
[i
].field
->offset
;
2499 usize
= info
->fields
[i
+ 1].offset
- info
->fields
[i
].offset
;
2502 if (klass
!= mono_defaults
.safehandle_class
){
2504 * FIXME: Should really check for usize==0 and msize>0, but we apply
2505 * the layout to the managed structure as well.
2508 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) && (usize
== 0)) {
2509 if (MONO_TYPE_IS_REFERENCE (info
->fields
[i
].field
->type
) ||
2510 ((!last_field
&& MONO_TYPE_IS_REFERENCE (info
->fields
[i
+ 1].field
->type
))))
2511 g_error ("Type %s which has an [ExplicitLayout] attribute cannot have a "
2512 "reference field at the same offset as another field.",
2513 mono_type_full_name (&klass
->byval_arg
));
2516 if ((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_AUTO_LAYOUT
)
2517 g_error ("Type %s which is passed to unmanaged code must have a StructLayout attribute",
2518 mono_type_full_name (&klass
->byval_arg
));
2523 case MONO_MARSHAL_CONV_NONE
: {
2526 if (ftype
->byref
|| ftype
->type
== MONO_TYPE_I
||
2527 ftype
->type
== MONO_TYPE_U
) {
2528 mono_mb_emit_ldloc (mb
, 1);
2529 mono_mb_emit_ldloc (mb
, 0);
2530 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
2531 mono_mb_emit_byte (mb
, CEE_STIND_I
);
2542 case MONO_TYPE_BOOLEAN
:
2545 case MONO_TYPE_CHAR
:
2551 mono_mb_emit_ldloc (mb
, 1);
2552 mono_mb_emit_ldloc (mb
, 0);
2553 mono_mb_emit_byte (mb
, mono_type_to_ldind (ftype
));
2554 mono_mb_emit_byte (mb
, mono_type_to_stind (ftype
));
2556 case MONO_TYPE_VALUETYPE
: {
2557 int src_var
, dst_var
;
2559 if (ftype
->data
.klass
->enumtype
) {
2560 ftype
= ftype
->data
.klass
->enum_basetype
;
2564 src_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
2565 dst_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
2567 /* save the old src pointer */
2568 mono_mb_emit_ldloc (mb
, 0);
2569 mono_mb_emit_stloc (mb
, src_var
);
2570 /* save the old dst pointer */
2571 mono_mb_emit_ldloc (mb
, 1);
2572 mono_mb_emit_stloc (mb
, dst_var
);
2574 emit_struct_conv (mb
, ftype
->data
.klass
, to_object
);
2576 /* restore the old src pointer */
2577 mono_mb_emit_ldloc (mb
, src_var
);
2578 mono_mb_emit_stloc (mb
, 0);
2579 /* restore the old dst pointer */
2580 mono_mb_emit_ldloc (mb
, dst_var
);
2581 mono_mb_emit_stloc (mb
, 1);
2584 case MONO_TYPE_OBJECT
: {
2585 mono_init_com_types ();
2587 static MonoMethod
*variant_clear
= NULL
;
2588 static MonoMethod
*get_object_for_native_variant
= NULL
;
2591 variant_clear
= mono_class_get_method_from_name (mono_defaults
.variant_class
, "Clear", 0);
2592 if (!get_object_for_native_variant
)
2593 get_object_for_native_variant
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "GetObjectForNativeVariant", 1);
2594 mono_mb_emit_ldloc (mb
, 1);
2595 mono_mb_emit_ldloc (mb
, 0);
2596 mono_mb_emit_managed_call (mb
, get_object_for_native_variant
, NULL
);
2597 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
2599 mono_mb_emit_ldloc (mb
, 0);
2600 mono_mb_emit_managed_call (mb
, variant_clear
, NULL
);
2603 static MonoMethod
*get_native_variant_for_object
= NULL
;
2605 if (!get_native_variant_for_object
)
2606 get_native_variant_for_object
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "GetNativeVariantForObject", 2);
2608 mono_mb_emit_ldloc (mb
, 0);
2609 mono_mb_emit_byte(mb
, CEE_LDIND_REF
);
2610 mono_mb_emit_ldloc (mb
, 1);
2611 mono_mb_emit_managed_call (mb
, get_native_variant_for_object
, NULL
);
2617 g_warning ("marshaling type %02x not implemented", ftype
->type
);
2618 g_assert_not_reached ();
2623 int src_var
, dst_var
;
2625 src_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
2626 dst_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
2628 /* save the old src pointer */
2629 mono_mb_emit_ldloc (mb
, 0);
2630 mono_mb_emit_stloc (mb
, src_var
);
2631 /* save the old dst pointer */
2632 mono_mb_emit_ldloc (mb
, 1);
2633 mono_mb_emit_stloc (mb
, dst_var
);
2636 emit_ptr_to_object_conv (mb
, ftype
, conv
, info
->fields
[i
].mspec
);
2638 emit_object_to_ptr_conv (mb
, ftype
, conv
, info
->fields
[i
].mspec
);
2640 /* restore the old src pointer */
2641 mono_mb_emit_ldloc (mb
, src_var
);
2642 mono_mb_emit_stloc (mb
, 0);
2643 /* restore the old dst pointer */
2644 mono_mb_emit_ldloc (mb
, dst_var
);
2645 mono_mb_emit_stloc (mb
, 1);
2650 mono_mb_emit_add_to_local (mb
, 0, usize
);
2651 mono_mb_emit_add_to_local (mb
, 1, msize
);
2653 mono_mb_emit_add_to_local (mb
, 0, msize
);
2654 mono_mb_emit_add_to_local (mb
, 1, usize
);
2660 emit_struct_free (MonoMethodBuilder
*mb
, MonoClass
*klass
, int struct_var
)
2662 /* Call DestroyStructure */
2663 /* FIXME: Only do this if needed */
2664 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
2665 mono_mb_emit_op (mb
, CEE_MONO_CLASSCONST
, klass
);
2666 mono_mb_emit_ldloc (mb
, struct_var
);
2667 mono_mb_emit_icall (mb
, mono_struct_delete_old
);
2671 emit_thread_interrupt_checkpoint_call (MonoMethodBuilder
*mb
, gpointer checkpoint_func
)
2675 mono_mb_emit_ptr (mb
, (gpointer
) mono_thread_interruption_request_flag ());
2676 mono_mb_emit_byte (mb
, CEE_LDIND_U4
);
2677 pos_noabort
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
2679 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
2680 mono_mb_emit_byte (mb
, CEE_MONO_NOT_TAKEN
);
2682 mono_mb_emit_icall (mb
, checkpoint_func
);
2684 mono_mb_patch_addr (mb
, pos_noabort
, mb
->pos
- (pos_noabort
+ 4));
2688 emit_thread_interrupt_checkpoint (MonoMethodBuilder
*mb
)
2690 if (strstr (mb
->name
, "mono_thread_interruption_checkpoint"))
2693 emit_thread_interrupt_checkpoint_call (mb
, mono_thread_interruption_checkpoint
);
2697 emit_thread_force_interrupt_checkpoint (MonoMethodBuilder
*mb
)
2699 emit_thread_interrupt_checkpoint_call (mb
, mono_thread_force_interruption_checkpoint
);
2702 static MonoAsyncResult
*
2703 mono_delegate_begin_invoke (MonoDelegate
*delegate
, gpointer
*params
)
2705 MonoMethodMessage
*msg
;
2706 MonoDelegate
*async_callback
;
2710 MonoMethod
*method
= NULL
, *method2
= NULL
;
2712 g_assert (delegate
);
2714 if (delegate
->target
&& mono_object_class (delegate
->target
) == mono_defaults
.transparent_proxy_class
) {
2716 MonoTransparentProxy
* tp
= (MonoTransparentProxy
*)delegate
->target
;
2717 if (!tp
->remote_class
->proxy_class
->contextbound
|| tp
->rp
->context
!= (MonoObject
*) mono_context_get ()) {
2719 /* If the target is a proxy, make a direct call. Is proxy's work
2720 // to make the call asynchronous.
2722 MonoAsyncResult
*ares
;
2724 MonoArray
*out_args
;
2726 method
= delegate
->method_info
->method
;
2728 msg
= mono_method_call_message_new (mono_marshal_method_from_wrapper (method
), params
, NULL
, &async_callback
, &state
);
2729 handle
= CreateEvent (NULL
, TRUE
, FALSE
, NULL
);
2730 g_assert(handle
!= NULL
);
2731 ares
= mono_async_result_new (mono_domain_get (), handle
, state
, handle
, NULL
);
2732 MONO_OBJECT_SETREF (ares
, async_delegate
, (MonoObject
*)delegate
);
2733 MONO_OBJECT_SETREF (ares
, async_callback
, (MonoObject
*)async_callback
);
2734 MONO_OBJECT_SETREF (msg
, async_result
, ares
);
2735 msg
->call_type
= CallType_BeginInvoke
;
2737 mono_remoting_invoke ((MonoObject
*)tp
->rp
, msg
, &exc
, &out_args
);
2742 klass
= delegate
->object
.vtable
->klass
;
2744 method
= mono_get_delegate_invoke (klass
);
2745 method2
= mono_class_get_method_from_name (klass
, "BeginInvoke", -1);
2748 g_assert (method
!= NULL
);
2750 im
= mono_get_delegate_invoke (method
->klass
);
2751 msg
= mono_method_call_message_new (method
, params
, im
, &async_callback
, &state
);
2753 return mono_thread_pool_add ((MonoObject
*)delegate
, msg
, async_callback
, state
);
2757 mono_mb_emit_save_args (MonoMethodBuilder
*mb
, MonoMethodSignature
*sig
, gboolean save_this
)
2759 int i
, params_var
, tmp_var
;
2761 /* allocate local (pointer) *params[] */
2762 params_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
2763 /* allocate local (pointer) tmp */
2764 tmp_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
2766 /* alloate space on stack to store an array of pointers to the arguments */
2767 mono_mb_emit_icon (mb
, sizeof (gpointer
) * (sig
->param_count
+ 1));
2768 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
2769 mono_mb_emit_byte (mb
, CEE_LOCALLOC
);
2770 mono_mb_emit_stloc (mb
, params_var
);
2773 mono_mb_emit_ldloc (mb
, params_var
);
2774 mono_mb_emit_stloc (mb
, tmp_var
);
2776 if (save_this
&& sig
->hasthis
) {
2777 mono_mb_emit_ldloc (mb
, tmp_var
);
2778 mono_mb_emit_ldarg_addr (mb
, 0);
2779 mono_mb_emit_byte (mb
, CEE_STIND_I
);
2780 /* tmp = tmp + sizeof (gpointer) */
2781 if (sig
->param_count
)
2782 mono_mb_emit_add_to_local (mb
, tmp_var
, sizeof (gpointer
));
2786 for (i
= 0; i
< sig
->param_count
; i
++) {
2787 mono_mb_emit_ldloc (mb
, tmp_var
);
2788 mono_mb_emit_ldarg_addr (mb
, i
+ sig
->hasthis
);
2789 mono_mb_emit_byte (mb
, CEE_STIND_I
);
2790 /* tmp = tmp + sizeof (gpointer) */
2791 if (i
< (sig
->param_count
- 1))
2792 mono_mb_emit_add_to_local (mb
, tmp_var
, sizeof (gpointer
));
2799 mono_signature_to_name (MonoMethodSignature
*sig
, const char *prefix
)
2803 GString
*res
= g_string_new ("");
2806 g_string_append (res
, prefix
);
2807 g_string_append_c (res
, '_');
2810 mono_type_get_desc (res
, sig
->ret
, FALSE
);
2812 for (i
= 0; i
< sig
->param_count
; ++i
) {
2813 g_string_append_c (res
, '_');
2814 mono_type_get_desc (res
, sig
->params
[i
], FALSE
);
2817 g_string_free (res
, FALSE
);
2822 * mono_marshal_get_string_encoding:
2824 * Return the string encoding which should be used for a given parameter.
2826 static MonoMarshalNative
2827 mono_marshal_get_string_encoding (MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
*spec
)
2829 /* First try the parameter marshal info */
2831 if (spec
->native
== MONO_NATIVE_LPARRAY
) {
2832 if ((spec
->data
.array_data
.elem_type
!= 0) && (spec
->data
.array_data
.elem_type
!= MONO_NATIVE_MAX
))
2833 return spec
->data
.array_data
.elem_type
;
2836 return spec
->native
;
2840 return MONO_NATIVE_LPSTR
;
2842 /* Then try the method level marshal info */
2843 switch (piinfo
->piflags
& PINVOKE_ATTRIBUTE_CHAR_SET_MASK
) {
2844 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI
:
2845 return MONO_NATIVE_LPSTR
;
2846 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE
:
2847 return MONO_NATIVE_LPWSTR
;
2848 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO
:
2849 #ifdef PLATFORM_WIN32
2850 return MONO_NATIVE_LPWSTR
;
2852 return MONO_NATIVE_LPSTR
;
2855 return MONO_NATIVE_LPSTR
;
2859 static MonoMarshalConv
2860 mono_marshal_get_string_to_ptr_conv (MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
*spec
)
2862 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (piinfo
, spec
);
2865 case MONO_NATIVE_LPWSTR
:
2866 return MONO_MARSHAL_CONV_STR_LPWSTR
;
2867 case MONO_NATIVE_LPSTR
:
2868 return MONO_MARSHAL_CONV_STR_LPSTR
;
2869 case MONO_NATIVE_LPTSTR
:
2870 return MONO_MARSHAL_CONV_STR_LPTSTR
;
2871 case MONO_NATIVE_BSTR
:
2872 return MONO_MARSHAL_CONV_STR_BSTR
;
2878 static MonoMarshalConv
2879 mono_marshal_get_stringbuilder_to_ptr_conv (MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
*spec
)
2881 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (piinfo
, spec
);
2884 case MONO_NATIVE_LPWSTR
:
2885 return MONO_MARSHAL_CONV_SB_LPWSTR
;
2887 case MONO_NATIVE_LPSTR
:
2888 return MONO_MARSHAL_CONV_SB_LPSTR
;
2890 case MONO_NATIVE_LPTSTR
:
2891 return MONO_MARSHAL_CONV_SB_LPTSTR
;
2898 static MonoMarshalConv
2899 mono_marshal_get_ptr_to_string_conv (MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
*spec
, gboolean
*need_free
)
2901 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (piinfo
, spec
);
2906 case MONO_NATIVE_LPWSTR
:
2907 return MONO_MARSHAL_CONV_LPWSTR_STR
;
2908 case MONO_NATIVE_LPSTR
:
2909 return MONO_MARSHAL_CONV_LPSTR_STR
;
2910 case MONO_NATIVE_LPTSTR
:
2911 return MONO_MARSHAL_CONV_LPTSTR_STR
;
2912 case MONO_NATIVE_BSTR
:
2913 return MONO_MARSHAL_CONV_BSTR_STR
;
2919 static MonoMarshalConv
2920 mono_marshal_get_ptr_to_stringbuilder_conv (MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
*spec
, gboolean
*need_free
)
2922 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (piinfo
, spec
);
2927 case MONO_NATIVE_LPWSTR
:
2929 * mono_string_builder_to_utf16 does not allocate a
2930 * new buffer, so no need to free it.
2933 return MONO_MARSHAL_CONV_LPWSTR_SB
;
2934 case MONO_NATIVE_LPSTR
:
2935 return MONO_MARSHAL_CONV_LPSTR_SB
;
2937 case MONO_NATIVE_LPTSTR
:
2938 return MONO_MARSHAL_CONV_LPTSTR_SB
;
2946 * Return whenever a field of a native structure or an array member needs to
2950 mono_marshal_need_free (MonoType
*t
, MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
*spec
)
2952 MonoMarshalNative encoding
;
2953 MonoMarshalConv conv
;
2956 case MONO_TYPE_VALUETYPE
:
2957 /* FIXME: Optimize this */
2959 case MONO_TYPE_OBJECT
:
2960 case MONO_TYPE_CLASS
:
2961 if (t
->data
.klass
== mono_defaults
.stringbuilder_class
) {
2963 conv
= mono_marshal_get_ptr_to_stringbuilder_conv (piinfo
, spec
, &need_free
);
2967 case MONO_TYPE_STRING
:
2968 encoding
= mono_marshal_get_string_encoding (piinfo
, spec
);
2969 return (encoding
== MONO_NATIVE_LPWSTR
) ? FALSE
: TRUE
;
2975 static inline MonoMethod
*
2976 mono_marshal_find_in_cache (GHashTable
*cache
, gpointer key
)
2980 mono_marshal_lock ();
2981 res
= g_hash_table_lookup (cache
, key
);
2982 mono_marshal_unlock ();
2986 /* Create the method from the builder and place it in the cache */
2987 static inline MonoMethod
*
2988 mono_mb_create_and_cache (GHashTable
*cache
, gpointer key
,
2989 MonoMethodBuilder
*mb
, MonoMethodSignature
*sig
,
2994 mono_marshal_lock ();
2995 res
= g_hash_table_lookup (cache
, key
);
2996 mono_marshal_unlock ();
2999 newm
= mono_mb_create_method (mb
, sig
, max_stack
);
3000 mono_marshal_lock ();
3001 res
= g_hash_table_lookup (cache
, key
);
3004 g_hash_table_insert (cache
, key
, res
);
3005 g_hash_table_insert (wrapper_hash
, res
, key
);
3006 mono_marshal_unlock ();
3008 mono_marshal_unlock ();
3009 mono_free_method (newm
);
3017 static inline MonoMethod
*
3018 mono_marshal_remoting_find_in_cache (MonoMethod
*method
, int wrapper_type
)
3020 MonoMethod
*res
= NULL
;
3021 MonoRemotingMethods
*wrps
;
3023 mono_marshal_lock ();
3024 wrps
= g_hash_table_lookup (method
->klass
->image
->remoting_invoke_cache
, method
);
3027 switch (wrapper_type
) {
3028 case MONO_WRAPPER_REMOTING_INVOKE
: res
= wrps
->invoke
; break;
3029 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
: res
= wrps
->invoke_with_check
; break;
3030 case MONO_WRAPPER_XDOMAIN_INVOKE
: res
= wrps
->xdomain_invoke
; break;
3031 case MONO_WRAPPER_XDOMAIN_DISPATCH
: res
= wrps
->xdomain_dispatch
; break;
3035 /* it is important to do the unlock after the load from wrps, since in
3036 * mono_remoting_mb_create_and_cache () we drop the marshal lock to be able
3037 * to take the loader lock and some other thread may set the fields.
3039 mono_marshal_unlock ();
3043 /* Create the method from the builder and place it in the cache */
3044 static inline MonoMethod
*
3045 mono_remoting_mb_create_and_cache (MonoMethod
*key
, MonoMethodBuilder
*mb
,
3046 MonoMethodSignature
*sig
, int max_stack
)
3048 MonoMethod
**res
= NULL
;
3049 MonoRemotingMethods
*wrps
;
3050 GHashTable
*cache
= key
->klass
->image
->remoting_invoke_cache
;
3052 mono_marshal_lock ();
3053 wrps
= g_hash_table_lookup (cache
, key
);
3055 wrps
= g_new0 (MonoRemotingMethods
, 1);
3056 g_hash_table_insert (cache
, key
, wrps
);
3059 switch (mb
->method
->wrapper_type
) {
3060 case MONO_WRAPPER_REMOTING_INVOKE
: res
= &wrps
->invoke
; break;
3061 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
: res
= &wrps
->invoke_with_check
; break;
3062 case MONO_WRAPPER_XDOMAIN_INVOKE
: res
= &wrps
->xdomain_invoke
; break;
3063 case MONO_WRAPPER_XDOMAIN_DISPATCH
: res
= &wrps
->xdomain_dispatch
; break;
3064 default: g_assert_not_reached (); break;
3066 mono_marshal_unlock ();
3070 newm
= mono_mb_create_method (mb
, sig
, max_stack
);
3072 mono_marshal_lock ();
3075 g_hash_table_insert (wrapper_hash
, *res
, key
);
3076 mono_marshal_unlock ();
3078 mono_marshal_unlock ();
3079 mono_free_method (newm
);
3087 mono_marshal_method_from_wrapper (MonoMethod
*wrapper
)
3091 if (wrapper
->wrapper_type
== MONO_WRAPPER_NONE
)
3094 mono_marshal_lock ();
3095 res
= g_hash_table_lookup (wrapper_hash
, wrapper
);
3096 mono_marshal_unlock ();
3101 mono_marshal_get_delegate_begin_invoke (MonoMethod
*method
)
3103 MonoMethodSignature
*sig
;
3104 MonoMethodBuilder
*mb
;
3110 g_assert (method
&& method
->klass
->parent
== mono_defaults
.multicastdelegate_class
&&
3111 !strcmp (method
->name
, "BeginInvoke"));
3113 sig
= signature_no_pinvoke (method
);
3115 cache
= method
->klass
->image
->delegate_begin_invoke_cache
;
3116 if ((res
= mono_marshal_find_in_cache (cache
, sig
)))
3119 g_assert (sig
->hasthis
);
3121 name
= mono_signature_to_name (sig
, "begin_invoke");
3122 mb
= mono_mb_new (mono_defaults
.multicastdelegate_class
, name
, MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
);
3125 mb
->method
->save_lmf
= 1;
3127 params_var
= mono_mb_emit_save_args (mb
, sig
, FALSE
);
3129 mono_mb_emit_ldarg (mb
, 0);
3130 mono_mb_emit_ldloc (mb
, params_var
);
3131 mono_mb_emit_icall (mb
, mono_delegate_begin_invoke
);
3132 emit_thread_interrupt_checkpoint (mb
);
3133 mono_mb_emit_byte (mb
, CEE_RET
);
3135 res
= mono_mb_create_and_cache (cache
, sig
, mb
, sig
, sig
->param_count
+ 16);
3141 mono_delegate_end_invoke (MonoDelegate
*delegate
, gpointer
*params
)
3143 MonoDomain
*domain
= mono_domain_get ();
3144 MonoAsyncResult
*ares
;
3145 MonoMethod
*method
= NULL
;
3146 MonoMethodSignature
*sig
;
3147 MonoMethodMessage
*msg
;
3148 MonoObject
*res
, *exc
;
3149 MonoArray
*out_args
;
3152 g_assert (delegate
);
3154 if (!delegate
->method_info
|| !delegate
->method_info
->method
)
3155 g_assert_not_reached ();
3157 klass
= delegate
->object
.vtable
->klass
;
3159 method
= mono_class_get_method_from_name (klass
, "EndInvoke", -1);
3160 g_assert (method
!= NULL
);
3162 sig
= signature_no_pinvoke (method
);
3164 msg
= mono_method_call_message_new (method
, params
, NULL
, NULL
, NULL
);
3166 ares
= mono_array_get (msg
->args
, gpointer
, sig
->param_count
- 1);
3169 if (ares
->async_delegate
!= (MonoObject
*)delegate
&& mono_get_runtime_info ()->framework_version
[0] >= '2') {
3170 mono_raise_exception (mono_get_exception_invalid_operation (
3171 "The IAsyncResult object provided does not match this delegate."));
3175 if (delegate
->target
&& mono_object_class (delegate
->target
) == mono_defaults
.transparent_proxy_class
) {
3176 MonoTransparentProxy
* tp
= (MonoTransparentProxy
*)delegate
->target
;
3177 msg
= (MonoMethodMessage
*)mono_object_new (domain
, mono_defaults
.mono_method_message_class
);
3178 mono_message_init (domain
, msg
, delegate
->method_info
, NULL
);
3179 msg
->call_type
= CallType_EndInvoke
;
3180 MONO_OBJECT_SETREF (msg
, async_result
, ares
);
3181 res
= mono_remoting_invoke ((MonoObject
*)tp
->rp
, msg
, &exc
, &out_args
);
3183 res
= mono_thread_pool_finish (ares
, &out_args
, &exc
);
3187 if (((MonoException
*)exc
)->stack_trace
) {
3188 char *strace
= mono_string_to_utf8 (((MonoException
*)exc
)->stack_trace
);
3190 tmp
= g_strdup_printf ("%s\nException Rethrown at:\n", strace
);
3192 MONO_OBJECT_SETREF (((MonoException
*)exc
), stack_trace
, mono_string_new (domain
, tmp
));
3195 mono_raise_exception ((MonoException
*)exc
);
3198 mono_method_return_message_restore (method
, params
, out_args
);
3203 mono_mb_emit_restore_result (MonoMethodBuilder
*mb
, MonoType
*return_type
)
3205 MonoType
*t
= mono_type_get_underlying_type (return_type
);
3207 if (return_type
->byref
)
3208 return_type
= &mono_defaults
.int_class
->byval_arg
;
3211 case MONO_TYPE_VOID
:
3212 g_assert_not_reached ();
3215 case MONO_TYPE_STRING
:
3216 case MONO_TYPE_CLASS
:
3217 case MONO_TYPE_OBJECT
:
3218 case MONO_TYPE_ARRAY
:
3219 case MONO_TYPE_SZARRAY
:
3223 case MONO_TYPE_BOOLEAN
:
3226 case MONO_TYPE_CHAR
:
3236 mono_mb_emit_op (mb
, CEE_UNBOX
, mono_class_from_mono_type (return_type
));
3237 mono_mb_emit_byte (mb
, mono_type_to_ldind (return_type
));
3239 case MONO_TYPE_GENERICINST
:
3240 if (!mono_type_generic_inst_is_valuetype (return_type
))
3243 case MONO_TYPE_VALUETYPE
: {
3244 MonoClass
*klass
= mono_class_from_mono_type (return_type
);
3245 mono_mb_emit_op (mb
, CEE_UNBOX
, klass
);
3246 mono_mb_emit_op (mb
, CEE_LDOBJ
, klass
);
3250 g_warning ("type 0x%x not handled", return_type
->type
);
3251 g_assert_not_reached ();
3254 mono_mb_emit_byte (mb
, CEE_RET
);
3258 mono_marshal_get_delegate_end_invoke (MonoMethod
*method
)
3260 MonoMethodSignature
*sig
;
3261 MonoMethodBuilder
*mb
;
3267 g_assert (method
&& method
->klass
->parent
== mono_defaults
.multicastdelegate_class
&&
3268 !strcmp (method
->name
, "EndInvoke"));
3270 sig
= signature_no_pinvoke (method
);
3272 cache
= method
->klass
->image
->delegate_end_invoke_cache
;
3273 if ((res
= mono_marshal_find_in_cache (cache
, sig
)))
3276 g_assert (sig
->hasthis
);
3278 name
= mono_signature_to_name (sig
, "end_invoke");
3279 mb
= mono_mb_new (mono_defaults
.multicastdelegate_class
, name
, MONO_WRAPPER_DELEGATE_END_INVOKE
);
3282 mb
->method
->save_lmf
= 1;
3284 params_var
= mono_mb_emit_save_args (mb
, sig
, FALSE
);
3286 mono_mb_emit_ldarg (mb
, 0);
3287 mono_mb_emit_ldloc (mb
, params_var
);
3288 mono_mb_emit_icall (mb
, mono_delegate_end_invoke
);
3289 emit_thread_interrupt_checkpoint (mb
);
3291 if (sig
->ret
->type
== MONO_TYPE_VOID
) {
3292 mono_mb_emit_byte (mb
, CEE_POP
);
3293 mono_mb_emit_byte (mb
, CEE_RET
);
3295 mono_mb_emit_restore_result (mb
, sig
->ret
);
3297 res
= mono_mb_create_and_cache (cache
, sig
,
3298 mb
, sig
, sig
->param_count
+ 16);
3305 mono_remoting_wrapper (MonoMethod
*method
, gpointer
*params
)
3307 MonoMethodMessage
*msg
;
3308 MonoTransparentProxy
*this;
3309 MonoObject
*res
, *exc
;
3310 MonoArray
*out_args
;
3312 this = *((MonoTransparentProxy
**)params
[0]);
3315 g_assert (((MonoObject
*)this)->vtable
->klass
== mono_defaults
.transparent_proxy_class
);
3317 /* skip the this pointer */
3320 if (this->remote_class
->proxy_class
->contextbound
&& this->rp
->context
== (MonoObject
*) mono_context_get ())
3323 MonoMethodSignature
*sig
= mono_method_signature (method
);
3324 int count
= sig
->param_count
;
3325 gpointer
* mparams
= (gpointer
*) alloca(count
*sizeof(gpointer
));
3327 for (i
=0; i
<count
; i
++) {
3328 MonoClass
*class = mono_class_from_mono_type (sig
->params
[i
]);
3329 if (class->valuetype
) {
3330 if (sig
->params
[i
]->byref
)
3331 mparams
[i
] = *((gpointer
*)params
[i
]);
3333 mparams
[i
] = params
[i
];
3335 mparams
[i
] = *((gpointer
**)params
[i
]);
3339 return mono_runtime_invoke (method
, method
->klass
->valuetype
? mono_object_unbox ((MonoObject
*)this): this, mparams
, NULL
);
3342 msg
= mono_method_call_message_new (method
, params
, NULL
, NULL
, NULL
);
3344 res
= mono_remoting_invoke ((MonoObject
*)this->rp
, msg
, &exc
, &out_args
);
3347 mono_raise_exception ((MonoException
*)exc
);
3349 mono_method_return_message_restore (method
, params
, out_args
);
3355 * cominterop_get_native_wrapper_adjusted:
3356 * @method: managed COM Interop method
3358 * Returns: the generated method to call with signature matching
3359 * the unmanaged COM Method signature
3362 cominterop_get_native_wrapper_adjusted (MonoMethod
*method
)
3365 MonoMethodBuilder
*mb_native
;
3366 MonoMarshalSpec
**mspecs
;
3367 MonoMethodSignature
*sig
, *sig_native
;
3368 MonoMethodPInvoke
*piinfo
= (MonoMethodPInvoke
*) method
;
3371 sig
= mono_method_signature (method
);
3373 // create unmanaged wrapper
3374 mb_native
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_MANAGED_TO_NATIVE
);
3375 sig_native
= cominterop_method_signature (method
);
3377 mspecs
= g_new (MonoMarshalSpec
*, sig_native
->param_count
+1);
3378 memset (mspecs
, 0, sizeof(MonoMarshalSpec
*)*(sig_native
->param_count
+1));
3380 mono_method_get_marshal_info (method
, mspecs
);
3382 // move managed args up one
3383 for (i
= sig
->param_count
; i
>= 1; i
--)
3384 mspecs
[i
+1] = mspecs
[i
];
3386 // first arg is IntPtr for interface
3389 if (!(method
->iflags
& METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG
)) {
3390 // move return spec to last param
3391 if (!MONO_TYPE_IS_VOID (sig
->ret
))
3392 mspecs
[sig_native
->param_count
] = mspecs
[0];
3397 mono_marshal_emit_native_wrapper(mono_defaults
.corlib
, mb_native
, sig_native
, piinfo
, mspecs
, piinfo
->addr
);
3399 res
= mono_mb_create_method (mb_native
, sig_native
, sig_native
->param_count
+ 16);
3401 mono_mb_free (mb_native
);
3403 for (i
= sig_native
->param_count
; i
>= 0; i
--)
3405 mono_metadata_free_marshal_spec (mspecs
[i
]);
3412 * cominterop_get_native_wrapper:
3413 * @method: managed method
3415 * Returns: the generated method to call
3418 cominterop_get_native_wrapper (MonoMethod
*method
)
3422 MonoMethodBuilder
*mb
;
3423 MonoMethodSignature
*sig
, *csig
;
3427 cache
= method
->klass
->image
->cominterop_wrapper_cache
;
3428 if ((res
= mono_marshal_find_in_cache (cache
, method
)))
3431 mono_init_com_types ();
3433 sig
= mono_method_signature (method
);
3434 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_COMINTEROP
);
3436 /* if method klass is import, that means method
3437 * is really a com call. let interop system emit it.
3439 if (MONO_CLASS_IS_IMPORT(method
->klass
)) {
3440 /* FIXME: we have to call actual class .ctor
3441 * instead of just __ComObject .ctor.
3443 if (!strcmp(method
->name
, ".ctor")) {
3444 static MonoMethod
*ctor
= NULL
;
3447 ctor
= mono_class_get_method_from_name (mono_defaults
.com_object_class
, ".ctor", 0);
3448 mono_mb_emit_ldarg (mb
, 0);
3449 mono_mb_emit_managed_call (mb
, ctor
, NULL
);
3450 mono_mb_emit_byte (mb
, CEE_RET
);
3453 static MonoMethod
* ThrowExceptionForHR
= NULL
;
3454 static MonoMethod
* GetInterface
= NULL
;
3455 MonoMethod
*adjusted_method
;
3459 gboolean preserve_sig
= method
->iflags
& METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG
;
3461 GetInterface
= mono_class_get_method_from_name (mono_defaults
.com_object_class
, "GetInterface", 1);
3463 // add local variables
3464 ptr_this
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
3465 if (!MONO_TYPE_IS_VOID (sig
->ret
))
3466 retval
= mono_mb_add_local (mb
, sig
->ret
);
3468 // get the type for the interface the method is defined on
3469 // and then get the underlying COM interface for that type
3470 mono_mb_emit_ldarg (mb
, 0);
3471 mono_mb_emit_ptr (mb
, method
);
3472 mono_mb_emit_icall (mb
, cominterop_get_method_interface
);
3473 mono_mb_emit_managed_call (mb
, GetInterface
, NULL
);
3474 mono_mb_emit_stloc (mb
, ptr_this
);
3476 // arg 1 is unmanaged this pointer
3477 mono_mb_emit_ldloc (mb
, ptr_this
);
3480 for (i
= 1; i
<= sig
->param_count
; i
++)
3481 mono_mb_emit_ldarg (mb
, i
);
3483 // push managed return value as byref last argument
3484 if (!MONO_TYPE_IS_VOID (sig
->ret
) && !preserve_sig
)
3485 mono_mb_emit_ldloc_addr (mb
, retval
);
3487 adjusted_method
= cominterop_get_native_wrapper_adjusted (method
);
3488 mono_mb_emit_managed_call (mb
, adjusted_method
, NULL
);
3490 if (!preserve_sig
) {
3491 if (!ThrowExceptionForHR
)
3492 ThrowExceptionForHR
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "ThrowExceptionForHR", 1);
3493 mono_mb_emit_managed_call (mb
, ThrowExceptionForHR
, NULL
);
3495 // load return value managed is expecting
3496 if (!MONO_TYPE_IS_VOID (sig
->ret
))
3497 mono_mb_emit_ldloc (mb
, retval
);
3500 mono_mb_emit_byte (mb
, CEE_RET
);
3505 /* Does this case ever get hit? */
3507 char *msg
= g_strdup ("non imported interfaces on \
3508 imported classes is not yet implemented.");
3509 mono_mb_emit_exception (mb
, "NotSupportedException", msg
);
3511 csig
= signature_dup (method
->klass
->image
, sig
);
3513 res
= mono_mb_create_and_cache (cache
, method
,
3514 mb
, csig
, csig
->param_count
+ 16);
3520 * cominterop_get_invoke:
3521 * @method: managed method
3523 * Returns: the generated method that calls the underlying __ComObject
3524 * rather than the proxy object.
3527 cominterop_get_invoke (MonoMethod
*method
)
3529 MonoMethodSignature
*sig
;
3530 MonoMethodBuilder
*mb
;
3533 GHashTable
* cache
= method
->klass
->image
->cominterop_invoke_cache
;
3537 if ((res
= mono_marshal_find_in_cache (cache
, method
)))
3540 sig
= signature_no_pinvoke (method
);
3542 /* we cant remote methods without this pointer */
3546 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_COMINTEROP_INVOKE
);
3548 /* get real proxy object, which is a ComInteropProxy in this case*/
3549 temp_obj
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
3550 mono_mb_emit_ldarg (mb
, 0);
3551 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
3552 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
3554 /* load the RCW from the ComInteropProxy*/
3555 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoComInteropProxy
, com_object
));
3556 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
3558 /* load args and make the call on the RCW */
3559 for (i
= 1; i
<= sig
->param_count
; i
++)
3560 mono_mb_emit_ldarg (mb
, i
);
3562 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) {
3563 MonoMethod
* native_wrapper
= cominterop_get_native_wrapper(method
);
3564 mono_mb_emit_managed_call (mb
, native_wrapper
, NULL
);
3567 if (method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
)
3568 mono_mb_emit_op (mb
, CEE_CALLVIRT
, method
);
3570 mono_mb_emit_op (mb
, CEE_CALL
, method
);
3573 if (!strcmp(method
->name
, ".ctor")) {
3574 static MonoClass
*com_interop_proxy_class
= NULL
;
3575 static MonoMethod
*cache_proxy
= NULL
;
3577 if (!com_interop_proxy_class
)
3578 com_interop_proxy_class
= mono_class_from_name (mono_defaults
.corlib
, "Mono.Interop", "ComInteropProxy");
3580 cache_proxy
= mono_class_get_method_from_name (com_interop_proxy_class
, "CacheProxy", 0);
3582 mono_mb_emit_ldarg (mb
, 0);
3583 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
3584 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
3585 mono_mb_emit_managed_call (mb
, cache_proxy
, NULL
);
3588 emit_thread_interrupt_checkpoint (mb
);
3590 mono_mb_emit_byte (mb
, CEE_RET
);
3592 res
= mono_mb_create_and_cache (cache
, method
, mb
, sig
, sig
->param_count
+ 16);
3597 /* Maps a managed object to its unmanaged representation
3598 * i.e. it's COM Callable Wrapper (CCW).
3602 static GHashTable
* ccw_hash
= NULL
;
3604 /* Maps a CCW interface to it's containing CCW.
3605 * Note that a CCW support many interfaces.
3607 * Value: MonoCCWInterface*
3609 static GHashTable
* ccw_interface_hash
= NULL
;
3611 /* Maps the IUnknown value of a RCW to
3612 * it's MonoComInteropProxy*.
3616 static GHashTable
* rcw_hash
= NULL
;
3619 mono_marshal_get_remoting_invoke (MonoMethod
*method
)
3621 MonoMethodSignature
*sig
;
3622 MonoMethodBuilder
*mb
;
3628 if (method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE
|| method
->wrapper_type
== MONO_WRAPPER_XDOMAIN_INVOKE
)
3631 /* this seems to be the best plase to put this, as all remoting invokes seem to get filtered through here */
3632 if ((method
->klass
->is_com_object
|| method
->klass
== mono_defaults
.com_object_class
) && !mono_class_vtable (mono_domain_get (), method
->klass
)->remote
)
3633 return cominterop_get_invoke(method
);
3635 sig
= signature_no_pinvoke (method
);
3637 /* we cant remote methods without this pointer */
3641 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_REMOTING_INVOKE
)))
3644 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_REMOTING_INVOKE
);
3645 mb
->method
->save_lmf
= 1;
3647 params_var
= mono_mb_emit_save_args (mb
, sig
, TRUE
);
3649 mono_mb_emit_ptr (mb
, method
);
3650 mono_mb_emit_ldloc (mb
, params_var
);
3651 mono_mb_emit_icall (mb
, mono_remoting_wrapper
);
3652 emit_thread_interrupt_checkpoint (mb
);
3654 if (sig
->ret
->type
== MONO_TYPE_VOID
) {
3655 mono_mb_emit_byte (mb
, CEE_POP
);
3656 mono_mb_emit_byte (mb
, CEE_RET
);
3658 mono_mb_emit_restore_result (mb
, sig
->ret
);
3661 res
= mono_remoting_mb_create_and_cache (method
, mb
, sig
, sig
->param_count
+ 16);
3667 /* mono_get_xdomain_marshal_type()
3668 * Returns the kind of marshalling that a type needs for cross domain calls.
3670 static MonoXDomainMarshalType
3671 mono_get_xdomain_marshal_type (MonoType
*t
)
3674 case MONO_TYPE_VOID
:
3675 g_assert_not_reached ();
3679 case MONO_TYPE_BOOLEAN
:
3682 case MONO_TYPE_CHAR
:
3689 return MONO_MARSHAL_NONE
;
3690 case MONO_TYPE_STRING
:
3691 return MONO_MARSHAL_COPY
;
3692 case MONO_TYPE_ARRAY
:
3693 case MONO_TYPE_SZARRAY
: {
3694 MonoClass
*elem_class
= mono_class_from_mono_type (t
)->element_class
;
3695 if (mono_get_xdomain_marshal_type (&(elem_class
->byval_arg
)) != MONO_MARSHAL_SERIALIZE
)
3696 return MONO_MARSHAL_COPY
;
3701 return MONO_MARSHAL_SERIALIZE
;
3705 /* mono_marshal_xdomain_copy_value
3706 * Makes a copy of "val" suitable for the current domain.
3709 mono_marshal_xdomain_copy_value (MonoObject
*val
)
3712 if (val
== NULL
) return NULL
;
3714 domain
= mono_domain_get ();
3716 switch (mono_object_class (val
)->byval_arg
.type
) {
3717 case MONO_TYPE_VOID
:
3718 g_assert_not_reached ();
3722 case MONO_TYPE_BOOLEAN
:
3725 case MONO_TYPE_CHAR
:
3731 case MONO_TYPE_R8
: {
3732 return mono_value_box (domain
, mono_object_class (val
), ((char*)val
) + sizeof(MonoObject
));
3734 case MONO_TYPE_STRING
: {
3735 MonoString
*str
= (MonoString
*) val
;
3736 return (MonoObject
*) mono_string_new_utf16 (domain
, mono_string_chars (str
), mono_string_length (str
));
3738 case MONO_TYPE_ARRAY
:
3739 case MONO_TYPE_SZARRAY
: {
3741 MonoXDomainMarshalType mt
= mono_get_xdomain_marshal_type (&(mono_object_class (val
)->element_class
->byval_arg
));
3742 if (mt
== MONO_MARSHAL_SERIALIZE
) return NULL
;
3743 acopy
= mono_array_clone_in_domain (domain
, (MonoArray
*) val
);
3744 if (mt
== MONO_MARSHAL_COPY
) {
3745 int i
, len
= mono_array_length (acopy
);
3746 for (i
= 0; i
< len
; i
++) {
3747 MonoObject
*item
= mono_array_get (acopy
, gpointer
, i
);
3748 mono_array_setref (acopy
, i
, mono_marshal_xdomain_copy_value (item
));
3751 return (MonoObject
*) acopy
;
3755 if (mono_object_class (val
) == mono_defaults
.stringbuilder_class
) {
3756 MonoStringBuilder
*oldsb
= (MonoStringBuilder
*) val
;
3757 MonoStringBuilder
*newsb
= (MonoStringBuilder
*) mono_object_new (domain
, mono_defaults
.stringbuilder_class
);
3758 MONO_OBJECT_SETREF (newsb
, str
, mono_string_new_utf16 (domain
, mono_string_chars (oldsb
->str
), mono_string_length (oldsb
->str
)));
3759 newsb
->length
= oldsb
->length
;
3760 newsb
->max_capacity
= (gint32
)0x7fffffff;
3761 return (MonoObject
*) newsb
;
3766 /* mono_marshal_xdomain_copy_out_value()
3767 * Copies the contents of the src instance into the dst instance. src and dst
3768 * must have the same type, and if they are arrays, the same size.
3771 mono_marshal_xdomain_copy_out_value (MonoObject
*src
, MonoObject
*dst
)
3773 if (src
== NULL
|| dst
== NULL
) return;
3775 g_assert (mono_object_class (src
) == mono_object_class (dst
));
3777 switch (mono_object_class (src
)->byval_arg
.type
) {
3778 case MONO_TYPE_ARRAY
:
3779 case MONO_TYPE_SZARRAY
: {
3780 int mt
= mono_get_xdomain_marshal_type (&(mono_object_class (src
)->element_class
->byval_arg
));
3781 if (mt
== MONO_MARSHAL_SERIALIZE
) return;
3782 if (mt
== MONO_MARSHAL_COPY
) {
3783 int i
, len
= mono_array_length ((MonoArray
*)dst
);
3784 for (i
= 0; i
< len
; i
++) {
3785 MonoObject
*item
= mono_array_get ((MonoArray
*)src
, gpointer
, i
);
3786 mono_array_setref ((MonoArray
*)dst
, i
, mono_marshal_xdomain_copy_value (item
));
3789 mono_array_full_copy ((MonoArray
*)src
, (MonoArray
*)dst
);
3795 if (mono_object_class (src
) == mono_defaults
.stringbuilder_class
) {
3796 MonoStringBuilder
*src_sb
= (MonoStringBuilder
*) src
;
3797 MonoStringBuilder
*dst_sb
= (MonoStringBuilder
*) dst
;
3799 MONO_OBJECT_SETREF (dst_sb
, str
, mono_string_new_utf16 (mono_object_domain (dst
), mono_string_chars (src_sb
->str
), mono_string_length (src_sb
->str
)));
3800 dst_sb
->cached_str
= NULL
;
3801 dst_sb
->length
= src_sb
->length
;
3806 mono_marshal_emit_xdomain_copy_value (MonoMethodBuilder
*mb
, MonoClass
*pclass
)
3808 mono_mb_emit_icall (mb
, mono_marshal_xdomain_copy_value
);
3809 mono_mb_emit_op (mb
, CEE_CASTCLASS
, pclass
);
3813 mono_marshal_emit_xdomain_copy_out_value (MonoMethodBuilder
*mb
, MonoClass
*pclass
)
3815 mono_mb_emit_icall (mb
, mono_marshal_xdomain_copy_out_value
);
3818 /* mono_marshal_supports_fast_xdomain()
3819 * Returns TRUE if the method can use the fast xdomain wrapper.
3822 mono_marshal_supports_fast_xdomain (MonoMethod
*method
)
3824 return !method
->klass
->contextbound
&&
3825 !((method
->flags
& METHOD_ATTRIBUTE_SPECIAL_NAME
) && (strcmp (".ctor", method
->name
) == 0));
3829 mono_marshal_set_domain_by_id (gint32 id
, MonoBoolean push
)
3831 MonoDomain
*current_domain
= mono_domain_get ();
3832 MonoDomain
*domain
= mono_domain_get_by_id (id
);
3834 if (!domain
|| !mono_domain_set (domain
, FALSE
))
3835 mono_raise_exception (mono_get_exception_appdomain_unloaded ());
3838 mono_thread_push_appdomain_ref (domain
);
3840 mono_thread_pop_appdomain_ref ();
3842 return current_domain
->domain_id
;
3846 mono_marshal_emit_switch_domain (MonoMethodBuilder
*mb
)
3848 mono_mb_emit_icall (mb
, mono_marshal_set_domain_by_id
);
3851 /* mono_marshal_emit_load_domain_method ()
3852 * Loads into the stack a pointer to the code of the provided method for
3853 * the current domain.
3856 mono_marshal_emit_load_domain_method (MonoMethodBuilder
*mb
, MonoMethod
*method
)
3858 /* We need a pointer to the method for the running domain (not the domain
3859 * that compiles the method).
3861 mono_mb_emit_ptr (mb
, method
);
3862 mono_mb_emit_icall (mb
, mono_compile_method
);
3865 /* mono_marshal_check_domain_image ()
3866 * Returns TRUE if the image is loaded in the specified
3867 * application domain.
3870 mono_marshal_check_domain_image (gint32 domain_id
, MonoImage
*image
)
3875 MonoDomain
*domain
= mono_domain_get_by_id (domain_id
);
3879 mono_domain_assemblies_lock (domain
);
3880 for (tmp
= domain
->domain_assemblies
; tmp
; tmp
= tmp
->next
) {
3882 if (ass
->image
== image
)
3885 mono_domain_assemblies_unlock (domain
);
3890 /* mono_marshal_get_xappdomain_dispatch ()
3891 * Generates a method that dispatches a method call from another domain into
3892 * the current domain.
3895 mono_marshal_get_xappdomain_dispatch (MonoMethod
*method
, int *marshal_types
, int complex_count
, int complex_out_count
, int ret_marshal_type
)
3897 MonoMethodSignature
*sig
, *csig
;
3898 MonoMethodBuilder
*mb
;
3900 int i
, j
, param_index
, copy_locals_base
;
3901 MonoClass
*ret_class
= NULL
;
3902 int loc_array
=0, loc_return
=0, loc_serialized_exc
=0;
3903 MonoExceptionClause
*main_clause
;
3904 MonoMethodHeader
*header
;
3906 gboolean copy_return
;
3908 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_XDOMAIN_DISPATCH
)))
3911 sig
= mono_method_signature (method
);
3912 copy_return
= (sig
->ret
->type
!= MONO_TYPE_VOID
&& ret_marshal_type
!= MONO_MARSHAL_SERIALIZE
);
3915 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3 + sig
->param_count
- complex_count
);
3916 csig
->params
[j
++] = &mono_defaults
.object_class
->byval_arg
;
3917 csig
->params
[j
++] = &byte_array_class
->this_arg
;
3918 csig
->params
[j
++] = &byte_array_class
->this_arg
;
3919 for (i
= 0; i
< sig
->param_count
; i
++) {
3920 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
)
3921 csig
->params
[j
++] = sig
->params
[i
];
3924 csig
->ret
= sig
->ret
;
3926 csig
->ret
= &mono_defaults
.void_class
->byval_arg
;
3928 csig
->hasthis
= FALSE
;
3930 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_XDOMAIN_DISPATCH
);
3931 mb
->method
->save_lmf
= 1;
3935 loc_serialized_exc
= mono_mb_add_local (mb
, &byte_array_class
->byval_arg
);
3936 if (complex_count
> 0)
3937 loc_array
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
3938 if (sig
->ret
->type
!= MONO_TYPE_VOID
) {
3939 loc_return
= mono_mb_add_local (mb
, sig
->ret
);
3940 ret_class
= mono_class_from_mono_type (sig
->ret
);
3945 main_clause
= g_new0 (MonoExceptionClause
, 1);
3946 main_clause
->try_offset
= mb
->pos
;
3948 /* Clean the call context */
3950 mono_mb_emit_byte (mb
, CEE_LDNULL
);
3951 mono_mb_emit_managed_call (mb
, method_set_call_context
, NULL
);
3952 mono_mb_emit_byte (mb
, CEE_POP
);
3954 /* Deserialize call data */
3956 mono_mb_emit_ldarg (mb
, 1);
3957 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
3958 mono_mb_emit_byte (mb
, CEE_DUP
);
3959 pos
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
3961 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
3962 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
3964 if (complex_count
> 0)
3965 mono_mb_emit_stloc (mb
, loc_array
);
3967 mono_mb_emit_byte (mb
, CEE_POP
);
3969 mono_mb_patch_addr_s (mb
, pos
, mb
->pos
- (pos
+ 1));
3971 /* Get the target object */
3973 mono_mb_emit_ldarg (mb
, 0);
3974 mono_mb_emit_managed_call (mb
, method_rs_appdomain_target
, NULL
);
3976 /* Load the arguments */
3978 copy_locals_base
= mb
->locals
;
3979 param_index
= 3; // Index of the first non-serialized parameter of this wrapper
3981 for (i
= 0; i
< sig
->param_count
; i
++) {
3982 MonoType
*pt
= sig
->params
[i
];
3983 MonoClass
*pclass
= mono_class_from_mono_type (pt
);
3984 switch (marshal_types
[i
]) {
3985 case MONO_MARSHAL_SERIALIZE
: {
3986 /* take the value from the serialized array */
3987 mono_mb_emit_ldloc (mb
, loc_array
);
3988 mono_mb_emit_icon (mb
, j
++);
3990 if (pclass
->valuetype
) {
3991 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
3992 mono_mb_emit_op (mb
, CEE_UNBOX
, pclass
);
3994 mono_mb_emit_op (mb
, CEE_LDELEMA
, pclass
);
3997 if (pclass
->valuetype
) {
3998 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
3999 mono_mb_emit_op (mb
, CEE_UNBOX
, pclass
);
4000 mono_mb_emit_op (mb
, CEE_LDOBJ
, pclass
);
4002 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
4003 if (pclass
!= mono_defaults
.object_class
) {
4004 mono_mb_emit_op (mb
, CEE_CASTCLASS
, pclass
);
4010 case MONO_MARSHAL_COPY_OUT
: {
4011 /* Keep a local copy of the value since we need to copy it back after the call */
4012 int copy_local
= mono_mb_add_local (mb
, &(pclass
->byval_arg
));
4013 mono_mb_emit_ldarg (mb
, param_index
++);
4014 mono_marshal_emit_xdomain_copy_value (mb
, pclass
);
4015 mono_mb_emit_byte (mb
, CEE_DUP
);
4016 mono_mb_emit_stloc (mb
, copy_local
);
4019 case MONO_MARSHAL_COPY
: {
4020 mono_mb_emit_ldarg (mb
, param_index
);
4022 mono_mb_emit_byte (mb
, CEE_DUP
);
4023 mono_mb_emit_byte (mb
, CEE_DUP
);
4024 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
4025 mono_marshal_emit_xdomain_copy_value (mb
, pclass
);
4026 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
4028 mono_marshal_emit_xdomain_copy_value (mb
, pclass
);
4033 case MONO_MARSHAL_NONE
:
4034 mono_mb_emit_ldarg (mb
, param_index
++);
4039 /* Make the call to the real object */
4041 emit_thread_force_interrupt_checkpoint (mb
);
4043 mono_mb_emit_op (mb
, CEE_CALLVIRT
, method
);
4045 if (sig
->ret
->type
!= MONO_TYPE_VOID
)
4046 mono_mb_emit_stloc (mb
, loc_return
);
4048 /* copy back MONO_MARSHAL_COPY_OUT parameters */
4052 for (i
= 0; i
< sig
->param_count
; i
++) {
4053 if (marshal_types
[i
] == MONO_MARSHAL_SERIALIZE
) continue;
4054 if (marshal_types
[i
] == MONO_MARSHAL_COPY_OUT
) {
4055 mono_mb_emit_ldloc (mb
, copy_locals_base
+ (j
++));
4056 mono_mb_emit_ldarg (mb
, param_index
);
4057 mono_marshal_emit_xdomain_copy_out_value (mb
, mono_class_from_mono_type (sig
->params
[i
]));
4062 /* Serialize the return values */
4064 if (complex_out_count
> 0) {
4065 /* Reset parameters in the array that don't need to be serialized back */
4067 for (i
= 0; i
< sig
->param_count
; i
++) {
4068 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
) continue;
4069 if (!sig
->params
[i
]->byref
) {
4070 mono_mb_emit_ldloc (mb
, loc_array
);
4071 mono_mb_emit_icon (mb
, j
);
4072 mono_mb_emit_byte (mb
, CEE_LDNULL
);
4073 mono_mb_emit_byte (mb
, CEE_STELEM_REF
);
4078 /* Add the return value to the array */
4080 if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
4081 mono_mb_emit_ldloc (mb
, loc_array
);
4082 mono_mb_emit_icon (mb
, complex_count
); /* The array has an additional slot to hold the ret value */
4083 mono_mb_emit_ldloc (mb
, loc_return
);
4084 if (ret_class
->valuetype
) {
4085 mono_mb_emit_op (mb
, CEE_BOX
, ret_class
);
4087 mono_mb_emit_byte (mb
, CEE_STELEM_REF
);
4092 mono_mb_emit_ldarg (mb
, 1);
4093 mono_mb_emit_ldloc (mb
, loc_array
);
4094 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
4095 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
4096 } else if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
4097 mono_mb_emit_ldarg (mb
, 1);
4098 mono_mb_emit_ldloc (mb
, loc_return
);
4099 if (ret_class
->valuetype
) {
4100 mono_mb_emit_op (mb
, CEE_BOX
, ret_class
);
4102 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
4103 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
4105 mono_mb_emit_ldarg (mb
, 1);
4106 mono_mb_emit_byte (mb
, CEE_LDNULL
);
4107 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
4108 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
4111 mono_mb_emit_ldarg (mb
, 2);
4112 mono_mb_emit_byte (mb
, CEE_LDNULL
);
4113 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
4114 pos_leave
= mono_mb_emit_branch (mb
, CEE_LEAVE
);
4116 /* Main exception catch */
4117 main_clause
->flags
= MONO_EXCEPTION_CLAUSE_NONE
;
4118 main_clause
->try_len
= mb
->pos
- main_clause
->try_offset
;
4119 main_clause
->data
.catch_class
= mono_defaults
.object_class
;
4122 main_clause
->handler_offset
= mb
->pos
;
4123 mono_mb_emit_managed_call (mb
, method_rs_serialize_exc
, NULL
);
4124 mono_mb_emit_stloc (mb
, loc_serialized_exc
);
4125 mono_mb_emit_ldarg (mb
, 2);
4126 mono_mb_emit_ldloc (mb
, loc_serialized_exc
);
4127 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
4128 mono_mb_emit_branch (mb
, CEE_LEAVE
);
4129 main_clause
->handler_len
= mb
->pos
- main_clause
->handler_offset
;
4132 mono_mb_patch_addr (mb
, pos_leave
, mb
->pos
- (pos_leave
+ 4));
4135 mono_mb_emit_ldloc (mb
, loc_return
);
4137 mono_mb_emit_byte (mb
, CEE_RET
);
4139 res
= mono_remoting_mb_create_and_cache (method
, mb
, csig
, csig
->param_count
+ 16);
4142 header
= ((MonoMethodNormal
*)res
)->header
;
4143 header
->num_clauses
= 1;
4144 header
->clauses
= main_clause
;
4149 /* mono_marshal_get_xappdomain_invoke ()
4150 * Generates a fast remoting wrapper for cross app domain calls.
4153 mono_marshal_get_xappdomain_invoke (MonoMethod
*method
)
4155 MonoMethodSignature
*sig
;
4156 MonoMethodBuilder
*mb
;
4158 int i
, j
, complex_count
, complex_out_count
, copy_locals_base
;
4160 MonoClass
*ret_class
= NULL
;
4161 MonoMethod
*xdomain_method
;
4162 int ret_marshal_type
= MONO_MARSHAL_NONE
;
4163 int loc_array
=0, loc_serialized_data
=-1, loc_real_proxy
;
4164 int loc_old_domainid
, loc_domainid
, loc_return
=0, loc_serialized_exc
=0, loc_context
;
4165 int pos
, pos_dispatch
, pos_noex
;
4166 gboolean copy_return
= FALSE
;
4170 if (method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE
|| method
->wrapper_type
== MONO_WRAPPER_XDOMAIN_INVOKE
)
4173 /* we cant remote methods without this pointer */
4174 if (!mono_method_signature (method
)->hasthis
)
4177 if (!mono_marshal_supports_fast_xdomain (method
))
4178 return mono_marshal_get_remoting_invoke (method
);
4180 mono_remoting_marshal_init ();
4182 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_XDOMAIN_INVOKE
)))
4185 sig
= signature_no_pinvoke (method
);
4187 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_XDOMAIN_INVOKE
);
4188 mb
->method
->save_lmf
= 1;
4190 /* Count the number of parameters that need to be serialized */
4192 marshal_types
= alloca (sizeof (int) * sig
->param_count
);
4193 complex_count
= complex_out_count
= 0;
4194 for (i
= 0; i
< sig
->param_count
; i
++) {
4195 MonoType
*ptype
= sig
->params
[i
];
4196 int mt
= mono_get_xdomain_marshal_type (ptype
);
4198 /* If the [Out] attribute is applied to a parameter that can be internally copied,
4199 * the copy will be made by reusing the original object instance
4201 if ((ptype
->attrs
& PARAM_ATTRIBUTE_OUT
) != 0 && mt
== MONO_MARSHAL_COPY
&& !ptype
->byref
)
4202 mt
= MONO_MARSHAL_COPY_OUT
;
4203 else if (mt
== MONO_MARSHAL_SERIALIZE
) {
4205 if (ptype
->byref
) complex_out_count
++;
4207 marshal_types
[i
] = mt
;
4210 if (sig
->ret
->type
!= MONO_TYPE_VOID
) {
4211 ret_marshal_type
= mono_get_xdomain_marshal_type (sig
->ret
);
4212 ret_class
= mono_class_from_mono_type (sig
->ret
);
4213 copy_return
= ret_marshal_type
!= MONO_MARSHAL_SERIALIZE
;
4218 if (complex_count
> 0)
4219 loc_array
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
4220 loc_serialized_data
= mono_mb_add_local (mb
, &byte_array_class
->byval_arg
);
4221 loc_real_proxy
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
4223 loc_return
= mono_mb_add_local (mb
, sig
->ret
);
4224 loc_old_domainid
= mono_mb_add_local (mb
, &mono_defaults
.int32_class
->byval_arg
);
4225 loc_domainid
= mono_mb_add_local (mb
, &mono_defaults
.int32_class
->byval_arg
);
4226 loc_serialized_exc
= mono_mb_add_local (mb
, &byte_array_class
->byval_arg
);
4227 loc_context
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
4229 /* Save thread domain data */
4231 mono_mb_emit_icall (mb
, mono_context_get
);
4232 mono_mb_emit_byte (mb
, CEE_DUP
);
4233 mono_mb_emit_stloc (mb
, loc_context
);
4235 /* If the thread is not running in the default context, it needs to go
4236 * through the whole remoting sink, since the context is going to change
4238 mono_mb_emit_managed_call (mb
, method_needs_context_sink
, NULL
);
4239 pos
= mono_mb_emit_short_branch (mb
, CEE_BRTRUE_S
);
4241 /* Another case in which the fast path can't be used: when the target domain
4242 * has a different image for the same assembly.
4245 /* Get the target domain id */
4247 mono_mb_emit_ldarg (mb
, 0);
4248 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
4249 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
4250 mono_mb_emit_byte (mb
, CEE_DUP
);
4251 mono_mb_emit_stloc (mb
, loc_real_proxy
);
4253 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoRealProxy
, target_domain_id
));
4254 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
4255 mono_mb_emit_stloc (mb
, loc_domainid
);
4257 /* Check if the target domain has the same image for the required assembly */
4259 mono_mb_emit_ldloc (mb
, loc_domainid
);
4260 mono_mb_emit_ptr (mb
, method
->klass
->image
);
4261 mono_mb_emit_icall (mb
, mono_marshal_check_domain_image
);
4262 pos_dispatch
= mono_mb_emit_short_branch (mb
, CEE_BRTRUE_S
);
4264 /* Use the whole remoting sink to dispatch this message */
4266 mono_mb_patch_short_branch (mb
, pos
);
4268 mono_mb_emit_ldarg (mb
, 0);
4269 for (i
= 0; i
< sig
->param_count
; i
++)
4270 mono_mb_emit_ldarg (mb
, i
+ 1);
4272 mono_mb_emit_managed_call (mb
, mono_marshal_get_remoting_invoke (method
), NULL
);
4273 mono_mb_emit_byte (mb
, CEE_RET
);
4274 mono_mb_patch_short_branch (mb
, pos_dispatch
);
4276 /* Create the array that will hold the parameters to be serialized */
4278 if (complex_count
> 0) {
4279 mono_mb_emit_icon (mb
, (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
&& complex_out_count
> 0) ? complex_count
+ 1 : complex_count
); /* +1 for the return type */
4280 mono_mb_emit_op (mb
, CEE_NEWARR
, mono_defaults
.object_class
);
4283 for (i
= 0; i
< sig
->param_count
; i
++) {
4285 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
) continue;
4286 pclass
= mono_class_from_mono_type (sig
->params
[i
]);
4287 mono_mb_emit_byte (mb
, CEE_DUP
);
4288 mono_mb_emit_icon (mb
, j
);
4289 mono_mb_emit_ldarg (mb
, i
+ 1); /* 0=this */
4290 if (sig
->params
[i
]->byref
) {
4291 if (pclass
->valuetype
)
4292 mono_mb_emit_op (mb
, CEE_LDOBJ
, pclass
);
4294 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
4296 if (pclass
->valuetype
)
4297 mono_mb_emit_op (mb
, CEE_BOX
, pclass
);
4298 mono_mb_emit_byte (mb
, CEE_STELEM_REF
);
4301 mono_mb_emit_stloc (mb
, loc_array
);
4303 /* Serialize parameters */
4305 mono_mb_emit_ldloc (mb
, loc_array
);
4306 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
4307 mono_mb_emit_stloc (mb
, loc_serialized_data
);
4309 mono_mb_emit_byte (mb
, CEE_LDNULL
);
4310 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
4311 mono_mb_emit_stloc (mb
, loc_serialized_data
);
4316 mono_mb_emit_ldloc (mb
, loc_domainid
);
4317 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
4318 mono_marshal_emit_switch_domain (mb
);
4319 mono_mb_emit_stloc (mb
, loc_old_domainid
);
4321 /* Load the arguments */
4323 mono_mb_emit_ldloc (mb
, loc_real_proxy
);
4324 mono_mb_emit_ldloc_addr (mb
, loc_serialized_data
);
4325 mono_mb_emit_ldloc_addr (mb
, loc_serialized_exc
);
4327 copy_locals_base
= mb
->locals
;
4328 for (i
= 0; i
< sig
->param_count
; i
++) {
4329 switch (marshal_types
[i
]) {
4330 case MONO_MARSHAL_SERIALIZE
:
4332 case MONO_MARSHAL_COPY
: {
4333 mono_mb_emit_ldarg (mb
, i
+1);
4334 if (sig
->params
[i
]->byref
) {
4335 /* make a local copy of the byref parameter. The real parameter
4336 * will be updated after the xdomain call
4338 MonoClass
*pclass
= mono_class_from_mono_type (sig
->params
[i
]);
4339 int copy_local
= mono_mb_add_local (mb
, &(pclass
->byval_arg
));
4340 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
4341 mono_mb_emit_stloc (mb
, copy_local
);
4342 mono_mb_emit_ldloc_addr (mb
, copy_local
);
4346 case MONO_MARSHAL_COPY_OUT
:
4347 case MONO_MARSHAL_NONE
:
4348 mono_mb_emit_ldarg (mb
, i
+1);
4353 /* Make the call to the invoke wrapper in the target domain */
4355 xdomain_method
= mono_marshal_get_xappdomain_dispatch (method
, marshal_types
, complex_count
, complex_out_count
, ret_marshal_type
);
4356 mono_marshal_emit_load_domain_method (mb
, xdomain_method
);
4357 mono_mb_emit_calli (mb
, mono_method_signature (xdomain_method
));
4360 mono_mb_emit_stloc (mb
, loc_return
);
4364 mono_mb_emit_ldloc (mb
, loc_old_domainid
);
4365 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
4366 mono_marshal_emit_switch_domain (mb
);
4367 mono_mb_emit_byte (mb
, CEE_POP
);
4369 /* Restore thread domain data */
4371 mono_mb_emit_ldloc (mb
, loc_context
);
4372 mono_mb_emit_icall (mb
, mono_context_set
);
4374 /* if (loc_serialized_exc != null) ... */
4376 mono_mb_emit_ldloc (mb
, loc_serialized_exc
);
4377 pos_noex
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
4379 mono_mb_emit_ldloc (mb
, loc_serialized_exc
);
4380 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
4381 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
4382 mono_mb_emit_op (mb
, CEE_CASTCLASS
, mono_defaults
.exception_class
);
4383 mono_mb_emit_managed_call (mb
, method_exc_fixexc
, NULL
);
4384 mono_mb_emit_byte (mb
, CEE_THROW
);
4385 mono_mb_patch_addr_s (mb
, pos_noex
, mb
->pos
- pos_noex
- 1);
4387 /* copy back non-serialized output parameters */
4390 for (i
= 0; i
< sig
->param_count
; i
++) {
4391 if (!sig
->params
[i
]->byref
|| marshal_types
[i
] != MONO_MARSHAL_COPY
) continue;
4392 mono_mb_emit_ldarg (mb
, i
+ 1);
4393 mono_mb_emit_ldloc (mb
, copy_locals_base
+ (j
++));
4394 mono_marshal_emit_xdomain_copy_value (mb
, mono_class_from_mono_type (sig
->params
[i
]));
4395 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
4398 /* Deserialize out parameters */
4400 if (complex_out_count
> 0) {
4401 mono_mb_emit_ldloc (mb
, loc_serialized_data
);
4402 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
4403 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
4404 mono_mb_emit_stloc (mb
, loc_array
);
4406 /* Copy back output parameters and return type */
4409 for (i
= 0; i
< sig
->param_count
; i
++) {
4410 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
) continue;
4411 if (sig
->params
[i
]->byref
) {
4412 MonoClass
*pclass
= mono_class_from_mono_type (sig
->params
[i
]);
4413 mono_mb_emit_ldarg (mb
, i
+ 1);
4414 mono_mb_emit_ldloc (mb
, loc_array
);
4415 mono_mb_emit_icon (mb
, j
);
4416 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
4417 if (pclass
->valuetype
) {
4418 mono_mb_emit_op (mb
, CEE_UNBOX
, pclass
);
4419 mono_mb_emit_op (mb
, CEE_LDOBJ
, pclass
);
4420 mono_mb_emit_op (mb
, CEE_STOBJ
, pclass
);
4422 if (pclass
!= mono_defaults
.object_class
)
4423 mono_mb_emit_op (mb
, CEE_CASTCLASS
, pclass
);
4424 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
4430 if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
4431 mono_mb_emit_ldloc (mb
, loc_array
);
4432 mono_mb_emit_icon (mb
, complex_count
);
4433 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
4434 if (ret_class
->valuetype
) {
4435 mono_mb_emit_op (mb
, CEE_UNBOX
, ret_class
);
4436 mono_mb_emit_op (mb
, CEE_LDOBJ
, ret_class
);
4439 } else if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
4440 mono_mb_emit_ldloc (mb
, loc_serialized_data
);
4441 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
4442 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
4443 if (ret_class
->valuetype
) {
4444 mono_mb_emit_op (mb
, CEE_UNBOX
, ret_class
);
4445 mono_mb_emit_op (mb
, CEE_LDOBJ
, ret_class
);
4446 } else if (ret_class
!= mono_defaults
.object_class
) {
4447 mono_mb_emit_op (mb
, CEE_CASTCLASS
, ret_class
);
4450 mono_mb_emit_ldloc (mb
, loc_serialized_data
);
4451 mono_mb_emit_byte (mb
, CEE_DUP
);
4452 pos
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
4453 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
4455 mono_mb_patch_addr_s (mb
, pos
, mb
->pos
- (pos
+ 1));
4456 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
4457 mono_mb_emit_byte (mb
, CEE_POP
);
4461 mono_mb_emit_ldloc (mb
, loc_return
);
4462 if (ret_marshal_type
== MONO_MARSHAL_COPY
)
4463 mono_marshal_emit_xdomain_copy_value (mb
, ret_class
);
4466 mono_mb_emit_byte (mb
, CEE_RET
);
4468 res
= mono_remoting_mb_create_and_cache (method
, mb
, sig
, sig
->param_count
+ 16);
4475 mono_marshal_get_remoting_invoke_for_target (MonoMethod
*method
, MonoRemotingTarget target_type
)
4477 if (target_type
== MONO_REMOTING_TARGET_APPDOMAIN
)
4478 return mono_marshal_get_xappdomain_invoke (method
);
4479 else if (target_type
== MONO_REMOTING_TARGET_COMINTEROP
)
4480 return cominterop_get_invoke (method
);
4482 return mono_marshal_get_remoting_invoke (method
);
4485 G_GNUC_UNUSED
static gpointer
4486 mono_marshal_load_remoting_wrapper (MonoRealProxy
*rp
, MonoMethod
*method
)
4488 if (rp
->target_domain_id
!= -1)
4489 return mono_compile_method (mono_marshal_get_xappdomain_invoke (method
));
4491 return mono_compile_method (mono_marshal_get_remoting_invoke (method
));
4495 mono_marshal_get_remoting_invoke_with_check (MonoMethod
*method
)
4497 MonoMethodSignature
*sig
;
4498 MonoMethodBuilder
*mb
;
4499 MonoMethod
*res
, *native
;
4500 int i
, pos
, pos_rem
;
4504 if (method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
)
4507 /* we cant remote methods without this pointer */
4508 g_assert (mono_method_signature (method
)->hasthis
);
4510 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
)))
4513 sig
= signature_no_pinvoke (method
);
4515 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
);
4517 for (i
= 0; i
<= sig
->param_count
; i
++)
4518 mono_mb_emit_ldarg (mb
, i
);
4520 mono_mb_emit_ldarg (mb
, 0);
4521 pos
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
4523 if (mono_marshal_supports_fast_xdomain (method
)) {
4524 mono_mb_emit_ldarg (mb
, 0);
4525 pos_rem
= mono_mb_emit_xdomain_check (mb
, CEE_BEQ
);
4527 /* wrapper for cross app domain calls */
4528 native
= mono_marshal_get_xappdomain_invoke (method
);
4529 mono_mb_emit_managed_call (mb
, native
, mono_method_signature (native
));
4530 mono_mb_emit_byte (mb
, CEE_RET
);
4532 mono_mb_patch_addr (mb
, pos_rem
, mb
->pos
- (pos_rem
+ 4));
4534 /* wrapper for normal remote calls */
4535 native
= mono_marshal_get_remoting_invoke (method
);
4536 mono_mb_emit_managed_call (mb
, native
, mono_method_signature (native
));
4537 mono_mb_emit_byte (mb
, CEE_RET
);
4540 mono_mb_patch_branch (mb
, pos
);
4541 mono_mb_emit_managed_call (mb
, method
, mono_method_signature (method
));
4542 mono_mb_emit_byte (mb
, CEE_RET
);
4544 res
= mono_remoting_mb_create_and_cache (method
, mb
, sig
, sig
->param_count
+ 16);
4551 * the returned method invokes all methods in a multicast delegate
4554 mono_marshal_get_delegate_invoke (MonoMethod
*method
)
4556 MonoMethodSignature
*sig
, *static_sig
;
4558 MonoMethodBuilder
*mb
;
4564 g_assert (method
&& method
->klass
->parent
== mono_defaults
.multicastdelegate_class
&&
4565 !strcmp (method
->name
, "Invoke"));
4567 sig
= signature_no_pinvoke (method
);
4569 cache
= method
->klass
->image
->delegate_invoke_cache
;
4570 if ((res
= mono_marshal_find_in_cache (cache
, sig
)))
4573 static_sig
= signature_dup (method
->klass
->image
, sig
);
4574 static_sig
->hasthis
= 0;
4576 name
= mono_signature_to_name (sig
, "invoke");
4577 mb
= mono_mb_new (mono_defaults
.multicastdelegate_class
, name
, MONO_WRAPPER_DELEGATE_INVOKE
);
4580 /* allocate local 0 (object) */
4581 mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
4583 g_assert (sig
->hasthis
);
4587 * prev.Invoke( args .. );
4588 * return this.<target>( args .. );
4591 /* this wrapper can be used in unmanaged-managed transitions */
4592 emit_thread_interrupt_checkpoint (mb
);
4594 /* get this->prev */
4595 mono_mb_emit_ldarg (mb
, 0);
4596 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoMulticastDelegate
, prev
));
4597 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
4599 /* if prev != null */
4600 pos0
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
4604 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
4605 mono_mb_emit_byte (mb
, CEE_MONO_NOT_TAKEN
);
4607 mono_mb_emit_ldarg (mb
, 0);
4608 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoMulticastDelegate
, prev
));
4609 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
4610 for (i
= 0; i
< sig
->param_count
; i
++)
4611 mono_mb_emit_ldarg (mb
, i
+ 1);
4612 mono_mb_emit_managed_call (mb
, method
, mono_method_signature (method
));
4613 if (sig
->ret
->type
!= MONO_TYPE_VOID
)
4614 mono_mb_emit_byte (mb
, CEE_POP
);
4616 /* continued or prev == null */
4617 mono_mb_patch_branch (mb
, pos0
);
4619 /* get this->target */
4620 mono_mb_emit_ldarg (mb
, 0);
4621 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoDelegate
, target
));
4622 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
4623 mono_mb_emit_stloc (mb
, 0);
4625 /* if target != null */
4626 mono_mb_emit_ldloc (mb
, 0);
4627 pos0
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
4629 /* then call this->method_ptr nonstatic */
4630 mono_mb_emit_ldloc (mb
, 0);
4631 for (i
= 0; i
< sig
->param_count
; ++i
)
4632 mono_mb_emit_ldarg (mb
, i
+ 1);
4633 mono_mb_emit_ldarg (mb
, 0);
4634 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoDelegate
, method_ptr
));
4635 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
4636 mono_mb_emit_op (mb
, CEE_CALLI
, sig
);
4638 pos1
= mono_mb_emit_branch (mb
, CEE_BR
);
4640 /* else [target == null] call this->method_ptr static */
4641 mono_mb_patch_branch (mb
, pos0
);
4643 for (i
= 0; i
< sig
->param_count
; ++i
)
4644 mono_mb_emit_ldarg (mb
, i
+ 1);
4645 mono_mb_emit_ldarg (mb
, 0);
4646 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoDelegate
, method_ptr
));
4647 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
4648 mono_mb_emit_op (mb
, CEE_CALLI
, static_sig
);
4651 mono_mb_patch_branch (mb
, pos1
);
4652 mono_mb_emit_byte (mb
, CEE_RET
);
4654 res
= mono_mb_create_and_cache (cache
, sig
,
4655 mb
, sig
, sig
->param_count
+ 16);
4662 * signature_dup_add_this:
4664 * Make a copy of @sig, adding an explicit this argument.
4666 static MonoMethodSignature
*
4667 signature_dup_add_this (MonoMethodSignature
*sig
, MonoClass
*klass
)
4669 MonoMethodSignature
*res
;
4672 res
= mono_metadata_signature_alloc (klass
->image
, sig
->param_count
+ 1);
4673 memcpy (res
, sig
, sizeof (MonoMethodSignature
));
4674 res
->param_count
= sig
->param_count
+ 1;
4675 res
->hasthis
= FALSE
;
4676 for (i
= sig
->param_count
- 1; i
>= 0; i
--)
4677 res
->params
[i
+ 1] = sig
->params
[i
];
4678 res
->params
[0] = &mono_ptr_class_get (&klass
->byval_arg
)->byval_arg
;
4684 MonoMethodSignature
*ctor_sig
;
4685 MonoMethodSignature
*sig
;
4688 /* protected by the marshal lock, contains CtorSigPair pointers */
4689 static GSList
*strsig_list
= NULL
;
4691 static MonoMethodSignature
*
4692 lookup_string_ctor_signature (MonoMethodSignature
*sig
)
4694 MonoMethodSignature
*callsig
;
4698 mono_marshal_lock ();
4700 for (item
= strsig_list
; item
; item
= item
->next
) {
4702 /* mono_metadata_signature_equal () is safe to call with the marshal lock
4703 * because it is lock-free.
4705 if (mono_metadata_signature_equal (sig
, cs
->ctor_sig
)) {
4710 mono_marshal_unlock ();
4714 static MonoMethodSignature
*
4715 add_string_ctor_signature (MonoMethod
*method
)
4717 MonoMethodSignature
*callsig
;
4720 callsig
= signature_dup (method
->klass
->image
, mono_method_signature (method
));
4721 callsig
->ret
= &mono_defaults
.string_class
->byval_arg
;
4722 cs
= g_new (CtorSigPair
, 1);
4724 cs
->ctor_sig
= mono_method_signature (method
);
4726 mono_marshal_lock ();
4727 strsig_list
= g_slist_prepend (strsig_list
, cs
);
4728 mono_marshal_unlock ();
4733 * generates IL code for the runtime invoke function
4734 * MonoObject *runtime_invoke (MonoObject *this, void **params, MonoObject **exc, void* method)
4736 * we also catch exceptions if exc != null
4739 mono_marshal_get_runtime_invoke (MonoMethod
*method
)
4741 MonoMethodSignature
*sig
, *csig
, *callsig
;
4742 MonoExceptionClause
*clause
;
4743 MonoMethodHeader
*header
;
4744 MonoMethodBuilder
*mb
;
4745 GHashTable
*cache
= NULL
;
4746 MonoClass
*target_klass
;
4747 MonoMethod
*res
= NULL
;
4748 static MonoString
*string_dummy
= NULL
;
4749 static MonoMethodSignature
*dealy_abort_sig
= NULL
;
4755 if (method
->string_ctor
) {
4756 callsig
= lookup_string_ctor_signature (mono_method_signature (method
));
4758 callsig
= add_string_ctor_signature (method
);
4760 if (method
->klass
->valuetype
&& mono_method_signature (method
)->hasthis
) {
4762 * Valuetype methods receive a managed pointer as the this argument.
4763 * Create a new signature to reflect this.
4765 callsig
= signature_dup_add_this (mono_method_signature (method
), method
->klass
);
4767 callsig
= mono_method_signature (method
);
4771 target_klass
= mono_defaults
.object_class
;
4773 * if types in the signature belong to non-mscorlib, we cache only
4774 * in the method image
4776 if (mono_class_from_mono_type (callsig
->ret
)->image
!= mono_defaults
.corlib
) {
4777 target_klass
= method
->klass
;
4779 for (i
= 0; i
< callsig
->param_count
; i
++) {
4780 if (mono_class_from_mono_type (callsig
->params
[i
])->image
!= mono_defaults
.corlib
) {
4781 target_klass
= method
->klass
;
4786 cache
= target_klass
->image
->runtime_invoke_cache
;
4788 /* from mono_marshal_find_in_cache */
4789 mono_marshal_lock ();
4790 res
= g_hash_table_lookup (cache
, callsig
);
4791 mono_marshal_unlock ();
4797 if (!dealy_abort_sig
) {
4798 dealy_abort_sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
4799 dealy_abort_sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
4800 dealy_abort_sig
->pinvoke
= 0;
4803 /* to make it work with our special string constructors */
4804 if (!string_dummy
) {
4805 MONO_GC_REGISTER_ROOT (string_dummy
);
4806 string_dummy
= mono_string_new_wrapper ("dummy");
4809 sig
= mono_method_signature (method
);
4811 csig
= mono_metadata_signature_alloc (target_klass
->image
, 4);
4813 csig
->ret
= &mono_defaults
.object_class
->byval_arg
;
4814 if (method
->klass
->valuetype
&& mono_method_signature (method
)->hasthis
)
4815 csig
->params
[0] = callsig
->params
[0];
4817 csig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
4818 csig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
4819 csig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
4820 csig
->params
[3] = &mono_defaults
.int_class
->byval_arg
;
4822 name
= mono_signature_to_name (callsig
, "runtime_invoke");
4823 mb
= mono_mb_new (target_klass
, name
, MONO_WRAPPER_RUNTIME_INVOKE
);
4826 /* allocate local 0 (object) tmp */
4827 mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
4828 /* allocate local 1 (object) exc */
4829 mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
4831 /* cond set *exc to null */
4832 mono_mb_emit_byte (mb
, CEE_LDARG_2
);
4833 mono_mb_emit_byte (mb
, CEE_BRFALSE_S
);
4834 mono_mb_emit_byte (mb
, 3);
4835 mono_mb_emit_byte (mb
, CEE_LDARG_2
);
4836 mono_mb_emit_byte (mb
, CEE_LDNULL
);
4837 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
4839 emit_thread_force_interrupt_checkpoint (mb
);
4842 if (method
->string_ctor
) {
4843 mono_mb_emit_ptr (mb
, string_dummy
);
4845 mono_mb_emit_ldarg (mb
, 0);
4849 for (i
= 0; i
< sig
->param_count
; i
++) {
4850 MonoType
*t
= sig
->params
[i
];
4853 mono_mb_emit_ldarg (mb
, 1);
4855 mono_mb_emit_icon (mb
, sizeof (gpointer
) * i
);
4856 mono_mb_emit_byte (mb
, CEE_ADD
);
4858 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
4863 type
= sig
->params
[i
]->type
;
4867 case MONO_TYPE_BOOLEAN
:
4871 case MONO_TYPE_CHAR
:
4880 mono_mb_emit_byte (mb
, mono_type_to_ldind (sig
->params
[i
]));
4882 case MONO_TYPE_STRING
:
4883 case MONO_TYPE_CLASS
:
4884 case MONO_TYPE_ARRAY
:
4886 case MONO_TYPE_SZARRAY
:
4887 case MONO_TYPE_OBJECT
:
4890 case MONO_TYPE_GENERICINST
:
4891 if (!mono_type_generic_inst_is_valuetype (sig
->params
[i
])) {
4897 case MONO_TYPE_VALUETYPE
:
4898 if (type
== MONO_TYPE_VALUETYPE
&& t
->data
.klass
->enumtype
) {
4899 type
= t
->data
.klass
->enum_basetype
->type
;
4902 if (mono_class_is_nullable (mono_class_from_mono_type (sig
->params
[i
]))) {
4903 /* Need to convert a boxed vtype to an mp to a Nullable struct */
4904 mono_mb_emit_op (mb
, CEE_UNBOX
, mono_class_from_mono_type (sig
->params
[i
]));
4905 mono_mb_emit_op (mb
, CEE_LDOBJ
, mono_class_from_mono_type (sig
->params
[i
]));
4907 mono_mb_emit_op (mb
, CEE_LDOBJ
, mono_class_from_mono_type (sig
->params
[i
]));
4911 g_assert_not_reached ();
4915 mono_mb_emit_ldarg (mb
, 3);
4916 mono_mb_emit_calli (mb
, callsig
);
4918 if (sig
->ret
->byref
) {
4920 g_assert_not_reached ();
4924 switch (sig
->ret
->type
) {
4925 case MONO_TYPE_VOID
:
4926 if (!method
->string_ctor
)
4927 mono_mb_emit_byte (mb
, CEE_LDNULL
);
4929 case MONO_TYPE_BOOLEAN
:
4930 case MONO_TYPE_CHAR
:
4943 case MONO_TYPE_VALUETYPE
:
4944 case MONO_TYPE_TYPEDBYREF
:
4945 case MONO_TYPE_GENERICINST
:
4946 /* box value types */
4947 mono_mb_emit_op (mb
, CEE_BOX
, mono_class_from_mono_type (sig
->ret
));
4949 case MONO_TYPE_STRING
:
4950 case MONO_TYPE_CLASS
:
4951 case MONO_TYPE_ARRAY
:
4952 case MONO_TYPE_SZARRAY
:
4953 case MONO_TYPE_OBJECT
:
4958 g_assert_not_reached ();
4961 mono_mb_emit_stloc (mb
, 0);
4963 pos
= mono_mb_emit_branch (mb
, CEE_LEAVE
);
4965 mono_loader_lock ();
4966 clause
= mono_mempool_alloc0 (target_klass
->image
->mempool
, sizeof (MonoExceptionClause
));
4967 mono_loader_unlock ();
4968 clause
->flags
= MONO_EXCEPTION_CLAUSE_FILTER
;
4969 clause
->try_len
= mb
->pos
;
4972 clause
->data
.filter_offset
= mb
->pos
;
4974 mono_mb_emit_byte (mb
, CEE_POP
);
4975 mono_mb_emit_byte (mb
, CEE_LDARG_2
);
4976 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
4977 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
4978 mono_mb_emit_byte (mb
, CEE_CGT_UN
);
4979 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
4980 mono_mb_emit_byte (mb
, CEE_ENDFILTER
);
4982 clause
->handler_offset
= mb
->pos
;
4985 /* store exception */
4986 mono_mb_emit_stloc (mb
, 1);
4988 mono_mb_emit_byte (mb
, CEE_LDARG_2
);
4989 mono_mb_emit_ldloc (mb
, 1);
4990 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
4992 mono_mb_emit_byte (mb
, CEE_LDNULL
);
4993 mono_mb_emit_stloc (mb
, 0);
4995 /* Check for the abort exception */
4996 mono_mb_emit_ldloc (mb
, 1);
4997 mono_mb_emit_op (mb
, CEE_ISINST
, mono_defaults
.threadabortexception_class
);
4998 posna
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
5000 /* Delay the abort exception */
5001 mono_mb_emit_native_call (mb
, dealy_abort_sig
, ves_icall_System_Threading_Thread_ResetAbort
);
5003 mono_mb_patch_short_branch (mb
, posna
);
5004 mono_mb_emit_branch (mb
, CEE_LEAVE
);
5006 clause
->handler_len
= mb
->pos
- clause
->handler_offset
;
5009 mono_mb_patch_branch (mb
, pos
);
5010 mono_mb_emit_ldloc (mb
, 0);
5011 mono_mb_emit_byte (mb
, CEE_RET
);
5013 /* taken from mono_mb_create_and_cache */
5014 mono_marshal_lock ();
5015 res
= g_hash_table_lookup (cache
, callsig
);
5016 mono_marshal_unlock ();
5018 /* Somebody may have created it before us */
5021 newm
= mono_mb_create_method (mb
, csig
, sig
->param_count
+ 16);
5023 mono_marshal_lock ();
5024 res
= g_hash_table_lookup (cache
, callsig
);
5027 g_hash_table_insert (cache
, callsig
, res
);
5028 g_hash_table_insert (wrapper_hash
, res
, callsig
);
5030 mono_free_method (newm
);
5032 mono_marshal_unlock ();
5035 /* end mono_mb_create_and_cache */
5039 header
= ((MonoMethodNormal
*)res
)->header
;
5040 header
->num_clauses
= 1;
5041 header
->clauses
= clause
;
5047 mono_mb_emit_auto_layout_exception (MonoMethodBuilder
*mb
, MonoClass
*klass
)
5049 char *msg
= g_strdup_printf ("The type `%s.%s' layout needs to be Sequential or Explicit",
5050 klass
->name_space
, klass
->name
);
5052 mono_mb_emit_exception_marshal_directive (mb
, msg
);
5056 * mono_marshal_get_ldfld_remote_wrapper:
5057 * @klass: The return type
5059 * This method generates a wrapper for calling mono_load_remote_field_new with
5060 * the appropriate return type.
5063 mono_marshal_get_ldfld_remote_wrapper (MonoClass
*klass
)
5065 MonoMethodSignature
*sig
, *csig
;
5066 MonoMethodBuilder
*mb
;
5071 cache
= klass
->image
->ldfld_remote_wrapper_cache
;
5072 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
5076 * This wrapper is similar to an icall wrapper but all the wrappers
5077 * call the same C function, but with a different signature.
5079 name
= g_strdup_printf ("__mono_load_remote_field_new_wrapper_%s.%s", klass
->name_space
, klass
->name
);
5080 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_LDFLD_REMOTE
);
5083 mb
->method
->save_lmf
= 1;
5085 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3);
5086 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
5087 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
5088 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
5089 sig
->ret
= &klass
->this_arg
;
5091 mono_mb_emit_ldarg (mb
, 0);
5092 mono_mb_emit_ldarg (mb
, 1);
5093 mono_mb_emit_ldarg (mb
, 2);
5095 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3);
5096 csig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
5097 csig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
5098 csig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
5099 csig
->ret
= &klass
->this_arg
;
5102 mono_mb_emit_native_call (mb
, csig
, mono_load_remote_field_new
);
5103 emit_thread_interrupt_checkpoint (mb
);
5105 mono_mb_emit_byte (mb
, CEE_RET
);
5107 res
= mono_mb_create_and_cache (cache
, klass
,
5108 mb
, sig
, sig
->param_count
+ 16);
5115 * mono_marshal_get_ldfld_wrapper:
5116 * @type: the type of the field
5118 * This method generates a function which can be use to load a field with type
5119 * @type from an object. The generated function has the following signature:
5120 * <@type> ldfld_wrapper (MonoObject *this, MonoClass *class, MonoClassField *field, int offset)
5123 mono_marshal_get_ldfld_wrapper (MonoType
*type
)
5125 MonoMethodSignature
*sig
;
5126 MonoMethodBuilder
*mb
;
5131 int t
, pos0
, pos1
= 0;
5133 type
= mono_type_get_underlying_type (type
);
5138 if (type
->type
== MONO_TYPE_SZARRAY
) {
5139 klass
= mono_defaults
.array_class
;
5140 } else if (type
->type
== MONO_TYPE_VALUETYPE
) {
5141 klass
= type
->data
.klass
;
5142 } else if (t
== MONO_TYPE_OBJECT
|| t
== MONO_TYPE_CLASS
|| t
== MONO_TYPE_STRING
) {
5143 klass
= mono_defaults
.object_class
;
5144 } else if (t
== MONO_TYPE_PTR
|| t
== MONO_TYPE_FNPTR
) {
5145 klass
= mono_defaults
.int_class
;
5146 } else if (t
== MONO_TYPE_GENERICINST
) {
5147 if (mono_type_generic_inst_is_valuetype (type
))
5148 klass
= mono_class_from_mono_type (type
);
5150 klass
= mono_defaults
.object_class
;
5152 klass
= mono_class_from_mono_type (type
);
5155 klass
= mono_defaults
.int_class
;
5158 cache
= klass
->image
->ldfld_wrapper_cache
;
5159 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
5162 /* we add the %p pointer value of klass because class names are not unique */
5163 name
= g_strdup_printf ("__ldfld_wrapper_%p_%s.%s", klass
, klass
->name_space
, klass
->name
);
5164 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_LDFLD
);
5167 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 4);
5168 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
5169 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
5170 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
5171 sig
->params
[3] = &mono_defaults
.int_class
->byval_arg
;
5172 sig
->ret
= &klass
->byval_arg
;
5174 mono_mb_emit_ldarg (mb
, 0);
5175 pos0
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
5177 mono_mb_emit_ldarg (mb
, 0);
5178 mono_mb_emit_ldarg (mb
, 1);
5179 mono_mb_emit_ldarg (mb
, 2);
5181 mono_mb_emit_managed_call (mb
, mono_marshal_get_ldfld_remote_wrapper (klass
), NULL
);
5184 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5185 csig->params [0] = &mono_defaults.object_class->byval_arg;
5186 csig->params [1] = &mono_defaults.int_class->byval_arg;
5187 csig->params [2] = &mono_defaults.int_class->byval_arg;
5188 csig->ret = &klass->this_arg;
5191 mono_mb_emit_native_call (mb, csig, mono_load_remote_field_new);
5192 emit_thread_interrupt_checkpoint (mb);
5195 if (klass
->valuetype
) {
5196 mono_mb_emit_op (mb
, CEE_UNBOX
, klass
);
5197 pos1
= mono_mb_emit_branch (mb
, CEE_BR
);
5199 mono_mb_emit_byte (mb
, CEE_RET
);
5203 mono_mb_patch_branch (mb
, pos0
);
5205 mono_mb_emit_ldarg (mb
, 0);
5206 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
5207 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
5208 mono_mb_emit_ldarg (mb
, 3);
5209 mono_mb_emit_byte (mb
, CEE_ADD
);
5211 if (klass
->valuetype
)
5212 mono_mb_patch_branch (mb
, pos1
);
5217 case MONO_TYPE_BOOLEAN
:
5218 case MONO_TYPE_CHAR
:
5227 case MONO_TYPE_ARRAY
:
5228 case MONO_TYPE_SZARRAY
:
5229 case MONO_TYPE_OBJECT
:
5230 case MONO_TYPE_CLASS
:
5231 case MONO_TYPE_STRING
:
5235 case MONO_TYPE_FNPTR
:
5236 mono_mb_emit_byte (mb
, mono_type_to_ldind (type
));
5238 case MONO_TYPE_VALUETYPE
:
5239 g_assert (!klass
->enumtype
);
5240 mono_mb_emit_op (mb
, CEE_LDOBJ
, klass
);
5242 case MONO_TYPE_GENERICINST
:
5243 if (mono_type_generic_inst_is_valuetype (type
)) {
5244 mono_mb_emit_op (mb
, CEE_LDOBJ
, klass
);
5246 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
5250 g_warning ("type %x not implemented", type
->type
);
5251 g_assert_not_reached ();
5254 mono_mb_emit_byte (mb
, CEE_RET
);
5256 res
= mono_mb_create_and_cache (cache
, klass
,
5257 mb
, sig
, sig
->param_count
+ 16);
5264 * mono_marshal_get_ldflda_wrapper:
5265 * @type: the type of the field
5267 * This method generates a function which can be used to load a field address
5268 * from an object. The generated function has the following signature:
5269 * gpointer ldflda_wrapper (MonoObject *this, MonoClass *class, MonoClassField *field, int offset);
5272 mono_marshal_get_ldflda_wrapper (MonoType
*type
)
5274 MonoMethodSignature
*sig
;
5275 MonoMethodBuilder
*mb
;
5282 type
= mono_type_get_underlying_type (type
);
5286 if (type
->type
== MONO_TYPE_SZARRAY
) {
5287 klass
= mono_defaults
.array_class
;
5288 } else if (type
->type
== MONO_TYPE_VALUETYPE
) {
5289 klass
= type
->data
.klass
;
5290 } else if (t
== MONO_TYPE_OBJECT
|| t
== MONO_TYPE_CLASS
|| t
== MONO_TYPE_STRING
||
5291 t
== MONO_TYPE_CLASS
) {
5292 klass
= mono_defaults
.object_class
;
5293 } else if (t
== MONO_TYPE_PTR
|| t
== MONO_TYPE_FNPTR
) {
5294 klass
= mono_defaults
.int_class
;
5295 } else if (t
== MONO_TYPE_GENERICINST
) {
5296 if (mono_type_generic_inst_is_valuetype (type
))
5297 klass
= mono_class_from_mono_type (type
);
5299 klass
= mono_defaults
.object_class
;
5301 klass
= mono_class_from_mono_type (type
);
5304 klass
= mono_defaults
.int_class
;
5307 cache
= klass
->image
->ldflda_wrapper_cache
;
5308 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
5311 /* we add the %p pointer value of klass because class names are not unique */
5312 name
= g_strdup_printf ("__ldflda_wrapper_%p_%s.%s", klass
, klass
->name_space
, klass
->name
);
5313 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_LDFLDA
);
5316 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 4);
5317 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
5318 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
5319 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
5320 sig
->params
[3] = &mono_defaults
.int_class
->byval_arg
;
5321 sig
->ret
= &mono_defaults
.int_class
->byval_arg
;
5323 mono_mb_emit_ldarg (mb
, 0);
5324 pos0
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
5326 /* FIXME: Only throw this if the object is in another appdomain */
5327 mono_mb_emit_exception_full (mb
, "System", "InvalidOperationException", "Attempt to load field address from object in another appdomain.");
5329 mono_mb_patch_branch (mb
, pos0
);
5331 mono_mb_emit_ldarg (mb
, 0);
5332 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
5333 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
5334 mono_mb_emit_ldarg (mb
, 3);
5335 mono_mb_emit_byte (mb
, CEE_ADD
);
5337 mono_mb_emit_byte (mb
, CEE_RET
);
5339 res
= mono_mb_create_and_cache (cache
, klass
,
5340 mb
, sig
, sig
->param_count
+ 16);
5347 * mono_marshal_get_stfld_remote_wrapper:
5348 * klass: The type of the field
5350 * This function generates a wrapper for calling mono_store_remote_field_new
5351 * with the appropriate signature.
5354 mono_marshal_get_stfld_remote_wrapper (MonoClass
*klass
)
5356 MonoMethodSignature
*sig
, *csig
;
5357 MonoMethodBuilder
*mb
;
5362 cache
= klass
->image
->stfld_remote_wrapper_cache
;
5363 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
5366 name
= g_strdup_printf ("__mono_store_remote_field_new_wrapper_%s.%s", klass
->name_space
, klass
->name
);
5367 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_STFLD_REMOTE
);
5370 mb
->method
->save_lmf
= 1;
5372 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 4);
5373 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
5374 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
5375 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
5376 sig
->params
[3] = &klass
->byval_arg
;
5377 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
5379 mono_mb_emit_ldarg (mb
, 0);
5380 mono_mb_emit_ldarg (mb
, 1);
5381 mono_mb_emit_ldarg (mb
, 2);
5382 mono_mb_emit_ldarg (mb
, 3);
5384 if (klass
->valuetype
)
5385 mono_mb_emit_op (mb
, CEE_BOX
, klass
);
5387 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 4);
5388 csig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
5389 csig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
5390 csig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
5391 csig
->params
[3] = &klass
->byval_arg
;
5392 csig
->ret
= &mono_defaults
.void_class
->byval_arg
;
5395 mono_mb_emit_native_call (mb
, csig
, mono_store_remote_field_new
);
5396 emit_thread_interrupt_checkpoint (mb
);
5398 mono_mb_emit_byte (mb
, CEE_RET
);
5400 res
= mono_mb_create_and_cache (cache
, klass
,
5401 mb
, sig
, sig
->param_count
+ 16);
5408 * mono_marshal_get_stfld_wrapper:
5409 * @type: the type of the field
5411 * This method generates a function which can be use to store a field with type
5412 * @type. The generated function has the following signature:
5413 * void stfld_wrapper (MonoObject *this, MonoClass *class, MonoClassField *field, int offset, <@type> val)
5416 mono_marshal_get_stfld_wrapper (MonoType
*type
)
5418 MonoMethodSignature
*sig
;
5419 MonoMethodBuilder
*mb
;
5426 type
= mono_type_get_underlying_type (type
);
5430 if (type
->type
== MONO_TYPE_SZARRAY
) {
5431 klass
= mono_defaults
.array_class
;
5432 } else if (type
->type
== MONO_TYPE_VALUETYPE
) {
5433 klass
= type
->data
.klass
;
5434 } else if (t
== MONO_TYPE_OBJECT
|| t
== MONO_TYPE_CLASS
|| t
== MONO_TYPE_STRING
) {
5435 klass
= mono_defaults
.object_class
;
5436 } else if (t
== MONO_TYPE_PTR
|| t
== MONO_TYPE_FNPTR
) {
5437 klass
= mono_defaults
.int_class
;
5438 } else if (t
== MONO_TYPE_GENERICINST
) {
5439 if (mono_type_generic_inst_is_valuetype (type
))
5440 klass
= mono_class_from_mono_type (type
);
5442 klass
= mono_defaults
.object_class
;
5444 klass
= mono_class_from_mono_type (type
);
5447 klass
= mono_defaults
.int_class
;
5450 cache
= klass
->image
->stfld_wrapper_cache
;
5451 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
5454 /* we add the %p pointer value of klass because class names are not unique */
5455 name
= g_strdup_printf ("__stfld_wrapper_%p_%s.%s", klass
, klass
->name_space
, klass
->name
);
5456 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_STFLD
);
5459 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 5);
5460 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
5461 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
5462 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
5463 sig
->params
[3] = &mono_defaults
.int_class
->byval_arg
;
5464 sig
->params
[4] = &klass
->byval_arg
;
5465 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
5467 mono_mb_emit_ldarg (mb
, 0);
5468 pos
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
5470 mono_mb_emit_ldarg (mb
, 0);
5471 mono_mb_emit_ldarg (mb
, 1);
5472 mono_mb_emit_ldarg (mb
, 2);
5473 mono_mb_emit_ldarg (mb
, 4);
5475 mono_mb_emit_managed_call (mb
, mono_marshal_get_stfld_remote_wrapper (klass
), NULL
);
5477 mono_mb_emit_byte (mb
, CEE_RET
);
5479 mono_mb_patch_branch (mb
, pos
);
5481 mono_mb_emit_ldarg (mb
, 0);
5482 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
5483 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
5484 mono_mb_emit_ldarg (mb
, 3);
5485 mono_mb_emit_byte (mb
, CEE_ADD
);
5486 mono_mb_emit_ldarg (mb
, 4);
5491 case MONO_TYPE_BOOLEAN
:
5492 case MONO_TYPE_CHAR
:
5501 case MONO_TYPE_ARRAY
:
5502 case MONO_TYPE_SZARRAY
:
5503 case MONO_TYPE_OBJECT
:
5504 case MONO_TYPE_CLASS
:
5505 case MONO_TYPE_STRING
:
5509 case MONO_TYPE_FNPTR
:
5510 mono_mb_emit_byte (mb
, mono_type_to_stind (type
));
5512 case MONO_TYPE_VALUETYPE
:
5513 g_assert (!klass
->enumtype
);
5514 mono_mb_emit_op (mb
, CEE_STOBJ
, klass
);
5516 case MONO_TYPE_GENERICINST
:
5517 mono_mb_emit_op (mb
, CEE_STOBJ
, klass
);
5520 g_warning ("type %x not implemented", type
->type
);
5521 g_assert_not_reached ();
5524 mono_mb_emit_byte (mb
, CEE_RET
);
5526 res
= mono_mb_create_and_cache (cache
, klass
,
5527 mb
, sig
, sig
->param_count
+ 16);
5534 * generates IL code for the icall wrapper (the generated method
5535 * calls the unmanaged code in func)
5538 mono_marshal_get_icall_wrapper (MonoMethodSignature
*sig
, const char *name
, gconstpointer func
)
5540 MonoMethodSignature
*csig
;
5541 MonoMethodBuilder
*mb
;
5545 g_assert (sig
->pinvoke
);
5547 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_MANAGED_TO_NATIVE
);
5549 mb
->method
->save_lmf
= 1;
5551 /* we copy the signature, so that we can modify it */
5554 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
5556 for (i
= 0; i
< sig
->param_count
; i
++)
5557 mono_mb_emit_ldarg (mb
, i
+ sig
->hasthis
);
5559 mono_mb_emit_native_call (mb
, sig
, (gpointer
) func
);
5560 emit_thread_interrupt_checkpoint (mb
);
5561 mono_mb_emit_byte (mb
, CEE_RET
);
5563 csig
= signature_dup (mono_defaults
.corlib
, sig
);
5565 if (csig
->call_convention
== MONO_CALL_VARARG
)
5566 csig
->call_convention
= 0;
5568 res
= mono_mb_create_method (mb
, csig
, csig
->param_count
+ 16);
5575 MonoMethodBuilder
*mb
;
5576 MonoMethodSignature
*sig
;
5577 MonoMethodPInvoke
*piinfo
;
5578 int *orig_conv_args
; /* Locals containing the original values of byref args */
5580 MonoClass
*retobj_class
;
5581 MonoMethodSignature
*csig
; /* Might need to be changed due to MarshalAs directives */
5582 MonoImage
*image
; /* The image to use for looking up custom marshallers */
5583 } EmitMarshalContext
;
5587 * This is invoked to convert arguments from the current types to
5588 * the underlying types expected by the platform routine. If required,
5589 * the methods create a temporary variable with the proper type, and return
5590 * the location for it (either the passed argument, or the newly allocated
5593 MARSHAL_ACTION_CONV_IN
,
5596 * This operation is called to push the actual value that was optionally
5597 * converted on the first stage
5599 MARSHAL_ACTION_PUSH
,
5602 * Convert byref arguments back or free resources allocated during the
5605 MARSHAL_ACTION_CONV_OUT
,
5608 * The result from the unmanaged call is at the top of the stack when
5609 * this action is invoked. The result should be stored in the
5610 * third local variable slot.
5612 MARSHAL_ACTION_CONV_RESULT
,
5614 MARSHAL_ACTION_MANAGED_CONV_IN
,
5615 MARSHAL_ACTION_MANAGED_CONV_OUT
,
5616 MARSHAL_ACTION_MANAGED_CONV_RESULT
5620 emit_marshal_custom (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
5621 MonoMarshalSpec
*spec
,
5622 int conv_arg
, MonoType
**conv_arg_type
,
5623 MarshalAction action
)
5627 static MonoClass
*ICustomMarshaler
= NULL
;
5628 static MonoMethod
*cleanup_native
, *cleanup_managed
;
5629 static MonoMethod
*marshal_managed_to_native
, *marshal_native_to_managed
;
5630 MonoMethod
*get_instance
;
5631 MonoMethodBuilder
*mb
= m
->mb
;
5632 char *exception_msg
= NULL
;
5636 if (!ICustomMarshaler
) {
5637 ICustomMarshaler
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.InteropServices", "ICustomMarshaler");
5638 g_assert (ICustomMarshaler
);
5640 cleanup_native
= mono_class_get_method_from_name (ICustomMarshaler
, "CleanUpNativeData", 1);
5641 g_assert (cleanup_native
);
5642 cleanup_managed
= mono_class_get_method_from_name (ICustomMarshaler
, "CleanUpManagedData", 1);
5643 g_assert (cleanup_managed
);
5644 marshal_managed_to_native
= mono_class_get_method_from_name (ICustomMarshaler
, "MarshalManagedToNative", 1);
5645 g_assert (marshal_managed_to_native
);
5646 marshal_native_to_managed
= mono_class_get_method_from_name (ICustomMarshaler
, "MarshalNativeToManaged", 1);
5647 g_assert (marshal_native_to_managed
);
5650 mtype
= mono_reflection_type_from_name (spec
->data
.custom_data
.custom_name
, m
->image
);
5651 g_assert (mtype
!= NULL
);
5652 mklass
= mono_class_from_mono_type (mtype
);
5653 g_assert (mklass
!= NULL
);
5655 if (!mono_class_is_assignable_from (ICustomMarshaler
, mklass
))
5656 exception_msg
= g_strdup_printf ("Custom marshaler '%s' does not implement the ICustomMarshaler interface.", mklass
->name
);
5658 get_instance
= mono_class_get_method_from_name_flags (mklass
, "GetInstance", 1, METHOD_ATTRIBUTE_STATIC
);
5660 MonoMethodSignature
*get_sig
= mono_method_signature (get_instance
);
5661 if ((get_sig
->ret
->type
!= MONO_TYPE_CLASS
) ||
5662 (mono_class_from_mono_type (get_sig
->ret
) != ICustomMarshaler
) ||
5663 (get_sig
->params
[0]->type
!= MONO_TYPE_STRING
))
5664 get_instance
= NULL
;
5668 exception_msg
= g_strdup_printf ("Custom marshaler '%s' does not implement a static GetInstance method that takes a single string parameter and returns an ICustomMarshaler.", mklass
->name
);
5670 /* Throw exception and emit compensation code if neccesary */
5671 if (exception_msg
) {
5673 case MARSHAL_ACTION_CONV_IN
:
5674 case MARSHAL_ACTION_CONV_RESULT
:
5675 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
5676 if ((action
== MARSHAL_ACTION_CONV_RESULT
) || (action
== MARSHAL_ACTION_MANAGED_CONV_RESULT
))
5677 mono_mb_emit_byte (mb
, CEE_POP
);
5679 mono_mb_emit_exception_full (mb
, "System", "ApplicationException", exception_msg
);
5680 g_free (exception_msg
);
5683 case MARSHAL_ACTION_PUSH
:
5684 mono_mb_emit_byte (mb
, CEE_LDNULL
);
5692 /* FIXME: MS.NET seems to create one instance for each klass + cookie pair */
5693 /* FIXME: MS.NET throws an exception if GetInstance returns null */
5696 case MARSHAL_ACTION_CONV_IN
:
5698 case MONO_TYPE_CLASS
:
5699 case MONO_TYPE_OBJECT
:
5700 case MONO_TYPE_STRING
:
5701 case MONO_TYPE_ARRAY
:
5702 case MONO_TYPE_SZARRAY
:
5703 case MONO_TYPE_VALUETYPE
:
5707 g_warning ("custom marshalling of type %x is currently not supported", t
->type
);
5708 g_assert_not_reached ();
5712 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
5714 mono_mb_emit_byte (mb
, CEE_LDNULL
);
5715 mono_mb_emit_stloc (mb
, conv_arg
);
5717 if (t
->byref
&& (t
->attrs
& PARAM_ATTRIBUTE_OUT
))
5720 /* Minic MS.NET behavior */
5721 if (!t
->byref
&& (t
->attrs
& PARAM_ATTRIBUTE_OUT
) && !(t
->attrs
& PARAM_ATTRIBUTE_IN
))
5724 /* Check for null */
5725 mono_mb_emit_ldarg (mb
, argnum
);
5727 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
5728 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5730 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5732 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5734 mono_mb_emit_ldarg (mb
, argnum
);
5736 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
5738 if (t
->type
== MONO_TYPE_VALUETYPE
) {
5740 * Since we can't determine the type of the argument, we
5741 * will assume the unmanaged function takes a pointer.
5743 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
5745 mono_mb_emit_op (mb
, CEE_BOX
, mono_class_from_mono_type (t
));
5748 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_managed_to_native
);
5749 mono_mb_emit_stloc (mb
, conv_arg
);
5751 mono_mb_patch_branch (mb
, pos2
);
5754 case MARSHAL_ACTION_CONV_OUT
:
5755 /* Check for null */
5756 mono_mb_emit_ldloc (mb
, conv_arg
);
5757 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5760 mono_mb_emit_ldarg (mb
, argnum
);
5762 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5764 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5766 mono_mb_emit_ldloc (mb
, conv_arg
);
5767 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_native_to_managed
);
5768 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
5769 } else if (t
->attrs
&PARAM_ATTRIBUTE_OUT
) {
5770 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5772 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5774 mono_mb_emit_ldloc (mb
, conv_arg
);
5775 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_native_to_managed
);
5777 /* We have nowhere to store the result */
5778 mono_mb_emit_byte (mb
, CEE_POP
);
5781 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5783 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5785 mono_mb_emit_ldloc (mb
, conv_arg
);
5787 mono_mb_emit_op (mb
, CEE_CALLVIRT
, cleanup_native
);
5789 mono_mb_patch_branch (mb
, pos2
);
5792 case MARSHAL_ACTION_PUSH
:
5794 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
5796 mono_mb_emit_ldloc (mb
, conv_arg
);
5799 case MARSHAL_ACTION_CONV_RESULT
:
5800 loc1
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
5802 mono_mb_emit_stloc (mb
, 3);
5804 mono_mb_emit_ldloc (mb
, 3);
5805 mono_mb_emit_stloc (mb
, loc1
);
5807 /* Check for null */
5808 mono_mb_emit_ldloc (mb
, 3);
5809 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5811 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5813 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5814 mono_mb_emit_byte (mb
, CEE_DUP
);
5816 mono_mb_emit_ldloc (mb
, 3);
5817 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_native_to_managed
);
5818 mono_mb_emit_stloc (mb
, 3);
5820 mono_mb_emit_ldloc (mb
, loc1
);
5821 mono_mb_emit_op (mb
, CEE_CALLVIRT
, cleanup_native
);
5823 mono_mb_patch_branch (mb
, pos2
);
5826 case MARSHAL_ACTION_MANAGED_CONV_IN
:
5827 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
5829 mono_mb_emit_byte (mb
, CEE_LDNULL
);
5830 mono_mb_emit_stloc (mb
, conv_arg
);
5832 /* Check for null */
5833 mono_mb_emit_ldarg (mb
, argnum
);
5835 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
5836 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5838 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5839 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5841 mono_mb_emit_ldarg (mb
, argnum
);
5843 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
5845 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_native_to_managed
);
5846 mono_mb_emit_stloc (mb
, conv_arg
);
5848 mono_mb_patch_branch (mb
, pos2
);
5851 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
5852 g_assert (!t
->byref
);
5854 loc1
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
5856 mono_mb_emit_stloc (mb
, 3);
5858 mono_mb_emit_ldloc (mb
, 3);
5859 mono_mb_emit_stloc (mb
, loc1
);
5861 /* Check for null */
5862 mono_mb_emit_ldloc (mb
, 3);
5863 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5865 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5866 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5867 mono_mb_emit_byte (mb
, CEE_DUP
);
5869 mono_mb_emit_ldloc (mb
, 3);
5870 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_managed_to_native
);
5871 mono_mb_emit_stloc (mb
, 3);
5873 mono_mb_emit_ldloc (mb
, loc1
);
5874 mono_mb_emit_op (mb
, CEE_CALLVIRT
, cleanup_managed
);
5876 mono_mb_patch_branch (mb
, pos2
);
5879 case MARSHAL_ACTION_MANAGED_CONV_OUT
:
5881 /* Check for null */
5882 mono_mb_emit_ldloc (mb
, conv_arg
);
5883 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5886 mono_mb_emit_ldarg (mb
, argnum
);
5888 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5890 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5892 mono_mb_emit_ldloc (mb
, conv_arg
);
5893 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_managed_to_native
);
5894 mono_mb_emit_byte (mb
, CEE_STIND_I
);
5897 /* Call CleanUpManagedData */
5898 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5900 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5902 mono_mb_emit_ldloc (mb
, conv_arg
);
5903 mono_mb_emit_op (mb
, CEE_CALLVIRT
, cleanup_managed
);
5905 mono_mb_patch_branch (mb
, pos2
);
5909 g_assert_not_reached ();
5916 emit_marshal_asany (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
5917 MonoMarshalSpec
*spec
,
5918 int conv_arg
, MonoType
**conv_arg_type
,
5919 MarshalAction action
)
5921 MonoMethodBuilder
*mb
= m
->mb
;
5924 case MARSHAL_ACTION_CONV_IN
: {
5925 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (m
->piinfo
, NULL
);
5927 g_assert (t
->type
== MONO_TYPE_OBJECT
);
5928 g_assert (!t
->byref
);
5930 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
5931 mono_mb_emit_ldarg (mb
, argnum
);
5932 mono_mb_emit_icon (mb
, encoding
);
5933 mono_mb_emit_icon (mb
, t
->attrs
);
5934 mono_mb_emit_icall (mb
, mono_marshal_asany
);
5935 mono_mb_emit_stloc (mb
, conv_arg
);
5939 case MARSHAL_ACTION_PUSH
:
5940 mono_mb_emit_ldloc (mb
, conv_arg
);
5943 case MARSHAL_ACTION_CONV_OUT
: {
5944 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (m
->piinfo
, NULL
);
5946 mono_mb_emit_ldarg (mb
, argnum
);
5947 mono_mb_emit_ldloc (mb
, conv_arg
);
5948 mono_mb_emit_icon (mb
, encoding
);
5949 mono_mb_emit_icon (mb
, t
->attrs
);
5950 mono_mb_emit_icall (mb
, mono_marshal_free_asany
);
5955 g_assert_not_reached ();
5962 emit_marshal_vtype (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
5963 MonoMarshalSpec
*spec
,
5964 int conv_arg
, MonoType
**conv_arg_type
,
5965 MarshalAction action
)
5967 MonoMethodBuilder
*mb
= m
->mb
;
5971 klass
= t
->data
.klass
;
5974 case MARSHAL_ACTION_CONV_IN
:
5975 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
5976 klass
->blittable
|| klass
->enumtype
)
5979 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
5981 /* store the address of the source into local variable 0 */
5983 mono_mb_emit_ldarg (mb
, argnum
);
5985 mono_mb_emit_ldarg_addr (mb
, argnum
);
5987 mono_mb_emit_stloc (mb
, 0);
5989 /* allocate space for the native struct and
5990 * store the address into local variable 1 (dest) */
5991 mono_mb_emit_icon (mb
, mono_class_native_size (klass
, NULL
));
5992 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
5993 mono_mb_emit_byte (mb
, CEE_LOCALLOC
);
5994 mono_mb_emit_stloc (mb
, conv_arg
);
5997 mono_mb_emit_ldloc (mb
, 0);
5998 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6001 if (!(t
->byref
&& !(t
->attrs
& PARAM_ATTRIBUTE_IN
) && (t
->attrs
& PARAM_ATTRIBUTE_OUT
))) {
6003 mono_mb_emit_ldloc (mb
, conv_arg
);
6004 mono_mb_emit_stloc (mb
, 1);
6006 /* emit valuetype conversion code */
6007 emit_struct_conv (mb
, klass
, FALSE
);
6011 mono_mb_patch_branch (mb
, pos
);
6014 case MARSHAL_ACTION_PUSH
:
6015 if (spec
&& spec
->native
== MONO_NATIVE_LPSTRUCT
) {
6017 g_assert (!t
->byref
);
6019 /* Have to change the signature since the vtype is passed byref */
6020 m
->csig
->params
[argnum
- m
->csig
->hasthis
] = &mono_defaults
.int_class
->byval_arg
;
6022 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
6023 klass
->blittable
|| klass
->enumtype
)
6024 mono_mb_emit_ldarg_addr (mb
, argnum
);
6026 mono_mb_emit_ldloc (mb
, conv_arg
);
6030 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
6031 klass
->blittable
|| klass
->enumtype
) {
6032 mono_mb_emit_ldarg (mb
, argnum
);
6035 mono_mb_emit_ldloc (mb
, conv_arg
);
6037 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6038 mono_mb_emit_op (mb
, CEE_MONO_LDNATIVEOBJ
, klass
);
6042 case MARSHAL_ACTION_CONV_OUT
:
6043 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
6044 klass
->blittable
|| klass
->enumtype
)
6048 /* dst = argument */
6049 mono_mb_emit_ldarg (mb
, argnum
);
6050 mono_mb_emit_stloc (mb
, 1);
6052 mono_mb_emit_ldloc (mb
, 1);
6053 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6055 if (!((t
->attrs
& PARAM_ATTRIBUTE_IN
) && !(t
->attrs
& PARAM_ATTRIBUTE_OUT
))) {
6056 /* src = tmp_locals [i] */
6057 mono_mb_emit_ldloc (mb
, conv_arg
);
6058 mono_mb_emit_stloc (mb
, 0);
6060 /* emit valuetype conversion code */
6061 emit_struct_conv (mb
, klass
, TRUE
);
6065 emit_struct_free (mb
, klass
, conv_arg
);
6068 mono_mb_patch_branch (mb
, pos
);
6071 case MARSHAL_ACTION_CONV_RESULT
:
6072 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
6074 mono_mb_emit_stloc (mb
, 3);
6077 /* load pointer to returned value type */
6078 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6079 mono_mb_emit_byte (mb
, CEE_MONO_VTADDR
);
6080 /* store the address of the source into local variable 0 */
6081 mono_mb_emit_stloc (mb
, 0);
6083 mono_mb_emit_ldloc_addr (mb
, 3);
6084 mono_mb_emit_stloc (mb
, 1);
6086 /* emit valuetype conversion code */
6087 emit_struct_conv (mb
, klass
, TRUE
);
6090 case MARSHAL_ACTION_MANAGED_CONV_IN
:
6091 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
6092 klass
->blittable
|| klass
->enumtype
) {
6097 conv_arg
= mono_mb_add_local (mb
, &klass
->byval_arg
);
6099 if (t
->attrs
& PARAM_ATTRIBUTE_OUT
)
6103 mono_mb_emit_ldarg (mb
, argnum
);
6105 mono_mb_emit_ldarg_addr (mb
, argnum
);
6106 mono_mb_emit_stloc (mb
, 0);
6109 mono_mb_emit_ldloc (mb
, 0);
6110 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6113 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
6114 mono_mb_emit_stloc (mb
, 1);
6116 /* emit valuetype conversion code */
6117 emit_struct_conv (mb
, klass
, TRUE
);
6120 mono_mb_patch_branch (mb
, pos
);
6123 case MARSHAL_ACTION_MANAGED_CONV_OUT
:
6124 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
6125 klass
->blittable
|| klass
->enumtype
) {
6129 /* Check for null */
6130 mono_mb_emit_ldarg (mb
, argnum
);
6131 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6134 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
6135 mono_mb_emit_stloc (mb
, 0);
6138 mono_mb_emit_ldarg (mb
, argnum
);
6139 mono_mb_emit_stloc (mb
, 1);
6141 /* emit valuetype conversion code */
6142 emit_struct_conv (mb
, klass
, FALSE
);
6144 mono_mb_patch_branch (mb
, pos2
);
6147 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
6148 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
6149 klass
->blittable
|| klass
->enumtype
) {
6150 mono_mb_emit_stloc (mb
, 3);
6155 /* load pointer to returned value type */
6156 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6157 mono_mb_emit_byte (mb
, CEE_MONO_VTADDR
);
6159 /* store the address of the source into local variable 0 */
6160 mono_mb_emit_stloc (mb
, 0);
6161 /* allocate space for the native struct and
6162 * store the address into dst_ptr */
6163 m
->retobj_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6164 m
->retobj_class
= klass
;
6165 g_assert (m
->retobj_var
);
6166 mono_mb_emit_icon (mb
, mono_class_native_size (klass
, NULL
));
6167 mono_mb_emit_byte (mb
, CEE_CONV_I
);
6168 mono_mb_emit_icall (mb
, mono_marshal_alloc
);
6169 mono_mb_emit_stloc (mb
, 1);
6170 mono_mb_emit_ldloc (mb
, 1);
6171 mono_mb_emit_stloc (mb
, m
->retobj_var
);
6173 /* emit valuetype conversion code */
6174 emit_struct_conv (mb
, klass
, FALSE
);
6178 g_assert_not_reached ();
6185 emit_marshal_string (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
6186 MonoMarshalSpec
*spec
,
6187 int conv_arg
, MonoType
**conv_arg_type
,
6188 MarshalAction action
)
6190 MonoMethodBuilder
*mb
= m
->mb
;
6191 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (m
->piinfo
, spec
);
6192 MonoMarshalConv conv
= mono_marshal_get_string_to_ptr_conv (m
->piinfo
, spec
);
6196 case MARSHAL_ACTION_CONV_IN
:
6197 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
6198 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6201 if (t
->attrs
& PARAM_ATTRIBUTE_OUT
)
6204 mono_mb_emit_ldarg (mb
, argnum
);
6205 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6207 mono_mb_emit_ldarg (mb
, argnum
);
6211 char *msg
= g_strdup_printf ("string marshalling conversion %d not implemented", encoding
);
6212 MonoException
*exc
= mono_get_exception_not_implemented (msg
);
6215 mono_raise_exception (exc
);
6218 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
6220 mono_mb_emit_stloc (mb
, conv_arg
);
6223 case MARSHAL_ACTION_CONV_OUT
:
6224 if (t
->byref
&& (t
->attrs
& PARAM_ATTRIBUTE_OUT
)) {
6225 mono_mb_emit_ldarg (mb
, argnum
);
6226 mono_mb_emit_ldloc (mb
, conv_arg
);
6227 if (conv
== MONO_MARSHAL_CONV_STR_BSTR
) {
6228 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_BSTR_STR
));
6229 // BSTRs always need freed
6230 mono_mb_emit_ldloc (mb
, conv_arg
);
6231 mono_mb_emit_icall (mb
, mono_free_bstr
);
6234 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_LPSTR_STR
));
6235 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
6237 if (mono_marshal_need_free (t
, m
->piinfo
, spec
)) {
6238 mono_mb_emit_ldloc (mb
, conv_arg
);
6239 if (conv
== MONO_MARSHAL_CONV_STR_BSTR
)
6240 mono_mb_emit_icall (mb
, mono_free_bstr
);
6242 mono_mb_emit_icall (mb
, mono_marshal_free
);
6247 case MARSHAL_ACTION_PUSH
:
6249 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
6251 mono_mb_emit_ldloc (mb
, conv_arg
);
6254 case MARSHAL_ACTION_CONV_RESULT
:
6255 mono_mb_emit_stloc (mb
, 0);
6257 conv
= mono_marshal_get_ptr_to_string_conv (m
->piinfo
, spec
, &need_free
);
6259 char *msg
= g_strdup_printf ("string marshalling conversion %d not implemented", encoding
);
6260 mono_mb_emit_exception_marshal_directive (mb
, msg
);
6264 mono_mb_emit_ldloc (mb
, 0);
6265 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
6266 mono_mb_emit_stloc (mb
, 3);
6268 /* free the string */
6269 mono_mb_emit_ldloc (mb
, 0);
6270 if (conv
== MONO_MARSHAL_CONV_BSTR_STR
)
6271 mono_mb_emit_icall (mb
, mono_free_bstr
);
6273 mono_mb_emit_icall (mb
, g_free
);
6276 case MARSHAL_ACTION_MANAGED_CONV_IN
:
6282 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
6283 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
6285 conv
= mono_marshal_get_ptr_to_string_conv (m
->piinfo
, spec
, &need_free
);
6287 char *msg
= g_strdup_printf ("string marshalling conversion %d not implemented", encoding
);
6288 mono_mb_emit_exception_marshal_directive (mb
, msg
);
6292 mono_mb_emit_ldarg (mb
, argnum
);
6293 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
6294 mono_mb_emit_stloc (mb
, conv_arg
);
6297 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
6298 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_STR_LPSTR
));
6299 mono_mb_emit_stloc (mb
, 3);
6303 g_assert_not_reached ();
6310 emit_marshal_safehandle (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
6311 MonoMarshalSpec
*spec
, int conv_arg
,
6312 MonoType
**conv_arg_type
, MarshalAction action
)
6314 MonoMethodBuilder
*mb
= m
->mb
;
6317 case MARSHAL_ACTION_CONV_IN
: {
6318 MonoType
*intptr_type
;
6319 int dar_release_slot
, pos
;
6321 intptr_type
= &mono_defaults
.int_class
->byval_arg
;
6322 conv_arg
= mono_mb_add_local (mb
, intptr_type
);
6323 *conv_arg_type
= intptr_type
;
6325 if (!sh_dangerous_add_ref
)
6326 init_safe_handle ();
6328 mono_mb_emit_ldarg (mb
, argnum
);
6329 pos
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
6330 mono_mb_emit_exception (mb
, "ArgumentNullException", NULL
);
6332 mono_mb_patch_branch (mb
, pos
);
6335 * My tests in show that ref SafeHandles are not really
6336 * passed as ref objects. Instead a NULL is passed as the
6339 mono_mb_emit_icon (mb
, 0);
6340 mono_mb_emit_stloc (mb
, conv_arg
);
6344 /* Create local to hold the ref parameter to DangerousAddRef */
6345 dar_release_slot
= mono_mb_add_local (mb
, &mono_defaults
.boolean_class
->byval_arg
);
6347 /* set release = false; */
6348 mono_mb_emit_icon (mb
, 0);
6349 mono_mb_emit_stloc (mb
, dar_release_slot
);
6351 /* safehandle.DangerousAddRef (ref release) */
6352 mono_mb_emit_ldarg (mb
, argnum
);
6353 mono_mb_emit_ldloc_addr (mb
, dar_release_slot
);
6354 mono_mb_emit_managed_call (mb
, sh_dangerous_add_ref
, NULL
);
6356 /* Pull the handle field from SafeHandle */
6357 mono_mb_emit_ldarg (mb
, argnum
);
6358 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoSafeHandle
, handle
));
6359 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6360 mono_mb_emit_stloc (mb
, conv_arg
);
6365 case MARSHAL_ACTION_PUSH
:
6367 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
6369 mono_mb_emit_ldloc (mb
, conv_arg
);
6372 case MARSHAL_ACTION_CONV_OUT
: {
6373 /* The slot for the boolean is the next temporary created after conv_arg, see the CONV_IN code */
6374 int dar_release_slot
= conv_arg
+ 1;
6377 if (!sh_dangerous_release
)
6378 init_safe_handle ();
6384 * My tests indicate that ref SafeHandles parameters are not actually
6385 * passed by ref, but instead a new Handle is created regardless of
6386 * whether a change happens in the unmanaged side.
6388 * Also, the Handle is created before calling into unmanaged code,
6389 * but we do not support that mechanism (getting to the original
6390 * handle) and it makes no difference where we create this
6392 ctor
= mono_class_get_method_from_name (t
->data
.klass
, ".ctor", 0);
6394 mono_mb_emit_exception (mb
, "MissingMethodException", "paramterless constructor required");
6397 /* refval = new SafeHandleDerived ()*/
6398 mono_mb_emit_ldarg (mb
, argnum
);
6399 mono_mb_emit_op (mb
, CEE_NEWOBJ
, ctor
);
6400 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
6402 /* refval.handle = returned_handle */
6403 mono_mb_emit_ldarg (mb
, argnum
);
6404 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
6405 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoSafeHandle
, handle
));
6406 mono_mb_emit_ldloc (mb
, conv_arg
);
6407 mono_mb_emit_byte (mb
, CEE_STIND_I
);
6409 mono_mb_emit_ldloc (mb
, dar_release_slot
);
6410 label_next
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6411 mono_mb_emit_ldarg (mb
, argnum
);
6412 mono_mb_emit_managed_call (mb
, sh_dangerous_release
, NULL
);
6413 mono_mb_patch_addr (mb
, label_next
, mb
->pos
- (label_next
+ 4));
6418 case MARSHAL_ACTION_CONV_RESULT
: {
6419 MonoMethod
*ctor
= NULL
;
6420 int intptr_handle_slot
;
6422 if (t
->data
.klass
->flags
& TYPE_ATTRIBUTE_ABSTRACT
){
6423 mono_mb_emit_byte (mb
, CEE_POP
);
6424 mono_mb_emit_exception_marshal_directive (mb
, "Returned SafeHandles should not be abstract");
6428 ctor
= mono_class_get_method_from_name (t
->data
.klass
, ".ctor", 0);
6430 mono_mb_emit_byte (mb
, CEE_POP
);
6431 mono_mb_emit_exception (mb
, "MissingMethodException", "paramterless constructor required");
6434 /* Store the IntPtr results into a local */
6435 intptr_handle_slot
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6436 mono_mb_emit_stloc (mb
, intptr_handle_slot
);
6438 /* Create return value */
6439 mono_mb_emit_op (mb
, CEE_NEWOBJ
, ctor
);
6440 mono_mb_emit_stloc (mb
, 3);
6442 /* Set the return.handle to the value, am using ldflda, not sure if thats a good idea */
6443 mono_mb_emit_ldloc (mb
, 3);
6444 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoSafeHandle
, handle
));
6445 mono_mb_emit_ldloc (mb
, intptr_handle_slot
);
6446 mono_mb_emit_byte (mb
, CEE_STIND_I
);
6450 case MARSHAL_ACTION_MANAGED_CONV_IN
:
6451 fprintf (stderr
, "mono/marshal: SafeHandles missing MANAGED_CONV_IN\n");
6454 case MARSHAL_ACTION_MANAGED_CONV_OUT
:
6455 fprintf (stderr
, "mono/marshal: SafeHandles missing MANAGED_CONV_OUT\n");
6458 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
6459 fprintf (stderr
, "mono/marshal: SafeHandles missing MANAGED_CONV_RESULT\n");
6462 printf ("Unhandled case for MarshalAction: %d\n", action
);
6469 emit_marshal_handleref (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
6470 MonoMarshalSpec
*spec
, int conv_arg
,
6471 MonoType
**conv_arg_type
, MarshalAction action
)
6473 MonoMethodBuilder
*mb
= m
->mb
;
6476 case MARSHAL_ACTION_CONV_IN
: {
6477 MonoType
*intptr_type
;
6479 intptr_type
= &mono_defaults
.int_class
->byval_arg
;
6480 conv_arg
= mono_mb_add_local (mb
, intptr_type
);
6481 *conv_arg_type
= intptr_type
;
6484 mono_mb_emit_exception_marshal_directive (mb
,
6485 "HandleRefs can not be returned from unmanaged code (or passed by ref)");
6488 mono_mb_emit_ldarg_addr (mb
, argnum
);
6489 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoHandleRef
, handle
));
6490 mono_mb_emit_byte (mb
, CEE_ADD
);
6491 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6492 mono_mb_emit_stloc (mb
, conv_arg
);
6496 case MARSHAL_ACTION_PUSH
:
6497 mono_mb_emit_ldloc (mb
, conv_arg
);
6500 case MARSHAL_ACTION_CONV_OUT
: {
6501 /* no resource release required */
6505 case MARSHAL_ACTION_CONV_RESULT
: {
6506 mono_mb_emit_exception_marshal_directive (mb
,
6507 "HandleRefs can not be returned from unmanaged code (or passed by ref)");
6511 case MARSHAL_ACTION_MANAGED_CONV_IN
:
6512 fprintf (stderr
, "mono/marshal: SafeHandles missing MANAGED_CONV_IN\n");
6515 case MARSHAL_ACTION_MANAGED_CONV_OUT
:
6516 fprintf (stderr
, "mono/marshal: SafeHandles missing MANAGED_CONV_OUT\n");
6519 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
6520 fprintf (stderr
, "mono/marshal: SafeHandles missing MANAGED_CONV_RESULT\n");
6523 fprintf (stderr
, "Unhandled case for MarshalAction: %d\n", action
);
6530 emit_marshal_object (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
6531 MonoMarshalSpec
*spec
,
6532 int conv_arg
, MonoType
**conv_arg_type
,
6533 MarshalAction action
)
6535 MonoMethodBuilder
*mb
= m
->mb
;
6536 MonoClass
*klass
= t
->data
.klass
;
6539 if (mono_class_from_mono_type (t
) == mono_defaults
.object_class
) {
6540 mono_raise_exception (mono_get_exception_not_implemented ("Marshalling of type object is not implemented"));
6544 case MARSHAL_ACTION_CONV_IN
:
6545 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
6546 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6548 m
->orig_conv_args
[argnum
] = 0;
6550 if (klass
->delegate
) {
6551 g_assert (!t
->byref
);
6552 mono_mb_emit_ldarg (mb
, argnum
);
6553 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_DEL_FTN
));
6554 mono_mb_emit_stloc (mb
, conv_arg
);
6555 } else if (klass
== mono_defaults
.stringbuilder_class
) {
6556 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (m
->piinfo
, spec
);
6557 MonoMarshalConv conv
= mono_marshal_get_stringbuilder_to_ptr_conv (m
->piinfo
, spec
);
6559 g_assert (!t
->byref
);
6560 mono_mb_emit_ldarg (mb
, argnum
);
6563 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
6565 char *msg
= g_strdup_printf ("stringbuilder marshalling conversion %d not implemented", encoding
);
6566 MonoException
*exc
= mono_get_exception_not_implemented (msg
);
6569 mono_raise_exception (exc
);
6572 mono_mb_emit_stloc (mb
, conv_arg
);
6573 } else if (klass
->blittable
) {
6574 mono_mb_emit_byte (mb
, CEE_LDNULL
);
6575 mono_mb_emit_stloc (mb
, conv_arg
);
6577 mono_mb_emit_ldarg (mb
, argnum
);
6578 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6580 mono_mb_emit_ldarg (mb
, argnum
);
6581 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
6582 mono_mb_emit_stloc (mb
, conv_arg
);
6584 mono_mb_patch_branch (mb
, pos
);
6587 mono_mb_emit_byte (mb
, CEE_LDNULL
);
6588 mono_mb_emit_stloc (mb
, conv_arg
);
6591 /* we dont need any conversions for out parameters */
6592 if (t
->attrs
& PARAM_ATTRIBUTE_OUT
)
6595 mono_mb_emit_ldarg (mb
, argnum
);
6596 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6599 mono_mb_emit_ldarg (mb
, argnum
);
6600 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6601 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
6604 /* store the address of the source into local variable 0 */
6605 mono_mb_emit_stloc (mb
, 0);
6606 mono_mb_emit_ldloc (mb
, 0);
6607 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6609 /* allocate space for the native struct and store the address */
6610 mono_mb_emit_icon (mb
, mono_class_native_size (klass
, NULL
));
6611 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
6612 mono_mb_emit_byte (mb
, CEE_LOCALLOC
);
6613 mono_mb_emit_stloc (mb
, conv_arg
);
6616 /* Need to store the original buffer so we can free it later */
6617 m
->orig_conv_args
[argnum
] = mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6618 mono_mb_emit_ldloc (mb
, conv_arg
);
6619 mono_mb_emit_stloc (mb
, m
->orig_conv_args
[argnum
]);
6622 /* set the src_ptr */
6623 mono_mb_emit_ldloc (mb
, 0);
6624 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
6625 mono_mb_emit_stloc (mb
, 0);
6628 mono_mb_emit_ldloc (mb
, conv_arg
);
6629 mono_mb_emit_stloc (mb
, 1);
6631 /* emit valuetype conversion code */
6632 emit_struct_conv (mb
, klass
, FALSE
);
6634 mono_mb_patch_branch (mb
, pos
);
6638 case MARSHAL_ACTION_CONV_OUT
:
6639 if (klass
== mono_defaults
.stringbuilder_class
) {
6641 MonoMarshalNative encoding
;
6642 MonoMarshalConv conv
;
6644 encoding
= mono_marshal_get_string_encoding (m
->piinfo
, spec
);
6645 conv
= mono_marshal_get_ptr_to_stringbuilder_conv (m
->piinfo
, spec
, &need_free
);
6647 g_assert (!t
->byref
);
6648 g_assert (encoding
!= -1);
6650 mono_mb_emit_ldarg (mb
, argnum
);
6651 mono_mb_emit_ldloc (mb
, conv_arg
);
6653 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
6656 mono_mb_emit_ldloc (mb
, conv_arg
);
6657 mono_mb_emit_icall (mb
, mono_marshal_free
);
6662 if (klass
->delegate
)
6665 if (t
->byref
&& (t
->attrs
& PARAM_ATTRIBUTE_OUT
)) {
6666 /* allocate a new object */
6667 mono_mb_emit_ldarg (mb
, argnum
);
6668 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6669 mono_mb_emit_op (mb
, CEE_MONO_NEWOBJ
, t
->data
.klass
);
6670 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
6673 /* dst = *argument */
6674 mono_mb_emit_ldarg (mb
, argnum
);
6677 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6679 mono_mb_emit_stloc (mb
, 1);
6681 mono_mb_emit_ldloc (mb
, 1);
6682 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6684 if (t
->byref
|| (t
->attrs
& PARAM_ATTRIBUTE_OUT
)) {
6685 mono_mb_emit_ldloc (mb
, 1);
6686 mono_mb_emit_icon (mb
, sizeof (MonoObject
));
6687 mono_mb_emit_byte (mb
, CEE_ADD
);
6688 mono_mb_emit_stloc (mb
, 1);
6690 /* src = tmp_locals [i] */
6691 mono_mb_emit_ldloc (mb
, conv_arg
);
6692 mono_mb_emit_stloc (mb
, 0);
6694 /* emit valuetype conversion code */
6695 emit_struct_conv (mb
, t
->data
.klass
, TRUE
);
6697 /* Free the structure returned by the native code */
6698 emit_struct_free (mb
, klass
, conv_arg
);
6700 if (m
->orig_conv_args
[argnum
]) {
6702 * If the native function changed the pointer, then free
6703 * the original structure plus the new pointer.
6705 mono_mb_emit_ldloc (mb
, m
->orig_conv_args
[argnum
]);
6706 mono_mb_emit_ldloc (mb
, conv_arg
);
6707 pos2
= mono_mb_emit_branch (mb
, CEE_BEQ
);
6709 if (!(t
->attrs
& PARAM_ATTRIBUTE_OUT
)) {
6710 g_assert (m
->orig_conv_args
[argnum
]);
6712 emit_struct_free (mb
, klass
, m
->orig_conv_args
[argnum
]);
6715 mono_mb_emit_ldloc (mb
, conv_arg
);
6716 mono_mb_emit_icall (mb
, g_free
);
6718 mono_mb_patch_branch (mb
, pos2
);
6722 /* Free the original structure passed to native code */
6723 emit_struct_free (mb
, klass
, conv_arg
);
6725 mono_mb_patch_branch (mb
, pos
);
6728 case MARSHAL_ACTION_PUSH
:
6730 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
6732 mono_mb_emit_ldloc (mb
, conv_arg
);
6735 case MARSHAL_ACTION_CONV_RESULT
:
6736 if (klass
->delegate
) {
6737 g_assert (!t
->byref
);
6738 mono_mb_emit_stloc (mb
, 0);
6739 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6740 mono_mb_emit_op (mb
, CEE_MONO_CLASSCONST
, klass
);
6741 mono_mb_emit_ldloc (mb
, 0);
6742 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_FTN_DEL
));
6743 mono_mb_emit_stloc (mb
, 3);
6746 mono_mb_emit_stloc (mb
, 0);
6748 /* Make a copy since emit_conv modifies local 0 */
6749 loc
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6750 mono_mb_emit_ldloc (mb
, 0);
6751 mono_mb_emit_stloc (mb
, loc
);
6753 mono_mb_emit_byte (mb
, CEE_LDNULL
);
6754 mono_mb_emit_stloc (mb
, 3);
6756 mono_mb_emit_ldloc (mb
, 0);
6757 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6759 /* allocate result object */
6761 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6762 mono_mb_emit_op (mb
, CEE_MONO_NEWOBJ
, klass
);
6763 mono_mb_emit_stloc (mb
, 3);
6767 mono_mb_emit_ldloc (mb
, 3);
6768 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
6769 mono_mb_emit_stloc (mb
, 1);
6771 /* emit conversion code */
6772 emit_struct_conv (mb
, klass
, TRUE
);
6774 emit_struct_free (mb
, klass
, loc
);
6776 /* Free the pointer allocated by unmanaged code */
6777 mono_mb_emit_ldloc (mb
, loc
);
6778 mono_mb_emit_icall (mb
, g_free
);
6779 mono_mb_patch_branch (mb
, pos
);
6783 case MARSHAL_ACTION_MANAGED_CONV_IN
:
6784 conv_arg
= mono_mb_add_local (mb
, &klass
->byval_arg
);
6786 if (klass
->delegate
) {
6787 g_assert (!t
->byref
);
6788 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6789 mono_mb_emit_op (mb
, CEE_MONO_CLASSCONST
, klass
);
6790 mono_mb_emit_ldarg (mb
, argnum
);
6791 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_FTN_DEL
));
6792 mono_mb_emit_stloc (mb
, conv_arg
);
6796 /* The class can not have an automatic layout */
6797 if ((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_AUTO_LAYOUT
) {
6798 mono_mb_emit_auto_layout_exception (mb
, klass
);
6802 if (t
->attrs
& PARAM_ATTRIBUTE_OUT
) {
6803 mono_mb_emit_byte (mb
, CEE_LDNULL
);
6804 mono_mb_emit_stloc (mb
, conv_arg
);
6809 mono_mb_emit_ldarg (mb
, argnum
);
6813 /* Check for NULL and raise an exception */
6814 pos2
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
6816 mono_mb_emit_exception (mb
, "ArgumentNullException", NULL
);
6818 mono_mb_patch_branch (mb
, pos2
);
6819 mono_mb_emit_ldarg (mb
, argnum
);
6820 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6823 mono_mb_emit_stloc (mb
, 0);
6825 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
6826 mono_mb_emit_stloc (mb
, conv_arg
);
6828 mono_mb_emit_ldloc (mb
, 0);
6829 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6831 /* Create and set dst */
6832 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6833 mono_mb_emit_op (mb
, CEE_MONO_NEWOBJ
, klass
);
6834 mono_mb_emit_stloc (mb
, conv_arg
);
6835 mono_mb_emit_ldloc (mb
, conv_arg
);
6836 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
6837 mono_mb_emit_stloc (mb
, 1);
6839 /* emit valuetype conversion code */
6840 emit_struct_conv (mb
, klass
, TRUE
);
6842 mono_mb_patch_branch (mb
, pos
);
6845 case MARSHAL_ACTION_MANAGED_CONV_OUT
:
6847 /* Check for null */
6848 mono_mb_emit_ldloc (mb
, conv_arg
);
6849 pos
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
6850 mono_mb_emit_ldarg (mb
, argnum
);
6851 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
6852 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
6853 pos2
= mono_mb_emit_branch (mb
, CEE_BR
);
6855 mono_mb_patch_branch (mb
, pos
);
6858 mono_mb_emit_ldloc (mb
, conv_arg
);
6859 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
6860 mono_mb_emit_stloc (mb
, 0);
6862 /* Allocate and set dest */
6863 mono_mb_emit_icon (mb
, mono_class_native_size (klass
, NULL
));
6864 mono_mb_emit_byte (mb
, CEE_CONV_I
);
6865 mono_mb_emit_icall (mb
, mono_marshal_alloc
);
6866 mono_mb_emit_stloc (mb
, 1);
6868 /* Update argument pointer */
6869 mono_mb_emit_ldarg (mb
, argnum
);
6870 mono_mb_emit_ldloc (mb
, 1);
6871 mono_mb_emit_byte (mb
, CEE_STIND_I
);
6873 /* emit valuetype conversion code */
6874 emit_struct_conv (mb
, klass
, FALSE
);
6876 mono_mb_patch_branch (mb
, pos2
);
6878 /* byval [Out] marshalling */
6880 /* FIXME: Handle null */
6883 mono_mb_emit_ldloc (mb
, conv_arg
);
6884 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
6885 mono_mb_emit_stloc (mb
, 0);
6888 mono_mb_emit_ldarg (mb
, argnum
);
6889 mono_mb_emit_stloc (mb
, 1);
6891 /* emit valuetype conversion code */
6892 emit_struct_conv (mb
, klass
, FALSE
);
6896 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
6897 if (klass
->delegate
) {
6898 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_DEL_FTN
));
6899 mono_mb_emit_stloc (mb
, 3);
6903 /* The class can not have an automatic layout */
6904 if ((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_AUTO_LAYOUT
) {
6905 mono_mb_emit_auto_layout_exception (mb
, klass
);
6909 mono_mb_emit_stloc (mb
, 0);
6910 /* Check for null */
6911 mono_mb_emit_ldloc (mb
, 0);
6912 pos
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
6913 mono_mb_emit_byte (mb
, CEE_LDNULL
);
6914 mono_mb_emit_stloc (mb
, 3);
6915 pos2
= mono_mb_emit_branch (mb
, CEE_BR
);
6917 mono_mb_patch_branch (mb
, pos
);
6920 mono_mb_emit_ldloc (mb
, 0);
6921 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
6922 mono_mb_emit_stloc (mb
, 0);
6924 /* Allocate and set dest */
6925 mono_mb_emit_icon (mb
, mono_class_native_size (klass
, NULL
));
6926 mono_mb_emit_byte (mb
, CEE_CONV_I
);
6927 mono_mb_emit_icall (mb
, mono_marshal_alloc
);
6928 mono_mb_emit_byte (mb
, CEE_DUP
);
6929 mono_mb_emit_stloc (mb
, 1);
6930 mono_mb_emit_stloc (mb
, 3);
6932 emit_struct_conv (mb
, klass
, FALSE
);
6934 mono_mb_patch_branch (mb
, pos2
);
6938 g_assert_not_reached ();
6945 emit_marshal_com_interface (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
6946 MonoMarshalSpec
*spec
,
6947 int conv_arg
, MonoType
**conv_arg_type
,
6948 MarshalAction action
)
6950 MonoMethodBuilder
*mb
= m
->mb
;
6951 MonoClass
*klass
= t
->data
.klass
;
6952 static MonoMethod
* get_object_for_iunknown
= NULL
;
6953 static MonoMethod
* get_iunknown_for_object_internal
= NULL
;
6954 static MonoMethod
* get_com_interface_for_object_internal
= NULL
;
6955 static MonoMethod
* get_idispatch_for_object_internal
= NULL
;
6956 if (!get_object_for_iunknown
)
6957 get_object_for_iunknown
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "GetObjectForIUnknown", 1);
6958 if (!get_iunknown_for_object_internal
)
6959 get_iunknown_for_object_internal
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "GetIUnknownForObjectInternal", 1);
6960 if (!get_idispatch_for_object_internal
)
6961 get_idispatch_for_object_internal
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "GetIDispatchForObjectInternal", 1);
6962 if (!get_com_interface_for_object_internal
)
6963 get_com_interface_for_object_internal
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "GetComInterfaceForObjectInternal", 2);
6965 /* COM types are initialized lazily */
6966 mono_init_com_types ();
6969 case MARSHAL_ACTION_CONV_IN
: {
6970 guint32 pos_null
= 0;
6972 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
6973 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6975 mono_mb_emit_ptr (mb
, NULL
);
6976 mono_mb_emit_stloc (mb
, conv_arg
);
6978 /* we dont need any conversions for out parameters */
6979 if (t
->byref
&& t
->attrs
& PARAM_ATTRIBUTE_OUT
)
6982 mono_mb_emit_ldarg (mb
, argnum
);
6984 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
6985 /* if null just break, conv arg was already inited to 0 */
6986 pos_null
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
6988 mono_mb_emit_ldarg (mb
, argnum
);
6990 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
6992 if (klass
&& klass
!= mono_defaults
.object_class
) {
6993 mono_mb_emit_ptr (mb
, t
);
6994 mono_mb_emit_icall (mb
, type_from_handle
);
6995 mono_mb_emit_managed_call (mb
, get_com_interface_for_object_internal
, NULL
);
6997 else if (spec
->native
== MONO_NATIVE_IUNKNOWN
)
6998 mono_mb_emit_managed_call (mb
, get_iunknown_for_object_internal
, NULL
);
6999 else if (spec
->native
== MONO_NATIVE_IDISPATCH
)
7000 mono_mb_emit_managed_call (mb
, get_idispatch_for_object_internal
, NULL
);
7001 else if (!klass
&& spec
->native
== MONO_NATIVE_INTERFACE
)
7002 mono_mb_emit_managed_call (mb
, get_iunknown_for_object_internal
, NULL
);
7004 g_assert_not_reached ();
7005 mono_mb_emit_stloc (mb
, conv_arg
);
7006 mono_mb_patch_short_branch (mb
, pos_null
);
7010 case MARSHAL_ACTION_CONV_OUT
: {
7011 if (t
->byref
&& (t
->attrs
& PARAM_ATTRIBUTE_OUT
)) {
7013 guint32 pos_null
= 0, pos_ccw
= 0, pos_end
= 0;
7014 ccw_obj
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
7016 mono_mb_emit_ldarg (mb
, argnum
);
7017 mono_mb_emit_byte (mb
, CEE_LDNULL
);
7018 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
7020 mono_mb_emit_ldloc (mb
, conv_arg
);
7021 pos_null
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
7023 mono_mb_emit_ldloc (mb
, conv_arg
);
7024 mono_mb_emit_icon (mb
, TRUE
);
7025 mono_mb_emit_icall (mb
, cominterop_get_ccw_object
);
7026 mono_mb_emit_stloc (mb
, ccw_obj
);
7027 mono_mb_emit_ldloc (mb
, ccw_obj
);
7028 pos_ccw
= mono_mb_emit_short_branch (mb
, CEE_BRTRUE_S
);
7030 mono_mb_emit_ldarg (mb
, argnum
);
7031 mono_mb_emit_ldloc (mb
, conv_arg
);
7032 mono_mb_emit_managed_call (mb
, get_object_for_iunknown
, NULL
);
7034 if (klass
&& klass
!= mono_defaults
.object_class
)
7035 mono_mb_emit_op (mb
, CEE_CASTCLASS
, klass
);
7036 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
7037 pos_end
= mono_mb_emit_short_branch (mb
, CEE_BR_S
);
7039 /* is already managed object */
7040 mono_mb_patch_short_branch (mb
, pos_ccw
);
7041 mono_mb_emit_ldarg (mb
, argnum
);
7042 mono_mb_emit_ldloc (mb
, ccw_obj
);
7044 if (klass
&& klass
!= mono_defaults
.object_class
)
7045 mono_mb_emit_op (mb
, CEE_CASTCLASS
, klass
);
7046 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
7049 mono_mb_patch_short_branch (mb
, pos_end
);
7051 mono_mb_patch_short_branch (mb
, pos_null
);
7055 case MARSHAL_ACTION_PUSH
:
7057 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
7059 mono_mb_emit_ldloc (mb
, conv_arg
);
7062 case MARSHAL_ACTION_CONV_RESULT
: {
7063 char *msg
= g_strdup ("Marshalling of COM Objects is not yet implemented.");
7064 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7068 case MARSHAL_ACTION_MANAGED_CONV_IN
: {
7070 guint32 pos_null
= 0, pos_ccw
= 0, pos_end
= 0;
7071 ccw_obj
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
7073 klass
= mono_class_from_mono_type (t
);
7074 conv_arg
= mono_mb_add_local (mb
, &klass
->byval_arg
);
7075 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
7077 mono_mb_emit_byte (mb
, CEE_LDNULL
);
7078 mono_mb_emit_stloc (mb
, conv_arg
);
7079 if (t
->attrs
& PARAM_ATTRIBUTE_OUT
)
7082 mono_mb_emit_ldarg (mb
, argnum
);
7084 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
7085 pos_null
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
7087 mono_mb_emit_ldarg (mb
, argnum
);
7089 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
7090 mono_mb_emit_icon (mb
, TRUE
);
7091 mono_mb_emit_icall (mb
, cominterop_get_ccw_object
);
7092 mono_mb_emit_stloc (mb
, ccw_obj
);
7093 mono_mb_emit_ldloc (mb
, ccw_obj
);
7094 pos_ccw
= mono_mb_emit_short_branch (mb
, CEE_BRTRUE_S
);
7097 mono_mb_emit_ldarg (mb
, argnum
);
7099 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
7100 mono_mb_emit_managed_call (mb
, get_object_for_iunknown
, NULL
);
7102 if (klass
&& klass
!= mono_defaults
.object_class
)
7103 mono_mb_emit_op (mb
, CEE_CASTCLASS
, klass
);
7104 mono_mb_emit_stloc (mb
, conv_arg
);
7105 pos_end
= mono_mb_emit_short_branch (mb
, CEE_BR_S
);
7107 /* is already managed object */
7108 mono_mb_patch_short_branch (mb
, pos_ccw
);
7109 mono_mb_emit_ldloc (mb
, ccw_obj
);
7110 if (klass
&& klass
!= mono_defaults
.object_class
)
7111 mono_mb_emit_op (mb
, CEE_CASTCLASS
, klass
);
7112 mono_mb_emit_stloc (mb
, conv_arg
);
7114 mono_mb_patch_short_branch (mb
, pos_end
);
7116 mono_mb_patch_short_branch (mb
, pos_null
);
7120 case MARSHAL_ACTION_MANAGED_CONV_OUT
: {
7121 if (t
->byref
&& t
->attrs
& PARAM_ATTRIBUTE_OUT
) {
7122 static MonoMethod
* AddRef
= NULL
;
7123 guint32 pos_null
= 0;
7126 AddRef
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "AddRef", 1);
7128 mono_mb_emit_ldloc (mb
, conv_arg
);
7129 /* if null just break, conv arg was already inited to 0 */
7130 pos_null
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
7132 /* to store later */
7133 mono_mb_emit_ldarg (mb
, argnum
);
7134 mono_mb_emit_ldloc (mb
, conv_arg
);
7135 if (klass
&& klass
!= mono_defaults
.object_class
) {
7136 mono_mb_emit_ptr (mb
, t
);
7137 mono_mb_emit_icall (mb
, type_from_handle
);
7138 mono_mb_emit_managed_call (mb
, get_com_interface_for_object_internal
, NULL
);
7140 else if (spec
->native
== MONO_NATIVE_IUNKNOWN
)
7141 mono_mb_emit_managed_call (mb
, get_iunknown_for_object_internal
, NULL
);
7142 else if (spec
->native
== MONO_NATIVE_IDISPATCH
)
7143 mono_mb_emit_managed_call (mb
, get_idispatch_for_object_internal
, NULL
);
7144 else if (!klass
&& spec
->native
== MONO_NATIVE_INTERFACE
)
7145 mono_mb_emit_managed_call (mb
, get_iunknown_for_object_internal
, NULL
);
7147 g_assert_not_reached ();
7148 mono_mb_emit_byte (mb
, CEE_STIND_I
);
7150 mono_mb_emit_ldarg (mb
, argnum
);
7151 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7152 mono_mb_emit_managed_call (mb
, AddRef
, NULL
);
7153 mono_mb_emit_byte (mb
, CEE_POP
);
7155 mono_mb_patch_short_branch (mb
, pos_null
);
7160 case MARSHAL_ACTION_MANAGED_CONV_RESULT
: {
7161 char *msg
= g_strdup ("Marshalling of COM Objects is not yet implemented.");
7162 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7167 g_assert_not_reached ();
7174 emit_marshal_variant (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
7175 MonoMarshalSpec
*spec
,
7176 int conv_arg
, MonoType
**conv_arg_type
,
7177 MarshalAction action
)
7179 MonoMethodBuilder
*mb
= m
->mb
;
7180 static MonoMethod
*get_object_for_native_variant
= NULL
;
7181 static MonoMethod
*get_native_variant_for_object
= NULL
;
7183 mono_init_com_types ();
7185 if (!get_object_for_native_variant
)
7186 get_object_for_native_variant
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "GetObjectForNativeVariant", 1);
7187 g_assert (get_object_for_native_variant
);
7189 if (!get_native_variant_for_object
)
7190 get_native_variant_for_object
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "GetNativeVariantForObject", 2);
7191 g_assert (get_native_variant_for_object
);
7194 case MARSHAL_ACTION_CONV_IN
: {
7195 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.variant_class
->byval_arg
);
7198 *conv_arg_type
= &mono_defaults
.variant_class
->this_arg
;
7200 *conv_arg_type
= &mono_defaults
.variant_class
->byval_arg
;
7202 if (t
->byref
&& t
->attrs
& PARAM_ATTRIBUTE_OUT
)
7205 mono_mb_emit_ldarg (mb
, argnum
);
7207 mono_mb_emit_byte(mb
, CEE_LDIND_REF
);
7208 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
7209 mono_mb_emit_managed_call (mb
, get_native_variant_for_object
, NULL
);
7213 case MARSHAL_ACTION_CONV_OUT
: {
7214 static MonoMethod
*variant_clear
= NULL
;
7217 variant_clear
= mono_class_get_method_from_name (mono_defaults
.variant_class
, "Clear", 0);
7218 g_assert (variant_clear
);
7222 mono_mb_emit_ldarg (mb
, argnum
);
7223 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
7224 mono_mb_emit_managed_call (mb
, get_object_for_native_variant
, NULL
);
7225 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
7228 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
7229 mono_mb_emit_managed_call (mb
, variant_clear
, NULL
);
7233 case MARSHAL_ACTION_PUSH
:
7235 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
7237 mono_mb_emit_ldloc (mb
, conv_arg
);
7240 case MARSHAL_ACTION_CONV_RESULT
: {
7241 char *msg
= g_strdup ("Marshalling of VARIANT not supported as a return type.");
7242 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7246 case MARSHAL_ACTION_MANAGED_CONV_IN
: {
7247 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
7250 *conv_arg_type
= &mono_defaults
.variant_class
->this_arg
;
7252 *conv_arg_type
= &mono_defaults
.variant_class
->byval_arg
;
7254 if (t
->byref
&& t
->attrs
& PARAM_ATTRIBUTE_OUT
)
7258 mono_mb_emit_ldarg (mb
, argnum
);
7260 mono_mb_emit_ldarg_addr (mb
, argnum
);
7261 mono_mb_emit_managed_call (mb
, get_object_for_native_variant
, NULL
);
7262 mono_mb_emit_stloc (mb
, conv_arg
);
7266 case MARSHAL_ACTION_MANAGED_CONV_OUT
: {
7268 mono_mb_emit_ldloc (mb
, conv_arg
);
7269 mono_mb_emit_ldarg (mb
, argnum
);
7270 mono_mb_emit_managed_call (mb
, get_native_variant_for_object
, NULL
);
7275 case MARSHAL_ACTION_MANAGED_CONV_RESULT
: {
7276 char *msg
= g_strdup ("Marshalling of VARIANT not supported as a return type.");
7277 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7282 g_assert_not_reached ();
7289 emit_marshal_array (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
7290 MonoMarshalSpec
*spec
,
7291 int conv_arg
, MonoType
**conv_arg_type
,
7292 MarshalAction action
)
7294 MonoMethodBuilder
*mb
= m
->mb
;
7295 MonoClass
*klass
= mono_class_from_mono_type (t
);
7296 gboolean need_convert
, need_free
;
7297 MonoMarshalNative encoding
;
7299 encoding
= mono_marshal_get_string_encoding (m
->piinfo
, spec
);
7302 case MARSHAL_ACTION_CONV_IN
:
7303 *conv_arg_type
= &mono_defaults
.object_class
->byval_arg
;
7304 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
7306 if (klass
->element_class
->blittable
) {
7307 mono_mb_emit_ldarg (mb
, argnum
);
7309 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7310 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_ARRAY_LPARRAY
));
7311 mono_mb_emit_stloc (mb
, conv_arg
);
7314 guint32 label1
, label2
, label3
;
7315 int index_var
, src_var
, dest_ptr
, esize
;
7316 MonoMarshalConv conv
;
7317 gboolean is_string
= FALSE
;
7319 dest_ptr
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7321 eklass
= klass
->element_class
;
7323 if (eklass
== mono_defaults
.string_class
) {
7325 conv
= mono_marshal_get_string_to_ptr_conv (m
->piinfo
, spec
);
7327 else if (eklass
== mono_defaults
.stringbuilder_class
) {
7329 conv
= mono_marshal_get_stringbuilder_to_ptr_conv (m
->piinfo
, spec
);
7334 src_var
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
7335 mono_mb_emit_ldarg (mb
, argnum
);
7337 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7338 mono_mb_emit_stloc (mb
, src_var
);
7341 mono_mb_emit_ldloc (mb
, src_var
);
7342 mono_mb_emit_stloc (mb
, conv_arg
);
7343 mono_mb_emit_ldloc (mb
, src_var
);
7344 label1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7348 char *msg
= g_strdup_printf ("string/stringbuilder marshalling conversion %d not implemented", encoding
);
7349 MonoException
*exc
= mono_get_exception_not_implemented (msg
);
7352 mono_raise_exception (exc
);
7357 esize
= sizeof (gpointer
);
7359 esize
= mono_class_native_size (eklass
, NULL
);
7361 /* allocate space for the native struct and store the address */
7362 mono_mb_emit_icon (mb
, esize
);
7363 mono_mb_emit_ldloc (mb
, src_var
);
7364 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7366 if (eklass
== mono_defaults
.string_class
) {
7367 /* Make the array bigger for the terminating null */
7368 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
7369 mono_mb_emit_byte (mb
, CEE_ADD
);
7371 mono_mb_emit_byte (mb
, CEE_MUL
);
7372 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
7373 mono_mb_emit_byte (mb
, CEE_LOCALLOC
);
7374 mono_mb_emit_stloc (mb
, conv_arg
);
7376 mono_mb_emit_ldloc (mb
, conv_arg
);
7377 mono_mb_emit_stloc (mb
, dest_ptr
);
7379 /* Emit marshalling loop */
7380 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7381 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7382 mono_mb_emit_stloc (mb
, index_var
);
7384 mono_mb_emit_ldloc (mb
, index_var
);
7385 mono_mb_emit_ldloc (mb
, src_var
);
7386 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7387 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
7389 /* Emit marshalling code */
7392 mono_mb_emit_ldloc (mb
, dest_ptr
);
7393 mono_mb_emit_ldloc (mb
, src_var
);
7394 mono_mb_emit_ldloc (mb
, index_var
);
7395 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
7396 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
7397 mono_mb_emit_byte (mb
, CEE_STIND_I
);
7399 /* set the src_ptr */
7400 mono_mb_emit_ldloc (mb
, src_var
);
7401 mono_mb_emit_ldloc (mb
, index_var
);
7402 mono_mb_emit_op (mb
, CEE_LDELEMA
, eklass
);
7403 mono_mb_emit_stloc (mb
, 0);
7406 mono_mb_emit_ldloc (mb
, dest_ptr
);
7407 mono_mb_emit_stloc (mb
, 1);
7409 /* emit valuetype conversion code */
7410 emit_struct_conv (mb
, eklass
, FALSE
);
7413 mono_mb_emit_add_to_local (mb
, index_var
, 1);
7414 mono_mb_emit_add_to_local (mb
, dest_ptr
, esize
);
7416 mono_mb_emit_byte (mb
, CEE_BR
);
7417 mono_mb_emit_i4 (mb
, label2
- (mb
->pos
+ 4));
7419 mono_mb_patch_branch (mb
, label3
);
7421 if (eklass
== mono_defaults
.string_class
) {
7422 /* Null terminate */
7423 mono_mb_emit_ldloc (mb
, dest_ptr
);
7424 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7425 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
7428 mono_mb_patch_branch (mb
, label1
);
7433 case MARSHAL_ACTION_CONV_OUT
:
7434 /* Unicode character arrays are implicitly marshalled as [Out] under MS.NET */
7435 need_convert
= ((klass
->element_class
== mono_defaults
.char_class
) && (encoding
== MONO_NATIVE_LPWSTR
)) || (klass
->element_class
== mono_defaults
.stringbuilder_class
) || (t
->attrs
& PARAM_ATTRIBUTE_OUT
);
7436 need_free
= mono_marshal_need_free (&klass
->element_class
->byval_arg
,
7439 if (need_convert
|| need_free
) {
7440 /* FIXME: Optimize blittable case */
7442 guint32 label1
, label2
, label3
;
7443 int index_var
, src_ptr
, loc
, esize
;
7445 eklass
= klass
->element_class
;
7446 if ((eklass
== mono_defaults
.stringbuilder_class
) || (eklass
== mono_defaults
.string_class
))
7447 esize
= sizeof (gpointer
);
7449 esize
= mono_class_native_size (eklass
, NULL
);
7450 src_ptr
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7451 loc
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7454 mono_mb_emit_ldarg (mb
, argnum
);
7456 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7457 label1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7459 mono_mb_emit_ldloc (mb
, conv_arg
);
7460 mono_mb_emit_stloc (mb
, src_ptr
);
7462 /* Emit marshalling loop */
7463 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7464 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7465 mono_mb_emit_stloc (mb
, index_var
);
7467 mono_mb_emit_ldloc (mb
, index_var
);
7468 mono_mb_emit_ldarg (mb
, argnum
);
7470 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
7471 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7472 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
7474 /* Emit marshalling code */
7476 if (eklass
== mono_defaults
.stringbuilder_class
) {
7477 gboolean need_free2
;
7478 MonoMarshalConv conv
= mono_marshal_get_ptr_to_stringbuilder_conv (m
->piinfo
, spec
, &need_free2
);
7480 g_assert (conv
!= -1);
7483 mono_mb_emit_ldarg (mb
, argnum
);
7485 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7486 mono_mb_emit_ldloc (mb
, index_var
);
7487 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
7490 mono_mb_emit_ldloc (mb
, src_ptr
);
7491 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7493 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
7497 mono_mb_emit_ldloc (mb
, src_ptr
);
7498 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7500 mono_mb_emit_icall (mb
, mono_marshal_free
);
7503 else if (eklass
== mono_defaults
.string_class
) {
7506 mono_mb_emit_ldloc (mb
, src_ptr
);
7507 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7509 mono_mb_emit_icall (mb
, mono_marshal_free
);
7514 /* set the src_ptr */
7515 mono_mb_emit_ldloc (mb
, src_ptr
);
7516 mono_mb_emit_stloc (mb
, 0);
7519 mono_mb_emit_ldarg (mb
, argnum
);
7521 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7522 mono_mb_emit_ldloc (mb
, index_var
);
7523 mono_mb_emit_op (mb
, CEE_LDELEMA
, eklass
);
7524 mono_mb_emit_stloc (mb
, 1);
7526 /* emit valuetype conversion code */
7527 emit_struct_conv (mb
, eklass
, TRUE
);
7531 mono_mb_emit_ldloc (mb
, src_ptr
);
7532 mono_mb_emit_stloc (mb
, loc
);
7533 mono_mb_emit_ldloc (mb
, loc
);
7535 emit_struct_free (mb
, eklass
, loc
);
7539 mono_mb_emit_add_to_local (mb
, index_var
, 1);
7540 mono_mb_emit_add_to_local (mb
, src_ptr
, esize
);
7542 mono_mb_emit_byte (mb
, CEE_BR
);
7543 mono_mb_emit_i4 (mb
, label2
- (mb
->pos
+ 4));
7545 mono_mb_patch_branch (mb
, label1
);
7546 mono_mb_patch_branch (mb
, label3
);
7550 case MARSHAL_ACTION_PUSH
:
7552 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
7554 mono_mb_emit_ldloc (mb
, conv_arg
);
7557 case MARSHAL_ACTION_CONV_RESULT
:
7558 /* fixme: we need conversions here */
7559 mono_mb_emit_stloc (mb
, 3);
7562 case MARSHAL_ACTION_MANAGED_CONV_IN
: {
7564 guint32 label1
, label2
, label3
;
7565 int index_var
, src_ptr
, loc
, esize
, param_num
, num_elem
;
7566 MonoMarshalConv conv
;
7567 gboolean is_string
= FALSE
;
7569 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
7570 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
7573 char *msg
= g_strdup ("Byref array marshalling to managed code is not implemented.");
7574 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7578 char *msg
= g_strdup ("[MarshalAs] attribute required to marshal arrays to managed code.");
7579 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7582 if (spec
->native
!= MONO_NATIVE_LPARRAY
) {
7583 char *msg
= g_strdup ("Non LPArray marshalling of arrays to managed code is not implemented.");
7584 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7588 /* FIXME: t is from the method which is wrapped, not the delegate type */
7589 /* g_assert (t->attrs & PARAM_ATTRIBUTE_IN); */
7591 param_num
= spec
->data
.array_data
.param_num
;
7592 num_elem
= spec
->data
.array_data
.num_elem
;
7593 if (spec
->data
.array_data
.elem_mult
== 0)
7594 /* param_num is not specified */
7597 if (param_num
== -1) {
7598 if (num_elem
<= 0) {
7599 char *msg
= g_strdup ("Either SizeConst or SizeParamIndex should be specified when marshalling arrays to managed code.");
7600 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7605 /* FIXME: Optimize blittable case */
7607 eklass
= klass
->element_class
;
7608 if (eklass
== mono_defaults
.string_class
) {
7610 conv
= mono_marshal_get_ptr_to_string_conv (m
->piinfo
, spec
, &need_free
);
7612 else if (eklass
== mono_defaults
.stringbuilder_class
) {
7614 conv
= mono_marshal_get_ptr_to_stringbuilder_conv (m
->piinfo
, spec
, &need_free
);
7619 mono_marshal_load_type_info (eklass
);
7622 esize
= sizeof (gpointer
);
7624 esize
= mono_class_native_size (eklass
, NULL
);
7625 src_ptr
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7626 loc
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7628 mono_mb_emit_byte (mb
, CEE_LDNULL
);
7629 mono_mb_emit_stloc (mb
, conv_arg
);
7631 /* Check param index */
7632 if (param_num
!= -1) {
7633 if (param_num
>= m
->sig
->param_count
) {
7634 char *msg
= g_strdup ("Array size control parameter index is out of range.");
7635 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7638 switch (m
->sig
->params
[param_num
]->type
) {
7651 char *msg
= g_strdup ("Array size control parameter must be an integral type.");
7652 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7659 mono_mb_emit_ldarg (mb
, argnum
);
7660 label1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7662 mono_mb_emit_ldarg (mb
, argnum
);
7663 mono_mb_emit_stloc (mb
, src_ptr
);
7665 /* Create managed array */
7667 * The LPArray marshalling spec says that sometimes param_num starts
7668 * from 1, sometimes it starts from 0. But MS seems to allways start
7672 if (param_num
== -1)
7673 mono_mb_emit_icon (mb
, num_elem
);
7675 /* FIXME: Add the two together */
7676 mono_mb_emit_ldarg (mb
, param_num
);
7678 mono_mb_emit_icon (mb
, num_elem
);
7679 mono_mb_emit_byte (mb
, CEE_ADD
);
7683 mono_mb_emit_op (mb
, CEE_NEWARR
, eklass
);
7684 mono_mb_emit_stloc (mb
, conv_arg
);
7686 if (eklass
->blittable
) {
7687 mono_mb_emit_ldloc (mb
, conv_arg
);
7688 mono_mb_emit_byte (mb
, CEE_CONV_I
);
7689 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoArray
, vector
));
7690 mono_mb_emit_byte (mb
, CEE_ADD
);
7691 mono_mb_emit_ldarg (mb
, argnum
);
7692 mono_mb_emit_ldloc (mb
, conv_arg
);
7693 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7694 mono_mb_emit_icon (mb
, esize
);
7695 mono_mb_emit_byte (mb
, CEE_MUL
);
7696 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
7697 mono_mb_emit_byte (mb
, CEE_CPBLK
);
7701 /* Emit marshalling loop */
7702 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7703 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7704 mono_mb_emit_stloc (mb
, index_var
);
7706 mono_mb_emit_ldloc (mb
, index_var
);
7707 mono_mb_emit_ldloc (mb
, conv_arg
);
7708 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7709 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
7711 /* Emit marshalling code */
7713 g_assert (conv
!= -1);
7715 mono_mb_emit_ldloc (mb
, conv_arg
);
7716 mono_mb_emit_ldloc (mb
, index_var
);
7718 mono_mb_emit_ldloc (mb
, src_ptr
);
7719 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7721 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
7722 mono_mb_emit_byte (mb
, CEE_STELEM_REF
);
7725 char *msg
= g_strdup ("Marshalling of non-string and non-blittable arrays to managed code is not implemented.");
7726 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7730 mono_mb_emit_add_to_local (mb
, index_var
, 1);
7731 mono_mb_emit_add_to_local (mb
, src_ptr
, esize
);
7733 mono_mb_emit_byte (mb
, CEE_BR
);
7734 mono_mb_emit_i4 (mb
, label2
- (mb
->pos
+ 4));
7736 mono_mb_patch_branch (mb
, label1
);
7737 mono_mb_patch_branch (mb
, label3
);
7741 case MARSHAL_ACTION_MANAGED_CONV_OUT
: {
7743 guint32 label1
, label2
, label3
;
7744 int index_var
, dest_ptr
, loc
, esize
, param_num
, num_elem
;
7745 MonoMarshalConv conv
;
7746 gboolean is_string
= FALSE
;
7749 /* Already handled in CONV_IN */
7752 /* These are already checked in CONV_IN */
7753 g_assert (!t
->byref
);
7754 g_assert (spec
->native
== MONO_NATIVE_LPARRAY
);
7755 g_assert (t
->attrs
& PARAM_ATTRIBUTE_OUT
);
7757 param_num
= spec
->data
.array_data
.param_num
;
7758 num_elem
= spec
->data
.array_data
.num_elem
;
7760 if (spec
->data
.array_data
.elem_mult
== 0)
7761 /* param_num is not specified */
7764 if (param_num
== -1) {
7765 if (num_elem
<= 0) {
7766 g_assert_not_reached ();
7770 /* FIXME: Optimize blittable case */
7772 eklass
= klass
->element_class
;
7773 if (eklass
== mono_defaults
.string_class
) {
7775 conv
= mono_marshal_get_string_to_ptr_conv (m
->piinfo
, spec
);
7777 else if (eklass
== mono_defaults
.stringbuilder_class
) {
7779 conv
= mono_marshal_get_stringbuilder_to_ptr_conv (m
->piinfo
, spec
);
7784 mono_marshal_load_type_info (eklass
);
7787 esize
= sizeof (gpointer
);
7789 esize
= mono_class_native_size (eklass
, NULL
);
7791 dest_ptr
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7792 loc
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7795 mono_mb_emit_ldloc (mb
, conv_arg
);
7796 label1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7798 mono_mb_emit_ldarg (mb
, argnum
);
7799 mono_mb_emit_stloc (mb
, dest_ptr
);
7801 if (eklass
->blittable
) {
7803 mono_mb_emit_ldarg (mb
, argnum
);
7805 mono_mb_emit_ldloc (mb
, conv_arg
);
7806 mono_mb_emit_byte (mb
, CEE_CONV_I
);
7807 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoArray
, vector
));
7808 mono_mb_emit_byte (mb
, CEE_ADD
);
7810 mono_mb_emit_ldloc (mb
, conv_arg
);
7811 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7812 mono_mb_emit_icon (mb
, esize
);
7813 mono_mb_emit_byte (mb
, CEE_MUL
);
7814 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
7815 mono_mb_emit_byte (mb
, CEE_CPBLK
);
7819 /* Emit marshalling loop */
7820 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7821 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7822 mono_mb_emit_stloc (mb
, index_var
);
7824 mono_mb_emit_ldloc (mb
, index_var
);
7825 mono_mb_emit_ldloc (mb
, conv_arg
);
7826 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7827 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
7829 /* Emit marshalling code */
7831 g_assert (conv
!= -1);
7834 mono_mb_emit_ldloc (mb
, dest_ptr
);
7837 mono_mb_emit_ldloc (mb
, conv_arg
);
7838 mono_mb_emit_ldloc (mb
, index_var
);
7840 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
7842 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
7843 mono_mb_emit_byte (mb
, CEE_STIND_I
);
7846 char *msg
= g_strdup ("Marshalling of non-string and non-blittable arrays to managed code is not implemented.");
7847 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7851 mono_mb_emit_add_to_local (mb
, index_var
, 1);
7852 mono_mb_emit_add_to_local (mb
, dest_ptr
, esize
);
7854 mono_mb_emit_byte (mb
, CEE_BR
);
7855 mono_mb_emit_i4 (mb
, label2
- (mb
->pos
+ 4));
7857 mono_mb_patch_branch (mb
, label1
);
7858 mono_mb_patch_branch (mb
, label3
);
7862 case MARSHAL_ACTION_MANAGED_CONV_RESULT
: {
7864 guint32 label1
, label2
, label3
;
7865 int index_var
, src
, dest
, esize
;
7866 MonoMarshalConv conv
= -1;
7867 gboolean is_string
= FALSE
;
7869 g_assert (!t
->byref
);
7871 eklass
= klass
->element_class
;
7873 mono_marshal_load_type_info (eklass
);
7875 if (eklass
== mono_defaults
.string_class
) {
7877 conv
= mono_marshal_get_string_to_ptr_conv (m
->piinfo
, spec
);
7880 g_assert_not_reached ();
7884 esize
= sizeof (gpointer
);
7886 esize
= mono_class_native_size (eklass
, NULL
);
7888 src
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
7889 dest
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7891 mono_mb_emit_stloc (mb
, src
);
7892 mono_mb_emit_ldloc (mb
, src
);
7893 mono_mb_emit_stloc (mb
, 3);
7895 /* Check for null */
7896 mono_mb_emit_ldloc (mb
, src
);
7897 label1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7899 /* Allocate native array */
7900 mono_mb_emit_icon (mb
, esize
);
7901 mono_mb_emit_ldloc (mb
, src
);
7902 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7904 if (eklass
== mono_defaults
.string_class
) {
7905 /* Make the array bigger for the terminating null */
7906 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
7907 mono_mb_emit_byte (mb
, CEE_ADD
);
7909 mono_mb_emit_byte (mb
, CEE_MUL
);
7910 mono_mb_emit_icall (mb
, mono_marshal_alloc
);
7911 mono_mb_emit_stloc (mb
, dest
);
7912 mono_mb_emit_ldloc (mb
, dest
);
7913 mono_mb_emit_stloc (mb
, 3);
7915 /* Emit marshalling loop */
7916 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7917 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7918 mono_mb_emit_stloc (mb
, index_var
);
7920 mono_mb_emit_ldloc (mb
, index_var
);
7921 mono_mb_emit_ldloc (mb
, src
);
7922 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7923 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
7925 /* Emit marshalling code */
7927 g_assert (conv
!= -1);
7930 mono_mb_emit_ldloc (mb
, dest
);
7933 mono_mb_emit_ldloc (mb
, src
);
7934 mono_mb_emit_ldloc (mb
, index_var
);
7936 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
7938 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
7939 mono_mb_emit_byte (mb
, CEE_STIND_I
);
7942 char *msg
= g_strdup ("Marshalling of non-string arrays to managed code is not implemented.");
7943 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7947 mono_mb_emit_add_to_local (mb
, index_var
, 1);
7948 mono_mb_emit_add_to_local (mb
, dest
, esize
);
7950 mono_mb_emit_byte (mb
, CEE_BR
);
7951 mono_mb_emit_i4 (mb
, label2
- (mb
->pos
+ 4));
7953 mono_mb_patch_branch (mb
, label3
);
7954 mono_mb_patch_branch (mb
, label1
);
7958 g_assert_not_reached ();
7965 emit_marshal_boolean (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
7966 MonoMarshalSpec
*spec
,
7967 int conv_arg
, MonoType
**conv_arg_type
,
7968 MarshalAction action
)
7970 MonoMethodBuilder
*mb
= m
->mb
;
7973 case MARSHAL_ACTION_CONV_IN
: {
7974 MonoType
*local_type
;
7975 int variant_bool
= 0;
7979 local_type
= &mono_defaults
.int32_class
->byval_arg
;
7981 switch (spec
->native
) {
7982 case MONO_NATIVE_I1
:
7983 local_type
= &mono_defaults
.byte_class
->byval_arg
;
7985 case MONO_NATIVE_VARIANTBOOL
:
7986 local_type
= &mono_defaults
.int16_class
->byval_arg
;
7990 g_warning ("marshalling bool as native type %x is currently not supported", spec
->native
);
7991 local_type
= &mono_defaults
.int32_class
->byval_arg
;
7995 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
7996 conv_arg
= mono_mb_add_local (mb
, local_type
);
7997 mono_mb_emit_ldarg (mb
, argnum
);
7998 mono_mb_emit_byte (mb
, CEE_LDIND_I1
);
8000 mono_mb_emit_byte (mb
, CEE_NEG
);
8001 mono_mb_emit_stloc (mb
, conv_arg
);
8005 case MARSHAL_ACTION_CONV_OUT
:
8008 mono_mb_emit_ldarg (mb
, argnum
);
8009 mono_mb_emit_ldloc (mb
, conv_arg
);
8010 if (spec
!= NULL
&& spec
->native
== MONO_NATIVE_VARIANTBOOL
)
8011 mono_mb_emit_byte (mb
, CEE_NEG
);
8012 mono_mb_emit_byte (mb
, CEE_STIND_I1
);
8015 case MARSHAL_ACTION_PUSH
:
8017 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
8019 mono_mb_emit_ldarg (mb
, argnum
);
8022 case MARSHAL_ACTION_CONV_RESULT
:
8023 /* maybe we need to make sure that it fits within 8 bits */
8024 mono_mb_emit_stloc (mb
, 3);
8028 g_assert_not_reached ();
8035 emit_marshal_ptr (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
8036 MonoMarshalSpec
*spec
, int conv_arg
,
8037 MonoType
**conv_arg_type
, MarshalAction action
)
8039 MonoMethodBuilder
*mb
= m
->mb
;
8042 case MARSHAL_ACTION_CONV_IN
:
8043 if (MONO_TYPE_ISSTRUCT (t
->data
.type
)) {
8044 char *msg
= g_strdup_printf ("Can not marshal 'parameter #%d': Pointers can not reference marshaled structures. Use byref instead.", argnum
+ 1);
8045 mono_mb_emit_exception_marshal_directive (m
->mb
, msg
);
8049 case MARSHAL_ACTION_PUSH
:
8050 mono_mb_emit_ldarg (mb
, argnum
);
8053 case MARSHAL_ACTION_CONV_RESULT
:
8054 /* no conversions necessary */
8055 mono_mb_emit_stloc (mb
, 3);
8066 emit_marshal_char (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
8067 MonoMarshalSpec
*spec
, int conv_arg
,
8068 MonoType
**conv_arg_type
, MarshalAction action
)
8070 MonoMethodBuilder
*mb
= m
->mb
;
8073 case MARSHAL_ACTION_PUSH
:
8074 /* fixme: dont know how to marshal that. We cant simply
8075 * convert it to a one byte UTF8 character, because an
8076 * unicode character may need more that one byte in UTF8 */
8077 mono_mb_emit_ldarg (mb
, argnum
);
8080 case MARSHAL_ACTION_CONV_RESULT
:
8081 /* fixme: we need conversions here */
8082 mono_mb_emit_stloc (mb
, 3);
8093 emit_marshal_scalar (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
8094 MonoMarshalSpec
*spec
, int conv_arg
,
8095 MonoType
**conv_arg_type
, MarshalAction action
)
8097 MonoMethodBuilder
*mb
= m
->mb
;
8100 case MARSHAL_ACTION_PUSH
:
8101 mono_mb_emit_ldarg (mb
, argnum
);
8104 case MARSHAL_ACTION_CONV_RESULT
:
8105 /* no conversions necessary */
8106 mono_mb_emit_stloc (mb
, 3);
8117 emit_marshal (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
8118 MonoMarshalSpec
*spec
, int conv_arg
,
8119 MonoType
**conv_arg_type
, MarshalAction action
)
8121 /* Ensure that we have marshalling info for this param */
8122 mono_marshal_load_type_info (mono_class_from_mono_type (t
));
8124 if (spec
&& spec
->native
== MONO_NATIVE_CUSTOM
)
8125 return emit_marshal_custom (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8127 if (spec
&& spec
->native
== MONO_NATIVE_ASANY
)
8128 return emit_marshal_asany (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8131 case MONO_TYPE_VALUETYPE
:
8132 if (t
->data
.klass
== mono_defaults
.handleref_class
)
8133 return emit_marshal_handleref (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8135 return emit_marshal_vtype (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8136 case MONO_TYPE_STRING
:
8137 return emit_marshal_string (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8138 case MONO_TYPE_CLASS
:
8139 case MONO_TYPE_OBJECT
:
8140 if (spec
&& spec
->native
== MONO_NATIVE_STRUCT
)
8141 return emit_marshal_variant (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8143 if (spec
&& (spec
->native
== MONO_NATIVE_IUNKNOWN
||
8144 spec
->native
== MONO_NATIVE_IDISPATCH
||
8145 spec
->native
== MONO_NATIVE_INTERFACE
))
8146 return emit_marshal_com_interface (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8148 if (mono_defaults
.safehandle_class
!= NULL
&&
8149 mono_class_is_subclass_of (t
->data
.klass
, mono_defaults
.safehandle_class
, FALSE
))
8150 return emit_marshal_safehandle (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8152 return emit_marshal_object (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8153 case MONO_TYPE_ARRAY
:
8154 case MONO_TYPE_SZARRAY
:
8155 return emit_marshal_array (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8156 case MONO_TYPE_BOOLEAN
:
8157 return emit_marshal_boolean (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8159 return emit_marshal_ptr (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8160 case MONO_TYPE_CHAR
:
8161 return emit_marshal_char (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8174 case MONO_TYPE_FNPTR
:
8175 return emit_marshal_scalar (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8182 * mono_marshal_emit_native_wrapper:
8183 * @image: the image to use for looking up custom marshallers
8184 * @sig: The signature of the native function
8185 * @piinfo: Marshalling information
8186 * @mspecs: Marshalling information
8187 * @func: the native function to call
8189 * generates IL code for the pinvoke wrapper, the generated code calls @func.
8192 mono_marshal_emit_native_wrapper (MonoImage
*image
, MonoMethodBuilder
*mb
, MonoMethodSignature
*sig
, MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
**mspecs
, gpointer func
)
8194 EmitMarshalContext m
;
8195 MonoMethodSignature
*csig
;
8197 int i
, argnum
, *tmp_locals
;
8199 static MonoMethodSignature
*get_last_error_sig
= NULL
;
8204 /* we copy the signature, so that we can set pinvoke to 0 */
8205 csig
= signature_dup (mb
->method
->klass
->image
, sig
);
8210 /* we allocate local for use with emit_struct_conv() */
8211 /* allocate local 0 (pointer) src_ptr */
8212 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
8213 /* allocate local 1 (pointer) dst_ptr */
8214 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
8215 /* allocate local 2 (boolean) delete_old */
8216 mono_mb_add_local (mb
, &mono_defaults
.boolean_class
->byval_arg
);
8218 /* delete_old = FALSE */
8219 mono_mb_emit_icon (mb
, 0);
8220 mono_mb_emit_stloc (mb
, 2);
8222 if (!MONO_TYPE_IS_VOID(sig
->ret
)) {
8223 /* allocate local 3 to store the return value */
8224 mono_mb_add_local (mb
, sig
->ret
);
8227 if (mspecs
[0] && mspecs
[0]->native
== MONO_NATIVE_CUSTOM
) {
8228 /* Return type custom marshaling */
8230 * Since we can't determine the return type of the unmanaged function,
8231 * we assume it returns a pointer, and pass that pointer to
8232 * MarshalNativeToManaged.
8234 csig
->ret
= &mono_defaults
.int_class
->byval_arg
;
8237 /* we first do all conversions */
8238 tmp_locals
= alloca (sizeof (int) * sig
->param_count
);
8239 m
.orig_conv_args
= alloca (sizeof (int) * (sig
->param_count
+ 1));
8241 for (i
= 0; i
< sig
->param_count
; i
++) {
8242 tmp_locals
[i
] = emit_marshal (&m
, i
+ sig
->hasthis
, sig
->params
[i
], mspecs
[i
+ 1], 0, &csig
->params
[i
], MARSHAL_ACTION_CONV_IN
);
8245 /* push all arguments */
8248 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
8251 for (i
= 0; i
< sig
->param_count
; i
++) {
8252 emit_marshal (&m
, i
+ sig
->hasthis
, sig
->params
[i
], mspecs
[i
+ 1], tmp_locals
[i
], NULL
, MARSHAL_ACTION_PUSH
);
8255 /* call the native method */
8256 if (MONO_CLASS_IS_IMPORT (mb
->method
->klass
)) {
8257 mono_mb_emit_cominterop_call (mb
, csig
, &piinfo
->method
);
8260 mono_mb_emit_native_call (mb
, csig
, func
);
8263 /* Set LastError if needed */
8264 if (piinfo
->piflags
& PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR
) {
8265 if (!get_last_error_sig
) {
8266 get_last_error_sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
8267 get_last_error_sig
->ret
= &mono_defaults
.int_class
->byval_arg
;
8268 get_last_error_sig
->pinvoke
= 1;
8271 #ifdef PLATFORM_WIN32
8273 * Have to call GetLastError () early and without a wrapper, since various runtime components could
8274 * clobber its value.
8276 mono_mb_emit_native_call (mb
, get_last_error_sig
, GetLastError
);
8277 mono_mb_emit_icall (mb
, mono_marshal_set_last_error_windows
);
8279 mono_mb_emit_icall (mb
, mono_marshal_set_last_error
);
8283 /* convert the result */
8284 if (!sig
->ret
->byref
) {
8285 MonoMarshalSpec
*spec
= mspecs
[0];
8286 type
= sig
->ret
->type
;
8288 if (spec
&& spec
->native
== MONO_NATIVE_CUSTOM
) {
8289 emit_marshal (&m
, 0, sig
->ret
, spec
, 0, NULL
, MARSHAL_ACTION_CONV_RESULT
);
8294 case MONO_TYPE_VOID
:
8296 case MONO_TYPE_VALUETYPE
:
8297 klass
= sig
->ret
->data
.klass
;
8298 if (klass
->enumtype
) {
8299 type
= sig
->ret
->data
.klass
->enum_basetype
->type
;
8302 emit_marshal (&m
, 0, sig
->ret
, spec
, 0, NULL
, MARSHAL_ACTION_CONV_RESULT
);
8316 case MONO_TYPE_FNPTR
:
8317 case MONO_TYPE_STRING
:
8318 case MONO_TYPE_CLASS
:
8319 case MONO_TYPE_OBJECT
:
8320 case MONO_TYPE_BOOLEAN
:
8321 case MONO_TYPE_ARRAY
:
8322 case MONO_TYPE_SZARRAY
:
8323 case MONO_TYPE_CHAR
:
8325 emit_marshal (&m
, 0, sig
->ret
, spec
, 0, NULL
, MARSHAL_ACTION_CONV_RESULT
);
8327 case MONO_TYPE_TYPEDBYREF
:
8329 g_warning ("return type 0x%02x unknown", sig
->ret
->type
);
8330 g_assert_not_reached ();
8334 mono_mb_emit_stloc (mb
, 3);
8338 * Need to call this after converting the result since MONO_VTADDR needs
8339 * to be adjacent to the call instruction.
8341 emit_thread_interrupt_checkpoint (mb
);
8343 /* we need to convert byref arguments back and free string arrays */
8344 for (i
= 0; i
< sig
->param_count
; i
++) {
8345 MonoType
*t
= sig
->params
[i
];
8346 MonoMarshalSpec
*spec
= mspecs
[i
+ 1];
8348 argnum
= i
+ sig
->hasthis
;
8350 if (spec
&& ((spec
->native
== MONO_NATIVE_CUSTOM
) || (spec
->native
== MONO_NATIVE_ASANY
))) {
8351 emit_marshal (&m
, argnum
, t
, spec
, tmp_locals
[i
], NULL
, MARSHAL_ACTION_CONV_OUT
);
8356 case MONO_TYPE_STRING
:
8357 case MONO_TYPE_VALUETYPE
:
8358 case MONO_TYPE_CLASS
:
8359 case MONO_TYPE_OBJECT
:
8360 case MONO_TYPE_SZARRAY
:
8361 case MONO_TYPE_BOOLEAN
:
8362 emit_marshal (&m
, argnum
, t
, spec
, tmp_locals
[i
], NULL
, MARSHAL_ACTION_CONV_OUT
);
8367 if (!MONO_TYPE_IS_VOID(sig
->ret
))
8368 mono_mb_emit_ldloc (mb
, 3);
8370 mono_mb_emit_byte (mb
, CEE_RET
);
8374 * mono_marshal_get_native_wrapper:
8375 * @method: The MonoMethod to wrap.
8377 * generates IL code for the pinvoke wrapper (the generated method
8378 * calls the unmanaged code in piinfo->addr)
8381 mono_marshal_get_native_wrapper (MonoMethod
*method
)
8383 MonoMethodSignature
*sig
, *csig
;
8384 MonoMethodPInvoke
*piinfo
= (MonoMethodPInvoke
*) method
;
8385 MonoMethodBuilder
*mb
;
8386 MonoMarshalSpec
**mspecs
;
8389 gboolean pinvoke
= FALSE
;
8392 const char *exc_class
= "MissingMethodException";
8393 const char *exc_arg
= NULL
;
8395 g_assert (method
!= NULL
);
8396 g_assert (mono_method_signature (method
)->pinvoke
);
8398 cache
= method
->klass
->image
->native_wrapper_cache
;
8399 if ((res
= mono_marshal_find_in_cache (cache
, method
)))
8402 if (MONO_CLASS_IS_IMPORT (method
->klass
))
8403 return cominterop_get_native_wrapper (method
);
8405 sig
= mono_method_signature (method
);
8407 if (!(method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) &&
8408 (method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
))
8411 if (!piinfo
->addr
) {
8413 mono_lookup_pinvoke_call (method
, &exc_class
, &exc_arg
);
8415 piinfo
->addr
= mono_lookup_internal_call (method
);
8418 /* hack - redirect certain string constructors to CreateString */
8419 if (piinfo
->addr
== ves_icall_System_String_ctor_RedirectToCreateString
) {
8420 g_assert (!pinvoke
);
8421 g_assert (method
->string_ctor
);
8422 g_assert (sig
->hasthis
);
8424 /* CreateString returns a value */
8425 csig
= signature_dup (method
->klass
->image
, sig
);
8426 csig
->ret
= &mono_defaults
.string_class
->byval_arg
;
8430 while ((res
= mono_class_get_methods (mono_defaults
.string_class
, &iter
))) {
8431 if (!strcmp ("CreateString", res
->name
) &&
8432 mono_metadata_signature_equal (csig
, mono_method_signature (res
))) {
8434 g_assert (!(res
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
));
8435 g_assert (!(res
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
));
8437 /* create a wrapper to preserve .ctor in stack trace */
8438 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_MANAGED_TO_MANAGED
);
8440 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
8441 for (i
= 1; i
<= csig
->param_count
; i
++)
8442 mono_mb_emit_ldarg (mb
, i
);
8443 mono_mb_emit_managed_call (mb
, res
, NULL
);
8444 mono_mb_emit_byte (mb
, CEE_RET
);
8446 /* use native_wrapper_cache because internal calls are looked up there */
8447 res
= mono_mb_create_and_cache (cache
, method
,
8448 mb
, csig
, csig
->param_count
+ 1);
8456 /* exception will be thrown */
8457 piinfo
->addr
= NULL
;
8458 g_warning ("cannot find CreateString for .ctor");
8461 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_MANAGED_TO_NATIVE
);
8463 mb
->method
->save_lmf
= 1;
8465 if (!piinfo
->addr
) {
8466 mono_mb_emit_exception (mb
, exc_class
, exc_arg
);
8467 csig
= signature_dup (method
->klass
->image
, sig
);
8469 res
= mono_mb_create_and_cache (cache
, method
,
8470 mb
, csig
, csig
->param_count
+ 16);
8475 /* internal calls: we simply push all arguments and call the method (no conversions) */
8476 if (method
->iflags
& (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
| METHOD_IMPL_ATTRIBUTE_RUNTIME
)) {
8478 /* hack - string constructors returns a value */
8479 if (method
->string_ctor
) {
8480 csig
= signature_dup (method
->klass
->image
, sig
);
8481 csig
->ret
= &mono_defaults
.string_class
->byval_arg
;
8486 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
8488 for (i
= 0; i
< sig
->param_count
; i
++)
8489 mono_mb_emit_ldarg (mb
, i
+ sig
->hasthis
);
8491 g_assert (piinfo
->addr
);
8492 mono_mb_emit_native_call (mb
, csig
, piinfo
->addr
);
8493 emit_thread_interrupt_checkpoint (mb
);
8494 mono_mb_emit_byte (mb
, CEE_RET
);
8496 csig
= signature_dup (method
->klass
->image
, csig
);
8498 res
= mono_mb_create_and_cache (cache
, method
,
8499 mb
, csig
, csig
->param_count
+ 16);
8506 mspecs
= g_new (MonoMarshalSpec
*, sig
->param_count
+ 1);
8507 mono_method_get_marshal_info (method
, mspecs
);
8509 mono_marshal_emit_native_wrapper (mb
->method
->klass
->image
, mb
, sig
, piinfo
, mspecs
, piinfo
->addr
);
8511 csig
= signature_dup (method
->klass
->image
, sig
);
8513 res
= mono_mb_create_and_cache (cache
, method
,
8514 mb
, csig
, csig
->param_count
+ 16);
8517 for (i
= sig
->param_count
; i
>= 0; i
--)
8519 mono_metadata_free_marshal_spec (mspecs
[i
]);
8522 /* printf ("CODE FOR %s: \n%s.\n", mono_method_full_name (res, TRUE), mono_disasm_code (0, res, ((MonoMethodNormal*)res)->header->code, ((MonoMethodNormal*)res)->header->code + ((MonoMethodNormal*)res)->header->code_size)); */
8528 * mono_marshal_get_native_func_wrapper:
8529 * @image: The image to use for memory allocation and for looking up custom marshallers.
8530 * @sig: The signature of the function
8531 * @func: The native function to wrap
8533 * Returns a wrapper method around native functions, similar to the pinvoke
8537 mono_marshal_get_native_func_wrapper (MonoImage
*image
, MonoMethodSignature
*sig
,
8538 MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
**mspecs
, gpointer func
)
8540 MonoMethodSignature
*csig
;
8542 MonoMethodBuilder
*mb
;
8547 cache
= image
->native_wrapper_cache
;
8548 if ((res
= mono_marshal_find_in_cache (cache
, func
)))
8551 name
= g_strdup_printf ("wrapper_native_%p", func
);
8552 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_MANAGED_TO_NATIVE
);
8553 mb
->method
->save_lmf
= 1;
8555 mono_marshal_emit_native_wrapper (image
, mb
, sig
, piinfo
, mspecs
, func
);
8557 csig
= signature_dup (image
, sig
);
8559 res
= mono_mb_create_and_cache (cache
, func
,
8560 mb
, csig
, csig
->param_count
+ 16);
8563 /* printf ("CODE FOR %s: \n%s.\n", mono_method_full_name (res, TRUE), mono_disasm_code (0, res, ((MonoMethodNormal*)res)->header->code, ((MonoMethodNormal*)res)->header->code + ((MonoMethodNormal*)res)->header->code_size)); */
8568 /* FIXME: moving GC */
8570 mono_marshal_emit_managed_wrapper (MonoMethodBuilder
*mb
, MonoMethodSignature
*invoke_sig
, MonoMarshalSpec
**mspecs
, EmitMarshalContext
* m
, MonoMethod
*method
, MonoObject
* this)
8572 MonoMethodSignature
*sig
, *csig
;
8578 /* allocate local 0 (pointer) src_ptr */
8579 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
8580 /* allocate local 1 (pointer) dst_ptr */
8581 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
8582 /* allocate local 2 (boolean) delete_old */
8583 mono_mb_add_local (mb
, &mono_defaults
.boolean_class
->byval_arg
);
8585 if (!MONO_TYPE_IS_VOID(sig
->ret
)) {
8586 /* allocate local 3 to store the return value */
8587 mono_mb_add_local (mb
, sig
->ret
);
8590 mono_mb_emit_icon (mb
, 0);
8591 mono_mb_emit_stloc (mb
, 2);
8593 /* fixme: howto handle this ? */
8596 /* FIXME: need a solution for the moving GC here */
8597 mono_mb_emit_ptr (mb
, this);
8600 g_assert_not_reached ();
8604 /* we first do all conversions */
8605 tmp_locals
= alloca (sizeof (int) * sig
->param_count
);
8606 for (i
= 0; i
< sig
->param_count
; i
++) {
8607 MonoType
*t
= sig
->params
[i
];
8610 case MONO_TYPE_OBJECT
:
8611 case MONO_TYPE_CLASS
:
8612 case MONO_TYPE_VALUETYPE
:
8613 case MONO_TYPE_ARRAY
:
8614 case MONO_TYPE_SZARRAY
:
8615 case MONO_TYPE_STRING
:
8616 tmp_locals
[i
] = emit_marshal (m
, i
, sig
->params
[i
], mspecs
[i
+ 1], 0, &csig
->params
[i
], MARSHAL_ACTION_MANAGED_CONV_IN
);
8625 emit_thread_interrupt_checkpoint (mb
);
8627 for (i
= 0; i
< sig
->param_count
; i
++) {
8628 MonoType
*t
= sig
->params
[i
];
8630 if (tmp_locals
[i
]) {
8632 mono_mb_emit_ldloc_addr (mb
, tmp_locals
[i
]);
8634 mono_mb_emit_ldloc (mb
, tmp_locals
[i
]);
8637 mono_mb_emit_ldarg (mb
, i
);
8640 mono_mb_emit_managed_call (mb
, method
, NULL
);
8642 if (mspecs
[0] && mspecs
[0]->native
== MONO_NATIVE_CUSTOM
) {
8643 emit_marshal (m
, 0, sig
->ret
, mspecs
[0], 0, NULL
, MARSHAL_ACTION_MANAGED_CONV_RESULT
);
8646 if (!sig
->ret
->byref
) {
8647 switch (sig
->ret
->type
) {
8648 case MONO_TYPE_VOID
:
8650 case MONO_TYPE_BOOLEAN
:
8664 case MONO_TYPE_OBJECT
:
8665 mono_mb_emit_stloc (mb
, 3);
8667 case MONO_TYPE_STRING
:
8668 csig
->ret
= &mono_defaults
.int_class
->byval_arg
;
8669 emit_marshal (m
, 0, sig
->ret
, mspecs
[0], 0, NULL
, MARSHAL_ACTION_MANAGED_CONV_RESULT
);
8671 case MONO_TYPE_VALUETYPE
:
8672 case MONO_TYPE_CLASS
:
8673 case MONO_TYPE_SZARRAY
:
8674 emit_marshal (m
, 0, sig
->ret
, mspecs
[0], 0, NULL
, MARSHAL_ACTION_MANAGED_CONV_RESULT
);
8677 g_warning ("return type 0x%02x unknown", sig
->ret
->type
);
8678 g_assert_not_reached ();
8681 mono_mb_emit_stloc (mb
, 3);
8684 /* Convert byref arguments back */
8685 for (i
= 0; i
< sig
->param_count
; i
++) {
8686 MonoType
*t
= sig
->params
[i
];
8687 MonoMarshalSpec
*spec
= mspecs
[i
+ 1];
8689 if (spec
&& spec
->native
== MONO_NATIVE_CUSTOM
) {
8690 emit_marshal (m
, i
, t
, mspecs
[i
+ 1], tmp_locals
[i
], NULL
, MARSHAL_ACTION_MANAGED_CONV_OUT
);
8692 else if (t
->byref
) {
8694 case MONO_TYPE_CLASS
:
8695 case MONO_TYPE_VALUETYPE
:
8696 case MONO_TYPE_OBJECT
:
8697 emit_marshal (m
, i
, t
, mspecs
[i
+ 1], tmp_locals
[i
], NULL
, MARSHAL_ACTION_MANAGED_CONV_OUT
);
8701 else if (invoke_sig
->params
[i
]->attrs
& PARAM_ATTRIBUTE_OUT
) {
8702 /* The [Out] information is encoded in the delegate signature */
8704 case MONO_TYPE_SZARRAY
:
8705 case MONO_TYPE_CLASS
:
8706 case MONO_TYPE_VALUETYPE
:
8707 emit_marshal (m
, i
, invoke_sig
->params
[i
], mspecs
[i
+ 1], tmp_locals
[i
], NULL
, MARSHAL_ACTION_MANAGED_CONV_OUT
);
8710 g_assert_not_reached ();
8715 if (m
->retobj_var
) {
8716 mono_mb_emit_ldloc (mb
, m
->retobj_var
);
8717 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
8718 mono_mb_emit_op (mb
, CEE_MONO_RETOBJ
, m
->retobj_class
);
8721 if (!MONO_TYPE_IS_VOID(sig
->ret
))
8722 mono_mb_emit_ldloc (mb
, 3);
8723 mono_mb_emit_byte (mb
, CEE_RET
);
8729 * generates IL code to call managed methods from unmanaged code
8732 mono_marshal_get_managed_wrapper (MonoMethod
*method
, MonoClass
*delegate_klass
, MonoObject
*this)
8734 static MonoClass
*UnmanagedFunctionPointerAttribute
;
8735 MonoMethodSignature
*sig
, *csig
, *invoke_sig
;
8736 MonoMethodBuilder
*mb
;
8737 MonoMethod
*res
, *invoke
;
8738 MonoMarshalSpec
**mspecs
;
8739 MonoMethodPInvoke piinfo
;
8742 EmitMarshalContext m
;
8744 g_assert (method
!= NULL
);
8745 g_assert (!mono_method_signature (method
)->pinvoke
);
8748 * FIXME: Should cache the method+delegate type pair, since the same method
8749 * could be called with different delegates, thus different marshalling
8752 cache
= method
->klass
->image
->managed_wrapper_cache
;
8753 if (!this && (res
= mono_marshal_find_in_cache (cache
, method
)))
8756 invoke
= mono_class_get_method_from_name (delegate_klass
, "Invoke", mono_method_signature (method
)->param_count
);
8757 invoke_sig
= mono_method_signature (invoke
);
8759 mspecs
= g_new0 (MonoMarshalSpec
*, mono_method_signature (invoke
)->param_count
+ 1);
8760 mono_method_get_marshal_info (invoke
, mspecs
);
8762 sig
= mono_method_signature (method
);
8764 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_NATIVE_TO_MANAGED
);
8767 /* we copy the signature, so that we can modify it */
8768 csig
= signature_dup (method
->klass
->image
, sig
);
8777 m
.image
= method
->klass
->image
;
8779 #ifdef PLATFORM_WIN32
8781 * Under windows, delegates passed to native code must use the STDCALL
8782 * calling convention.
8784 csig
->call_convention
= MONO_CALL_STDCALL
;
8787 /* Change default calling convention if needed */
8788 /* Why is this a modopt ? */
8789 if (invoke_sig
->ret
&& invoke_sig
->ret
->num_mods
) {
8790 for (i
= 0; i
< invoke_sig
->ret
->num_mods
; ++i
) {
8791 MonoClass
*cmod_class
= mono_class_get (delegate_klass
->image
, invoke_sig
->ret
->modifiers
[i
].token
);
8792 g_assert (cmod_class
);
8793 if ((cmod_class
->image
== mono_defaults
.corlib
) && !strcmp (cmod_class
->name_space
, "System.Runtime.CompilerServices")) {
8794 if (!strcmp (cmod_class
->name
, "CallConvCdecl"))
8795 csig
->call_convention
= MONO_CALL_C
;
8796 else if (!strcmp (cmod_class
->name
, "CallConvStdcall"))
8797 csig
->call_convention
= MONO_CALL_STDCALL
;
8798 else if (!strcmp (cmod_class
->name
, "CallConvFastcall"))
8799 csig
->call_convention
= MONO_CALL_FASTCALL
;
8800 else if (!strcmp (cmod_class
->name
, "CallConvThiscall"))
8801 csig
->call_convention
= MONO_CALL_THISCALL
;
8806 /* Handle the UnmanagedFunctionPointerAttribute */
8807 if (!UnmanagedFunctionPointerAttribute
)
8808 UnmanagedFunctionPointerAttribute
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.InteropServices", "UnmanagedFunctionPointerAttribute");
8810 /* The attribute is only available in Net 2.0 */
8811 if (UnmanagedFunctionPointerAttribute
) {
8812 MonoReflectionUnmanagedFunctionPointerAttribute
*attr
;
8813 MonoCustomAttrInfo
*cinfo
;
8816 * The pinvoke attributes are stored in a real custom attribute so we have to
8819 cinfo
= mono_custom_attrs_from_class (delegate_klass
);
8821 attr
= (MonoReflectionUnmanagedFunctionPointerAttribute
*)mono_custom_attrs_get_attr (cinfo
, UnmanagedFunctionPointerAttribute
);
8823 memset (&piinfo
, 0, sizeof (piinfo
));
8825 piinfo
.piflags
= (attr
->call_conv
<< 8) | (attr
->charset
? (attr
->charset
- 1) * 2 : 1) | attr
->set_last_error
;
8827 csig
->call_convention
= attr
->call_conv
- 1;
8830 mono_custom_attrs_free (cinfo
);
8834 mono_marshal_emit_managed_wrapper (mb
, invoke_sig
, mspecs
, &m
, method
, this);
8837 res
= mono_mb_create_and_cache (cache
, method
,
8838 mb
, csig
, sig
->param_count
+ 16);
8841 res
= mono_mb_create_method (mb
, csig
, sig
->param_count
+ 16);
8845 for (i
= mono_method_signature (invoke
)->param_count
; i
>= 0; i
--)
8847 mono_metadata_free_marshal_spec (mspecs
[i
]);
8850 /* printf ("CODE FOR %s: \n%s.\n", mono_method_full_name (res, TRUE), mono_disasm_code (0, res, ((MonoMethodNormal*)res)->header->code, ((MonoMethodNormal*)res)->header->code + ((MonoMethodNormal*)res)->header->code_size)); */
8855 static MonoReflectionType
*
8856 type_from_handle (MonoType
*handle
)
8858 MonoDomain
*domain
= mono_domain_get ();
8859 MonoClass
*klass
= mono_class_from_mono_type (handle
);
8861 MONO_ARCH_SAVE_REGS
;
8863 mono_class_init (klass
);
8864 return mono_type_get_object (domain
, handle
);
8868 * mono_marshal_get_isinst:
8869 * @klass: the type of the field
8871 * This method generates a function which can be used to check if an object is
8872 * an instance of the given type, icluding the case where the object is a proxy.
8873 * The generated function has the following signature:
8874 * MonoObject* __isinst_wrapper_ (MonoObject *obj)
8877 mono_marshal_get_isinst (MonoClass
*klass
)
8879 static MonoMethodSignature
*isint_sig
= NULL
;
8882 int pos_was_ok
, pos_failed
, pos_end
, pos_end2
;
8884 MonoMethodBuilder
*mb
;
8886 cache
= klass
->image
->isinst_cache
;
8887 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
8891 isint_sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1);
8892 isint_sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
8893 isint_sig
->ret
= &mono_defaults
.object_class
->byval_arg
;
8894 isint_sig
->pinvoke
= 0;
8897 name
= g_strdup_printf ("__isinst_wrapper_%s", klass
->name
);
8898 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_ISINST
);
8901 mb
->method
->save_lmf
= 1;
8903 /* check if the object is a proxy that needs special cast */
8904 mono_mb_emit_ldarg (mb
, 0);
8905 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
8906 mono_mb_emit_op (mb
, CEE_MONO_CISINST
, klass
);
8908 /* The result of MONO_ISINST can be:
8909 0) the type check succeeded
8910 1) the type check did not succeed
8911 2) a CanCastTo call is needed */
8913 mono_mb_emit_byte (mb
, CEE_DUP
);
8914 pos_was_ok
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
8916 mono_mb_emit_byte (mb
, CEE_LDC_I4_2
);
8917 pos_failed
= mono_mb_emit_branch (mb
, CEE_BNE_UN
);
8919 /* get the real proxy from the transparent proxy*/
8921 mono_mb_emit_ldarg (mb
, 0);
8922 mono_mb_emit_managed_call (mb
, mono_marshal_get_proxy_cancast (klass
), NULL
);
8923 pos_end
= mono_mb_emit_branch (mb
, CEE_BR
);
8927 mono_mb_patch_addr (mb
, pos_failed
, mb
->pos
- (pos_failed
+ 4));
8928 mono_mb_emit_byte (mb
, CEE_LDNULL
);
8929 pos_end2
= mono_mb_emit_branch (mb
, CEE_BR
);
8933 mono_mb_patch_addr (mb
, pos_was_ok
, mb
->pos
- (pos_was_ok
+ 4));
8934 mono_mb_emit_byte (mb
, CEE_POP
);
8935 mono_mb_emit_ldarg (mb
, 0);
8939 mono_mb_patch_addr (mb
, pos_end
, mb
->pos
- (pos_end
+ 4));
8940 mono_mb_patch_addr (mb
, pos_end2
, mb
->pos
- (pos_end2
+ 4));
8941 mono_mb_emit_byte (mb
, CEE_RET
);
8943 res
= mono_mb_create_and_cache (cache
, klass
, mb
, isint_sig
, isint_sig
->param_count
+ 16);
8950 * mono_marshal_get_castclass:
8951 * @klass: the type of the field
8953 * This method generates a function which can be used to cast an object to
8954 * an instance of the given type, icluding the case where the object is a proxy.
8955 * The generated function has the following signature:
8956 * MonoObject* __castclass_wrapper_ (MonoObject *obj)
8959 mono_marshal_get_castclass (MonoClass
*klass
)
8961 static MonoMethodSignature
*castclass_sig
= NULL
;
8964 int pos_was_ok
, pos_was_ok2
;
8966 MonoMethodBuilder
*mb
;
8968 cache
= klass
->image
->castclass_cache
;
8969 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
8972 if (!castclass_sig
) {
8973 castclass_sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1);
8974 castclass_sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
8975 castclass_sig
->ret
= &mono_defaults
.object_class
->byval_arg
;
8976 castclass_sig
->pinvoke
= 0;
8979 name
= g_strdup_printf ("__castclass_wrapper_%s", klass
->name
);
8980 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_CASTCLASS
);
8983 mb
->method
->save_lmf
= 1;
8985 /* check if the object is a proxy that needs special cast */
8986 mono_mb_emit_ldarg (mb
, 0);
8987 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
8988 mono_mb_emit_op (mb
, CEE_MONO_CCASTCLASS
, klass
);
8990 /* The result of MONO_ISINST can be:
8991 0) the cast is valid
8992 1) cast of unknown proxy type
8993 or an exception if the cast is is invalid
8996 pos_was_ok
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
8998 /* get the real proxy from the transparent proxy*/
9000 mono_mb_emit_ldarg (mb
, 0);
9001 mono_mb_emit_managed_call (mb
, mono_marshal_get_proxy_cancast (klass
), NULL
);
9002 pos_was_ok2
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
9005 mono_mb_emit_exception (mb
, "InvalidCastException", NULL
);
9008 mono_mb_patch_addr (mb
, pos_was_ok
, mb
->pos
- (pos_was_ok
+ 4));
9009 mono_mb_patch_addr (mb
, pos_was_ok2
, mb
->pos
- (pos_was_ok2
+ 4));
9010 mono_mb_emit_ldarg (mb
, 0);
9013 mono_mb_emit_byte (mb
, CEE_RET
);
9015 res
= mono_mb_create_and_cache (cache
, klass
, mb
, castclass_sig
, castclass_sig
->param_count
+ 16);
9022 mono_marshal_get_proxy_cancast (MonoClass
*klass
)
9024 static MonoMethodSignature
*isint_sig
= NULL
;
9027 int pos_failed
, pos_end
;
9029 MonoMethod
*can_cast_to
;
9030 MonoMethodDesc
*desc
;
9031 MonoMethodBuilder
*mb
;
9033 cache
= klass
->image
->proxy_isinst_cache
;
9034 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
9038 isint_sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1);
9039 isint_sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
9040 isint_sig
->ret
= &mono_defaults
.object_class
->byval_arg
;
9041 isint_sig
->pinvoke
= 0;
9044 name
= g_strdup_printf ("__proxy_isinst_wrapper_%s", klass
->name
);
9045 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_PROXY_ISINST
);
9048 mb
->method
->save_lmf
= 1;
9050 /* get the real proxy from the transparent proxy*/
9051 mono_mb_emit_ldarg (mb
, 0);
9052 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
9053 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
9055 /* get the reflection type from the type handle */
9056 mono_mb_emit_ptr (mb
, &klass
->byval_arg
);
9057 mono_mb_emit_icall (mb
, type_from_handle
);
9059 mono_mb_emit_ldarg (mb
, 0);
9061 /* make the call to CanCastTo (type, ob) */
9062 desc
= mono_method_desc_new ("IRemotingTypeInfo:CanCastTo", FALSE
);
9063 can_cast_to
= mono_method_desc_search_in_class (desc
, mono_defaults
.iremotingtypeinfo_class
);
9064 g_assert (can_cast_to
);
9065 mono_method_desc_free (desc
);
9066 mono_mb_emit_op (mb
, CEE_CALLVIRT
, can_cast_to
);
9068 pos_failed
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
9070 /* Upgrade the proxy vtable by calling: mono_upgrade_remote_class_wrapper (type, ob)*/
9071 mono_mb_emit_ptr (mb
, &klass
->byval_arg
);
9072 mono_mb_emit_icall (mb
, type_from_handle
);
9073 mono_mb_emit_ldarg (mb
, 0);
9075 mono_mb_emit_icall (mb
, mono_upgrade_remote_class_wrapper
);
9076 emit_thread_interrupt_checkpoint (mb
);
9078 mono_mb_emit_ldarg (mb
, 0);
9079 pos_end
= mono_mb_emit_branch (mb
, CEE_BR
);
9083 mono_mb_patch_addr (mb
, pos_failed
, mb
->pos
- (pos_failed
+ 4));
9084 mono_mb_emit_byte (mb
, CEE_LDNULL
);
9088 mono_mb_patch_addr (mb
, pos_end
, mb
->pos
- (pos_end
+ 4));
9089 mono_mb_emit_byte (mb
, CEE_RET
);
9091 res
= mono_mb_create_and_cache (cache
, klass
, mb
, isint_sig
, isint_sig
->param_count
+ 16);
9098 mono_upgrade_remote_class_wrapper (MonoReflectionType
*rtype
, MonoTransparentProxy
*tproxy
)
9101 MonoDomain
*domain
= ((MonoObject
*)tproxy
)->vtable
->domain
;
9102 klass
= mono_class_from_mono_type (rtype
->type
);
9103 mono_upgrade_remote_class (domain
, (MonoObject
*)tproxy
, klass
);
9107 * mono_marshal_get_struct_to_ptr:
9110 * generates IL code for StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld)
9113 mono_marshal_get_struct_to_ptr (MonoClass
*klass
)
9115 MonoMethodBuilder
*mb
;
9116 static MonoMethod
*stoptr
= NULL
;
9119 g_assert (klass
!= NULL
);
9121 mono_marshal_load_type_info (klass
);
9123 if (klass
->marshal_info
->str_to_ptr
)
9124 return klass
->marshal_info
->str_to_ptr
;
9127 stoptr
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "StructureToPtr", 3);
9130 mb
= mono_mb_new (klass
, stoptr
->name
, MONO_WRAPPER_UNKNOWN
);
9132 if (klass
->blittable
) {
9133 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
9134 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
9135 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
9136 mono_mb_emit_icon (mb
, mono_class_value_size (klass
, NULL
));
9137 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
9138 mono_mb_emit_byte (mb
, CEE_CPBLK
);
9141 /* allocate local 0 (pointer) src_ptr */
9142 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
9143 /* allocate local 1 (pointer) dst_ptr */
9144 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
9145 /* allocate local 2 (boolean) delete_old */
9146 mono_mb_add_local (mb
, &mono_defaults
.boolean_class
->byval_arg
);
9147 mono_mb_emit_byte (mb
, CEE_LDARG_2
);
9148 mono_mb_emit_stloc (mb
, 2);
9150 /* initialize src_ptr to point to the start of object data */
9151 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
9152 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
9153 mono_mb_emit_stloc (mb
, 0);
9155 /* initialize dst_ptr */
9156 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
9157 mono_mb_emit_stloc (mb
, 1);
9159 emit_struct_conv (mb
, klass
, FALSE
);
9162 mono_mb_emit_byte (mb
, CEE_RET
);
9164 res
= mono_mb_create_method (mb
, mono_method_signature (stoptr
), 0);
9167 klass
->marshal_info
->str_to_ptr
= res
;
9172 * mono_marshal_get_ptr_to_struct:
9175 * generates IL code for PtrToStructure (IntPtr src, object structure)
9178 mono_marshal_get_ptr_to_struct (MonoClass
*klass
)
9180 MonoMethodBuilder
*mb
;
9181 static MonoMethodSignature
*ptostr
= NULL
;
9184 g_assert (klass
!= NULL
);
9186 mono_marshal_load_type_info (klass
);
9188 if (klass
->marshal_info
->ptr_to_str
)
9189 return klass
->marshal_info
->ptr_to_str
;
9192 /* Create the signature corresponding to
9193 static void PtrToStructure (IntPtr ptr, object structure);
9194 defined in class/corlib/System.Runtime.InteropServices/Marshal.cs */
9195 ptostr
= mono_create_icall_signature ("void ptr object");
9197 mb
= mono_mb_new (klass
, "PtrToStructure", MONO_WRAPPER_UNKNOWN
);
9199 if (klass
->blittable
) {
9200 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
9201 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
9202 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
9203 mono_mb_emit_icon (mb
, mono_class_value_size (klass
, NULL
));
9204 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
9205 mono_mb_emit_byte (mb
, CEE_CPBLK
);
9208 /* allocate local 0 (pointer) src_ptr */
9209 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
9210 /* allocate local 1 (pointer) dst_ptr */
9211 mono_mb_add_local (mb
, &klass
->this_arg
);
9213 /* initialize src_ptr to point to the start of object data */
9214 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
9215 mono_mb_emit_stloc (mb
, 0);
9217 /* initialize dst_ptr */
9218 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
9219 mono_mb_emit_op (mb
, CEE_UNBOX
, klass
);
9220 mono_mb_emit_stloc (mb
, 1);
9222 emit_struct_conv (mb
, klass
, TRUE
);
9225 mono_mb_emit_byte (mb
, CEE_RET
);
9227 res
= mono_mb_create_method (mb
, ptostr
, 0);
9230 klass
->marshal_info
->ptr_to_str
= res
;
9235 * generates IL code for the synchronized wrapper: the generated method
9236 * calls METHOD while locking 'this' or the parent type.
9239 mono_marshal_get_synchronized_wrapper (MonoMethod
*method
)
9241 static MonoMethod
*enter_method
, *exit_method
;
9242 MonoMethodSignature
*sig
;
9243 MonoExceptionClause
*clause
;
9244 MonoMethodHeader
*header
;
9245 MonoMethodBuilder
*mb
;
9248 int i
, pos
, this_local
, ret_local
= 0;
9252 if (method
->wrapper_type
== MONO_WRAPPER_SYNCHRONIZED
)
9255 cache
= method
->klass
->image
->synchronized_cache
;
9256 if ((res
= mono_marshal_find_in_cache (cache
, method
)))
9259 sig
= signature_dup (method
->klass
->image
, mono_method_signature (method
));
9262 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_SYNCHRONIZED
);
9265 if (!MONO_TYPE_IS_VOID (sig
->ret
))
9266 ret_local
= mono_mb_add_local (mb
, sig
->ret
);
9269 this_local
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
9271 mono_loader_lock ();
9272 clause
= mono_mempool_alloc0 (method
->klass
->image
->mempool
, sizeof (MonoExceptionClause
));
9273 mono_loader_unlock ();
9274 clause
->flags
= MONO_EXCEPTION_CLAUSE_FINALLY
;
9276 if (!enter_method
) {
9277 MonoMethodDesc
*desc
;
9279 desc
= mono_method_desc_new ("Monitor:Enter", FALSE
);
9280 enter_method
= mono_method_desc_search_in_class (desc
, mono_defaults
.monitor_class
);
9281 g_assert (enter_method
);
9282 mono_method_desc_free (desc
);
9283 desc
= mono_method_desc_new ("Monitor:Exit", FALSE
);
9284 exit_method
= mono_method_desc_search_in_class (desc
, mono_defaults
.monitor_class
);
9285 g_assert (exit_method
);
9286 mono_method_desc_free (desc
);
9289 /* Push this or the type object */
9290 if (method
->flags
& METHOD_ATTRIBUTE_STATIC
) {
9292 * GetTypeFromHandle isn't called as a managed method because it has
9293 * a funky calling sequence, e.g. ldtoken+GetTypeFromHandle gets
9294 * transformed into something else by the JIT.
9296 mono_mb_emit_ptr (mb
, &method
->klass
->byval_arg
);
9297 mono_mb_emit_icall (mb
, type_from_handle
);
9300 mono_mb_emit_ldarg (mb
, 0);
9301 mono_mb_emit_stloc (mb
, this_local
);
9303 /* Call Monitor::Enter() */
9304 mono_mb_emit_ldloc (mb
, this_local
);
9305 mono_mb_emit_managed_call (mb
, enter_method
, NULL
);
9307 clause
->try_offset
= mb
->pos
;
9309 /* Call the method */
9311 mono_mb_emit_ldarg (mb
, 0);
9312 for (i
= 0; i
< sig
->param_count
; i
++)
9313 mono_mb_emit_ldarg (mb
, i
+ (sig
->hasthis
== TRUE
));
9315 /* this is needed to avoid recursion */
9316 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
9317 mono_mb_emit_op (mb
, CEE_LDFTN
, method
);
9318 mono_mb_emit_calli (mb
, mono_method_signature (method
));
9320 if (!MONO_TYPE_IS_VOID (sig
->ret
))
9321 mono_mb_emit_stloc (mb
, ret_local
);
9323 pos
= mono_mb_emit_branch (mb
, CEE_LEAVE
);
9325 clause
->try_len
= mb
->pos
- clause
->try_offset
;
9326 clause
->handler_offset
= mb
->pos
;
9328 /* Call Monitor::Exit() */
9329 mono_mb_emit_ldloc (mb
, this_local
);
9330 /* mono_mb_emit_native_call (mb, exit_sig, mono_monitor_exit); */
9331 mono_mb_emit_managed_call (mb
, exit_method
, NULL
);
9332 mono_mb_emit_byte (mb
, CEE_ENDFINALLY
);
9334 clause
->handler_len
= mb
->pos
- clause
->handler_offset
;
9336 mono_mb_patch_branch (mb
, pos
);
9337 if (!MONO_TYPE_IS_VOID (sig
->ret
))
9338 mono_mb_emit_ldloc (mb
, ret_local
);
9339 mono_mb_emit_byte (mb
, CEE_RET
);
9341 res
= mono_mb_create_and_cache (cache
, method
,
9342 mb
, sig
, sig
->param_count
+ 16);
9345 header
= ((MonoMethodNormal
*)res
)->header
;
9346 header
->num_clauses
= 1;
9347 header
->clauses
= clause
;
9354 * the returned method calls 'method' unboxing the this argument
9357 mono_marshal_get_unbox_wrapper (MonoMethod
*method
)
9359 MonoMethodSignature
*sig
= mono_method_signature (method
);
9361 MonoMethodBuilder
*mb
;
9365 cache
= method
->klass
->image
->unbox_wrapper_cache
;
9366 if ((res
= mono_marshal_find_in_cache (cache
, method
)))
9369 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_UNBOX
);
9371 g_assert (sig
->hasthis
);
9373 mono_mb_emit_ldarg (mb
, 0);
9374 mono_mb_emit_icon (mb
, sizeof (MonoObject
));
9375 mono_mb_emit_byte (mb
, CEE_ADD
);
9376 for (i
= 0; i
< sig
->param_count
; ++i
)
9377 mono_mb_emit_ldarg (mb
, i
+ 1);
9378 mono_mb_emit_managed_call (mb
, method
, NULL
);
9379 mono_mb_emit_byte (mb
, CEE_RET
);
9381 res
= mono_mb_create_and_cache (cache
, method
,
9382 mb
, sig
, sig
->param_count
+ 16);
9385 /* printf ("CODE FOR %s: \n%s.\n", mono_method_full_name (res, TRUE), mono_disasm_code (0, res, ((MonoMethodNormal*)res)->header->code, ((MonoMethodNormal*)res)->header->code + ((MonoMethodNormal*)res)->header->code_size)); */
9391 mono_marshal_get_stelemref ()
9393 static MonoMethod
* ret
= NULL
;
9394 MonoMethodSignature
*sig
;
9395 MonoMethodBuilder
*mb
;
9397 guint32 b1
, b2
, b3
, b4
;
9400 int array_slot_addr
;
9405 mb
= mono_mb_new (mono_defaults
.object_class
, "stelemref", MONO_WRAPPER_STELEMREF
);
9408 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3);
9410 /* void stelemref (void* array, int idx, void* value) */
9411 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
9412 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
9413 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
; /* this is a natural sized int */
9414 sig
->params
[2] = &mono_defaults
.object_class
->byval_arg
;
9416 aklass
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
9417 vklass
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
9418 array_slot_addr
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->this_arg
);
9422 <ldelema (bound check)>
9426 aklass = array->vtable->klass->element_class;
9427 vklass = value->vtable->klass;
9429 if (vklass->idepth < aklass->idepth)
9432 if (vklass->supertypes [aklass->idepth - 1] != aklass)
9436 *array_slot_addr = value;
9440 if (mono_object_isinst (value, aklass))
9443 throw new ArrayTypeMismatchException ();
9446 /* ldelema (implicit bound check) */
9447 mono_mb_emit_ldarg (mb
, 0);
9448 mono_mb_emit_ldarg (mb
, 1);
9449 mono_mb_emit_op (mb
, CEE_LDELEMA
, mono_defaults
.object_class
);
9450 mono_mb_emit_stloc (mb
, array_slot_addr
);
9452 /* if (!value) goto do_store */
9453 mono_mb_emit_ldarg (mb
, 2);
9454 b1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
9456 /* aklass = array->vtable->klass->element_class */
9457 mono_mb_emit_ldarg (mb
, 0);
9458 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoObject
, vtable
));
9459 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9460 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoVTable
, klass
));
9461 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9462 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, element_class
));
9463 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9464 mono_mb_emit_stloc (mb
, aklass
);
9466 /* vklass = value->vtable->klass */
9467 mono_mb_emit_ldarg (mb
, 2);
9468 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoObject
, vtable
));
9469 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9470 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoVTable
, klass
));
9471 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9472 mono_mb_emit_stloc (mb
, vklass
);
9474 /* if (vklass->idepth < aklass->idepth) goto failue */
9475 mono_mb_emit_ldloc (mb
, vklass
);
9476 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, idepth
));
9477 mono_mb_emit_byte (mb
, CEE_LDIND_U2
);
9479 mono_mb_emit_ldloc (mb
, aklass
);
9480 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, idepth
));
9481 mono_mb_emit_byte (mb
, CEE_LDIND_U2
);
9483 b2
= mono_mb_emit_branch (mb
, CEE_BLT_UN
);
9485 /* if (vklass->supertypes [aklass->idepth - 1] != aklass) goto failure */
9486 mono_mb_emit_ldloc (mb
, vklass
);
9487 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, supertypes
));
9488 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9490 mono_mb_emit_ldloc (mb
, aklass
);
9491 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, idepth
));
9492 mono_mb_emit_byte (mb
, CEE_LDIND_U2
);
9493 mono_mb_emit_icon (mb
, 1);
9494 mono_mb_emit_byte (mb
, CEE_SUB
);
9495 mono_mb_emit_icon (mb
, sizeof (void*));
9496 mono_mb_emit_byte (mb
, CEE_MUL
);
9497 mono_mb_emit_byte (mb
, CEE_ADD
);
9498 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9500 mono_mb_emit_ldloc (mb
, aklass
);
9502 b3
= mono_mb_emit_branch (mb
, CEE_BNE_UN
);
9506 mono_mb_patch_branch (mb
, b1
);
9507 mono_mb_emit_ldloc (mb
, array_slot_addr
);
9508 mono_mb_emit_ldarg (mb
, 2);
9509 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
9511 mono_mb_emit_byte (mb
, CEE_RET
);
9514 mono_mb_patch_branch (mb
, b2
);
9515 mono_mb_patch_branch (mb
, b3
);
9517 mono_mb_emit_ldarg (mb
, 2);
9518 mono_mb_emit_ldloc (mb
, aklass
);
9519 mono_mb_emit_icall (mb
, mono_object_isinst
);
9521 b4
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
9522 mono_mb_patch_addr (mb
, b4
, copy_pos
- (b4
+ 4));
9523 mono_mb_emit_exception (mb
, "ArrayTypeMismatchException", NULL
);
9525 mono_mb_emit_byte (mb
, CEE_RET
);
9526 ret
= mono_mb_create_method (mb
, sig
, 4);
9537 /* LOCKING: vars accessed under the marshal lock */
9538 static ArrayElemAddr
*elem_addr_cache
= NULL
;
9539 static int elem_addr_cache_size
= 0;
9540 static int elem_addr_cache_next
= 0;
9543 * mono_marshal_get_array_address:
9544 * @rank: rank of the array type
9545 * @elem_size: size in bytes of an element of an array.
9547 * Returns a MonoMethd that implements the code to get the address
9548 * of an element in a multi-dimenasional array of @rank dimensions.
9549 * The returned method takes an array as the first argument and then
9550 * @rank indexes for the @rank dimensions.
9553 mono_marshal_get_array_address (int rank
, int elem_size
)
9556 MonoMethodBuilder
*mb
;
9557 MonoMethodSignature
*sig
;
9558 int i
, bounds
, ind
, realidx
;
9559 int branch_pos
, *branch_positions
;
9563 mono_marshal_lock ();
9564 for (i
= 0; i
< elem_addr_cache_next
; ++i
) {
9565 if (elem_addr_cache
[i
].rank
== rank
&& elem_addr_cache
[i
].elem_size
== elem_size
) {
9566 ret
= elem_addr_cache
[i
].method
;
9570 mono_marshal_unlock ();
9574 branch_positions
= g_new0 (int, rank
);
9576 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1 + rank
);
9578 /* void* address (void* array, int idx0, int idx1, int idx2, ...) */
9579 sig
->ret
= &mono_defaults
.int_class
->byval_arg
;
9580 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
9581 for (i
= 0; i
< rank
; ++i
) {
9582 sig
->params
[i
+ 1] = &mono_defaults
.int32_class
->byval_arg
;
9585 mb
= mono_mb_new (mono_defaults
.object_class
, "ElementAddr", MONO_WRAPPER_MANAGED_TO_MANAGED
);
9587 bounds
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
9588 ind
= mono_mb_add_local (mb
, &mono_defaults
.int32_class
->byval_arg
);
9589 realidx
= mono_mb_add_local (mb
, &mono_defaults
.int32_class
->byval_arg
);
9591 /* bounds = array->bounds; */
9592 mono_mb_emit_ldarg (mb
, 0);
9593 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoArray
, bounds
));
9594 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9595 mono_mb_emit_stloc (mb
, bounds
);
9597 /* ind is the overall element index, realidx is the partial index in a single dimension */
9598 /* ind = idx0 - bounds [0].lower_bound */
9599 mono_mb_emit_ldarg (mb
, 1);
9600 mono_mb_emit_ldloc (mb
, bounds
);
9601 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoArrayBounds
, lower_bound
));
9602 mono_mb_emit_byte (mb
, CEE_ADD
);
9603 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
9604 mono_mb_emit_byte (mb
, CEE_SUB
);
9605 mono_mb_emit_stloc (mb
, ind
);
9606 /* if (ind >= bounds [0].length) goto exeception; */
9607 mono_mb_emit_ldloc (mb
, ind
);
9608 mono_mb_emit_ldloc (mb
, bounds
);
9609 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoArrayBounds
, length
));
9610 mono_mb_emit_byte (mb
, CEE_ADD
);
9611 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
9612 /* note that we use unsigned comparison */
9613 branch_pos
= mono_mb_emit_branch (mb
, CEE_BGE_UN
);
9615 /* For large ranks (> 4?) use a loop n IL later to reduce code size.
9616 * We could also decide to ignore the passed elem_size and get it
9617 * from the array object, to reduce the number of methods we generate:
9618 * the additional cost is 3 memory loads and a non-immediate mul.
9620 for (i
= 1; i
< rank
; ++i
) {
9621 /* realidx = idxi - bounds [i].lower_bound */
9622 mono_mb_emit_ldarg (mb
, 1 + i
);
9623 mono_mb_emit_ldloc (mb
, bounds
);
9624 mono_mb_emit_icon (mb
, (i
* sizeof (MonoArrayBounds
)) + G_STRUCT_OFFSET (MonoArrayBounds
, lower_bound
));
9625 mono_mb_emit_byte (mb
, CEE_ADD
);
9626 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
9627 mono_mb_emit_byte (mb
, CEE_SUB
);
9628 mono_mb_emit_stloc (mb
, realidx
);
9629 /* if (realidx >= bounds [i].length) goto exeception; */
9630 mono_mb_emit_ldloc (mb
, realidx
);
9631 mono_mb_emit_ldloc (mb
, bounds
);
9632 mono_mb_emit_icon (mb
, (i
* sizeof (MonoArrayBounds
)) + G_STRUCT_OFFSET (MonoArrayBounds
, length
));
9633 mono_mb_emit_byte (mb
, CEE_ADD
);
9634 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
9635 branch_positions
[i
] = mono_mb_emit_branch (mb
, CEE_BGE_UN
);
9636 /* ind = ind * bounds [i].length + realidx */
9637 mono_mb_emit_ldloc (mb
, ind
);
9638 mono_mb_emit_ldloc (mb
, bounds
);
9639 mono_mb_emit_icon (mb
, (i
* sizeof (MonoArrayBounds
)) + G_STRUCT_OFFSET (MonoArrayBounds
, length
));
9640 mono_mb_emit_byte (mb
, CEE_ADD
);
9641 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
9642 mono_mb_emit_byte (mb
, CEE_MUL
);
9643 mono_mb_emit_ldloc (mb
, realidx
);
9644 mono_mb_emit_byte (mb
, CEE_ADD
);
9645 mono_mb_emit_stloc (mb
, ind
);
9648 /* return array->vector + ind * element_size */
9649 mono_mb_emit_ldarg (mb
, 0);
9650 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoArray
, vector
));
9651 mono_mb_emit_ldloc (mb
, ind
);
9652 mono_mb_emit_icon (mb
, elem_size
);
9653 mono_mb_emit_byte (mb
, CEE_MUL
);
9654 mono_mb_emit_byte (mb
, CEE_ADD
);
9655 mono_mb_emit_byte (mb
, CEE_RET
);
9657 /* patch the branches to get here and throw */
9658 for (i
= 1; i
< rank
; ++i
) {
9659 mono_mb_patch_branch (mb
, branch_positions
[i
]);
9661 mono_mb_patch_branch (mb
, branch_pos
);
9662 /* throw exception */
9663 mono_mb_emit_exception (mb
, "IndexOutOfRangeException", NULL
);
9665 g_free (branch_positions
);
9666 ret
= mono_mb_create_method (mb
, sig
, 4);
9669 /* cache the result */
9671 mono_marshal_lock ();
9672 for (i
= 0; i
< elem_addr_cache_next
; ++i
) {
9673 if (elem_addr_cache
[i
].rank
== rank
&& elem_addr_cache
[i
].elem_size
== elem_size
) {
9674 /* FIXME: free ret */
9675 ret
= elem_addr_cache
[i
].method
;
9681 if (elem_addr_cache_next
>= elem_addr_cache_size
) {
9682 int new_size
= elem_addr_cache_size
+ 4;
9683 ArrayElemAddr
*new_array
= g_new0 (ArrayElemAddr
, new_size
);
9684 memcpy (new_array
, elem_addr_cache
, elem_addr_cache_size
* sizeof (ArrayElemAddr
));
9685 g_free (elem_addr_cache
);
9686 elem_addr_cache
= new_array
;
9687 elem_addr_cache_size
= new_size
;
9689 elem_addr_cache
[elem_addr_cache_next
].rank
= rank
;
9690 elem_addr_cache
[elem_addr_cache_next
].elem_size
= elem_size
;
9691 elem_addr_cache
[elem_addr_cache_next
].method
= ret
;
9693 mono_marshal_unlock ();
9698 mono_marshal_get_write_barrier (void)
9700 static MonoMethod
* ret
= NULL
;
9701 MonoMethodSignature
*sig
;
9702 MonoMethodBuilder
*mb
;
9708 mb
= mono_mb_new (mono_defaults
.object_class
, "writebarrier", MONO_WRAPPER_WRITE_BARRIER
);
9710 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 2);
9712 /* void writebarrier (MonoObject** addr, MonoObject* obj) */
9713 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
9714 sig
->params
[0] = &mono_defaults
.object_class
->this_arg
;
9715 sig
->params
[1] = &mono_defaults
.object_class
->byval_arg
;
9717 /* just the store right now: add an hook for the GC to use, maybe something
9718 * that can be used for stelemref as well
9719 * We need a write barrier variant to be used with struct copies as well, though
9720 * there are also other approaches possible, like writing a wrapper specific to
9721 * the struct or to the reference pattern in the struct...
9722 * Depending on the GC, we may want variants that take the object we store to
9723 * when it is available.
9725 mono_mb_emit_ldarg (mb
, 0);
9726 mono_mb_emit_ldarg (mb
, 1);
9727 mono_mb_emit_icall (mb
, mono_gc_wbarrier_generic_store
);
9728 /*mono_mb_emit_byte (mb, CEE_STIND_REF);*/
9730 mono_mb_emit_byte (mb
, CEE_RET
);
9732 ret
= mono_mb_create_method (mb
, sig
, max_stack
);
9738 mono_marshal_alloc (gulong size
)
9742 #ifdef PLATFORM_WIN32
9743 res
= CoTaskMemAlloc (size
);
9745 res
= g_try_malloc ((gulong
)size
);
9747 mono_gc_out_of_memory ((gulong
)size
);
9753 mono_marshal_free (gpointer ptr
)
9755 #ifdef PLATFORM_WIN32
9756 CoTaskMemFree (ptr
);
9763 mono_marshal_free_array (gpointer
*ptr
, int size
)
9770 for (i
= 0; i
< size
; i
++)
9776 mono_marshal_string_to_utf16 (MonoString
*s
)
9778 return s
? mono_string_chars (s
) : NULL
;
9782 * mono_marshal_set_last_error:
9784 * This function is invoked to set the last error value from a P/Invoke call
9785 * which has SetLastError set.
9788 mono_marshal_set_last_error (void)
9791 TlsSetValue (last_error_tls_id
, GINT_TO_POINTER (GetLastError ()));
9793 TlsSetValue (last_error_tls_id
, GINT_TO_POINTER (errno
));
9798 mono_marshal_set_last_error_windows (int error
)
9801 TlsSetValue (last_error_tls_id
, GINT_TO_POINTER (error
));
9806 ves_icall_System_Runtime_InteropServices_Marshal_copy_to_unmanaged (MonoArray
*src
, gint32 start_index
,
9807 gpointer dest
, gint32 length
)
9812 MONO_ARCH_SAVE_REGS
;
9814 MONO_CHECK_ARG_NULL (src
);
9815 MONO_CHECK_ARG_NULL (dest
);
9817 if (src
->obj
.vtable
->klass
->rank
!= 1)
9818 mono_raise_exception (mono_get_exception_argument ("array", "array is multi-dimensional"));
9819 if (start_index
< 0)
9820 mono_raise_exception (mono_get_exception_argument ("startIndex", "Must be >= 0"));
9822 mono_raise_exception (mono_get_exception_argument ("length", "Must be >= 0"));
9823 if (start_index
+ length
> mono_array_length (src
))
9824 mono_raise_exception (mono_get_exception_argument ("length", "start_index + length > array length"));
9826 element_size
= mono_array_element_size (src
->obj
.vtable
->klass
);
9828 /* no references should be involved */
9829 source_addr
= mono_array_addr_with_size (src
, element_size
, start_index
);
9831 memcpy (dest
, source_addr
, length
* element_size
);
9835 ves_icall_System_Runtime_InteropServices_Marshal_copy_from_unmanaged (gpointer src
, gint32 start_index
,
9836 MonoArray
*dest
, gint32 length
)
9841 MONO_ARCH_SAVE_REGS
;
9843 MONO_CHECK_ARG_NULL (src
);
9844 MONO_CHECK_ARG_NULL (dest
);
9846 if (dest
->obj
.vtable
->klass
->rank
!= 1)
9847 mono_raise_exception (mono_get_exception_argument ("array", "array is multi-dimensional"));
9848 if (start_index
< 0)
9849 mono_raise_exception (mono_get_exception_argument ("startIndex", "Must be >= 0"));
9851 mono_raise_exception (mono_get_exception_argument ("length", "Must be >= 0"));
9852 if (start_index
+ length
> mono_array_length (dest
))
9853 mono_raise_exception (mono_get_exception_argument ("length", "start_index + length > array length"));
9855 element_size
= mono_array_element_size (dest
->obj
.vtable
->klass
);
9857 /* no references should be involved */
9858 dest_addr
= mono_array_addr_with_size (dest
, element_size
, start_index
);
9860 memcpy (dest_addr
, src
, length
* element_size
);
9863 #if NO_UNALIGNED_ACCESS
9864 #define RETURN_UNALIGNED(type, addr) \
9867 memcpy(&val, p + offset, sizeof(val)); \
9870 #define WRITE_UNALIGNED(type, addr, val) \
9871 memcpy(addr, &val, sizeof(type))
9873 #define RETURN_UNALIGNED(type, addr) \
9874 return *(type*)(p + offset);
9875 #define WRITE_UNALIGNED(type, addr, val) \
9876 (*(type *)(addr) = (val))
9880 ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr (gpointer ptr
, gint32 offset
)
9884 MONO_ARCH_SAVE_REGS
;
9886 RETURN_UNALIGNED(gpointer
, p
+ offset
);
9890 ves_icall_System_Runtime_InteropServices_Marshal_ReadByte (gpointer ptr
, gint32 offset
)
9894 MONO_ARCH_SAVE_REGS
;
9896 return *(unsigned char*)(p
+ offset
);
9900 ves_icall_System_Runtime_InteropServices_Marshal_ReadInt16 (gpointer ptr
, gint32 offset
)
9904 MONO_ARCH_SAVE_REGS
;
9906 RETURN_UNALIGNED(gint16
, p
+ offset
);
9910 ves_icall_System_Runtime_InteropServices_Marshal_ReadInt32 (gpointer ptr
, gint32 offset
)
9914 MONO_ARCH_SAVE_REGS
;
9916 RETURN_UNALIGNED(gint32
, p
+ offset
);
9920 ves_icall_System_Runtime_InteropServices_Marshal_ReadInt64 (gpointer ptr
, gint32 offset
)
9924 MONO_ARCH_SAVE_REGS
;
9926 RETURN_UNALIGNED(gint64
, p
+ offset
);
9930 ves_icall_System_Runtime_InteropServices_Marshal_WriteByte (gpointer ptr
, gint32 offset
, unsigned char val
)
9934 MONO_ARCH_SAVE_REGS
;
9936 *(unsigned char*)(p
+ offset
) = val
;
9940 ves_icall_System_Runtime_InteropServices_Marshal_WriteIntPtr (gpointer ptr
, gint32 offset
, gpointer val
)
9944 MONO_ARCH_SAVE_REGS
;
9946 WRITE_UNALIGNED(gpointer
, p
+ offset
, val
);
9950 ves_icall_System_Runtime_InteropServices_Marshal_WriteInt16 (gpointer ptr
, gint32 offset
, gint16 val
)
9954 MONO_ARCH_SAVE_REGS
;
9956 WRITE_UNALIGNED(gint16
, p
+ offset
, val
);
9960 ves_icall_System_Runtime_InteropServices_Marshal_WriteInt32 (gpointer ptr
, gint32 offset
, gint32 val
)
9964 MONO_ARCH_SAVE_REGS
;
9966 WRITE_UNALIGNED(gint32
, p
+ offset
, val
);
9970 ves_icall_System_Runtime_InteropServices_Marshal_WriteInt64 (gpointer ptr
, gint32 offset
, gint64 val
)
9974 MONO_ARCH_SAVE_REGS
;
9976 WRITE_UNALIGNED(gint64
, p
+ offset
, val
);
9980 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi (char *ptr
)
9982 MONO_ARCH_SAVE_REGS
;
9987 return mono_string_new (mono_domain_get (), ptr
);
9991 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi_len (char *ptr
, gint32 len
)
9993 MONO_ARCH_SAVE_REGS
;
9996 mono_raise_exception (mono_get_exception_argument_null ("ptr"));
9997 g_assert_not_reached ();
10000 return mono_string_new_len (mono_domain_get (), ptr
, len
);
10005 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni (guint16
*ptr
)
10007 MonoDomain
*domain
= mono_domain_get ();
10011 MONO_ARCH_SAVE_REGS
;
10019 return mono_string_new_utf16 (domain
, ptr
, len
);
10023 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni_len (guint16
*ptr
, gint32 len
)
10025 MonoDomain
*domain
= mono_domain_get ();
10027 MONO_ARCH_SAVE_REGS
;
10030 mono_raise_exception (mono_get_exception_argument_null ("ptr"));
10031 g_assert_not_reached ();
10034 return mono_string_new_utf16 (domain
, ptr
, len
);
10039 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringBSTR (gpointer ptr
)
10041 MONO_ARCH_SAVE_REGS
;
10043 return mono_string_from_bstr(ptr
);
10047 ves_icall_System_Runtime_InteropServices_Marshal_StringToBSTR (MonoString
* ptr
)
10049 MONO_ARCH_SAVE_REGS
;
10051 return mono_string_to_bstr(ptr
);
10056 int (STDCALL
*QueryInterface
)(gpointer pUnk
, gpointer riid
, gpointer
* ppv
);
10057 int (STDCALL
*AddRef
)(gpointer pUnk
);
10058 int (STDCALL
*Release
)(gpointer pUnk
);
10062 ves_icall_System_Runtime_InteropServices_Marshal_FreeBSTR (gpointer ptr
)
10064 MONO_ARCH_SAVE_REGS
;
10066 mono_free_bstr (ptr
);
10070 ves_icall_System_Runtime_InteropServices_Marshal_AddRefInternal (gpointer pUnk
)
10073 return (*(MonoIUnknown
**)pUnk
)->AddRef(pUnk
);
10077 ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (gpointer pUnk
, gpointer riid
, gpointer
* ppv
)
10080 return (*(MonoIUnknown
**)pUnk
)->QueryInterface(pUnk
, riid
, ppv
);
10084 ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (gpointer pUnk
)
10087 return (*(MonoIUnknown
**)pUnk
)->Release(pUnk
);
10091 cominterop_get_idispatch_for_object (MonoObject
* object
)
10096 if (cominterop_object_is_rcw (object
)) {
10097 /* FIXME: Implement this case */
10098 g_assert_not_reached ();
10102 return cominterop_get_ccw (object
, mono_defaults
.idispatch_class
);
10107 ves_icall_System_Runtime_InteropServices_Marshal_GetIUnknownForObjectInternal (MonoObject
* object
)
10112 if (cominterop_object_is_rcw (object
)) {
10113 MonoClass
*klass
= NULL
;
10114 MonoRealProxy
* real_proxy
= NULL
;
10117 klass
= mono_object_class (object
);
10118 if (klass
!= mono_defaults
.transparent_proxy_class
) {
10119 g_assert_not_reached ();
10123 real_proxy
= ((MonoTransparentProxy
*)object
)->rp
;
10125 g_assert_not_reached ();
10129 klass
= mono_object_class (real_proxy
);
10130 if (klass
!= mono_defaults
.com_interop_proxy_class
) {
10131 g_assert_not_reached ();
10135 if (!((MonoComInteropProxy
*)real_proxy
)->com_object
) {
10136 g_assert_not_reached ();
10140 return ((MonoComInteropProxy
*)real_proxy
)->com_object
->iunknown
;
10143 return cominterop_get_ccw (object
, mono_defaults
.iunknown_class
);
10148 ves_icall_System_Runtime_InteropServices_Marshal_GetObjectForCCW (void* pUnk
)
10150 MonoObject
* object
= NULL
;
10155 /* see if it is a CCW */
10156 object
= cominterop_get_ccw_object ((MonoCCWInterface
*)pUnk
, TRUE
);
10162 ves_icall_System_Runtime_InteropServices_Marshal_GetIDispatchForObjectInternal (MonoObject
* object
)
10164 return cominterop_get_idispatch_for_object (object
);
10168 ves_icall_System_Runtime_InteropServices_Marshal_GetCCW (MonoObject
* object
, MonoReflectionType
* type
)
10170 MonoClass
* klass
= NULL
;
10173 g_assert (type
->type
);
10174 klass
= mono_type_get_class (type
->type
);
10176 itf
= cominterop_get_ccw (object
, klass
);
10183 ves_icall_System_Runtime_InteropServices_Marshal_IsComObject (MonoObject
* object
)
10185 return (MonoBoolean
)cominterop_object_is_rcw (object
);
10189 ves_icall_System_Runtime_InteropServices_Marshal_ReleaseComObjectInternal (MonoObject
* object
)
10191 MonoComInteropProxy
* proxy
= NULL
;
10192 gint32 ref_count
= 0;
10195 g_assert (cominterop_object_is_rcw (object
));
10197 proxy
= (MonoComInteropProxy
*)((MonoTransparentProxy
*)object
)->rp
;
10200 ref_count
= InterlockedDecrement (&proxy
->ref_count
);
10201 g_assert (ref_count
>= 0);
10203 if (ref_count
== 0)
10204 ves_icall_System_ComObject_ReleaseInterfaces (proxy
->com_object
);
10210 ves_icall_System_Runtime_InteropServices_Marshal_GetComSlotForMethodInfoInternal (MonoReflectionMethod
*m
)
10212 MONO_ARCH_SAVE_REGS
;
10214 return cominterop_get_com_slot_for_method (m
->method
);
10218 ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error (void)
10220 MONO_ARCH_SAVE_REGS
;
10222 return (GPOINTER_TO_INT (TlsGetValue (last_error_tls_id
)));
10226 ves_icall_System_Runtime_InteropServices_Marshal_SizeOf (MonoReflectionType
*rtype
)
10232 MONO_ARCH_SAVE_REGS
;
10234 MONO_CHECK_ARG_NULL (rtype
);
10236 type
= rtype
->type
;
10237 klass
= mono_class_from_mono_type (type
);
10238 layout
= (klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
);
10240 if (layout
== TYPE_ATTRIBUTE_AUTO_LAYOUT
) {
10242 MonoException
*exc
;
10244 msg
= g_strdup_printf ("Type %s cannot be marshaled as an unmanaged structure.", klass
->name
);
10245 exc
= mono_get_exception_argument ("t", msg
);
10247 mono_raise_exception (exc
);
10251 return mono_class_native_size (klass
, NULL
);
10255 ves_icall_System_Runtime_InteropServices_Marshal_StructureToPtr (MonoObject
*obj
, gpointer dst
, MonoBoolean delete_old
)
10257 MonoMethod
*method
;
10260 MONO_ARCH_SAVE_REGS
;
10262 MONO_CHECK_ARG_NULL (obj
);
10263 MONO_CHECK_ARG_NULL (dst
);
10265 method
= mono_marshal_get_struct_to_ptr (obj
->vtable
->klass
);
10269 pa
[2] = &delete_old
;
10271 mono_runtime_invoke (method
, NULL
, pa
, NULL
);
10275 ptr_to_structure (gpointer src
, MonoObject
*dst
)
10277 MonoMethod
*method
;
10280 method
= mono_marshal_get_ptr_to_struct (dst
->vtable
->klass
);
10285 mono_runtime_invoke (method
, NULL
, pa
, NULL
);
10289 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure (gpointer src
, MonoObject
*dst
)
10293 MONO_ARCH_SAVE_REGS
;
10295 MONO_CHECK_ARG_NULL (src
);
10296 MONO_CHECK_ARG_NULL (dst
);
10298 t
= mono_type_get_underlying_type (mono_class_get_type (dst
->vtable
->klass
));
10300 if (t
->type
== MONO_TYPE_VALUETYPE
) {
10301 MonoException
*exc
;
10304 tmp
= g_strdup_printf ("Destination is a boxed value type.");
10305 exc
= mono_get_exception_argument ("dst", tmp
);
10308 mono_raise_exception (exc
);
10312 ptr_to_structure (src
, dst
);
10316 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure_type (gpointer src
, MonoReflectionType
*type
)
10318 MonoDomain
*domain
= mono_domain_get ();
10321 MONO_ARCH_SAVE_REGS
;
10323 MONO_CHECK_ARG_NULL (src
);
10324 MONO_CHECK_ARG_NULL (type
);
10326 res
= mono_object_new (domain
, mono_class_from_mono_type (type
->type
));
10328 ptr_to_structure (src
, res
);
10334 ves_icall_System_Runtime_InteropServices_Marshal_OffsetOf (MonoReflectionType
*type
, MonoString
*field_name
)
10336 MonoMarshalType
*info
;
10339 int match_index
= -1;
10341 MONO_ARCH_SAVE_REGS
;
10343 MONO_CHECK_ARG_NULL (type
);
10344 MONO_CHECK_ARG_NULL (field_name
);
10346 fname
= mono_string_to_utf8 (field_name
);
10347 klass
= mono_class_from_mono_type (type
->type
);
10349 while (klass
&& match_index
== -1) {
10350 MonoClassField
* field
;
10352 gpointer iter
= NULL
;
10353 while ((field
= mono_class_get_fields (klass
, &iter
))) {
10354 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
10356 if (!strcmp (fname
, field
->name
)) {
10363 if (match_index
== -1)
10364 klass
= klass
->parent
;
10369 if(match_index
== -1) {
10370 MonoException
* exc
;
10373 /* Get back original class instance */
10374 klass
= mono_class_from_mono_type (type
->type
);
10376 tmp
= g_strdup_printf ("Field passed in is not a marshaled member of the type %s", klass
->name
);
10377 exc
= mono_get_exception_argument ("fieldName", tmp
);
10380 mono_raise_exception ((MonoException
*)exc
);
10383 info
= mono_marshal_load_type_info (klass
);
10384 return info
->fields
[match_index
].offset
;
10388 ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi (MonoString
*string
)
10390 MONO_ARCH_SAVE_REGS
;
10392 return mono_string_to_utf8 (string
);
10396 ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalUni (MonoString
*string
)
10398 MONO_ARCH_SAVE_REGS
;
10400 if (string
== NULL
)
10403 gunichar2
*res
= g_malloc ((mono_string_length (string
) + 1) * 2);
10404 memcpy (res
, mono_string_chars (string
), mono_string_length (string
) * 2);
10405 res
[mono_string_length (string
)] = 0;
10411 mono_struct_delete_old (MonoClass
*klass
, char *ptr
)
10413 MonoMarshalType
*info
;
10416 info
= mono_marshal_load_type_info (klass
);
10418 for (i
= 0; i
< info
->num_fields
; i
++) {
10419 MonoMarshalNative ntype
;
10420 MonoMarshalConv conv
;
10421 MonoType
*ftype
= info
->fields
[i
].field
->type
;
10424 if (ftype
->attrs
& FIELD_ATTRIBUTE_STATIC
)
10427 ntype
= mono_type_to_unmanaged (ftype
, info
->fields
[i
].mspec
, TRUE
,
10428 klass
->unicode
, &conv
);
10430 cpos
= ptr
+ info
->fields
[i
].offset
;
10433 case MONO_MARSHAL_CONV_NONE
:
10434 if (MONO_TYPE_ISSTRUCT (ftype
)) {
10435 mono_struct_delete_old (ftype
->data
.klass
, cpos
);
10439 case MONO_MARSHAL_CONV_STR_LPWSTR
:
10440 /* We assume this field points inside a MonoString */
10442 case MONO_MARSHAL_CONV_STR_LPSTR
:
10443 case MONO_MARSHAL_CONV_STR_LPTSTR
:
10444 case MONO_MARSHAL_CONV_STR_BSTR
:
10445 case MONO_MARSHAL_CONV_STR_ANSIBSTR
:
10446 case MONO_MARSHAL_CONV_STR_TBSTR
:
10447 mono_marshal_free (*(gpointer
*)cpos
);
10457 ves_icall_System_Runtime_InteropServices_Marshal_DestroyStructure (gpointer src
, MonoReflectionType
*type
)
10461 MONO_ARCH_SAVE_REGS
;
10463 MONO_CHECK_ARG_NULL (src
);
10464 MONO_CHECK_ARG_NULL (type
);
10466 klass
= mono_class_from_mono_type (type
->type
);
10468 mono_struct_delete_old (klass
, (char *)src
);
10472 ves_icall_System_Runtime_InteropServices_Marshal_AllocHGlobal (int size
)
10476 MONO_ARCH_SAVE_REGS
;
10478 if ((gulong
)size
== 0)
10479 /* This returns a valid pointer for size 0 on MS.NET */
10482 #ifdef PLATFORM_WIN32
10483 res
= GlobalAlloc (GMEM_FIXED
, (gulong
)size
);
10485 res
= g_try_malloc ((gulong
)size
);
10488 mono_gc_out_of_memory ((gulong
)size
);
10494 ves_icall_System_Runtime_InteropServices_Marshal_ReAllocHGlobal (gpointer ptr
, int size
)
10499 mono_gc_out_of_memory ((gulong
)size
);
10503 #ifdef PLATFORM_WIN32
10504 res
= GlobalReAlloc (ptr
, (gulong
)size
, 0);
10506 res
= g_try_realloc (ptr
, (gulong
)size
);
10509 mono_gc_out_of_memory ((gulong
)size
);
10515 ves_icall_System_Runtime_InteropServices_Marshal_FreeHGlobal (void *ptr
)
10517 MONO_ARCH_SAVE_REGS
;
10519 #ifdef PLATFORM_WIN32
10527 ves_icall_System_Runtime_InteropServices_Marshal_AllocCoTaskMem (int size
)
10529 MONO_ARCH_SAVE_REGS
;
10531 #ifdef PLATFORM_WIN32
10532 return CoTaskMemAlloc (size
);
10534 return g_try_malloc ((gulong
)size
);
10539 ves_icall_System_Runtime_InteropServices_Marshal_FreeCoTaskMem (void *ptr
)
10541 MONO_ARCH_SAVE_REGS
;
10543 #ifdef PLATFORM_WIN32
10544 CoTaskMemFree (ptr
);
10551 ves_icall_System_Runtime_InteropServices_Marshal_ReAllocCoTaskMem (gpointer ptr
, int size
)
10553 MONO_ARCH_SAVE_REGS
;
10555 #ifdef PLATFORM_WIN32
10556 return CoTaskMemRealloc (ptr
, size
);
10558 return g_try_realloc (ptr
, (gulong
)size
);
10563 ves_icall_System_Runtime_InteropServices_Marshal_UnsafeAddrOfPinnedArrayElement (MonoArray
*arrayobj
, int index
)
10565 return mono_array_addr_with_size (arrayobj
, mono_array_element_size (arrayobj
->obj
.vtable
->klass
), index
);
10569 ves_icall_System_Runtime_InteropServices_Marshal_GetDelegateForFunctionPointerInternal (void *ftn
, MonoReflectionType
*type
)
10571 return mono_ftnptr_to_delegate (mono_type_get_class (type
->type
), ftn
);
10574 /* Only used for COM RCWs */
10576 ves_icall_System_ComObject_CreateRCW (MonoReflectionType
*type
)
10579 MonoDomain
*domain
;
10582 MONO_ARCH_SAVE_REGS
;
10584 domain
= mono_object_domain (type
);
10585 klass
= mono_class_from_mono_type (type
->type
);
10587 /* call mono_object_new_alloc_specific instead of mono_object_new
10588 * because we want to actually create object. mono_object_new checks
10589 * to see if type is import and creates transparent proxy. this method
10590 * is called by the corresponding real proxy to create the real RCW.
10591 * Constructor does not need to be called. Will be called later.
10593 obj
= mono_object_new_alloc_specific (mono_class_vtable (domain
, klass
));
10598 cominterop_finalizer (gpointer key
, gpointer value
, gpointer user_data
)
10600 ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (value
);
10605 ves_icall_System_ComObject_ReleaseInterfaces (MonoComObject
* obj
)
10608 if (obj
->itf_hash
) {
10609 guint32 gchandle
= 0;
10610 mono_cominterop_lock ();
10611 gchandle
= GPOINTER_TO_UINT (g_hash_table_lookup (rcw_hash
, obj
->iunknown
));
10613 mono_gchandle_free (gchandle
);
10614 g_hash_table_remove (rcw_hash
, obj
->iunknown
);
10617 g_hash_table_foreach_remove (obj
->itf_hash
, cominterop_finalizer
, NULL
);
10618 ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (obj
->iunknown
);
10619 obj
->itf_hash
= obj
->iunknown
= NULL
;
10620 mono_cominterop_unlock ();
10625 ves_icall_System_ComObject_FindInterface (MonoComObject
* obj
, MonoReflectionType
* type
)
10628 gpointer itf
= NULL
;
10631 if (!obj
->itf_hash
)
10634 klass
= mono_object_class (obj
);
10635 klass
= mono_class_from_mono_type (type
->type
);
10637 mono_cominterop_lock ();
10638 itf
= g_hash_table_lookup (obj
->itf_hash
, GUINT_TO_POINTER ((guint
)klass
->interface_id
));
10639 mono_cominterop_unlock ();
10645 ves_icall_System_ComObject_AddInterface (MonoComObject
* obj
, MonoReflectionType
* type
, gpointer pItf
)
10650 if (!obj
->itf_hash
) {
10651 mono_cominterop_lock ();
10652 obj
->itf_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
10653 mono_cominterop_unlock ();
10656 klass
= mono_object_class (obj
);
10657 klass
= mono_class_from_mono_type (type
->type
);
10659 mono_cominterop_lock ();
10660 g_hash_table_insert (obj
->itf_hash
, GUINT_TO_POINTER ((guint
)klass
->interface_id
), pItf
);
10661 mono_cominterop_unlock ();
10665 ves_icall_Mono_Interop_ComInteropProxy_AddProxy (gpointer pUnk
, MonoComInteropProxy
* proxy
)
10667 guint32 gchandle
= 0;
10669 mono_cominterop_lock ();
10670 rcw_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
10671 mono_cominterop_unlock ();
10674 gchandle
= mono_gchandle_new_weakref ((MonoObject
*)proxy
, FALSE
);
10676 mono_cominterop_lock ();
10677 g_hash_table_insert (rcw_hash
, pUnk
, GUINT_TO_POINTER (gchandle
));
10678 mono_cominterop_unlock ();
10681 MonoComInteropProxy
*
10682 ves_icall_Mono_Interop_ComInteropProxy_FindProxy (gpointer pUnk
)
10684 MonoComInteropProxy
* proxy
= NULL
;
10685 guint32 gchandle
= 0;
10687 mono_cominterop_lock ();
10689 gchandle
= GPOINTER_TO_UINT (g_hash_table_lookup (rcw_hash
, pUnk
));
10690 mono_cominterop_unlock ();
10692 proxy
= (MonoComInteropProxy
*)mono_gchandle_get_target (gchandle
);
10693 /* proxy is null means we need to free up old RCW */
10695 mono_gchandle_free (gchandle
);
10696 g_hash_table_remove (rcw_hash
, pUnk
);
10703 * mono_marshal_is_loading_type_info:
10705 * Return whenever mono_marshal_load_type_info () is being executed for KLASS by this
10709 mono_marshal_is_loading_type_info (MonoClass
*klass
)
10711 GSList
*loads_list
= TlsGetValue (load_type_info_tls_id
);
10713 return g_slist_find (loads_list
, klass
) != NULL
;
10717 * mono_marshal_load_type_info:
10719 * Initialize klass->marshal_info using information from metadata. This function can
10720 * recursively call itself, and the caller is responsible to avoid that by calling
10721 * mono_marshal_is_loading_type_info () beforehand.
10723 * LOCKING: Acquires the loader lock.
10726 mono_marshal_load_type_info (MonoClass
* klass
)
10729 guint32 native_size
= 0, min_align
= 1;
10730 MonoMarshalType
*info
;
10731 MonoClassField
* field
;
10734 GSList
*loads_list
;
10736 g_assert (klass
!= NULL
);
10738 if (klass
->marshal_info
)
10739 return klass
->marshal_info
;
10741 if (!klass
->inited
)
10742 mono_class_init (klass
);
10744 mono_loader_lock ();
10746 if (klass
->marshal_info
) {
10747 mono_loader_unlock ();
10748 return klass
->marshal_info
;
10752 * This function can recursively call itself, so we keep the list of classes which are
10753 * under initialization in a TLS list.
10755 g_assert (!mono_marshal_is_loading_type_info (klass
));
10756 loads_list
= TlsGetValue (load_type_info_tls_id
);
10757 loads_list
= g_slist_prepend (loads_list
, klass
);
10758 TlsSetValue (load_type_info_tls_id
, loads_list
);
10761 while ((field
= mono_class_get_fields (klass
, &iter
))) {
10762 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
10764 if (mono_field_is_deleted (field
))
10769 layout
= klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
;
10771 /* The mempool is protected by the loader lock */
10772 info
= mono_mempool_alloc0 (klass
->image
->mempool
, sizeof (MonoMarshalType
) + sizeof (MonoMarshalField
) * count
);
10773 info
->num_fields
= count
;
10775 /* Try to find a size for this type in metadata */
10776 mono_metadata_packing_from_typedef (klass
->image
, klass
->type_token
, NULL
, &native_size
);
10778 if (klass
->parent
) {
10779 int parent_size
= mono_class_native_size (klass
->parent
, NULL
);
10781 /* Add parent size to real size */
10782 native_size
+= parent_size
;
10783 info
->native_size
= parent_size
;
10788 while ((field
= mono_class_get_fields (klass
, &iter
))) {
10792 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
10795 if (mono_field_is_deleted (field
))
10797 if (field
->type
->attrs
& FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL
)
10798 mono_metadata_field_info (klass
->image
, mono_metadata_token_index (mono_class_get_field_token (field
)) - 1,
10799 NULL
, NULL
, &info
->fields
[j
].mspec
);
10801 info
->fields
[j
].field
= field
;
10803 if ((mono_class_num_fields (klass
) == 1) && (klass
->instance_size
== sizeof (MonoObject
)) &&
10804 (strcmp (field
->name
, "$PRIVATE$") == 0)) {
10805 /* This field is a hack inserted by MCS to empty structures */
10810 case TYPE_ATTRIBUTE_AUTO_LAYOUT
:
10811 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT
:
10812 size
= mono_marshal_type_size (field
->type
, info
->fields
[j
].mspec
,
10813 &align
, TRUE
, klass
->unicode
);
10814 align
= klass
->packing_size
? MIN (klass
->packing_size
, align
): align
;
10815 min_align
= MAX (align
, min_align
);
10816 info
->fields
[j
].offset
= info
->native_size
;
10817 info
->fields
[j
].offset
+= align
- 1;
10818 info
->fields
[j
].offset
&= ~(align
- 1);
10819 info
->native_size
= info
->fields
[j
].offset
+ size
;
10821 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
:
10822 size
= mono_marshal_type_size (field
->type
, info
->fields
[j
].mspec
,
10823 &align
, TRUE
, klass
->unicode
);
10824 align
= klass
->packing_size
? MIN (klass
->packing_size
, align
): align
;
10825 min_align
= MAX (align
, min_align
);
10826 info
->fields
[j
].offset
= field
->offset
- sizeof (MonoObject
);
10827 info
->native_size
= MAX (info
->native_size
, info
->fields
[j
].offset
+ size
);
10833 if(layout
!= TYPE_ATTRIBUTE_AUTO_LAYOUT
) {
10834 info
->native_size
= MAX (native_size
, info
->native_size
);
10837 if (info
->native_size
& (min_align
- 1)) {
10838 info
->native_size
+= min_align
- 1;
10839 info
->native_size
&= ~(min_align
- 1);
10842 /* Update the class's blittable info, if the layouts don't match */
10843 if (info
->native_size
!= mono_class_value_size (klass
, NULL
))
10844 klass
->blittable
= FALSE
;
10846 /* If this is an array type, ensure that we have element info */
10847 if (klass
->element_class
&& !mono_marshal_is_loading_type_info (klass
->element_class
)) {
10848 mono_marshal_load_type_info (klass
->element_class
);
10851 loads_list
= TlsGetValue (load_type_info_tls_id
);
10852 loads_list
= g_slist_remove (loads_list
, klass
);
10853 TlsSetValue (load_type_info_tls_id
, loads_list
);
10855 klass
->marshal_info
= info
;
10857 mono_loader_unlock ();
10859 return klass
->marshal_info
;
10863 * mono_class_native_size:
10866 * Returns: the native size of an object instance (when marshaled
10867 * to unmanaged code)
10870 mono_class_native_size (MonoClass
*klass
, guint32
*align
)
10872 if (!klass
->marshal_info
) {
10873 if (mono_marshal_is_loading_type_info (klass
))
10876 mono_marshal_load_type_info (klass
);
10880 *align
= klass
->min_align
;
10882 return klass
->marshal_info
->native_size
;
10886 * mono_type_native_stack_size:
10887 * @t: the type to return the size it uses on the stack
10889 * Returns: the number of bytes required to hold an instance of this
10890 * type on the native stack
10893 mono_type_native_stack_size (MonoType
*t
, guint32
*align
)
10897 g_assert (t
!= NULL
);
10908 case MONO_TYPE_BOOLEAN
:
10909 case MONO_TYPE_CHAR
:
10918 case MONO_TYPE_STRING
:
10919 case MONO_TYPE_OBJECT
:
10920 case MONO_TYPE_CLASS
:
10921 case MONO_TYPE_SZARRAY
:
10922 case MONO_TYPE_PTR
:
10923 case MONO_TYPE_FNPTR
:
10924 case MONO_TYPE_ARRAY
:
10925 case MONO_TYPE_TYPEDBYREF
:
10936 case MONO_TYPE_VALUETYPE
: {
10939 if (t
->data
.klass
->enumtype
)
10940 return mono_type_native_stack_size (t
->data
.klass
->enum_basetype
, align
);
10942 size
= mono_class_native_size (t
->data
.klass
, align
);
10943 *align
= *align
+ 3;
10953 g_error ("type 0x%02x unknown", t
->type
);
10958 /* __alignof__ returns the preferred alignment of values not the actual alignment used by
10959 the compiler so is wrong e.g. for Linux where doubles are aligned on a 4 byte boundary
10960 but __alignof__ returns 8 - using G_STRUCT_OFFSET works better */
10961 #define ALIGNMENT(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
10964 mono_marshal_type_size (MonoType
*type
, MonoMarshalSpec
*mspec
, guint32
*align
,
10965 gboolean as_field
, gboolean unicode
)
10967 MonoMarshalNative native_type
= mono_type_to_unmanaged (type
, mspec
, as_field
, unicode
, NULL
);
10970 switch (native_type
) {
10971 case MONO_NATIVE_BOOLEAN
:
10974 case MONO_NATIVE_I1
:
10975 case MONO_NATIVE_U1
:
10978 case MONO_NATIVE_I2
:
10979 case MONO_NATIVE_U2
:
10980 case MONO_NATIVE_VARIANTBOOL
:
10983 case MONO_NATIVE_I4
:
10984 case MONO_NATIVE_U4
:
10985 case MONO_NATIVE_ERROR
:
10988 case MONO_NATIVE_I8
:
10989 case MONO_NATIVE_U8
:
10990 *align
= ALIGNMENT(guint64
);
10992 case MONO_NATIVE_R4
:
10995 case MONO_NATIVE_R8
:
10996 *align
= ALIGNMENT(double);
10998 case MONO_NATIVE_INT
:
10999 case MONO_NATIVE_UINT
:
11000 case MONO_NATIVE_LPSTR
:
11001 case MONO_NATIVE_LPWSTR
:
11002 case MONO_NATIVE_LPTSTR
:
11003 case MONO_NATIVE_BSTR
:
11004 case MONO_NATIVE_ANSIBSTR
:
11005 case MONO_NATIVE_TBSTR
:
11006 case MONO_NATIVE_LPARRAY
:
11007 case MONO_NATIVE_SAFEARRAY
:
11008 case MONO_NATIVE_IUNKNOWN
:
11009 case MONO_NATIVE_IDISPATCH
:
11010 case MONO_NATIVE_INTERFACE
:
11011 case MONO_NATIVE_ASANY
:
11012 case MONO_NATIVE_FUNC
:
11013 case MONO_NATIVE_LPSTRUCT
:
11014 *align
= ALIGNMENT(gpointer
);
11015 return sizeof (gpointer
);
11016 case MONO_NATIVE_STRUCT
:
11017 klass
= mono_class_from_mono_type (type
);
11018 if (klass
== mono_defaults
.object_class
&&
11019 (mspec
&& mspec
->native
== MONO_NATIVE_STRUCT
)) {
11023 return mono_class_native_size (klass
, align
);
11024 case MONO_NATIVE_BYVALTSTR
: {
11025 int esize
= unicode
? 2: 1;
11028 return mspec
->data
.array_data
.num_elem
* esize
;
11030 case MONO_NATIVE_BYVALARRAY
: {
11031 // FIXME: Have to consider ArraySubType
11033 klass
= mono_class_from_mono_type (type
);
11034 if (klass
->element_class
== mono_defaults
.char_class
) {
11035 esize
= unicode
? 2 : 1;
11038 esize
= mono_class_native_size (klass
->element_class
, align
);
11041 return mspec
->data
.array_data
.num_elem
* esize
;
11043 case MONO_NATIVE_CUSTOM
:
11044 g_assert_not_reached ();
11046 case MONO_NATIVE_CURRENCY
:
11047 case MONO_NATIVE_VBBYREFSTR
:
11049 g_error ("native type %02x not implemented", native_type
);
11052 g_assert_not_reached ();
11057 mono_marshal_asany (MonoObject
*o
, MonoMarshalNative string_encoding
, int param_attrs
)
11065 t
= &o
->vtable
->klass
->byval_arg
;
11069 case MONO_TYPE_PTR
:
11072 case MONO_TYPE_BOOLEAN
:
11075 case MONO_TYPE_CHAR
:
11080 return mono_object_unbox (o
);
11082 case MONO_TYPE_STRING
:
11083 switch (string_encoding
) {
11084 case MONO_NATIVE_LPWSTR
:
11085 return mono_string_to_utf16 ((MonoString
*)o
);
11087 case MONO_NATIVE_LPSTR
:
11088 return mono_string_to_lpstr ((MonoString
*)o
);
11091 g_warning ("marshaling conversion %d not implemented", string_encoding
);
11092 g_assert_not_reached ();
11095 case MONO_TYPE_CLASS
:
11096 case MONO_TYPE_VALUETYPE
: {
11097 MonoMethod
*method
;
11100 MonoBoolean delete_old
= FALSE
;
11102 klass
= t
->data
.klass
;
11104 if ((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_AUTO_LAYOUT
)
11107 if (klass
->valuetype
&& (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
11108 klass
->blittable
|| klass
->enumtype
))
11109 return mono_object_unbox (o
);
11111 res
= mono_marshal_alloc (mono_class_native_size (klass
, NULL
));
11113 if (!((param_attrs
& PARAM_ATTRIBUTE_OUT
) && !(param_attrs
& PARAM_ATTRIBUTE_IN
))) {
11114 method
= mono_marshal_get_struct_to_ptr (o
->vtable
->klass
);
11118 pa
[2] = &delete_old
;
11120 mono_runtime_invoke (method
, NULL
, pa
, NULL
);
11127 mono_raise_exception (mono_get_exception_argument ("", "No PInvoke conversion exists for value passed to Object-typed parameter."));
11133 mono_marshal_free_asany (MonoObject
*o
, gpointer ptr
, MonoMarshalNative string_encoding
, int param_attrs
)
11141 t
= &o
->vtable
->klass
->byval_arg
;
11143 case MONO_TYPE_STRING
:
11144 switch (string_encoding
) {
11145 case MONO_NATIVE_LPWSTR
:
11146 case MONO_NATIVE_LPSTR
:
11147 mono_marshal_free (ptr
);
11150 g_warning ("marshaling conversion %d not implemented", string_encoding
);
11151 g_assert_not_reached ();
11154 case MONO_TYPE_CLASS
:
11155 case MONO_TYPE_VALUETYPE
: {
11156 klass
= t
->data
.klass
;
11158 if (klass
->valuetype
&& (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
11159 klass
->blittable
|| klass
->enumtype
))
11162 if (param_attrs
& PARAM_ATTRIBUTE_OUT
) {
11163 MonoMethod
*method
= mono_marshal_get_ptr_to_struct (o
->vtable
->klass
);
11169 mono_runtime_invoke (method
, NULL
, pa
, NULL
);
11172 if (!((param_attrs
& PARAM_ATTRIBUTE_OUT
) && !(param_attrs
& PARAM_ATTRIBUTE_IN
))) {
11173 mono_struct_delete_old (klass
, ptr
);
11176 mono_marshal_free (ptr
);
11185 mono_marshal_get_generic_array_helper (MonoClass
*class, MonoClass
*iface
, gchar
*name
, MonoMethod
*method
)
11187 MonoMethodSignature
*sig
, *csig
;
11188 MonoMethodBuilder
*mb
;
11192 mb
= mono_mb_new (class, name
, MONO_WRAPPER_MANAGED_TO_MANAGED
);
11193 mb
->method
->slot
= -1;
11195 mb
->method
->flags
= METHOD_ATTRIBUTE_PRIVATE
| METHOD_ATTRIBUTE_VIRTUAL
|
11196 METHOD_ATTRIBUTE_NEW_SLOT
| METHOD_ATTRIBUTE_HIDE_BY_SIG
| METHOD_ATTRIBUTE_FINAL
;
11198 sig
= mono_method_signature (method
);
11199 csig
= signature_dup (method
->klass
->image
, sig
);
11200 csig
->generic_param_count
= 0;
11202 mono_mb_emit_ldarg (mb
, 0);
11203 for (i
= 0; i
< csig
->param_count
; i
++)
11204 mono_mb_emit_ldarg (mb
, i
+ 1);
11205 mono_mb_emit_managed_call (mb
, method
, NULL
);
11206 mono_mb_emit_byte (mb
, CEE_RET
);
11208 res
= mono_mb_create_method (mb
, csig
, csig
->param_count
+ 16);
11215 /* Put COM Interop related stuff here */
11218 * cominterop_get_ccw_object:
11219 * @ccw_entry: a pointer to the CCWEntry
11220 * @verify: verify ccw_entry is in fact a ccw
11222 * Returns: the corresponding object for the CCW
11225 cominterop_get_ccw_object (MonoCCWInterface
* ccw_entry
, gboolean verify
)
11227 MonoCCW
*ccw
= NULL
;
11229 /* no CCW's exist yet */
11230 if (!ccw_interface_hash
)
11234 ccw
= g_hash_table_lookup (ccw_interface_hash
, ccw_entry
);
11237 ccw
= ccw_entry
->ccw
;
11241 return mono_gchandle_get_target (ccw
->gc_handle
);
11247 cominterop_setup_marshal_context (EmitMarshalContext
*m
, MonoMethod
*method
)
11249 MonoMethodSignature
*sig
, *csig
;
11250 sig
= mono_method_signature (method
);
11251 /* we copy the signature, so that we can modify it */
11252 /* FIXME: which to use? */
11253 csig
= signature_dup (method
->klass
->image
, sig
);
11254 /* csig = mono_metadata_signature_dup (sig); */
11255 csig
->call_convention
= MONO_CALL_STDCALL
;
11259 m
->image
= method
->klass
->image
;
11267 * cominterop_get_ccw:
11268 * @object: a pointer to the object
11269 * @itf: interface type needed
11271 * Returns: a value indicating if the object is a
11272 * Runtime Callable Wrapper (RCW) for a COM object
11275 cominterop_get_ccw (MonoObject
* object
, MonoClass
* itf
)
11278 MonoCCW
*ccw
= NULL
;
11279 MonoCCWInterface
* ccw_entry
= NULL
;
11280 gpointer
*vtable
= NULL
;
11281 static gpointer iunknown
[3] = {NULL
, NULL
, NULL
};
11282 static gpointer idispatch
[4] = {NULL
, NULL
, NULL
, NULL
};
11283 MonoClass
* iface
= NULL
;
11284 MonoClass
* klass
= NULL
;
11285 EmitMarshalContext m
;
11286 int start_slot
= 3;
11287 int method_count
= 0;
11288 GList
*ccw_list
, *ccw_list_item
;
11293 klass
= mono_object_get_class (object
);
11296 ccw_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
11297 if (!ccw_interface_hash
)
11298 ccw_interface_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
11300 ccw_list
= g_hash_table_lookup (ccw_hash
, GINT_TO_POINTER (mono_object_hash (object
)));
11302 ccw_list_item
= ccw_list
;
11303 while (ccw_list_item
) {
11304 MonoCCW
* ccw_iter
= ccw_list_item
->data
;
11305 if (mono_gchandle_get_target (ccw_iter
->gc_handle
) == object
) {
11309 ccw_list_item
= g_list_next(ccw_list_item
);
11312 if (!iunknown
[0]) {
11313 iunknown
[0] = cominterop_ccw_queryinterface
;
11314 iunknown
[1] = cominterop_ccw_addref
;
11315 iunknown
[2] = cominterop_ccw_release
;
11318 if (!idispatch
[0]) {
11319 idispatch
[0] = cominterop_ccw_get_type_info_count
;
11320 idispatch
[1] = cominterop_ccw_get_type_info
;
11321 idispatch
[2] = cominterop_ccw_get_ids_of_names
;
11322 idispatch
[3] = cominterop_ccw_invoke
;
11326 ccw
= g_new0 (MonoCCW
, 1);
11327 ccw
->vtable_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
11328 ccw
->ref_count
= 0;
11329 /* just alloc a weak handle until we are addref'd*/
11330 ccw
->gc_handle
= mono_gchandle_new_weakref (object
, FALSE
);
11333 ccw_list
= g_list_alloc ();
11334 ccw_list
->data
= ccw
;
11337 ccw_list
= g_list_append (ccw_list
, ccw
);
11338 g_hash_table_insert (ccw_hash
, GINT_TO_POINTER (mono_object_hash (object
)), ccw_list
);
11339 /* register for finalization to clean up ccw */
11340 mono_object_register_finalizer (object
);
11344 if (iface
== mono_defaults
.iunknown_class
) {
11347 else if (iface
== mono_defaults
.idispatch_class
) {
11352 method_count
+= iface
->method
.count
;
11353 if (iface
->interface_count
) {
11354 iface
= iface
->interfaces
[0];
11357 start_slot
= cominterop_get_com_slot_begin (iface
);
11363 ccw_entry
= g_hash_table_lookup (ccw
->vtable_hash
, itf
);
11366 int vtable_index
= method_count
-1+start_slot
;
11367 mono_loader_lock ();
11368 vtable
= mono_mempool_alloc0 (klass
->image
->mempool
, sizeof (gpointer
)*(method_count
+start_slot
));
11369 mono_loader_unlock ();
11370 memcpy (vtable
, iunknown
, sizeof (iunknown
));
11371 if (start_slot
== 7)
11372 memcpy (vtable
+3, idispatch
, sizeof (idispatch
));
11376 for (i
= iface
->method
.count
-1; i
>= 0;i
--) {
11377 int param_index
= 0;
11378 MonoMethodBuilder
*mb
;
11379 MonoMarshalSpec
** mspecs
;
11380 MonoMethod
*wrapper_method
, *adjust_method
;
11381 MonoMethod
*method
= iface
->methods
[i
];
11382 MonoMethodSignature
* sig_adjusted
;
11383 MonoMethodSignature
* sig
= mono_method_signature (method
);
11384 gboolean preserve_sig
= method
->iflags
& METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG
;
11387 mb
= mono_mb_new (iface
, method
->name
, MONO_WRAPPER_NATIVE_TO_MANAGED
);
11388 adjust_method
= cominterop_get_managed_wrapper_adjusted (method
);
11389 sig_adjusted
= mono_method_signature (adjust_method
);
11391 mspecs
= g_new (MonoMarshalSpec
*, sig_adjusted
->param_count
+ 1);
11392 mono_method_get_marshal_info (method
, mspecs
);
11395 /* move managed args up one */
11396 for (param_index
= sig
->param_count
; param_index
>= 1; param_index
--)
11397 mspecs
[param_index
+1] = mspecs
[param_index
];
11399 /* first arg is IntPtr for interface */
11402 /* move return spec to last param */
11403 if (!preserve_sig
&& !MONO_TYPE_IS_VOID (sig
->ret
))
11404 mspecs
[sig_adjusted
->param_count
] = mspecs
[0];
11408 cominterop_setup_marshal_context (&m
, adjust_method
);
11410 mono_marshal_emit_managed_wrapper (mb
, sig_adjusted
, mspecs
, &m
, adjust_method
, NULL
);
11411 mono_loader_lock ();
11412 mono_marshal_lock ();
11413 wrapper_method
= mono_mb_create_method (mb
, sig_adjusted
, sig_adjusted
->param_count
+ 16);
11414 mono_marshal_unlock ();
11415 mono_loader_unlock ();
11417 /* skip visiblity since we call internal methods */
11418 wrapper_method
->skip_visibility
= TRUE
;
11420 vtable
[vtable_index
--] = mono_compile_method (wrapper_method
);
11423 for (param_index
= sig_adjusted
->param_count
; param_index
>= 0; param_index
--)
11424 if (mspecs
[param_index
])
11425 mono_metadata_free_marshal_spec (mspecs
[param_index
]);
11428 if (iface
->interface_count
)
11429 iface
= iface
->interfaces
[0];
11434 ccw_entry
= g_new0 (MonoCCWInterface
, 1);
11435 ccw_entry
->ccw
= ccw
;
11436 ccw_entry
->vtable
= vtable
;
11437 g_hash_table_insert (ccw
->vtable_hash
, itf
, ccw_entry
);
11438 g_hash_table_insert (ccw_interface_hash
, ccw_entry
, ccw
);
11445 mono_marshal_free_ccw_entry (gpointer key
, gpointer value
, gpointer user_data
)
11453 * mono_marshal_free_ccw:
11454 * @object: the mono object
11456 * Returns: whether the object had a CCW
11459 mono_marshal_free_ccw (MonoObject
* object
)
11461 GList
*ccw_list
, *ccw_list_orig
, *ccw_list_item
;
11462 /* no ccw's were created */
11463 if (!ccw_hash
|| g_hash_table_size (ccw_hash
) == 0)
11466 /* need to cache orig list address to remove from hash_table if empty */
11467 mono_cominterop_lock ();
11468 ccw_list
= ccw_list_orig
= g_hash_table_lookup (ccw_hash
, GINT_TO_POINTER (mono_object_hash (object
)));
11469 mono_cominterop_unlock ();
11474 ccw_list_item
= ccw_list
;
11475 while (ccw_list_item
) {
11476 MonoCCW
* ccw_iter
= ccw_list_item
->data
;
11477 MonoObject
* handle_target
= mono_gchandle_get_target (ccw_iter
->gc_handle
);
11479 /* Looks like the GC NULLs the weakref handle target before running the
11480 * finalizer. So if we get a NULL target, destroy the CCW as well. */
11481 if (!handle_target
|| handle_target
== object
) {
11482 /* remove all interfaces */
11483 g_hash_table_foreach_remove (ccw_iter
->vtable_hash
, mono_marshal_free_ccw_entry
, NULL
);
11484 g_hash_table_destroy (ccw_iter
->vtable_hash
);
11486 /* get next before we delete */
11487 ccw_list_item
= g_list_next(ccw_list_item
);
11489 /* remove ccw from list */
11490 ccw_list
= g_list_remove (ccw_list
, ccw_iter
);
11494 ccw_list_item
= g_list_next(ccw_list_item
);
11497 /* if list is empty remove original address from hash */
11498 if (g_list_length (ccw_list
) == 0)
11499 g_hash_table_remove (ccw_hash
, ccw_list_orig
);
11506 * cominterop_get_native_wrapper_adjusted:
11507 * @method: managed COM Interop method
11509 * Returns: the generated method to call with signature matching
11510 * the unmanaged COM Method signature
11512 static MonoMethod
*
11513 cominterop_get_managed_wrapper_adjusted (MonoMethod
*method
)
11515 static MonoMethod
*get_hr_for_exception
= NULL
;
11516 MonoMethod
*res
= NULL
;
11517 MonoMethodBuilder
*mb
;
11518 MonoMarshalSpec
**mspecs
;
11519 MonoMethodSignature
*sig
, *sig_native
;
11520 MonoExceptionClause
*main_clause
= NULL
;
11521 MonoMethodHeader
*header
;
11525 gboolean preserve_sig
= method
->iflags
& METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG
;
11527 if (!get_hr_for_exception
)
11528 get_hr_for_exception
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "GetHRForException", -1);
11530 sig
= mono_method_signature (method
);
11532 /* create unmanaged wrapper */
11533 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_COMINTEROP
);
11535 sig_native
= cominterop_method_signature (method
);
11537 mspecs
= g_new0 (MonoMarshalSpec
*, sig_native
->param_count
+1);
11539 mono_method_get_marshal_info (method
, mspecs
);
11541 /* move managed args up one */
11542 for (i
= sig
->param_count
; i
>= 1; i
--)
11543 mspecs
[i
+1] = mspecs
[i
];
11545 /* first arg is IntPtr for interface */
11548 /* move return spec to last param */
11549 if (!preserve_sig
&& !MONO_TYPE_IS_VOID (sig
->ret
))
11550 mspecs
[sig_native
->param_count
] = mspecs
[0];
11554 if (!preserve_sig
) {
11555 hr
= mono_mb_add_local (mb
, &mono_defaults
.int32_class
->byval_arg
);
11558 main_clause
= g_new0 (MonoExceptionClause
, 1);
11559 main_clause
->try_offset
= mb
->pos
;
11562 /* load last param to store result if not preserve_sig and not void */
11563 if (!preserve_sig
&& !MONO_TYPE_IS_VOID (sig
->ret
))
11564 mono_mb_emit_ldarg (mb
, sig_native
->param_count
-1);
11566 /* the CCW -> object conversion */
11567 mono_mb_emit_ldarg (mb
, 0);
11568 mono_mb_emit_icon (mb
, FALSE
);
11569 mono_mb_emit_icall (mb
, cominterop_get_ccw_object
);
11571 for (i
= 0; i
< sig
->param_count
; i
++)
11572 mono_mb_emit_ldarg (mb
, i
+1);
11574 mono_mb_emit_managed_call (mb
, method
, NULL
);
11576 if (!preserve_sig
) {
11577 /* store result if not preserve_sig and we have one */
11578 if (!MONO_TYPE_IS_VOID (sig
->ret
))
11579 mono_mb_emit_byte (mb
, mono_type_to_stind (sig
->ret
));
11581 pos_leave
= mono_mb_emit_branch (mb
, CEE_LEAVE
);
11583 /* Main exception catch */
11584 main_clause
->flags
= MONO_EXCEPTION_CLAUSE_NONE
;
11585 main_clause
->try_len
= mb
->pos
- main_clause
->try_offset
;
11586 main_clause
->data
.catch_class
= mono_defaults
.object_class
;
11589 main_clause
->handler_offset
= mb
->pos
;
11590 mono_mb_emit_managed_call (mb
, get_hr_for_exception
, NULL
);
11591 mono_mb_emit_stloc (mb
, hr
);
11592 mono_mb_emit_branch (mb
, CEE_LEAVE
);
11593 main_clause
->handler_len
= mb
->pos
- main_clause
->handler_offset
;
11596 mono_mb_patch_addr (mb
, pos_leave
, mb
->pos
- (pos_leave
+ 4));
11598 mono_mb_emit_ldloc (mb
, hr
);
11601 mono_mb_emit_byte (mb
, CEE_RET
);
11603 mono_loader_lock ();
11604 mono_marshal_lock ();
11605 res
= mono_mb_create_method (mb
, sig_native
, sig_native
->param_count
+ 16);
11606 mono_marshal_unlock ();
11607 mono_loader_unlock ();
11611 for (i
= sig_native
->param_count
; i
>= 0; i
--)
11613 mono_metadata_free_marshal_spec (mspecs
[i
]);
11616 if (!preserve_sig
) {
11617 header
= ((MonoMethodNormal
*)res
)->header
;
11618 header
->num_clauses
= 1;
11619 header
->clauses
= main_clause
;
11626 * cominterop_mono_string_to_guid:
11628 * Converts the standard string representation of a GUID
11629 * to a 16 byte Microsoft GUID.
11632 cominterop_mono_string_to_guid (const MonoString
* string
, guint8
*guid
) {
11633 gunichar2
* chars
= mono_string_chars (string
);
11635 static guint8 indexes
[16] = {7, 5, 3, 1, 12, 10, 17, 15, 20, 22, 25, 27, 29, 31, 33, 35};
11637 for (i
= 0; i
< sizeof(indexes
); i
++)
11638 guid
[i
] = g_unichar_xdigit_value (chars
[indexes
[i
]]) + (g_unichar_xdigit_value (chars
[indexes
[i
] - 1]) << 4);
11640 //guid [0] = g_unichar_xdigit_value (chars [7]) + (g_unichar_xdigit_value (chars [6]) << 4);
11641 //guid [1] = g_unichar_xdigit_value (chars [5]) + (g_unichar_xdigit_value (chars [4]) << 4);
11642 //guid [2] = g_unichar_xdigit_value (chars [3]) + (g_unichar_xdigit_value (chars [2]) << 4);
11643 //guid [3] = g_unichar_xdigit_value (chars [1]) + (g_unichar_xdigit_value (chars [0]) << 4);
11644 //guid [4] = g_unichar_xdigit_value (chars [12]) + (g_unichar_xdigit_value (chars [11]) << 4);
11645 //guid [5] = g_unichar_xdigit_value (chars [10]) + (g_unichar_xdigit_value (chars [9]) << 4);
11646 //guid [6] = g_unichar_xdigit_value (chars [17]) + (g_unichar_xdigit_value (chars [16]) << 4);
11647 //guid [7] = g_unichar_xdigit_value (chars [15]) + (g_unichar_xdigit_value (chars [14]) << 4);
11648 //guid [8] = g_unichar_xdigit_value (chars [20]) + (g_unichar_xdigit_value (chars [19]) << 4);
11649 //guid [9] = g_unichar_xdigit_value (chars [22]) + (g_unichar_xdigit_value (chars [21]) << 4);
11650 //guid [10] = g_unichar_xdigit_value (chars [25]) + (g_unichar_xdigit_value (chars [24]) << 4);
11651 //guid [11] = g_unichar_xdigit_value (chars [27]) + (g_unichar_xdigit_value (chars [26]) << 4);
11652 //guid [12] = g_unichar_xdigit_value (chars [29]) + (g_unichar_xdigit_value (chars [28]) << 4);
11653 //guid [13] = g_unichar_xdigit_value (chars [31]) + (g_unichar_xdigit_value (chars [30]) << 4);
11654 //guid [14] = g_unichar_xdigit_value (chars [33]) + (g_unichar_xdigit_value (chars [32]) << 4);
11655 //guid [15] = g_unichar_xdigit_value (chars [35]) + (g_unichar_xdigit_value (chars [34]) << 4);
11659 cominterop_class_guid_equal (guint8
* guid
, MonoClass
* klass
)
11661 static MonoClass
*GuidAttribute
= NULL
;
11662 MonoCustomAttrInfo
*cinfo
;
11664 /* Handle the GuidAttribute */
11665 if (!GuidAttribute
)
11666 GuidAttribute
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.InteropServices", "GuidAttribute");
11668 cinfo
= mono_custom_attrs_from_class (klass
);
11670 guint8 klass_guid
[16];
11671 MonoReflectionGuidAttribute
*attr
= (MonoReflectionGuidAttribute
*)mono_custom_attrs_get_attr (cinfo
, GuidAttribute
);
11675 if (!cinfo
->cached
)
11676 mono_custom_attrs_free (cinfo
);
11678 cominterop_mono_string_to_guid (attr
->guid
, klass_guid
);
11679 return !memcmp (guid
, klass_guid
, sizeof (klass_guid
));
11685 cominterop_ccw_addref (MonoCCWInterface
* ccwe
)
11687 gint32 ref_count
= 0;
11688 MonoCCW
* ccw
= ccwe
->ccw
;
11690 g_assert (ccw
->gc_handle
);
11691 g_assert (ccw
->ref_count
>= 0);
11692 ref_count
= InterlockedIncrement ((gint32
*)&ccw
->ref_count
);
11693 if (ref_count
== 1) {
11694 guint32 oldhandle
= ccw
->gc_handle
;
11695 g_assert (oldhandle
);
11696 /* since we now have a ref count, alloc a strong handle*/
11697 ccw
->gc_handle
= mono_gchandle_new (mono_gchandle_get_target (oldhandle
), FALSE
);
11698 mono_gchandle_free (oldhandle
);
11704 cominterop_ccw_release (MonoCCWInterface
* ccwe
)
11706 gint32 ref_count
= 0;
11707 MonoCCW
* ccw
= ccwe
->ccw
;
11709 g_assert (ccw
->ref_count
> 0);
11710 ref_count
= InterlockedDecrement ((gint32
*)&ccw
->ref_count
);
11711 if (ref_count
== 0) {
11712 /* allow gc of object */
11713 guint32 oldhandle
= ccw
->gc_handle
;
11714 g_assert (oldhandle
);
11715 ccw
->gc_handle
= mono_gchandle_new_weakref (mono_gchandle_get_target (oldhandle
), FALSE
);
11716 mono_gchandle_free (oldhandle
);
11721 #define MONO_S_OK 0x00000000L
11722 #define MONO_E_NOINTERFACE 0x80004002L
11723 #define MONO_E_NOTIMPL 0x80004001L
11726 cominterop_ccw_queryinterface (MonoCCWInterface
* ccwe
, guint8
* riid
, gpointer
* ppv
)
11729 MonoClass
*itf
= NULL
;
11731 MonoCCW
* ccw
= ccwe
->ccw
;
11732 MonoClass
* klass
= NULL
;
11733 MonoObject
* object
= mono_gchandle_get_target (ccw
->gc_handle
);
11736 klass
= mono_object_class (object
);
11741 /* handle IUnknown special */
11742 if (cominterop_class_guid_equal (riid
, mono_defaults
.iunknown_class
)) {
11743 *ppv
= cominterop_get_ccw (object
, mono_defaults
.iunknown_class
);
11744 /* remember to addref on QI */
11745 cominterop_ccw_addref (*ppv
);
11749 /* handle IDispatch special */
11750 if (cominterop_class_guid_equal (riid
, mono_defaults
.idispatch_class
)) {
11751 *ppv
= cominterop_get_ccw (object
, mono_defaults
.idispatch_class
);
11752 /* remember to addref on QI */
11753 cominterop_ccw_addref (*ppv
);
11757 ifaces
= mono_class_get_implemented_interfaces (klass
);
11759 for (i
= 0; i
< ifaces
->len
; ++i
) {
11760 MonoClass
*ic
= NULL
;
11761 ic
= g_ptr_array_index (ifaces
, i
);
11762 if (cominterop_class_guid_equal (riid
, ic
)) {
11767 g_ptr_array_free (ifaces
, TRUE
);
11770 *ppv
= cominterop_get_ccw (object
, itf
);
11771 /* remember to addref on QI */
11772 cominterop_ccw_addref (*ppv
);
11776 return MONO_E_NOINTERFACE
;
11780 cominterop_ccw_get_type_info_count (MonoCCWInterface
* ccwe
, guint32
*pctinfo
)
11782 return MONO_E_NOTIMPL
;
11786 cominterop_ccw_get_type_info (MonoCCWInterface
* ccwe
, guint32 iTInfo
, guint32 lcid
, gpointer
*ppTInfo
)
11788 return MONO_E_NOTIMPL
;
11792 cominterop_ccw_get_ids_of_names (MonoCCWInterface
* ccwe
, gpointer riid
,
11793 gunichar2
** rgszNames
, guint32 cNames
,
11794 guint32 lcid
, gint32
*rgDispId
)
11796 return MONO_E_NOTIMPL
;
11800 cominterop_ccw_invoke (MonoCCWInterface
* ccwe
, guint32 dispIdMember
,
11801 gpointer riid
, guint32 lcid
,
11802 guint16 wFlags
, gpointer pDispParams
,
11803 gpointer pVarResult
, gpointer pExcepInfo
,
11806 return MONO_E_NOTIMPL
;