[runtime] Call error_init instead of mono_error_init (#4425)
[mono.git] / mono / metadata / remoting.c
index 30595ee5410e1a43cf886966b5e7175899ccf2ff..ecc5f94248485b893960e154aeda2b7b1d1a6d61 100644 (file)
 
 #include "config.h"
 
+#include "mono/metadata/handle.h"
 #include "mono/metadata/remoting.h"
 #include "mono/metadata/marshal.h"
+#include "mono/metadata/marshal-internals.h"
 #include "mono/metadata/abi-details.h"
 #include "mono/metadata/cominterop.h"
 #include "mono/metadata/tabledefs.h"
@@ -68,9 +70,9 @@ 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 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;
@@ -197,7 +199,10 @@ mono_remoting_marshal_init (void)
                register_icall (mono_marshal_xdomain_copy_out_value, "mono_marshal_xdomain_copy_out_value", "void object object", FALSE);
                register_icall (mono_remoting_wrapper, "mono_remoting_wrapper", "object ptr ptr", FALSE);
                register_icall (mono_upgrade_remote_class_wrapper, "mono_upgrade_remote_class_wrapper", "void object object", FALSE);
+
+#ifndef DISABLE_JIT
                register_icall (mono_compile_method_icall, "mono_compile_method_icall", "ptr ptr", FALSE);
+#endif
 
        }
 
@@ -361,7 +366,7 @@ mono_remoting_wrapper (MonoMethod *method, gpointer *params)
        this_obj = *((MonoTransparentProxy **)params [0]);
 
        g_assert (this_obj);
-       g_assert (((MonoObject *)this_obj)->vtable->klass == mono_defaults.transparent_proxy_class);
+       g_assert (mono_object_is_transparent_proxy (this_obj));
        
        /* skip the this pointer */
        params++;
@@ -408,7 +413,7 @@ mono_remoting_wrapper (MonoMethod *method, gpointer *params)
                goto fail;
 
        if (exc) {
-               mono_error_init (&error);
+               error_init (&error);
                mono_error_set_exception_instance (&error, (MonoException *)exc);
                goto fail;
        }
@@ -1849,14 +1854,15 @@ mono_marshal_get_proxy_cancast (MonoClass *klass)
 }
 
 void
-mono_upgrade_remote_class_wrapper (MonoReflectionType *rtype, MonoTransparentProxy *tproxy)
+mono_upgrade_remote_class_wrapper (MonoReflectionType *rtype_raw, MonoTransparentProxy *tproxy_raw)
 {
-       MonoError error;
-       MonoClass *klass;
-       MonoDomain *domain = ((MonoObject*)tproxy)->vtable->domain;
-       klass = mono_class_from_mono_type (rtype->type);
-       mono_upgrade_remote_class (domain, (MonoObject*)tproxy, klass, &error);
-       mono_error_set_pending_exception (&error);
+       ICALL_ENTRY ();
+       MONO_HANDLE_DCL (MonoReflectionType, rtype);
+       MONO_HANDLE_DCL (MonoTransparentProxy, tproxy);
+       MonoDomain *domain = MONO_HANDLE_DOMAIN (tproxy);
+       MonoClass *klass = mono_class_from_mono_type (MONO_HANDLE_GETVAL (rtype, type));
+       mono_upgrade_remote_class (domain, MONO_HANDLE_CAST (MonoObject, tproxy), klass, &error);
+       ICALL_RETURN ();
 }
 
 #else /* DISABLE_REMOTING */
@@ -1906,19 +1912,44 @@ mono_get_xdomain_marshal_type (MonoType *t)
        return MONO_MARSHAL_SERIALIZE;
 }
 
-/* mono_marshal_xdomain_copy_value
- * Makes a copy of "val" suitable for the current domain.
+/* Replace the given array element by a copy in the current domain */
+static gboolean
+xdomain_copy_array_element_inplace (MonoArrayHandle arr, int i, MonoError *error)
+{
+       HANDLE_FUNCTION_ENTER ();
+       error_init (error);
+       MonoObjectHandle item = MONO_HANDLE_NEW (MonoObject, NULL);
+       MONO_HANDLE_ARRAY_GETREF (item, arr, i);
+       
+       MonoObjectHandle item_copy = mono_marshal_xdomain_copy_value_handle (item, error);
+       if (!is_ok (error))
+               goto leave;
+       MONO_HANDLE_ARRAY_SETREF (arr, i, item_copy);
+leave:
+       HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
+}
+
+/**
+ * mono_marshal_xdomain_copy_value_handle:
+ * @val: The value to copy.
+ * @error: set on failure.
+ *
+ * Makes a copy of @val suitable for the current domain.
+ * On failure returns NULL and sets @error.
  */
-MonoObject *
-mono_marshal_xdomain_copy_value (MonoObject *val, MonoError *error)
+MonoObjectHandle
+mono_marshal_xdomain_copy_value_handle (MonoObjectHandle val, MonoError *error)
 {
-       mono_error_init (error);
-       MonoDomain *domain;
-       if (val == NULL) return NULL;
+       error_init (error);
+       MonoObjectHandle result = MONO_HANDLE_NEW (MonoObject, NULL);
+       if (MONO_HANDLE_IS_NULL (val))
+               goto leave;
 
-       domain = mono_domain_get ();
+       MonoDomain *domain = mono_domain_get ();
 
-       switch (mono_object_class (val)->byval_arg.type) {
+       MonoClass *klass = mono_handle_class (val);
+
+       switch (klass->byval_arg.type) {
        case MONO_TYPE_VOID:
                g_assert_not_reached ();
                break;
@@ -1934,40 +1965,63 @@ mono_marshal_xdomain_copy_value (MonoObject *val, MonoError *error)
        case MONO_TYPE_U8:
        case MONO_TYPE_R4:
        case MONO_TYPE_R8: {
-               MonoObject *res = mono_value_box_checked (domain, mono_object_class (val), ((char*)val) + sizeof(MonoObject), error);
-               return res;
-
+               uint32_t gchandle = mono_gchandle_from_handle (val, TRUE);
+               MonoObjectHandle res = MONO_HANDLE_NEW (MonoObject, mono_value_box_checked (domain, klass, ((char*)val) + sizeof(MonoObject), error)); /* FIXME use handles in mono_value_box_checked */
+               mono_gchandle_free (gchandle);
+               if (!is_ok (error))
+                       goto leave;
+               MONO_HANDLE_ASSIGN (result, res);
+               break;
        }
        case MONO_TYPE_STRING: {
-               MonoString *str = (MonoString *) val;
-               MonoObject *res = NULL;
-               res = (MonoObject *) mono_string_new_utf16_checked (domain, mono_string_chars (str), mono_string_length (str), error);
-               return res;
+               MonoStringHandle str = MONO_HANDLE_CAST (MonoString, val);
+               uint32_t gchandle = mono_gchandle_from_handle (val, TRUE);
+               MonoStringHandle res = mono_string_new_utf16_handle (domain, mono_string_chars (MONO_HANDLE_RAW (str)), mono_string_handle_length (str), error);
+               mono_gchandle_free (gchandle);
+               if (!is_ok (error))
+                       goto leave;
+               MONO_HANDLE_ASSIGN (result, res);
+               break;
        }
        case MONO_TYPE_ARRAY:
        case MONO_TYPE_SZARRAY: {
-               MonoArray *acopy;
-               MonoXDomainMarshalType mt = mono_get_xdomain_marshal_type (&(mono_object_class (val)->element_class->byval_arg));
-               if (mt == MONO_MARSHAL_SERIALIZE) return NULL;
-               acopy = mono_array_clone_in_domain (domain, (MonoArray *) val, error);
-               return_val_if_nok (error, NULL);
+               MonoArrayHandle arr = MONO_HANDLE_CAST (MonoArray, val);
+               MonoXDomainMarshalType mt = mono_get_xdomain_marshal_type (&klass->element_class->byval_arg);
+               if (mt == MONO_MARSHAL_SERIALIZE)
+                       goto leave;
+               MonoArrayHandle acopy = mono_array_clone_in_domain (domain, arr, error);
+               if (!is_ok (error))
+                       goto leave;
 
                if (mt == MONO_MARSHAL_COPY) {
-                       int i, len = mono_array_length (acopy);
+                       int i, len = mono_array_handle_length (acopy);
                        for (i = 0; i < len; i++) {
-                               MonoObject *item = (MonoObject *)mono_array_get (acopy, gpointer, i);
-                               MonoObject *item_copy = mono_marshal_xdomain_copy_value (item, error);
-                               return_val_if_nok (error, NULL);
-                               mono_array_setref (acopy, i, item_copy);
+                               if (!xdomain_copy_array_element_inplace (acopy, i, error))
+                                       goto leave;
                        }
                }
-               return (MonoObject *) acopy;
+               MONO_HANDLE_ASSIGN (result, acopy);
+               break;
        }
        default:
                break;
        }
 
-       return NULL;
+leave:
+       return result;
+}
+
+/* mono_marshal_xdomain_copy_value
+ * Makes a copy of "val" suitable for the current domain.
+ */
+MonoObject*
+mono_marshal_xdomain_copy_value (MonoObject* val_raw, MonoError *error)
+{
+       HANDLE_FUNCTION_ENTER ();
+       /* FIXME callers of mono_marshal_xdomain_copy_value should use handles */
+       MONO_HANDLE_DCL (MonoObject, val);
+       MonoObjectHandle result = mono_marshal_xdomain_copy_value_handle (val, error);
+       HANDLE_FUNCTION_RETURN_OBJ (result);
 }
 
 /* mono_marshal_xdomain_copy_value