Merge pull request #2698 from esdrubal/iosxmlarray
[mono.git] / mono / metadata / remoting.c
index 22ab5cc7a6ecdcb5777d1e8e2e54ed3af10b78f5..9c329f4934247bb8218f87c7f518d48cca3e006a 100644 (file)
@@ -16,6 +16,7 @@
 #include "mono/metadata/tabledefs.h"
 #include "mono/metadata/exception.h"
 #include "mono/metadata/debug-helpers.h"
+#include "mono/metadata/reflection-internals.h"
 
 typedef enum {
        MONO_MARSHAL_NONE,                      /* No marshalling needed */
@@ -65,6 +66,11 @@ mono_marshal_xdomain_copy_out_value (MonoObject *src, MonoObject *dst);
 static MonoReflectionType *
 type_from_handle (MonoType *handle);
 
+/* Class lazy loading functions */
+static GENERATE_GET_CLASS_WITH_CACHE (remoting_services, System.Runtime.Remoting, RemotingServices)
+static GENERATE_GET_CLASS_WITH_CACHE (call_context, System.Runtime.Remoting.Messaging, CallContext)
+static GENERATE_GET_CLASS_WITH_CACHE (context, System.Runtime.Remoting.Contexts, Context)
+
 static mono_mutex_t remoting_mutex;
 static gboolean remoting_mutex_inited;
 
@@ -86,14 +92,14 @@ static inline void
 remoting_lock (void)
 {
        g_assert (remoting_mutex_inited);
-       mono_mutex_lock (&remoting_mutex);
+       mono_os_mutex_lock (&remoting_mutex);
 }
 
 static inline void
 remoting_unlock (void)
 {
        g_assert (remoting_mutex_inited);
-       mono_mutex_unlock (&remoting_mutex);
+       mono_os_mutex_unlock (&remoting_mutex);
 }
 
 /*
@@ -134,7 +140,7 @@ get_cache_full (GHashTable **var, GHashFunc hash_func, GCompareFunc equal_func,
 void
 mono_remoting_init (void)
 {
-       mono_mutex_init (&remoting_mutex);
+       mono_os_mutex_init (&remoting_mutex);
        remoting_mutex_inited = TRUE;
 }
 
@@ -152,7 +158,7 @@ mono_remoting_marshal_init (void)
        byte_array_class = mono_array_class_get (mono_defaults.byte_class, 1);
 
 #ifndef DISABLE_JIT
-       klass = mono_class_from_name (mono_defaults.corlib, "System.Runtime.Remoting", "RemotingServices");
+       klass = mono_class_get_remoting_services_class ();
        method_rs_serialize = mono_class_get_method_from_name (klass, "SerializeCallData", -1);
        g_assert (method_rs_serialize);
        method_rs_deserialize = mono_class_get_method_from_name (klass, "DeserializeCallData", -1);
@@ -168,11 +174,11 @@ mono_remoting_marshal_init (void)
        method_exc_fixexc = mono_class_get_method_from_name (klass, "FixRemotingException", -1);
        g_assert (method_exc_fixexc);
 
-       klass = mono_class_from_name (mono_defaults.corlib, "System.Runtime.Remoting.Messaging", "CallContext");
+       klass = mono_class_get_call_context_class ();
        method_set_call_context = mono_class_get_method_from_name (klass, "SetCurrentCallContext", -1);
        g_assert (method_set_call_context);
 
-       klass = mono_class_from_name (mono_defaults.corlib, "System.Runtime.Remoting.Contexts", "Context");
+       klass = mono_class_get_context_class ();
        method_needs_context_sink = mono_class_get_method_from_name (klass, "get_NeedsContextSink", -1);
        g_assert (method_needs_context_sink);
 #endif 
@@ -199,11 +205,17 @@ mono_remoting_marshal_init (void)
 static MonoReflectionType *
 type_from_handle (MonoType *handle)
 {
+       MonoError error;
+       MonoReflectionType *ret;
        MonoDomain *domain = mono_domain_get (); 
        MonoClass *klass = mono_class_from_mono_type (handle);
 
        mono_class_init (klass);
-       return mono_type_get_object (domain, handle);
+
+       ret = mono_type_get_object_checked (domain, handle, &error);
+       mono_error_raise_exception (&error); /* FIXME don't raise here */
+
+       return ret;
 }
 
 #ifndef DISABLE_JIT
@@ -266,7 +278,7 @@ mono_marshal_remoting_find_in_cache (MonoMethod *method, int wrapper_type)
 
        mono_marshal_lock_internal ();
        if (mono_method_get_wrapper_cache (method)->remoting_invoke_cache)
-               wrps = g_hash_table_lookup (mono_method_get_wrapper_cache (method)->remoting_invoke_cache, method);
+               wrps = (MonoRemotingMethods *)g_hash_table_lookup (mono_method_get_wrapper_cache (method)->remoting_invoke_cache, method);
 
        if (wrps) {
                switch (wrapper_type) {
@@ -297,7 +309,7 @@ mono_remoting_mb_create_and_cache (MonoMethod *key, MonoMethodBuilder *mb,
        cache = get_cache_full (&mono_method_get_wrapper_cache (key)->remoting_invoke_cache, mono_aligned_addr_hash, NULL, NULL, g_free);
 
        mono_marshal_lock_internal ();
-       wrps = g_hash_table_lookup (cache, key);
+       wrps = (MonoRemotingMethods *)g_hash_table_lookup (cache, key);
        if (!wrps) {
                wrps = g_new0 (MonoRemotingMethods, 1);
                g_hash_table_insert (cache, key, wrps);
@@ -333,6 +345,7 @@ mono_remoting_mb_create_and_cache (MonoMethod *key, MonoMethodBuilder *mb,
 static MonoObject *
 mono_remoting_wrapper (MonoMethod *method, gpointer *params)
 {
+       MonoError error;
        MonoMethodMessage *msg;
        MonoTransparentProxy *this_obj;
        MonoObject *res, *exc;
@@ -360,9 +373,10 @@ mono_remoting_wrapper (MonoMethod *method, gpointer *params)
                                        mparams[i] = *((gpointer *)params [i]);
                                } else {
                                        /* runtime_invoke expects a boxed instance */
-                                       if (mono_class_is_nullable (mono_class_from_mono_type (sig->params [i])))
-                                               mparams[i] = mono_nullable_box (params [i], klass);
-                                       else
+                                       if (mono_class_is_nullable (mono_class_from_mono_type (sig->params [i]))) {
+                                               mparams[i] = mono_nullable_box ((guint8 *)params [i], klass, &error);
+                                               mono_error_raise_exception (&error); /* FIXME don't raise here */
+                                       } else
                                                mparams[i] = params [i];
                                }
                        } else {
@@ -370,12 +384,16 @@ mono_remoting_wrapper (MonoMethod *method, gpointer *params)
                        }
                }
 
-               return mono_runtime_invoke (method, method->klass->valuetype? mono_object_unbox ((MonoObject*)this_obj): this_obj, mparams, NULL);
+               res = mono_runtime_invoke_checked (method, method->klass->valuetype? mono_object_unbox ((MonoObject*)this_obj): this_obj, mparams, &error);
+               mono_error_raise_exception (&error); /* FIXME don't raise here */
+
+               return res;
        }
 
        msg = mono_method_call_message_new (method, params, NULL, NULL, NULL);
 
-       res = mono_remoting_invoke ((MonoObject *)this_obj->rp, msg, &exc, &out_args);
+       res = mono_remoting_invoke ((MonoObject *)this_obj->rp, msg, &exc, &out_args, &error);
+       mono_error_raise_exception (&error); /* FIXME don't raise here */
 
        if (exc)
                mono_raise_exception ((MonoException *)exc);
@@ -401,7 +419,7 @@ mono_marshal_get_remoting_invoke (MonoMethod *method)
 
        /* this seems to be the best plase to put this, as all remoting invokes seem to get filtered through here */
 #ifndef DISABLE_COM
-       if (mono_class_is_com_object (method->klass) || method->klass == mono_class_get_com_object_class ()) {
+       if (mono_class_is_com_object (method->klass) || method->klass == mono_class_try_get_com_object_class ()) {
                MonoVTable *vtable = mono_class_vtable (mono_domain_get (), method->klass);
                g_assert (vtable); /*FIXME do proper error handling*/
 
@@ -469,7 +487,7 @@ mono_marshal_xdomain_copy_out_value (MonoObject *src, MonoObject *dst)
                if (mt == MONO_MARSHAL_COPY) {
                        int i, len = mono_array_length ((MonoArray *)dst);
                        for (i = 0; i < len; i++) {
-                               MonoObject *item = mono_array_get ((MonoArray *)src, gpointer, i);
+                               MonoObject *item = (MonoObject *)mono_array_get ((MonoArray *)src, gpointer, i);
                                mono_array_setref ((MonoArray *)dst, i, mono_marshal_xdomain_copy_value (item));
                        }
                } else {
@@ -564,7 +582,7 @@ mono_marshal_check_domain_image (gint32 domain_id, MonoImage *image)
        
        mono_domain_assemblies_lock (domain);
        for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
-               ass = tmp->data;
+               ass = (MonoAssembly *)tmp->data;
                if (ass->image == image)
                        break;
        }
@@ -629,7 +647,7 @@ mono_marshal_get_xappdomain_dispatch (MonoMethod *method, int *marshal_types, in
 
        /* try */
 
-       main_clause = mono_image_alloc0 (method->klass->image, sizeof (MonoExceptionClause));
+       main_clause = (MonoExceptionClause *)mono_image_alloc0 (method->klass->image, sizeof (MonoExceptionClause));
        main_clause->try_offset = mono_mb_get_label (mb);
 
        /* Clean the call context */
@@ -880,7 +898,7 @@ mono_marshal_get_xappdomain_invoke (MonoMethod *method)
 
        /* Count the number of parameters that need to be serialized */
 
-       marshal_types = alloca (sizeof (int) * sig->param_count);
+       marshal_types = (int *)alloca (sizeof (int) * sig->param_count);
        complex_count = complex_out_count = 0;
        for (i = 0; i < sig->param_count; i++) {
                MonoType *ptype = sig->params[i];
@@ -1970,6 +1988,7 @@ mono_get_xdomain_marshal_type (MonoType *t)
 MonoObject *
 mono_marshal_xdomain_copy_value (MonoObject *val)
 {
+       MonoError error;
        MonoDomain *domain;
        if (val == NULL) return NULL;
 
@@ -1991,11 +2010,17 @@ mono_marshal_xdomain_copy_value (MonoObject *val)
        case MONO_TYPE_U8:
        case MONO_TYPE_R4:
        case MONO_TYPE_R8: {
-               return mono_value_box (domain, mono_object_class (val), ((char*)val) + sizeof(MonoObject));
+               MonoObject *res = mono_value_box_checked (domain, mono_object_class (val), ((char*)val) + sizeof(MonoObject), &error);
+               mono_error_raise_exception (&error); /* FIXME don't raise here */
+               return res;
+
        }
        case MONO_TYPE_STRING: {
                MonoString *str = (MonoString *) val;
-               return (MonoObject *) mono_string_new_utf16 (domain, mono_string_chars (str), mono_string_length (str));
+               MonoObject *res = NULL;
+               res = (MonoObject *) mono_string_new_utf16_checked (domain, mono_string_chars (str), mono_string_length (str), &error);
+               mono_error_raise_exception (&error); /* FIXME don't raise here */
+               return res;
        }
        case MONO_TYPE_ARRAY:
        case MONO_TYPE_SZARRAY: {
@@ -2006,7 +2031,7 @@ mono_marshal_xdomain_copy_value (MonoObject *val)
                if (mt == MONO_MARSHAL_COPY) {
                        int i, len = mono_array_length (acopy);
                        for (i = 0; i < len; i++) {
-                               MonoObject *item = mono_array_get (acopy, gpointer, i);
+                               MonoObject *item = (MonoObject *)mono_array_get (acopy, gpointer, i);
                                mono_array_setref (acopy, i, mono_marshal_xdomain_copy_value (item));
                        }
                }