Merge pull request #4621 from alexanderkyte/strdup_env
[mono.git] / mono / metadata / icall.c
index 890e7155beeab41c07a6f82a18ab8b98f660c81b..cb562cde7613af2419d55b049d8a3a7ece434449 100644 (file)
@@ -1,5 +1,5 @@
-/*
- * icall.c:
+/**
+ * \file
  *
  * Authors:
  *   Dietmar Maurer (dietmar@ximian.com)
@@ -130,7 +130,7 @@ is_generic_parameter (MonoType *type)
 static void
 mono_class_init_checked (MonoClass *klass, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
 
        if (!mono_class_init (klass))
                mono_error_set_for_class_failure (error, klass);
@@ -232,7 +232,7 @@ ves_icall_System_Array_SetValueImpl (MonoArray *arr, MonoObject *value, guint32
        gint64 i64 = 0;
        gdouble r64 = 0;
 
-       mono_error_init (&error);
+       error_init (&error);
 
        if (value)
                vc = value->vtable->klass;
@@ -582,6 +582,11 @@ ves_icall_System_Array_CreateInstanceImpl (MonoReflectionType *type, MonoArray *
        if (mono_error_set_pending_exception (&error))
                return NULL;
 
+       if (klass->element_class->byval_arg.type == MONO_TYPE_VOID) {
+               mono_set_pending_exception (mono_get_exception_not_supported ("Arrays of System.Void are not supported."));
+               return NULL;
+       }
+
        if (bounds && (mono_array_length (bounds) == 1) && (mono_array_get (bounds, gint32, 0) != 0))
                /* vectors are not the same as one dimensional arrays with no-zero bounds */
                bounded = TRUE;
@@ -727,14 +732,6 @@ ves_icall_System_Array_ClearInternal (MonoArray *arr, int idx, int length)
        mono_gc_bzero_atomic (mono_array_addr_with_size_fast (arr, sz, idx), length * sz);
 }
 
-ICALL_EXPORT MonoArray*
-ves_icall_System_Array_Clone (MonoArray *arr)
-{
-       MonoError error;
-       MonoArray *result = mono_array_clone_checked (arr, &error);
-       mono_error_set_pending_exception (&error);
-       return result;
-}
 
 ICALL_EXPORT gboolean
 ves_icall_System_Array_FastCopy (MonoArray *source, int source_idx, MonoArray* dest, int dest_idx, int length)
@@ -844,7 +841,7 @@ ves_icall_System_Array_SetGenericValueImpl (MonoArray *arr, guint32 pos, gpointe
 ICALL_EXPORT void
 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_InitializeArray (MonoArrayHandle array, MonoClassField *field_handle, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
 
        MonoClass *klass = mono_handle_class (array);
        guint32 size = mono_array_element_size (klass);
@@ -1216,7 +1213,7 @@ ves_icall_System_ValueType_Equals (MonoObject *this_obj, MonoObject *that, MonoA
 ICALL_EXPORT MonoReflectionTypeHandle
 ves_icall_System_Object_GetType (MonoObjectHandle obj, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (obj);
        MonoClass *klass = mono_handle_class (obj);
 #ifndef DISABLE_REMOTING
@@ -1313,7 +1310,7 @@ type_from_parsed_name (MonoTypeNameParse *info, MonoBoolean ignoreCase, MonoAsse
        gboolean type_resolve = FALSE;
        MonoImage *rootimage = NULL;
 
-       mono_error_init (error);
+       error_init (error);
 
        /*
         * We must compute the calling assembly as type loading must happen under a metadata context.
@@ -1402,7 +1399,7 @@ ves_icall_System_Type_internal_from_name (MonoStringHandle name,
                                          MonoBoolean ignoreCase,
                                          MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoTypeNameParse info;
        gboolean parsedOk;
        MonoAssembly *caller_assembly;
@@ -1460,7 +1457,7 @@ leave:
 ICALL_EXPORT MonoReflectionTypeHandle
 ves_icall_System_Type_internal_from_handle (MonoType *handle, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = mono_domain_get (); 
 
        return mono_type_get_object_handle (domain, handle, error);
@@ -1518,7 +1515,7 @@ typedef enum {
 ICALL_EXPORT guint32
 ves_icall_type_GetTypeCodeInternal (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
        int t = type->type;
 
@@ -1612,7 +1609,7 @@ mono_type_get_underlying_type_ignore_byref (MonoType *type)
 ICALL_EXPORT guint32
 ves_icall_RuntimeTypeHandle_type_is_assignable_from (MonoReflectionTypeHandle ref_type, MonoReflectionTypeHandle ref_c, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
 
        g_assert (!MONO_HANDLE_IS_NULL (ref_type));
        
@@ -1652,7 +1649,7 @@ ves_icall_RuntimeTypeHandle_type_is_assignable_from (MonoReflectionTypeHandle re
 ICALL_EXPORT guint32
 ves_icall_RuntimeTypeHandle_IsInstanceOfType (MonoReflectionTypeHandle ref_type, MonoObjectHandle obj, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
        MonoClass *klass = mono_class_from_mono_type (type);
        mono_class_init_checked (klass, error);
@@ -1665,7 +1662,7 @@ ves_icall_RuntimeTypeHandle_IsInstanceOfType (MonoReflectionTypeHandle ref_type,
 ICALL_EXPORT guint32
 ves_icall_RuntimeTypeHandle_GetAttributes (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
        MonoClass *klass = mono_class_from_mono_type (type);
        return mono_class_get_flags (klass);
@@ -1674,7 +1671,7 @@ ves_icall_RuntimeTypeHandle_GetAttributes (MonoReflectionTypeHandle ref_type, Mo
 ICALL_EXPORT MonoReflectionMarshalAsAttributeHandle
 ves_icall_System_Reflection_FieldInfo_get_marshal_info (MonoReflectionFieldHandle field_h, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (field_h);
        MonoClassField *field = MONO_HANDLE_GETVAL (field_h, field);
        MonoClass *klass = field->parent;
@@ -1710,7 +1707,7 @@ ves_icall_System_Reflection_FieldInfo_internal_from_handle_type (MonoClassField
 
        g_assert (handle);
 
-       mono_error_init (error);
+       error_init (error);
 
        if (!type) {
                klass = handle->parent;
@@ -1734,7 +1731,7 @@ ves_icall_System_Reflection_EventInfo_internal_from_handle_type (MonoEvent *hand
 
        g_assert (handle);
 
-       mono_error_init (error);
+       error_init (error);
 
        if (!type) {
                klass = handle->parent;
@@ -1754,7 +1751,7 @@ ves_icall_System_Reflection_EventInfo_internal_from_handle_type (MonoEvent *hand
 ICALL_EXPORT MonoReflectionPropertyHandle
 ves_icall_System_Reflection_PropertyInfo_internal_from_handle_type (MonoProperty *handle, MonoType *type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoClass *klass;
 
        g_assert (handle);
@@ -1776,7 +1773,7 @@ ves_icall_System_Reflection_PropertyInfo_internal_from_handle_type (MonoProperty
 ICALL_EXPORT MonoArrayHandle
 ves_icall_System_Reflection_FieldInfo_GetTypeModifiers (MonoReflectionFieldHandle field_h, MonoBoolean optional, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoClassField *field = MONO_HANDLE_GETVAL (field_h, field);
 
        MonoType *type = mono_field_get_type_checked (field, error);
@@ -1793,52 +1790,42 @@ vell_icall_get_method_attributes (MonoMethod *method)
 }
 
 ICALL_EXPORT void
-ves_icall_get_method_info (MonoMethod *method, MonoMethodInfo *info)
+ves_icall_get_method_info (MonoMethod *method, MonoMethodInfo *info, MonoError *error)
 {
-       MonoError error;
-       MonoReflectionType *rt;
        MonoDomain *domain = mono_domain_get ();
-       MonoMethodSignature* sig;
 
-       sig = mono_method_signature_checked (method, &error);
-       if (!mono_error_ok (&error)) {
-               mono_error_set_pending_exception (&error);
-               return;
-       }
+       MonoMethodSignature* sig = mono_method_signature_checked (method, error);
+       return_if_nok (error);
 
-       rt = mono_type_get_object_checked (domain, &method->klass->byval_arg, &error);
-       if (!mono_error_ok (&error)) {
-               mono_error_set_pending_exception (&error);
-               return;
-       }
+       MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, &method->klass->byval_arg, error);
+       return_if_nok (error);
 
-       MONO_STRUCT_SETREF (info, parent, rt);
+       MONO_STRUCT_SETREF (info, parent, MONO_HANDLE_RAW (rt));
 
-       rt = mono_type_get_object_checked (domain, sig->ret, &error);
-       if (!mono_error_ok (&error)) {
-               mono_error_set_pending_exception (&error);
-               return;
-       }
+       MONO_HANDLE_ASSIGN (rt, mono_type_get_object_handle (domain, sig->ret, error));
+       return_if_nok (error);
 
-       MONO_STRUCT_SETREF (info, ret, rt);
+       MONO_STRUCT_SETREF (info, ret, MONO_HANDLE_RAW (rt));
 
        info->attrs = method->flags;
        info->implattrs = method->iflags;
+       guint32 callconv;
        if (sig->call_convention == MONO_CALL_DEFAULT)
-               info->callconv = sig->sentinelpos >= 0 ? 2 : 1;
+               callconv = sig->sentinelpos >= 0 ? 2 : 1;
        else {
                if (sig->call_convention == MONO_CALL_VARARG || sig->sentinelpos >= 0)
-                       info->callconv = 2;
+                       callconv = 2;
                else
-                       info->callconv = 1;
+                       callconv = 1;
        }
-       info->callconv |= (sig->hasthis << 5) | (sig->explicit_this << 6); 
+       callconv |= (sig->hasthis << 5) | (sig->explicit_this << 6);
+       info->callconv = callconv;
 }
 
 ICALL_EXPORT MonoArrayHandle
 ves_icall_System_Reflection_MonoMethodInfo_get_parameter_info (MonoMethod *method, MonoReflectionMethodHandle member, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = mono_domain_get (); 
 
        MonoReflectionTypeHandle reftype = MONO_HANDLE_NEW (MonoReflectionType, NULL);
@@ -1852,7 +1839,7 @@ ves_icall_System_Reflection_MonoMethodInfo_get_parameter_info (MonoMethod *metho
 ICALL_EXPORT MonoReflectionMarshalAsAttributeHandle
 ves_icall_System_MonoMethodInfo_get_retval_marshal (MonoMethod *method, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = mono_domain_get (); 
        MonoReflectionMarshalAsAttributeHandle res = MONO_HANDLE_NEW (MonoReflectionMarshalAsAttribute, NULL);
 
@@ -1886,7 +1873,7 @@ ves_icall_MonoField_GetFieldOffset (MonoReflectionField *field)
 ICALL_EXPORT MonoReflectionTypeHandle
 ves_icall_MonoField_GetParentType (MonoReflectionFieldHandle field, MonoBoolean declaring, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (field);
        MonoClass *parent;
 
@@ -2152,7 +2139,7 @@ ves_icall_MonoField_GetRawConstantValue (MonoReflectionField *rfield)
 ICALL_EXPORT MonoReflectionTypeHandle
 ves_icall_MonoField_ResolveType (MonoReflectionFieldHandle ref_field, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_field);
        MonoClassField *field = MONO_HANDLE_GETVAL (ref_field, field);
        MonoType *type = mono_field_get_type_checked (field, error);
@@ -2173,60 +2160,63 @@ typedef enum {
 } PInfo;
 
 ICALL_EXPORT void
-ves_icall_MonoPropertyInfo_get_property_info (const MonoReflectionProperty *property, MonoPropertyInfo *info, PInfo req_info)
+ves_icall_MonoPropertyInfo_get_property_info (MonoReflectionPropertyHandle property, MonoPropertyInfo *info, PInfo req_info, MonoError *error)
 {
-       MonoError error;
-       MonoReflectionType *rt;
-       MonoReflectionMethod *rm;
-       MonoDomain *domain = mono_object_domain (property); 
-       const MonoProperty *pproperty = property->property;
+       error_init (error);
+       MonoDomain *domain = MONO_HANDLE_DOMAIN (property); 
+       const MonoProperty *pproperty = MONO_HANDLE_GETVAL (property, property);
 
        if ((req_info & PInfo_ReflectedType) != 0) {
-               rt = mono_type_get_object_checked (domain, &property->klass->byval_arg, &error);
-               if (mono_error_set_pending_exception (&error))
-                       return;
+               MonoClass *klass = MONO_HANDLE_GETVAL (property, klass);
+               MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, &klass->byval_arg, error);
+               return_if_nok (error);
 
-               MONO_STRUCT_SETREF (info, parent, rt);
+               MONO_STRUCT_SETREF (info, parent, MONO_HANDLE_RAW (rt));
        }
        if ((req_info & PInfo_DeclaringType) != 0) {
-               rt = mono_type_get_object_checked (domain, &pproperty->parent->byval_arg, &error);
-               if (mono_error_set_pending_exception (&error))
-                       return;
+               MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, &pproperty->parent->byval_arg, error);
+               return_if_nok (error);
 
-               MONO_STRUCT_SETREF (info, declaring_type, rt);
+               MONO_STRUCT_SETREF (info, declaring_type, MONO_HANDLE_RAW (rt));
        }
 
-       if ((req_info & PInfo_Name) != 0)
-               MONO_STRUCT_SETREF (info, name, mono_string_new (domain, pproperty->name));
+       if ((req_info & PInfo_Name) != 0) {
+               MonoStringHandle name = mono_string_new_handle (domain, pproperty->name, error);
+               return_if_nok (error);
+
+               MONO_STRUCT_SETREF (info, name, MONO_HANDLE_RAW (name));
+       }
 
        if ((req_info & PInfo_Attributes) != 0)
                info->attrs = pproperty->attrs;
 
        if ((req_info & PInfo_GetMethod) != 0) {
+               MonoClass *property_klass = MONO_HANDLE_GETVAL (property, klass);
+               MonoReflectionMethodHandle rm;
                if (pproperty->get &&
                    (((pproperty->get->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) != METHOD_ATTRIBUTE_PRIVATE) ||
-                    pproperty->get->klass == property->klass)) {
-                       rm = mono_method_get_object_checked (domain, pproperty->get, property->klass, &error);
-                       if (mono_error_set_pending_exception (&error))
-                               return;
+                    pproperty->get->klass == property_klass)) {
+                       rm = mono_method_get_object_handle (domain, pproperty->get, property_klass, error);
+                       return_if_nok (error);
                } else {
-                       rm = NULL;
+                       rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
                }
 
-               MONO_STRUCT_SETREF (info, get, rm);
+               MONO_STRUCT_SETREF (info, get, MONO_HANDLE_RAW (rm));
        }
        if ((req_info & PInfo_SetMethod) != 0) {
+               MonoClass *property_klass = MONO_HANDLE_GETVAL (property, klass);
+               MonoReflectionMethodHandle rm;
                if (pproperty->set &&
                    (((pproperty->set->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) != METHOD_ATTRIBUTE_PRIVATE) ||
-                    pproperty->set->klass == property->klass)) {
-                       rm =  mono_method_get_object_checked (domain, pproperty->set, property->klass, &error);
-                       if (mono_error_set_pending_exception (&error))
-                               return;
+                    pproperty->set->klass == property_klass)) {
+                       rm =  mono_method_get_object_handle (domain, pproperty->set, property_klass, error);
+                       return_if_nok (error);
                } else {
-                       rm = NULL;
+                       rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
                }
 
-               MONO_STRUCT_SETREF (info, set, rm);
+               MONO_STRUCT_SETREF (info, set, MONO_HANDLE_RAW (rm));
        }
        /* 
         * There may be other methods defined for properties, though, it seems they are not exposed 
@@ -2377,7 +2367,7 @@ get_interfaces_hash (gconstpointer v1)
 ICALL_EXPORT MonoArrayHandle
 ves_icall_RuntimeType_GetInterfaces (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
        MonoClass *klass = mono_class_from_mono_type (type);
 
@@ -2437,7 +2427,7 @@ static gboolean
 set_interface_map_data_method_object (MonoDomain *domain, MonoMethod *method, MonoClass *iclass, int ioffset, MonoClass *klass, MonoArrayHandle targets, MonoArrayHandle methods, int i, MonoError *error)
 {
        HANDLE_FUNCTION_ENTER ();
-       mono_error_init (error);
+       error_init (error);
        MonoReflectionMethodHandle member = mono_method_get_object_handle (domain, method, iclass, error);
        if (!is_ok (error))
                goto leave;
@@ -2457,7 +2447,7 @@ leave:
 ICALL_EXPORT void
 ves_icall_RuntimeType_GetInterfaceMapData (MonoReflectionTypeHandle ref_type, MonoReflectionTypeHandle ref_iface, MonoArrayHandleOut targets, MonoArrayHandleOut methods, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
        MonoClass *klass = mono_class_from_mono_type (type);
        MonoType *iface = MONO_HANDLE_GETVAL (ref_iface, type);
@@ -2498,7 +2488,7 @@ ves_icall_RuntimeType_GetInterfaceMapData (MonoReflectionTypeHandle ref_type, Mo
 ICALL_EXPORT void
 ves_icall_RuntimeType_GetPacking (MonoReflectionTypeHandle ref_type, guint32 *packing, guint32 *size, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
        MonoClass *klass = mono_class_from_mono_type (type);
 
@@ -2518,7 +2508,7 @@ ves_icall_RuntimeType_GetPacking (MonoReflectionTypeHandle ref_type, guint32 *pa
 ICALL_EXPORT MonoReflectionTypeHandle
 ves_icall_RuntimeTypeHandle_GetElementType (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
 
        MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
@@ -2547,7 +2537,7 @@ ves_icall_RuntimeTypeHandle_GetElementType (MonoReflectionTypeHandle ref_type, M
 ICALL_EXPORT MonoReflectionTypeHandle
 ves_icall_RuntimeTypeHandle_GetBaseType (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
 
        MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
@@ -2565,7 +2555,7 @@ ves_icall_RuntimeTypeHandle_GetBaseType (MonoReflectionTypeHandle ref_type, Mono
 ICALL_EXPORT MonoBoolean
 ves_icall_RuntimeTypeHandle_IsPointer (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
        return type->type == MONO_TYPE_PTR;
 }
@@ -2573,15 +2563,27 @@ ves_icall_RuntimeTypeHandle_IsPointer (MonoReflectionTypeHandle ref_type, MonoEr
 ICALL_EXPORT MonoBoolean
 ves_icall_RuntimeTypeHandle_IsPrimitive (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
        return (!type->byref && (((type->type >= MONO_TYPE_BOOLEAN) && (type->type <= MONO_TYPE_R8)) || (type->type == MONO_TYPE_I) || (type->type == MONO_TYPE_U)));
 }
 
+ICALL_EXPORT MonoBoolean
+ves_icall_RuntimeTypeHandle_HasReferences (MonoReflectionTypeHandle ref_type, MonoError *error)
+{
+       error_init (error);
+       MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
+       MonoClass *klass;
+
+       klass = mono_class_from_mono_type (type);
+       mono_class_init (klass);
+       return klass->has_references;
+}
+
 ICALL_EXPORT MonoBoolean
 ves_icall_RuntimeTypeHandle_IsByRef (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
        return type->byref;
 }
@@ -2589,7 +2591,7 @@ ves_icall_RuntimeTypeHandle_IsByRef (MonoReflectionTypeHandle ref_type, MonoErro
 ICALL_EXPORT MonoBoolean
 ves_icall_RuntimeTypeHandle_IsComObject (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
        MonoClass *klass = mono_class_from_mono_type (type);
        mono_class_init_checked (klass, error);
@@ -2602,14 +2604,14 @@ ves_icall_RuntimeTypeHandle_IsComObject (MonoReflectionTypeHandle ref_type, Mono
 ICALL_EXPORT guint32
 ves_icall_reflection_get_token (MonoObjectHandle obj, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        return mono_reflection_get_token_checked (obj, error);
 }
 
 ICALL_EXPORT MonoReflectionModuleHandle
 ves_icall_RuntimeTypeHandle_GetModule (MonoReflectionTypeHandle type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (type);
        MonoType *t = MONO_HANDLE_GETVAL (type, type);
        MonoClass *klass = mono_class_from_mono_type (t);
@@ -2619,7 +2621,7 @@ ves_icall_RuntimeTypeHandle_GetModule (MonoReflectionTypeHandle type, MonoError
 ICALL_EXPORT MonoReflectionAssemblyHandle
 ves_icall_RuntimeTypeHandle_GetAssembly (MonoReflectionTypeHandle type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = mono_domain_get (); 
        MonoType *t = MONO_HANDLE_GETVAL (type, type);
        MonoClass *klass = mono_class_from_mono_type (t);
@@ -2629,7 +2631,7 @@ ves_icall_RuntimeTypeHandle_GetAssembly (MonoReflectionTypeHandle type, MonoErro
 ICALL_EXPORT MonoReflectionTypeHandle
 ves_icall_RuntimeType_get_DeclaringType (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = mono_domain_get ();
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
        MonoClass *klass;
@@ -2689,7 +2691,7 @@ ves_icall_RuntimeType_get_Namespace (MonoReflectionTypeHandle type, MonoError *e
 ICALL_EXPORT gint32
 ves_icall_RuntimeTypeHandle_GetArrayRank (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
 
        if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
@@ -2712,7 +2714,7 @@ static gboolean
 set_type_object_in_array (MonoDomain *domain, MonoType *type, MonoArrayHandle dest, int i, MonoError *error)
 {
        HANDLE_FUNCTION_ENTER();
-       mono_error_init (error);
+       error_init (error);
        MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, type, error);
        if (!is_ok (error))
                goto leave;
@@ -2726,7 +2728,7 @@ leave:
 ICALL_EXPORT MonoArrayHandle
 ves_icall_RuntimeType_GetGenericArguments (MonoReflectionTypeHandle ref_type, MonoBoolean runtimeTypeArray, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
 
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
@@ -2763,7 +2765,7 @@ leave:
 ICALL_EXPORT gboolean
 ves_icall_RuntimeTypeHandle_IsGenericTypeDefinition (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
 
        if (!IS_MONOTYPE (MONO_HANDLE_RAW(ref_type)))
                return FALSE;
@@ -2779,7 +2781,7 @@ ves_icall_RuntimeTypeHandle_IsGenericTypeDefinition (MonoReflectionTypeHandle re
 ICALL_EXPORT MonoReflectionTypeHandle
 ves_icall_RuntimeTypeHandle_GetGenericTypeDefinition_impl (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
 
        MonoReflectionTypeHandle ret = MONO_HANDLE_NEW (MonoReflectionType, NULL);
@@ -2815,7 +2817,7 @@ leave:
 ICALL_EXPORT MonoReflectionTypeHandle
 ves_icall_RuntimeType_MakeGenericType (MonoReflectionTypeHandle reftype, MonoArrayHandle type_array, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (reftype);
 
        g_assert (IS_MONOTYPE_HANDLE (reftype));
@@ -2853,7 +2855,7 @@ ves_icall_RuntimeType_MakeGenericType (MonoReflectionTypeHandle reftype, MonoArr
 ICALL_EXPORT gboolean
 ves_icall_RuntimeTypeHandle_HasInstantiation (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoClass *klass;
 
        if (!IS_MONOTYPE (MONO_HANDLE_RAW (ref_type)))
@@ -2870,7 +2872,7 @@ ves_icall_RuntimeTypeHandle_HasInstantiation (MonoReflectionTypeHandle ref_type,
 ICALL_EXPORT gint32
 ves_icall_RuntimeType_GetGenericParameterPosition (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        if (!IS_MONOTYPE_HANDLE (ref_type))
                return -1;
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
@@ -2883,7 +2885,7 @@ ves_icall_RuntimeType_GetGenericParameterPosition (MonoReflectionTypeHandle ref_
 ICALL_EXPORT MonoGenericParamInfo *
 ves_icall_RuntimeTypeHandle_GetGenericParameterInfo (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
        return mono_generic_param_info (type->data.generic_param);
 }
@@ -2900,7 +2902,7 @@ ves_icall_RuntimeType_GetCorrespondingInflatedMethod (MonoReflectionTypeHandle r
                                                      MonoReflectionMethodHandle generic,
                                                      MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
        MonoClass *klass = mono_class_from_mono_type (type);
@@ -2928,7 +2930,7 @@ ves_icall_RuntimeType_GetCorrespondingInflatedMethod (MonoReflectionTypeHandle r
 ICALL_EXPORT MonoReflectionMethodHandle
 ves_icall_RuntimeType_get_DeclaringMethod (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
        MonoReflectionMethodHandle ret = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
 
@@ -2952,7 +2954,7 @@ leave:
 ICALL_EXPORT MonoBoolean
 ves_icall_System_RuntimeType_IsTypeExportedToWindowsRuntime (MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        mono_error_set_not_implemented (error, "%s", "");
        return FALSE;
 }
@@ -2960,7 +2962,7 @@ ves_icall_System_RuntimeType_IsTypeExportedToWindowsRuntime (MonoError *error)
 ICALL_EXPORT MonoBoolean
 ves_icall_System_RuntimeType_IsWindowsRuntimeObjectType (MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        mono_error_set_not_implemented (error, "%s", "");
        return FALSE;
 }
@@ -2980,7 +2982,7 @@ ves_icall_MonoMethod_GetPInvoke (MonoReflectionMethodHandle ref_method, int* fla
        const char *import = NULL;
        const char *scope = NULL;
 
-       mono_error_init (error);
+       error_init (error);
 
        if (image_is_dynamic (image)) {
                MonoReflectionMethodAux *method_aux = 
@@ -3015,7 +3017,7 @@ ves_icall_MonoMethod_GetPInvoke (MonoReflectionMethodHandle ref_method, int* fla
 ICALL_EXPORT MonoReflectionMethodHandle
 ves_icall_MonoMethod_GetGenericMethodDefinition (MonoReflectionMethodHandle ref_method, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoMethod *method = MONO_HANDLE_GETVAL (ref_method, method);
 
        if (method->is_generic)
@@ -3076,7 +3078,7 @@ static gboolean
 set_array_generic_argument_handle_inflated (MonoDomain *domain, MonoGenericInst *inst, int i, MonoArrayHandle arr, MonoError *error)
 {
        HANDLE_FUNCTION_ENTER ();
-       mono_error_init (error);
+       error_init (error);
        MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, inst->type_argv [i], error);
        if (!is_ok (error))
                goto leave;
@@ -3089,7 +3091,7 @@ static gboolean
 set_array_generic_argument_handle_gparam (MonoDomain *domain, MonoGenericContainer *container, int i, MonoArrayHandle arr, MonoError *error)
 {
        HANDLE_FUNCTION_ENTER ();
-       mono_error_init (error);
+       error_init (error);
        MonoGenericParam *param = mono_generic_container_get_param (container, i);
        MonoClass *pklass = mono_class_from_generic_parameter_internal (param);
        MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, &pklass->byval_arg, error);
@@ -3103,7 +3105,7 @@ leave:
 ICALL_EXPORT MonoArrayHandle
 ves_icall_MonoMethod_GetGenericArguments (MonoReflectionMethodHandle ref_method, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_method);
        MonoMethod *method = MONO_HANDLE_GETVAL (ref_method, method);
 
@@ -3665,7 +3667,7 @@ ves_icall_System_Enum_get_hashcode (MonoObject *eobj)
 static void
 get_enum_field (MonoDomain *domain, MonoArrayHandle names, MonoArrayHandle values, int base_type, MonoClassField *field, guint* j, guint64 *previous_value, gboolean *sorted, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        HANDLE_FUNCTION_ENTER();
        guint64 field_value;
        const char *p;
@@ -3709,7 +3711,7 @@ ves_icall_System_Enum_GetEnumValuesAndNames (MonoReflectionTypeHandle type, Mono
        guint64 previous_value = 0;
        gboolean sorted = TRUE;
 
-       mono_error_init (error);
+       error_init (error);
        mono_class_init_checked (enumc, error);
        return_val_if_nok (error, FALSE);
 
@@ -3759,7 +3761,7 @@ enum {
 ICALL_EXPORT GPtrArray*
 ves_icall_RuntimeType_GetFields_native (MonoReflectionTypeHandle ref_type, char *utf8_name, guint32 bflags, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
 
        if (type->byref) {
@@ -3855,7 +3857,7 @@ mono_class_get_methods_by_name (MonoClass *klass, const char *name, guint32 bfla
 
        array = g_ptr_array_new ();
        startklass = klass;
-       mono_error_init (error);
+       error_init (error);
 
        if (name != NULL)
                compare_func = (ignore_case) ? mono_utf8_strcasecmp : strcmp;
@@ -3952,7 +3954,7 @@ loader_error:
 ICALL_EXPORT GPtrArray*
 ves_icall_RuntimeType_GetMethodsByName_native (MonoReflectionTypeHandle ref_type, const char *mname, guint32 bflags, MonoBoolean ignore_case, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
 
        MonoClass *klass = mono_class_from_mono_type (type);
@@ -3966,7 +3968,7 @@ ves_icall_RuntimeType_GetMethodsByName_native (MonoReflectionTypeHandle ref_type
 ICALL_EXPORT GPtrArray*
 ves_icall_RuntimeType_GetConstructors_native (MonoReflectionTypeHandle ref_type, guint32 bflags, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
        if (type->byref) {
                return g_ptr_array_new ();
@@ -4083,7 +4085,7 @@ property_accessor_nonpublic (MonoMethod* accessor, gboolean start_klass)
 ICALL_EXPORT GPtrArray*
 ves_icall_RuntimeType_GetPropertiesByName_native (MonoReflectionTypeHandle ref_type, gchar *propname, guint32 bflags, MonoBoolean ignore_case, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
 
 
@@ -4189,7 +4191,7 @@ event_equal (MonoEvent *event1, MonoEvent *event2)
 ICALL_EXPORT GPtrArray*
 ves_icall_RuntimeType_GetEvents_native (MonoReflectionTypeHandle ref_type, char *utf8_name, guint32 bflags, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
 
        if (type->byref) {
@@ -4281,7 +4283,7 @@ failure:
 ICALL_EXPORT GPtrArray *
 ves_icall_RuntimeType_GetNestedTypes_native (MonoReflectionTypeHandle ref_type, char *str, guint32 bflags, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
 
        if (type->byref) {
@@ -4331,7 +4333,7 @@ static MonoType*
 get_type_from_module_builder_module (MonoArrayHandle modules, int i, MonoTypeNameParse *info, MonoBoolean ignoreCase, gboolean *type_resolve, MonoError *error)
 {
        HANDLE_FUNCTION_ENTER ();
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = NULL;
        MonoReflectionModuleBuilderHandle mb = MONO_HANDLE_NEW (MonoReflectionModuleBuilder, NULL);
        MONO_HANDLE_ARRAY_GETREF (mb, modules, i);
@@ -4344,7 +4346,7 @@ static MonoType*
 get_type_from_module_builder_loaded_modules (MonoArrayHandle loaded_modules, int i, MonoTypeNameParse *info, MonoBoolean ignoreCase, gboolean *type_resolve, MonoError *error)
 {
        HANDLE_FUNCTION_ENTER ();
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = NULL;
        MonoReflectionModuleHandle mod = MONO_HANDLE_NEW (MonoReflectionModule, NULL);
        MONO_HANDLE_ARRAY_GETREF (mod, loaded_modules, i);
@@ -4356,7 +4358,7 @@ get_type_from_module_builder_loaded_modules (MonoArrayHandle loaded_modules, int
 ICALL_EXPORT MonoReflectionTypeHandle
 ves_icall_System_Reflection_Assembly_InternalGetType (MonoReflectionAssemblyHandle assembly_h, MonoReflectionModuleHandle module, MonoStringHandle name, MonoBoolean throwOnError, MonoBoolean ignoreCase, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
 
        MonoTypeNameParse info;
        gboolean type_resolve;
@@ -4522,7 +4524,7 @@ replace_shadow_path (MonoDomain *domain, gchar *dirname, gchar **filename)
 ICALL_EXPORT MonoStringHandle
 ves_icall_System_Reflection_Assembly_get_code_base (MonoReflectionAssemblyHandle assembly, MonoBoolean escaped, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly);
        MonoAssembly *mass = MONO_HANDLE_GETVAL (assembly, assembly);
        gchar *absolute;
@@ -4564,7 +4566,7 @@ ves_icall_System_Reflection_Assembly_get_code_base (MonoReflectionAssemblyHandle
 ICALL_EXPORT MonoBoolean
 ves_icall_System_Reflection_Assembly_get_global_assembly_cache (MonoReflectionAssemblyHandle assembly, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoAssembly *mass = MONO_HANDLE_GETVAL (assembly,assembly);
 
        return mass->in_gac;
@@ -4602,7 +4604,7 @@ ves_icall_System_Reflection_Assembly_get_location (MonoReflectionAssemblyHandle
 ICALL_EXPORT MonoBoolean
 ves_icall_System_Reflection_Assembly_get_ReflectionOnly (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
        return assembly->ref_only;
 }
@@ -4619,7 +4621,7 @@ ves_icall_System_Reflection_Assembly_InternalImageRuntimeVersion (MonoReflection
 ICALL_EXPORT MonoReflectionMethodHandle
 ves_icall_System_Reflection_Assembly_get_EntryPoint (MonoReflectionAssemblyHandle assembly_h, MonoError *error) 
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
        MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
        MonoMethod *method;
@@ -4641,7 +4643,7 @@ leave:
 ICALL_EXPORT MonoReflectionModuleHandle
 ves_icall_System_Reflection_Assembly_GetManifestModuleInternal (MonoReflectionAssemblyHandle assembly, MonoError *error) 
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly);
        MonoAssembly *a = MONO_HANDLE_GETVAL (assembly, assembly);
        return mono_module_get_object_handle (domain, a->image, error);
@@ -4651,7 +4653,7 @@ static gboolean
 add_manifest_resource_name_to_array (MonoDomain *domain, MonoImage *image, MonoTableInfo *table, int i, MonoArrayHandle dest, MonoError *error)
 {
        HANDLE_FUNCTION_ENTER ();
-       mono_error_init (error);
+       error_init (error);
        const char *val = mono_metadata_string_heap (image, mono_metadata_decode_row_col (table, i, MONO_MANIFEST_NAME));
        MonoStringHandle str = mono_string_new_handle (domain, val, error);
        if (!is_ok (error))
@@ -4664,7 +4666,7 @@ leave:
 ICALL_EXPORT MonoArrayHandle
 ves_icall_System_Reflection_Assembly_GetManifestResourceNames (MonoReflectionAssemblyHandle assembly_h, MonoError *error) 
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
        MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
        MonoTableInfo *table = &assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
@@ -4696,7 +4698,7 @@ ves_icall_System_Reflection_Assembly_GetAotId (MonoError *error)
 static MonoAssemblyName*
 create_referenced_assembly_name (MonoDomain *domain, MonoImage *image, MonoTableInfo *t, int i, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoAssemblyName *aname = g_new0 (MonoAssemblyName, 1);
 
        mono_assembly_get_assemblyref (image, i, aname);
@@ -4719,7 +4721,7 @@ create_referenced_assembly_name (MonoDomain *domain, MonoImage *image, MonoTable
 ICALL_EXPORT GPtrArray*
 ves_icall_System_Reflection_Assembly_InternalGetReferencedAssemblies (MonoReflectionAssemblyHandle assembly, MonoError *error) 
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly);
        MonoAssembly *ass = MONO_HANDLE_GETVAL(assembly, assembly);
        MonoImage *image = ass->image;
@@ -4758,7 +4760,7 @@ g_concat_dir_and_file (const char *dir, const char *file)
 ICALL_EXPORT void *
 ves_icall_System_Reflection_Assembly_GetManifestResourceInternal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, gint32 *size, MonoReflectionModuleHandleOut ref_module, MonoError *error) 
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
        MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
        MonoTableInfo *table = &assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
@@ -4887,7 +4889,7 @@ leave:
 ICALL_EXPORT gboolean
 ves_icall_System_Reflection_Assembly_GetManifestResourceInfoInternal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, MonoManifestResourceInfoHandle info_h, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        return get_manifest_resource_info_internal (assembly_h, name, info_h, error);
 }
 
@@ -4895,7 +4897,7 @@ static gboolean
 add_filename_to_files_array (MonoDomain *domain, MonoAssembly * assembly, MonoTableInfo *table, int i, MonoArrayHandle dest, int dest_idx, MonoError *error)
 {
        HANDLE_FUNCTION_ENTER();
-       mono_error_init (error);
+       error_init (error);
        const char *val = mono_metadata_string_heap (assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
        char *n = g_concat_dir_and_file (assembly->basedir, val);
        MonoStringHandle str = mono_string_new_handle (domain, n, error);
@@ -4910,7 +4912,7 @@ leave:
 ICALL_EXPORT MonoObjectHandle
 ves_icall_System_Reflection_Assembly_GetFilesInternal (MonoReflectionAssemblyHandle assembly_h, MonoStringHandle name, MonoBoolean resource_modules, MonoError *error) 
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly_h);
        MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
        MonoTableInfo *table = &assembly->image->tables [MONO_TABLE_FILE];
@@ -4965,7 +4967,7 @@ static gboolean
 add_module_to_modules_array (MonoDomain *domain, MonoArrayHandle dest, int *dest_idx, MonoImage* module, MonoError *error)
 {
        HANDLE_FUNCTION_ENTER ();
-       mono_error_init (error);
+       error_init (error);
        if (module) {
                MonoReflectionModuleHandle rm = mono_module_get_object_handle (domain, module, error);
                if (!is_ok (error))
@@ -4983,7 +4985,7 @@ static gboolean
 add_file_to_modules_array (MonoDomain *domain, MonoArrayHandle dest, int dest_idx, MonoImage *image, MonoTableInfo *table, int table_idx,  MonoError *error)
 {
        HANDLE_FUNCTION_ENTER ();
-       mono_error_init (error);
+       error_init (error);
 
        guint32 cols [MONO_FILE_SIZE];
        mono_metadata_decode_row (table, table_idx, cols, MONO_FILE_SIZE);
@@ -5014,7 +5016,7 @@ leave:
 ICALL_EXPORT MonoArrayHandle
 ves_icall_System_Reflection_Assembly_GetModulesInternal (MonoReflectionAssemblyHandle assembly_h, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = mono_domain_get();
        MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
        MonoClass *klass;
@@ -5067,7 +5069,7 @@ fail:
 ICALL_EXPORT MonoReflectionMethodHandle
 ves_icall_GetCurrentMethod (MonoError *error) 
 {
-       mono_error_init (error);
+       error_init (error);
 
        MonoMethod *m = mono_method_get_last_managed ();
 
@@ -5124,7 +5126,7 @@ mono_method_get_equivalent_method (MonoMethod *method, MonoClass *klass)
 ICALL_EXPORT MonoReflectionMethodHandle
 ves_icall_System_Reflection_MethodBase_GetMethodFromHandleInternalType_native (MonoMethod *method, MonoType *type, MonoBoolean generic_check, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoClass *klass;
        if (type && generic_check) {
                klass = mono_class_from_mono_type (type);
@@ -5146,14 +5148,14 @@ ves_icall_System_Reflection_MethodBase_GetMethodFromHandleInternalType_native (M
 ICALL_EXPORT MonoReflectionMethodBodyHandle
 ves_icall_System_Reflection_MethodBase_GetMethodBodyInternal (MonoMethod *method, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        return mono_method_body_get_object_handle (mono_domain_get (), method, error);
 }
 
 ICALL_EXPORT MonoReflectionAssemblyHandle
 ves_icall_System_Reflection_Assembly_GetExecutingAssembly (MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
 
        MonoMethod *dest = NULL;
        mono_stack_walk_no_il (get_executing, &dest);
@@ -5165,7 +5167,7 @@ ves_icall_System_Reflection_Assembly_GetExecutingAssembly (MonoError *error)
 ICALL_EXPORT MonoReflectionAssemblyHandle
 ves_icall_System_Reflection_Assembly_GetEntryAssembly (MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
 
        MonoDomain* domain = mono_domain_get ();
 
@@ -5178,7 +5180,7 @@ ves_icall_System_Reflection_Assembly_GetEntryAssembly (MonoError *error)
 ICALL_EXPORT MonoReflectionAssemblyHandle
 ves_icall_System_Reflection_Assembly_GetCallingAssembly (MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoMethod *m;
        MonoMethod *dest;
 
@@ -5230,7 +5232,7 @@ ves_icall_System_RuntimeType_getFullName (MonoReflectionTypeHandle object, gbool
 ICALL_EXPORT int
 vell_icall_RuntimeType_get_core_clr_security_level (MonoReflectionTypeHandle rfield, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (rfield, type);
        MonoClass *klass = mono_class_from_mono_type (type);
 
@@ -5250,7 +5252,7 @@ ves_icall_MonoField_get_core_clr_security_level (MonoReflectionField *rfield)
 ICALL_EXPORT int
 ves_icall_MonoMethod_get_core_clr_security_level (MonoReflectionMethodHandle rfield, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoMethod *method = MONO_HANDLE_GETVAL (rfield, method);
        return mono_security_core_clr_method_level (method, TRUE);
 }
@@ -5258,7 +5260,7 @@ ves_icall_MonoMethod_get_core_clr_security_level (MonoReflectionMethodHandle rfi
 ICALL_EXPORT MonoStringHandle
 ves_icall_System_Reflection_Assembly_get_fullName (MonoReflectionAssemblyHandle assembly, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (assembly);
        MonoAssembly *mass = MONO_HANDLE_GETVAL (assembly, assembly);
        gchar *name;
@@ -5285,7 +5287,7 @@ ves_icall_System_Reflection_Assembly_InternalGetAssemblyName (MonoStringHandle f
        MonoImage *image;
        char *dirname;
 
-       mono_error_init (error);
+       error_init (error);
 
        filename = mono_string_handle_to_utf8 (fname, error);
        return_if_nok (error);
@@ -5337,7 +5339,7 @@ ICALL_EXPORT MonoBoolean
 ves_icall_System_Reflection_Assembly_LoadPermissions (MonoReflectionAssemblyHandle assembly_h,
                                                      char **minimum, guint32 *minLength, char **optional, guint32 *optLength, char **refused, guint32 *refLength, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoAssembly *assembly = MONO_HANDLE_GETVAL (assembly_h, assembly);
        MonoBoolean result = FALSE;
        MonoDeclSecurityEntry entry;
@@ -5382,7 +5384,7 @@ mono_module_type_is_visible (MonoTableInfo *tdef, MonoImage *image, int type)
 static void
 image_get_type (MonoDomain *domain, MonoImage *image, MonoTableInfo *tdef, int table_idx, int count, MonoArrayHandle res, MonoArrayHandle exceptions, MonoBoolean exportedOnly, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        HANDLE_FUNCTION_ENTER ();
        MonoError klass_error;
        MonoClass *klass = mono_class_get_checked (image, table_idx | MONO_TOKEN_TYPE_DEF, &klass_error);
@@ -5405,7 +5407,7 @@ mono_module_get_types (MonoDomain *domain, MonoImage *image, MonoArrayHandleOut
        MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
        int i, count;
 
-       mono_error_init (error);
+       error_init (error);
 
        /* we start the count from 1 because we skip the special type <Module> */
        if (exportedOnly) {
@@ -5437,7 +5439,7 @@ static void
 append_module_types (MonoDomain *domain, MonoArrayHandleOut res, MonoArrayHandleOut exceptions, MonoImage *image, MonoBoolean exportedOnly, MonoError *error)
 {
        HANDLE_FUNCTION_ENTER ();
-       mono_error_init (error);
+       error_init (error);
        MonoArrayHandle ex2 = MONO_HANDLE_NEW (MonoArray, NULL);
        MonoArrayHandle res2 = mono_module_get_types (domain, image, ex2, exportedOnly, error);
        if (!is_ok (error))
@@ -5475,7 +5477,7 @@ set_class_failure_in_array (MonoArrayHandle exl, int i, MonoClass *klass)
 {
        HANDLE_FUNCTION_ENTER ();
        MonoError unboxed_error;
-       mono_error_init (&unboxed_error);
+       error_init (&unboxed_error);
        mono_error_set_for_class_failure (&unboxed_error, klass);
 
        MonoExceptionHandle exc = MONO_HANDLE_NEW (MonoException, mono_error_convert_to_exception (&unboxed_error));
@@ -5668,7 +5670,7 @@ ves_icall_System_Reflection_Module_GetMDStreamVersion (MonoImage *image, MonoErr
 ICALL_EXPORT MonoArrayHandle
 ves_icall_System_Reflection_Module_InternalGetTypes (MonoReflectionModuleHandle module, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
 
        MonoImage *image = MONO_HANDLE_GETVAL (module, image);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (module);
@@ -5760,7 +5762,7 @@ static MonoType*
 module_resolve_type_token (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
 {
        HANDLE_FUNCTION_ENTER ();
-       mono_error_init (error);
+       error_init (error);
        MonoType *result = NULL;
        MonoClass *klass;
        int table = mono_metadata_token_table (token);
@@ -5821,7 +5823,7 @@ static MonoMethod*
 module_resolve_method_token (MonoImage *image, guint32 token, MonoArrayHandle type_args, MonoArrayHandle method_args, MonoResolveTokenError *resolve_error, MonoError *error)
 {
        HANDLE_FUNCTION_ENTER ();
-       mono_error_init (error);
+       error_init (error);
        MonoMethod *method = NULL;
        int table = mono_metadata_token_table (token);
        int index = mono_metadata_token_index (token);
@@ -5920,7 +5922,7 @@ module_resolve_field_token (MonoImage *image, guint32 token, MonoArrayHandle typ
        MonoGenericContext context;
        MonoClassField *field = NULL;
 
-       mono_error_init (error);
+       error_init (error);
        *resolve_error = ResolveTokenError_Other;
 
        /* Validate token */
@@ -5976,7 +5978,7 @@ ves_icall_System_Reflection_Module_ResolveMemberToken (MonoImage *image, guint32
 {
        int table = mono_metadata_token_table (token);
 
-       mono_error_init (merror);
+       error_init (merror);
        *error = ResolveTokenError_Other;
 
        switch (table) {
@@ -6034,7 +6036,7 @@ ves_icall_System_Reflection_Module_ResolveMemberToken (MonoImage *image, guint32
 ICALL_EXPORT MonoArrayHandle
 ves_icall_System_Reflection_Module_ResolveSignature (MonoImage *image, guint32 token, MonoResolveTokenError *resolve_error, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        int table = mono_metadata_token_table (token);
        int idx = mono_metadata_token_index (token);
        MonoTableInfo *tables = image->tables;
@@ -6071,7 +6073,7 @@ ves_icall_System_Reflection_Module_ResolveSignature (MonoImage *image, guint32 t
 ICALL_EXPORT MonoBoolean
 ves_icall_RuntimeTypeHandle_IsArray (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
 
        MonoBoolean res = !type->byref && (type->type == MONO_TYPE_ARRAY || type->type == MONO_TYPE_SZARRAY);
@@ -6084,7 +6086,7 @@ check_for_invalid_type (MonoClass *klass, MonoError *error)
 {
        char *name;
 
-       mono_error_init (error);
+       error_init (error);
 
        if (klass->byval_arg.type != MONO_TYPE_TYPEDBYREF)
                return;
@@ -6092,10 +6094,11 @@ check_for_invalid_type (MonoClass *klass, MonoError *error)
        name = mono_type_get_full_name (klass);
        mono_error_set_type_load_name (error, name, g_strdup (""), "");
 }
+
 ICALL_EXPORT MonoReflectionTypeHandle
 ves_icall_RuntimeType_make_array_type (MonoReflectionTypeHandle ref_type, int rank, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
 
        MonoClass *klass = mono_class_from_mono_type (type);
@@ -6109,6 +6112,11 @@ ves_icall_RuntimeType_make_array_type (MonoReflectionTypeHandle ref_type, int ra
        else
                aklass = mono_bounded_array_class_get (klass, rank, TRUE);
 
+       if (mono_class_has_failure (aklass)) {
+               mono_error_set_for_class_failure (error, aklass);
+               return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
+       }
+
        MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
        return mono_type_get_object_handle (domain, &aklass->byval_arg, error);
 }
@@ -6116,7 +6124,7 @@ ves_icall_RuntimeType_make_array_type (MonoReflectionTypeHandle ref_type, int ra
 ICALL_EXPORT MonoReflectionTypeHandle
 ves_icall_RuntimeType_make_byref_type (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
 
        MonoClass *klass = mono_class_from_mono_type (type);
@@ -6135,7 +6143,7 @@ ves_icall_RuntimeType_make_byref_type (MonoReflectionTypeHandle ref_type, MonoEr
 ICALL_EXPORT MonoReflectionTypeHandle
 ves_icall_RuntimeType_MakePointerType (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
        MonoClass *klass = mono_class_from_mono_type (type);
@@ -6239,7 +6247,10 @@ ves_icall_System_Delegate_GetVirtualMethod_internal (MonoDelegate *delegate)
 {
        MonoReflectionMethod *ret = NULL;
        MonoError error;
-       ret = mono_method_get_object_checked (mono_domain_get (), mono_object_get_virtual_method (delegate->target, delegate->method), mono_object_class (delegate->target), &error);
+       MonoMethod *m;
+
+       m = mono_object_get_virtual_method (delegate->target, delegate->method);
+       ret = mono_method_get_object_checked (mono_domain_get (), m, m->klass, &error);
        mono_error_set_pending_exception (&error);
        return ret;
 }
@@ -6341,7 +6352,7 @@ ves_icall_System_Buffer_BlockCopyInternal (MonoArray *src, gint32 src_offset, Mo
 ICALL_EXPORT MonoObjectHandle
 ves_icall_Remoting_RealProxy_GetTransparentProxy (MonoObjectHandle this_obj, MonoStringHandle class_name, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (this_obj);
        MonoRealProxyHandle rp = MONO_HANDLE_CAST (MonoRealProxy, this_obj);
 
@@ -6385,6 +6396,8 @@ ICALL_EXPORT MonoReflectionType *
 ves_icall_Remoting_RealProxy_InternalGetProxyType (MonoTransparentProxy *tp)
 {
        MonoError error;
+       g_assert (tp != NULL && mono_object_class (tp) == mono_defaults.transparent_proxy_class);
+       g_assert (tp->remote_class != NULL && tp->remote_class->proxy_class != NULL);
        MonoReflectionType *ret = mono_type_get_object_checked (mono_object_domain (tp), &tp->remote_class->proxy_class->byval_arg, &error);
        mono_error_set_pending_exception (&error);
 
@@ -6505,7 +6518,7 @@ ves_icall_System_Environment_GetIs64BitOperatingSystem (void)
 ICALL_EXPORT MonoStringHandle
 ves_icall_System_Environment_GetEnvironmentVariable_native (const gchar *utf8_name, MonoError *error)
 {
-       const gchar *value;
+       gchar *value;
 
        if (utf8_name == NULL)
                return NULL_HANDLE_STRING;
@@ -6515,7 +6528,9 @@ ves_icall_System_Environment_GetEnvironmentVariable_native (const gchar *utf8_na
        if (value == 0)
                return NULL_HANDLE_STRING;
        
-       return mono_string_new_handle (mono_domain_get (), value, error);
+       MonoStringHandle res = mono_string_new_handle (mono_domain_get (), value, error);
+       g_free (value);
+       return res;
 }
 
 /*
@@ -6543,7 +6558,7 @@ char **environ;
 #endif
 
 ICALL_EXPORT MonoArray *
-ves_icall_System_Environment_GetCoomandLineArgs (void)
+ves_icall_System_Environment_GetCommandLineArgs (void)
 {
        MonoError error;
        MonoArray *result = mono_runtime_get_main_args_checked (&error);
@@ -6646,10 +6661,6 @@ ves_icall_System_Environment_Exit (int result)
        /* Suspend all managed threads since the runtime is going away */
        mono_thread_suspend_all_other_threads ();
 
-       //FIXME shutdown is, weirdly enough, abortible in gc.c so we add this hack for now, see https://bugzilla.xamarin.com/show_bug.cgi?id=51653
-       mono_threads_begin_abort_protected_block ();
-       mono_thread_info_clear_self_interrupt ();
-
        mono_runtime_quit ();
 #endif
 
@@ -7181,7 +7192,7 @@ ves_icall_System_Web_Util_ICalls_get_machine_install_dir (void)
 ICALL_EXPORT gboolean
 ves_icall_get_resources_ptr (MonoReflectionAssemblyHandle assembly, gpointer *result, gint32 *size, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoPEResourceDataEntry *entry;
        MonoImage *image;
 
@@ -7246,7 +7257,7 @@ ves_icall_System_Diagnostics_DefaultTraceListener_WriteWindowsDebugString (MonoS
 ICALL_EXPORT MonoObjectHandle
 ves_icall_System_Activator_CreateInstanceInternal (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
        MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
        MonoClass *klass = mono_class_from_mono_type (type);
@@ -7265,21 +7276,31 @@ ves_icall_System_Activator_CreateInstanceInternal (MonoReflectionTypeHandle ref_
 ICALL_EXPORT MonoReflectionMethodHandle
 ves_icall_MonoMethod_get_base_method (MonoReflectionMethodHandle m, gboolean definition, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoMethod *method = MONO_HANDLE_GETVAL (m, method);
 
        MonoMethod *base = mono_method_get_base_method (method, definition, error);
        return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
-       if (base == method)
-               return m;
-       else
-               return mono_method_get_object_handle (mono_domain_get (), base, NULL, error);
+       if (base == method) {
+               /* we want to short-circuit and return 'm' here. But we should
+                  return the same method object that
+                  mono_method_get_object_handle, below would return.  Since
+                  that call takes NULL for the reftype argument, it will take
+                  base->klass as the reflected type for the MonoMethod.  So we
+                  need to check that m also has base->klass as the reflected
+                  type. */
+               MonoReflectionTypeHandle orig_reftype = MONO_HANDLE_NEW_GET (MonoReflectionType, m, reftype);
+               MonoClass *orig_klass = mono_class_from_mono_type (MONO_HANDLE_GETVAL (orig_reftype, type));
+               if (base->klass == orig_klass)
+                       return m;
+       }
+       return mono_method_get_object_handle (mono_domain_get (), base, NULL, error);
 }
 
 ICALL_EXPORT MonoStringHandle
 ves_icall_MonoMethod_get_name (MonoReflectionMethodHandle m, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoMethod *method = MONO_HANDLE_GETVAL (m, method);
 
        MonoStringHandle s = mono_string_new_handle (MONO_HANDLE_DOMAIN (m), method->name, error);
@@ -7457,7 +7478,7 @@ prelink_method (MonoMethod *method, MonoError *error)
 {
        const char *exc_class, *exc_arg;
 
-       mono_error_init (error);
+       error_init (error);
        if (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
                return;
        mono_lookup_pinvoke_call (method, &exc_class, &exc_arg);
@@ -7518,7 +7539,7 @@ static gboolean
 add_modifier_to_array (MonoDomain *domain, MonoImage *image, MonoCustomMod *modifier, MonoArrayHandle dest, int dest_idx, MonoError *error)
 {
        HANDLE_FUNCTION_ENTER ();
-       mono_error_init (error);
+       error_init (error);
        MonoClass *klass = mono_class_get_checked (image, modifier->token, error);
        if (!is_ok (error))
                goto leave;
@@ -7542,7 +7563,7 @@ type_array_from_modifiers (MonoImage *image, MonoType *type, int optional, MonoE
        int i, count = 0;
        MonoDomain *domain = mono_domain_get ();
 
-       mono_error_init (error);
+       error_init (error);
        for (i = 0; i < type->num_mods; ++i) {
                if ((optional && !type->modifiers [i].required) || (!optional && type->modifiers [i].required))
                        count++;
@@ -7569,7 +7590,7 @@ fail:
 ICALL_EXPORT MonoArrayHandle
 ves_icall_ParameterInfo_GetTypeModifiers (MonoReflectionParameterHandle param, MonoBoolean optional, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoReflectionTypeHandle rt = MONO_HANDLE_NEW (MonoReflectionType, NULL);
        MONO_HANDLE_GET (rt, param, ClassImpl);
        MonoType *type = MONO_HANDLE_GETVAL (rt, type);
@@ -7623,7 +7644,7 @@ get_property_type (MonoProperty *prop)
 ICALL_EXPORT MonoArrayHandle
 ves_icall_MonoPropertyInfo_GetTypeModifiers (MonoReflectionPropertyHandle property, MonoBoolean optional, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoProperty *prop = MONO_HANDLE_GETVAL (property, property);
        MonoClass *klass = MONO_HANDLE_GETVAL (property, klass);
        MonoType *type = get_property_type (prop);
@@ -7748,7 +7769,7 @@ ves_icall_Mono_Runtime_GetDisplayName (MonoError *error)
        char *info;
        MonoStringHandle display_name;
 
-       mono_error_init (error);
+       error_init (error);
        info = mono_get_runtime_callbacks ()->get_runtime_build_info ();
        display_name = mono_string_new_handle (mono_domain_get (), info, error);
        g_free (info);
@@ -8070,12 +8091,12 @@ mono_icall_cleanup (void)
 
 /**
  * mono_add_internal_call:
- * @name: method specification to surface to the managed world
- * @method: pointer to a C method to invoke when the method is called
+ * \param name method specification to surface to the managed world
+ * \param method pointer to a C method to invoke when the method is called
  *
- * This method surfaces the C function pointed by @method as a method
+ * This method surfaces the C function pointed by \p method as a method
  * that has been surfaced in managed code with the method specified in
- * @name as an internal call.
+ * \p name as an internal call.
  *
  * Internal calls are surfaced to all app domains loaded and they are
  * accessibly by a type with the specified name.
@@ -8086,15 +8107,16 @@ mono_icall_cleanup (void)
  *
  * For example, the following are all valid declarations:
  *
- * "MyApp.Services.ScriptService:Accelerate"
- * "MyApp.Services.ScriptService:Slowdown(int,bool)"
+ * \c MyApp.Services.ScriptService:Accelerate
+ *
+ * \c MyApp.Services.ScriptService:Slowdown(int,bool)
  *
  * You use method parameters in cases where there might be more than
  * one surface method to managed code.  That way you can register different
  * internal calls for different method overloads.
  *
  * The internal calls are invoked with no marshalling.   This means that .NET
- * types like System.String are exposed as `MonoString *` parameters.   This is
+ * types like \c System.String are exposed as \c MonoString* parameters.   This is
  * different than the way that strings are surfaced in P/Invoke.
  *
  * For more information on how the parameters are marshalled, see the
@@ -8254,11 +8276,10 @@ no_icall_table (void)
 
 /**
  * mono_lookup_internal_call_full:
- * @method: the method to look up
- * @uses_handles: out argument if method needs handles around managed objects.
- *
- * Returns a pointer to the icall code for the given method.  If
- * uses_handles is not NULL, it will be set to TRUE if the method
+ * \param method the method to look up
+ * \param uses_handles out argument if method needs handles around managed objects.
+ * \returns a pointer to the icall code for the given method.  If
+ * \p uses_handles is not NULL, it will be set to TRUE if the method
  * needs managed objects wrapped using the infrastructure in handle.h
  *
  * If the method is not found, warns and returns NULL.
@@ -8387,6 +8408,9 @@ mono_lookup_internal_call_full (MonoMethod *method, mono_bool *uses_handles)
 #endif
 }
 
+/**
+ * mono_lookup_internal_call:
+ */
 gpointer
 mono_lookup_internal_call (MonoMethod *method)
 {
@@ -8543,7 +8567,7 @@ mono_create_icall_signature (const char *sigstr)
        res = mono_metadata_signature_alloc (corlib, len - 1);
        res->pinvoke = 1;
 
-#ifdef HOST_WIN32
+#ifdef TARGET_WIN32
        /* 
         * Under windows, the default pinvoke calling convention is STDCALL but
         * we need CDECL.