Revert until Martin is online to debug
[mono.git] / mono / metadata / icall.c
index 61266dbd49089470c8758ccadadfa20f9bf49127..ad2580f55928b83d43635f35d23a995c553dabe6 100644 (file)
@@ -92,6 +92,29 @@ mono_double_ParseImpl (char *ptr, double *result)
        return TRUE;
 }
 
+static MonoClass *
+mono_class_get_throw (MonoImage *image, guint32 type_token)
+{
+       MonoClass *class = mono_class_get (image, type_token);
+       MonoLoaderError *error;
+       MonoException *ex;
+       
+       if (class != NULL){
+               if (class->exception_type) {
+                       MonoException *exc = mono_class_get_exception_for_failure (class);
+                       g_assert (exc);
+                       mono_raise_exception (exc);
+               }
+               return class;
+       }
+       error = mono_loader_get_last_error ();
+       g_assert (error != NULL);
+       
+       ex = mono_loader_error_prepare_exception (error);
+       mono_raise_exception (ex);
+       return NULL;
+}
+
 static void
 ves_icall_System_Double_AssertEndianity (double *value)
 {
@@ -757,24 +780,6 @@ ves_icall_System_Object_MemberwiseClone (MonoObject *this)
        return mono_object_clone (this);
 }
 
-#define MONO_OBJECT_ALIGNMENT_SHIFT    3
-
-/*
- * Return hashcode based on object address. This function will need to be
- * smarter in the presence of a moving garbage collector, which will cache
- * the address hash before relocating the object.
- *
- * Wang's address-based hash function:
- *   http://www.concentric.net/~Ttwang/tech/addrhash.htm
- */
-static gint32
-ves_icall_System_Object_GetHashCode (MonoObject *this)
-{
-       MONO_ARCH_SAVE_REGS;
-
-       return (GPOINTER_TO_UINT (this) >> MONO_OBJECT_ALIGNMENT_SHIFT) * 2654435761u;
-}
-
 static gint32
 ves_icall_System_ValueType_InternalGetHashCode (MonoObject *this, MonoArray **fields)
 {
@@ -1474,9 +1479,9 @@ ves_icall_MonoField_GetValueInternal (MonoReflectionField *field, MonoObject *ob
 
                /* Convert the Nullable structure into a boxed vtype */
                if (is_static)
-                       buf = (char*)vtable->data + cf->offset;
+                       buf = (guint8*)vtable->data + cf->offset;
                else
-                       buf = (char*)obj + cf->offset;
+                       buf = (guint8*)obj + cf->offset;
 
                return mono_nullable_box (buf, nklass);
        }
@@ -1552,7 +1557,7 @@ ves_icall_FieldInfo_SetValueInternal (MonoReflectionField *field, MonoObject *ob
 
                                mono_nullable_init (buf, value, nklass);
 
-                               v = buf;
+                               v = (gchar*)buf;
                        }
                        else 
                                if (gclass->container_class->valuetype && (v != NULL))
@@ -1662,9 +1667,16 @@ ves_icall_Type_GetInterfaces (MonoReflectionType* type)
        MonoClass *class = mono_class_from_mono_type (type->type);
        MonoClass *parent;
        MonoBitSet *slots;
+       MonoGenericContext *context = NULL;
 
        MONO_ARCH_SAVE_REGS;
 
+       /* open generic-instance classes can share their interface_id */
+       if (class->generic_class && class->generic_class->inst->is_open) {
+               context = class->generic_class->context;
+               class = class->generic_class->container_class;
+       }
+
        mono_class_setup_vtable (class);
 
        slots = mono_bitset_new (class->max_interface_id + 1, 0);
@@ -1700,8 +1712,11 @@ ves_icall_Type_GetInterfaces (MonoReflectionType* type)
        intf = mono_array_new (domain, mono_defaults.monotype_class, ifaces->len);
        for (i = 0; i < ifaces->len; ++i) {
                MonoClass *ic = g_ptr_array_index (ifaces, i);
+               MonoType *ret = &ic->byval_arg;
+               if (context && ic->generic_class && ic->generic_class->inst->is_open)
+                       ret = mono_class_inflate_generic_type (ret, context);
                
-               mono_array_setref (intf, i, mono_type_get_object (domain, &ic->byval_arg));
+               mono_array_setref (intf, i, mono_type_get_object (domain, ret));
        }
        g_ptr_array_free (ifaces, TRUE);
 
@@ -2502,8 +2517,8 @@ ves_icall_MonoMethod_GetDllImportAttribute (MonoMethod *method)
        
        attr = (MonoReflectionDllImportAttribute*)mono_object_new (domain, DllImportAttributeClass);
 
-       attr->dll = mono_string_new (domain, scope);
-       attr->entry_point = mono_string_new (domain, import);
+       MONO_OBJECT_SETREF (attr, dll, mono_string_new (domain, scope));
+       MONO_OBJECT_SETREF (attr, entry_point, mono_string_new (domain, import));
        attr->call_conv = (flags & 0x700) >> 8;
        attr->charset = ((flags & 0x6) >> 1) + 1;
        if (attr->charset == 1)
@@ -2813,7 +2828,7 @@ write_enum_value (char *mem, int type, guint64 value)
        switch (type) {
        case MONO_TYPE_U1:
        case MONO_TYPE_I1: {
-               guint8 *p = mem;
+               guint8 *p = (guint8*)mem;
                *p = value;
                break;
        }
@@ -2858,9 +2873,10 @@ ves_icall_System_Enum_ToObject (MonoReflectionType *type, MonoObject *obj)
        enumc = mono_class_from_mono_type (type->type);
        objc = obj->vtable->klass;
 
-       MONO_CHECK_ARG (obj, enumc->enumtype == TRUE);
-       MONO_CHECK_ARG (obj, (objc->enumtype) || (objc->byval_arg.type >= MONO_TYPE_I1 &&
-                                                 objc->byval_arg.type <= MONO_TYPE_U8));
+       if (!enumc->enumtype)
+               mono_raise_exception (mono_get_exception_argument ("enumType", "Type provided must be an Enum."));
+       if (!((objc->enumtype) || (objc->byval_arg.type >= MONO_TYPE_I1 && objc->byval_arg.type <= MONO_TYPE_U8)))
+               mono_raise_exception (mono_get_exception_argument ("value", "The value passed in must be an enum base or an underlying type for an enum, such as an Int32."));
 
        res = mono_object_new (domain, enumc);
        val = read_enum_value ((char *)obj + sizeof (MonoObject), objc->enumtype? objc->enum_basetype->type: objc->byval_arg.type);
@@ -3047,7 +3063,6 @@ static MonoArray*
 ves_icall_Type_GetFields_internal (MonoReflectionType *type, guint32 bflags, MonoReflectionType *reftype)
 {
        MonoDomain *domain; 
-       GSList *l = NULL, *tmp;
        MonoClass *startklass, *klass, *refklass;
        MonoArray *res;
        MonoObject *member;
@@ -3059,10 +3074,13 @@ ves_icall_Type_GetFields_internal (MonoReflectionType *type, guint32 bflags, Mon
 
        domain = ((MonoObject *)type)->vtable->domain;
        if (type->type->byref)
-               return mono_array_new (domain, mono_defaults.method_info_class, 0);
+               return mono_array_new (domain, mono_defaults.field_info_class, 0);
        klass = startklass = mono_class_from_mono_type (type->type);
        refklass = mono_class_from_mono_type (reftype->type);
 
+       i = 0;
+       len = 2;
+       res = mono_array_new (domain, mono_defaults.field_info_class, len);
 handle_parent: 
        iter = NULL;
        while ((field = mono_class_get_fields (klass, &iter))) {
@@ -3091,17 +3109,26 @@ handle_parent:
                if (!match)
                        continue;
                member = (MonoObject*)mono_field_get_object (domain, refklass, field);
-               l = g_slist_prepend (l, member);
+               if (i >= len) {
+                       MonoArray *new_res = mono_array_new (domain, mono_defaults.field_info_class, len * 2);
+                       mono_array_memcpy_refs (new_res, 0, res, 0, len);
+                       len *= 2;
+                       res = new_res;
+               }
+               mono_array_setref (res, i, member);
+               ++i;
        }
        if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
                goto handle_parent;
-       len = g_slist_length (l);
-       res = mono_array_new (domain, mono_defaults.field_info_class, len);
-       i = 0;
-       tmp = l = g_slist_reverse (l);
-       for (; tmp; tmp = tmp->next, ++i)
-               mono_array_setref (res, i, tmp->data);
-       g_slist_free (l);
+       if (i != len) {
+               MonoArray *new_res = mono_array_new (domain, mono_defaults.field_info_class, i);
+               mono_array_memcpy_refs (new_res, 0, res, 0, i);
+               res = new_res;
+               /*
+                * Better solution for the new GC.
+                * res->max_length = i;
+                */
+       }
        return res;
 }
 
@@ -3109,13 +3136,12 @@ static MonoArray*
 ves_icall_Type_GetMethodsByName (MonoReflectionType *type, MonoString *name, guint32 bflags, MonoBoolean ignore_case, MonoReflectionType *reftype)
 {
        MonoDomain *domain; 
-       GSList *l = NULL, *tmp;
        MonoClass *startklass, *klass, *refklass;
        MonoArray *res;
        MonoMethod *method;
        gpointer iter;
        MonoObject *member;
-       int i, len, match;
+       int i, len, match, nslots;
        guint32 method_slots_default [8];
        guint32 *method_slots;
        gchar *mname = NULL;
@@ -3136,12 +3162,16 @@ ves_icall_Type_GetMethodsByName (MonoReflectionType *type, MonoString *name, gui
 
        mono_class_setup_vtable (klass);
 
-       if (klass->vtable_size >= sizeof (method_slots_default) * 8) {
-               method_slots = g_new0 (guint32, klass->vtable_size / 32 + 1);
+       nslots = MONO_CLASS_IS_INTERFACE (klass) ? mono_class_num_methods (klass) : klass->vtable_size;
+       if (nslots >= sizeof (method_slots_default) * 8) {
+               method_slots = g_new0 (guint32, nslots / 32 + 1);
        } else {
                method_slots = method_slots_default;
                memset (method_slots, 0, sizeof (method_slots_default));
        }
+       i = 0;
+       len = 1;
+       res = mono_array_new (domain, mono_defaults.method_info_class, len);
 handle_parent:
        mono_class_setup_vtable (klass);
        iter = NULL;
@@ -3178,6 +3208,7 @@ handle_parent:
                
                match = 0;
                if (method->slot != -1) {
+                       g_assert (method->slot < nslots);
                        if (method_slots [method->slot >> 5] & (1 << (method->slot & 0x1f)))
                                continue;
                        method_slots [method->slot >> 5] |= 1 << (method->slot & 0x1f);
@@ -3185,23 +3216,30 @@ handle_parent:
                
                member = (MonoObject*)mono_method_get_object (domain, method, refklass);
                
-               l = g_slist_prepend (l, member);
-               len++;
+               if (i >= len) {
+                       MonoArray *new_res = mono_array_new (domain, mono_defaults.method_info_class, len * 2);
+                       mono_array_memcpy_refs (new_res, 0, res, 0, len);
+                       len *= 2;
+                       res = new_res;
+               }
+               mono_array_setref (res, i, member);
+               ++i;
        }
        if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
                goto handle_parent;
 
        g_free (mname);
-       res = mono_array_new (domain, mono_defaults.method_info_class, len);
-       i = 0;
-
-       tmp = l = g_slist_reverse (l);
-
-       for (; tmp; tmp = tmp->next, ++i)
-               mono_array_setref (res, i, tmp->data);
-       g_slist_free (l);
        if (method_slots != method_slots_default)
                g_free (method_slots);
+       if (i != len) {
+               MonoArray *new_res = mono_array_new (domain, mono_defaults.method_info_class, i);
+               mono_array_memcpy_refs (new_res, 0, res, 0, i);
+               res = new_res;
+               /*
+                * Better solution for the new GC.
+                * res->max_length = i;
+                */
+       }
        return res;
 }
 
@@ -3209,7 +3247,6 @@ static MonoArray*
 ves_icall_Type_GetConstructors_internal (MonoReflectionType *type, guint32 bflags, MonoReflectionType *reftype)
 {
        MonoDomain *domain; 
-       GSList *l = NULL, *tmp;
        static MonoClass *System_Reflection_ConstructorInfo;
        MonoClass *startklass, *klass, *refklass;
        MonoArray *res;
@@ -3226,6 +3263,13 @@ ves_icall_Type_GetConstructors_internal (MonoReflectionType *type, guint32 bflag
        klass = startklass = mono_class_from_mono_type (type->type);
        refklass = mono_class_from_mono_type (reftype->type);
 
+       if (!System_Reflection_ConstructorInfo)
+               System_Reflection_ConstructorInfo = mono_class_from_name (
+                       mono_defaults.corlib, "System.Reflection", "ConstructorInfo");
+
+       i = 0;
+       len = 2;
+       res = mono_array_new (domain, System_Reflection_ConstructorInfo, len);
        iter = NULL;
        while ((method = mono_class_get_methods (klass, &iter))) {
                match = 0;
@@ -3253,19 +3297,25 @@ ves_icall_Type_GetConstructors_internal (MonoReflectionType *type, guint32 bflag
                if (!match)
                        continue;
                member = (MonoObject*)mono_method_get_object (domain, method, refklass);
-                       
-               l = g_slist_prepend (l, member);
+
+               if (i >= len) {
+                       MonoArray *new_res = mono_array_new (domain, System_Reflection_ConstructorInfo, len * 2);
+                       mono_array_memcpy_refs (new_res, 0, res, 0, len);
+                       len *= 2;
+                       res = new_res;
+               }
+               mono_array_setref (res, i, member);
+               ++i;
+       }
+       if (i != len) {
+               MonoArray *new_res = mono_array_new (domain, System_Reflection_ConstructorInfo, i);
+               mono_array_memcpy_refs (new_res, 0, res, 0, i);
+               res = new_res;
+               /*
+                * Better solution for the new GC.
+                * res->max_length = i;
+                */
        }
-       len = g_slist_length (l);
-       if (!System_Reflection_ConstructorInfo)
-               System_Reflection_ConstructorInfo = mono_class_from_name (
-                       mono_defaults.corlib, "System.Reflection", "ConstructorInfo");
-       res = mono_array_new (domain, System_Reflection_ConstructorInfo, len);
-       i = 0;
-       tmp = l = g_slist_reverse (l);
-       for (; tmp; tmp = tmp->next, ++i)
-               mono_array_setref (res, i, tmp->data);
-       g_slist_free (l);
        return res;
 }
 
@@ -3273,13 +3323,12 @@ static MonoArray*
 ves_icall_Type_GetPropertiesByName (MonoReflectionType *type, MonoString *name, guint32 bflags, MonoBoolean ignore_case, MonoReflectionType *reftype)
 {
        MonoDomain *domain; 
-       GSList *l = NULL, *tmp;
        static MonoClass *System_Reflection_PropertyInfo;
        MonoClass *startklass, *klass;
        MonoArray *res;
        MonoMethod *method;
        MonoProperty *prop;
-       int i, match;
+       int i, match, nslots;
        int len = 0;
        guint32 flags;
        guint32 method_slots_default [8];
@@ -3303,12 +3352,18 @@ ves_icall_Type_GetPropertiesByName (MonoReflectionType *type, MonoString *name,
                compare_func = (ignore_case) ? g_strcasecmp : strcmp;
        }
 
-       if (klass->vtable_size >= sizeof (method_slots_default) * 8) {
-               method_slots = g_new0 (guint32, klass->vtable_size / 32 + 1);
+       mono_class_setup_vtable (klass);
+
+       nslots = MONO_CLASS_IS_INTERFACE (klass) ? mono_class_num_methods (klass) : klass->vtable_size;
+       if (nslots >= sizeof (method_slots_default) * 8) {
+               method_slots = g_new0 (guint32, nslots / 32 + 1);
        } else {
                method_slots = method_slots_default;
                memset (method_slots, 0, sizeof (method_slots_default));
        }
+       i = 0;
+       len = 2;
+       res = mono_array_new (domain, System_Reflection_PropertyInfo, len);
 handle_parent:
        mono_class_setup_vtable (klass);
        iter = NULL;
@@ -3361,23 +3416,30 @@ handle_parent:
                        method_slots [prop->set->slot >> 5] |= 1 << (prop->set->slot & 0x1f);
                }
 
-               l = g_slist_prepend (l, mono_property_get_object (domain, startklass, prop));
-               len++;
+               if (i >= len) {
+                       MonoArray *new_res = mono_array_new (domain, System_Reflection_PropertyInfo, len * 2);
+                       mono_array_memcpy_refs (new_res, 0, res, 0, len);
+                       len *= 2;
+                       res = new_res;
+               }
+               mono_array_setref (res, i, mono_property_get_object (domain, startklass, prop));
+               ++i;
        }
        if ((!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent)))
                goto handle_parent;
 
        g_free (propname);
-       res = mono_array_new (domain, System_Reflection_PropertyInfo, len);
-       i = 0;
-
-       tmp = l = g_slist_reverse (l);
-
-       for (; tmp; tmp = tmp->next, ++i)
-               mono_array_setref (res, i, tmp->data);
-       g_slist_free (l);
        if (method_slots != method_slots_default)
                g_free (method_slots);
+       if (i != len) {
+               MonoArray *new_res = mono_array_new (domain, System_Reflection_PropertyInfo, i);
+               mono_array_memcpy_refs (new_res, 0, res, 0, i);
+               res = new_res;
+               /*
+                * Better solution for the new GC.
+                * res->max_length = i;
+                */
+       }
        return res;
 }
 
@@ -3438,7 +3500,6 @@ static MonoArray*
 ves_icall_Type_GetEvents_internal (MonoReflectionType *type, guint32 bflags, MonoReflectionType *reftype)
 {
        MonoDomain *domain; 
-       GSList *l = NULL, *tmp;
        static MonoClass *System_Reflection_EventInfo;
        MonoClass *startklass, *klass;
        MonoArray *res;
@@ -3458,6 +3519,9 @@ ves_icall_Type_GetEvents_internal (MonoReflectionType *type, guint32 bflags, Mon
                return mono_array_new (domain, System_Reflection_EventInfo, 0);
        klass = startklass = mono_class_from_mono_type (type->type);
 
+       i = 0;
+       len = 2;
+       res = mono_array_new (domain, System_Reflection_EventInfo, len);
 handle_parent: 
        iter = NULL;
        while ((event = mono_class_get_events (klass, &iter))) {
@@ -3498,19 +3562,26 @@ handle_parent:
                if (!match)
                        continue;
                match = 0;
-               l = g_slist_prepend (l, mono_event_get_object (domain, startklass, event));
+               if (i >= len) {
+                       MonoArray *new_res = mono_array_new (domain, System_Reflection_EventInfo, len * 2);
+                       mono_array_memcpy_refs (new_res, 0, res, 0, len);
+                       len *= 2;
+                       res = new_res;
+               }
+               mono_array_setref (res, i, mono_event_get_object (domain, startklass, event));
+               ++i;
        }
        if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
                goto handle_parent;
-       len = g_slist_length (l);
-       res = mono_array_new (domain, System_Reflection_EventInfo, len);
-       i = 0;
-
-       tmp = l = g_slist_reverse (l);
-
-       for (; tmp; tmp = tmp->next, ++i)
-               mono_array_setref (res, i, tmp->data);
-       g_slist_free (l);
+       if (i != len) {
+               MonoArray *new_res = mono_array_new (domain, System_Reflection_EventInfo, i);
+               mono_array_memcpy_refs (new_res, 0, res, 0, i);
+               res = new_res;
+               /*
+                * Better solution for the new GC.
+                * res->max_length = i;
+                */
+       }
        return res;
 }
 
@@ -3559,7 +3630,6 @@ static MonoArray*
 ves_icall_Type_GetNestedTypes (MonoReflectionType *type, guint32 bflags)
 {
        MonoDomain *domain; 
-       GSList *l = NULL, *tmp;
        GList *tmpn;
        MonoClass *startklass, *klass;
        MonoArray *res;
@@ -3574,6 +3644,9 @@ ves_icall_Type_GetNestedTypes (MonoReflectionType *type, guint32 bflags)
                return mono_array_new (domain, mono_defaults.monotype_class, 0);
        klass = startklass = mono_class_from_mono_type (type->type);
 
+       i = 0;
+       len = 1;
+       res = mono_array_new (domain, mono_defaults.monotype_class, len);
        for (tmpn = klass->nested_classes; tmpn; tmpn = tmpn->next) {
                match = 0;
                nested = tmpn->data;
@@ -3587,15 +3660,24 @@ ves_icall_Type_GetNestedTypes (MonoReflectionType *type, guint32 bflags)
                if (!match)
                        continue;
                member = (MonoObject*)mono_type_get_object (domain, &nested->byval_arg);
-               l = g_slist_prepend (l, member);
+               if (i >= len) {
+                       MonoArray *new_res = mono_array_new (domain, mono_defaults.monotype_class, len * 2);
+                       mono_array_memcpy_refs (new_res, 0, res, 0, len);
+                       len *= 2;
+                       res = new_res;
+               }
+               mono_array_setref (res, i, member);
+               ++i;
+       }
+       if (i != len) {
+               MonoArray *new_res = mono_array_new (domain, mono_defaults.monotype_class, i);
+               mono_array_memcpy_refs (new_res, 0, res, 0, i);
+               res = new_res;
+               /*
+                * Better solution for the new GC.
+                * res->max_length = i;
+                */
        }
-       len = g_slist_length (l);
-       res = mono_array_new (domain, mono_defaults.monotype_class, len);
-       i = 0;
-       tmp = l = g_slist_reverse (l);
-       for (; tmp; tmp = tmp->next, ++i)
-               mono_array_setref (res, i, tmp->data);
-       g_slist_free (l);
        return res;
 }
 
@@ -3694,6 +3776,14 @@ ves_icall_System_Reflection_Assembly_get_code_base (MonoReflectionAssembly *asse
        MONO_ARCH_SAVE_REGS;
 
        absolute = g_build_filename (mass->basedir, mass->image->module_name, NULL);
+#if PLATFORM_WIN32
+       {
+               gint i;
+               for (i = strlen (absolute) - 1; i >= 0; i--)
+                       if (absolute [i] == '\\')
+                               absolute [i] = '/';
+       }
+#endif
        if (escaped) {
                uri = g_filename_to_uri (absolute, NULL, NULL);
        } else {
@@ -3872,7 +3962,7 @@ ves_icall_System_Reflection_Assembly_GetReferencedAssemblies (MonoReflectionAsse
                aname = (MonoReflectionAssemblyName *) mono_object_new (
                        domain, System_Reflection_AssemblyName);
 
-               aname->name = mono_string_new (domain, mono_metadata_string_heap (image, cols [MONO_ASSEMBLYREF_NAME]));
+               MONO_OBJECT_SETREF (aname, name, mono_string_new (domain, mono_metadata_string_heap (image, cols [MONO_ASSEMBLYREF_NAME])));
 
                aname->major = cols [MONO_ASSEMBLYREF_MAJOR_VERSION];
                aname->minor = cols [MONO_ASSEMBLYREF_MINOR_VERSION];
@@ -3881,12 +3971,12 @@ ves_icall_System_Reflection_Assembly_GetReferencedAssemblies (MonoReflectionAsse
                aname->flags = cols [MONO_ASSEMBLYREF_FLAGS];
                aname->versioncompat = 1; /* SameMachine (default) */
                aname->hashalg = ASSEMBLY_HASH_SHA1; /* SHA1 (default) */
-               aname->version = create_version (domain, aname->major, aname->minor, aname->build, aname->revision);
+               MONO_OBJECT_SETREF (aname, version, create_version (domain, aname->major, aname->minor, aname->build, aname->revision));
 
                if (create_culture) {
                        gpointer args [1];
                        args [0] = mono_string_new (domain, mono_metadata_string_heap (image, cols [MONO_ASSEMBLYREF_CULTURE]));
-                       aname->cultureInfo = mono_runtime_invoke (create_culture, NULL, args, NULL);
+                       MONO_OBJECT_SETREF (aname, cultureInfo, mono_runtime_invoke (create_culture, NULL, args, NULL));
                }
                
                if (cols [MONO_ASSEMBLYREF_PUBLIC_KEY]) {
@@ -3896,10 +3986,10 @@ ves_icall_System_Reflection_Assembly_GetReferencedAssemblies (MonoReflectionAsse
                        if ((cols [MONO_ASSEMBLYREF_FLAGS] & ASSEMBLYREF_FULL_PUBLIC_KEY_FLAG)) {
                                /* public key token isn't copied - the class library will 
                                automatically generate it from the public key if required */
-                               aname->publicKey = mono_array_new (domain, mono_defaults.byte_class, pkey_len);
+                               MONO_OBJECT_SETREF (aname, publicKey, mono_array_new (domain, mono_defaults.byte_class, pkey_len));
                                memcpy (mono_array_addr (aname->publicKey, guint8, 0), pkey_ptr, pkey_len);
                        } else {
-                               aname->keyToken = mono_array_new (domain, mono_defaults.byte_class, pkey_len);
+                               MONO_OBJECT_SETREF (aname, keyToken, mono_array_new (domain, mono_defaults.byte_class, pkey_len));
                                memcpy (mono_array_addr (aname->keyToken, guint8, 0), pkey_ptr, pkey_len);
                        }
                }
@@ -4037,7 +4127,7 @@ ves_icall_System_Reflection_Assembly_GetManifestResourceInfoInternal (MonoReflec
                        table = &assembly->assembly->image->tables [MONO_TABLE_FILE];
                        mono_metadata_decode_row (table, i - 1, file_cols, MONO_FILE_SIZE);
                        val = mono_metadata_string_heap (assembly->assembly->image, file_cols [MONO_FILE_NAME]);
-                       info->filename = mono_string_new (mono_object_domain (assembly), val);
+                       MONO_OBJECT_SETREF (info, filename, mono_string_new (mono_object_domain (assembly), val));
                        if (file_cols [MONO_FILE_FLAGS] && FILE_CONTAINS_NO_METADATA)
                                info->location = 0;
                        else
@@ -4053,7 +4143,7 @@ ves_icall_System_Reflection_Assembly_GetManifestResourceInfoInternal (MonoReflec
                                g_free (msg);
                                mono_raise_exception (ex);
                        }
-                       info->assembly = mono_assembly_get_object (mono_domain_get (), assembly->assembly->image->references [i - 1]);
+                       MONO_OBJECT_SETREF (info, assembly, mono_assembly_get_object (mono_domain_get (), assembly->assembly->image->references [i - 1]));
 
                        /* Obtain info recursively */
                        ves_icall_System_Reflection_Assembly_GetManifestResourceInfoInternal (info->assembly, name, info);
@@ -4283,18 +4373,18 @@ fill_reflection_assembly_name (MonoDomain *domain, MonoReflectionAssemblyName *a
 
        MONO_ARCH_SAVE_REGS;
 
-       aname->name = mono_string_new (domain, name->name);
+       MONO_OBJECT_SETREF (aname, name, mono_string_new (domain, name->name));
        aname->major = name->major;
        aname->minor = name->minor;
        aname->build = name->build;
        aname->revision = name->revision;
        aname->hashalg = name->hash_alg;
        if (by_default_version)
-               aname->version = create_version (domain, name->major, name->minor, name->build, name->revision);
+               MONO_OBJECT_SETREF (aname, version, create_version (domain, name->major, name->minor, name->build, name->revision));
        
        codebase = g_filename_to_uri (absolute, NULL, NULL);
        if (codebase) {
-               aname->codebase = mono_string_new (domain, codebase);
+               MONO_OBJECT_SETREF (aname, codebase, mono_string_new (domain, codebase));
                g_free (codebase);
        }
 
@@ -4307,14 +4397,14 @@ fill_reflection_assembly_name (MonoDomain *domain, MonoReflectionAssemblyName *a
 
        if (name->culture) {
                args [0] = mono_string_new (domain, name->culture);
-               aname->cultureInfo = mono_runtime_invoke (create_culture, NULL, args, NULL);
+               MONO_OBJECT_SETREF (aname, cultureInfo, mono_runtime_invoke (create_culture, NULL, args, NULL));
        }
 
        if (name->public_key) {
                pkey_ptr = (char*)name->public_key;
                pkey_len = mono_metadata_decode_blob_size (pkey_ptr, &pkey_ptr);
 
-               aname->publicKey = mono_array_new (domain, mono_defaults.byte_class, pkey_len);
+               MONO_OBJECT_SETREF (aname, publicKey, mono_array_new (domain, mono_defaults.byte_class, pkey_len));
                memcpy (mono_array_addr (aname->publicKey, guint8, 0), pkey_ptr, pkey_len);
        }
 
@@ -4323,7 +4413,7 @@ fill_reflection_assembly_name (MonoDomain *domain, MonoReflectionAssemblyName *a
                int i, j;
                char *p;
 
-               aname->keyToken = mono_array_new (domain, mono_defaults.byte_class, 8);
+               MONO_OBJECT_SETREF (aname, keyToken, mono_array_new (domain, mono_defaults.byte_class, 8));
                p = mono_array_addr (aname->keyToken, char, 0);
 
                for (i = 0, j = 0; i < 8; i++) {
@@ -4363,7 +4453,7 @@ ves_icall_System_Reflection_Assembly_InternalGetAssemblyName (MonoString *fname,
        filename = mono_string_to_utf8 (fname);
 
        image = mono_image_open (filename, &status);
-       
+
        if (!image){
                MonoException *exc;
 
@@ -4415,8 +4505,7 @@ ves_icall_System_Reflection_Assembly_LoadPermissions (MonoReflectionAssembly *as
 }
 
 static MonoArray*
-mono_module_get_types (MonoDomain *domain, MonoImage *image, 
-                                          MonoBoolean exportedOnly)
+mono_module_get_types (MonoDomain *domain, MonoImage *image, MonoBoolean exportedOnly)
 {
        MonoArray *res;
        MonoClass *klass;
@@ -4442,7 +4531,9 @@ mono_module_get_types (MonoDomain *domain, MonoImage *image,
                attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
                visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
                if (!exportedOnly || (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)) {
-                       klass = mono_class_get (image, (i + 1) | MONO_TOKEN_TYPE_DEF);
+                       klass = mono_class_get_throw (image, (i + 1) | MONO_TOKEN_TYPE_DEF);
+                       if (mono_loader_get_last_error ())
+                               mono_loader_clear_error ();
                        mono_array_setref (res, count, mono_type_get_object (domain, &klass->byval_arg));
                        count++;
                }
@@ -4458,7 +4549,8 @@ ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly,
        MonoImage *image = NULL;
        MonoTableInfo *table = NULL;
        MonoDomain *domain;
-       int i;
+       GList *list = NULL;
+       int i, len;
 
        MONO_ARCH_SAVE_REGS;
 
@@ -4543,42 +4635,41 @@ ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly,
                }
        }
 
-       if (mono_is_security_manager_active ()) {
-               /* the ReflectionTypeLoadException must have all the types (Types property), 
-                * NULL replacing types which throws an exception. The LoaderException must
-                * contains all exceptions for NULL items.
-                */
+       /* the ReflectionTypeLoadException must have all the types (Types property), 
+        * NULL replacing types which throws an exception. The LoaderException must
+        * contain all exceptions for NULL items.
+        */
 
-               guint32 len = mono_array_length (res);
-               GList *list = NULL;
-
-               for (i = 0; i < len; i++) {
-                       MonoReflectionType *t = mono_array_get (res, gpointer, i);
-                       MonoClass *klass = mono_type_get_class (t->type);
-                       if ((klass != NULL) && klass->exception_type) {
-                               /* keep the class in the list */
-                               list = g_list_append (list, klass);
-                               /* and replace Type with NULL */
-                               mono_array_setref (res, i, NULL);
-                       }
+       len = mono_array_length (res);
+
+       for (i = 0; i < len; i++) {
+               MonoReflectionType *t = mono_array_get (res, gpointer, i);
+               MonoClass *klass = mono_type_get_class (t->type);
+               if ((klass != NULL) && klass->exception_type) {
+                       /* keep the class in the list */
+                       list = g_list_append (list, klass);
+                       /* and replace Type with NULL */
+                       mono_array_setref (res, i, NULL);
                }
+       }
 
-               if (list) {
-                       GList *tmp = NULL;
-                       MonoException *exc = NULL;
-                       int length = g_list_length (list);
+       if (list) {
+               GList *tmp = NULL;
+               MonoException *exc = NULL;
+               int length = g_list_length (list);
 
-                       MonoArray *exl = mono_array_new (domain, mono_defaults.exception_class, length);
-                       for (i = 0, tmp = list; i < length; i++, tmp = tmp->next) {
-                               MonoException *exc = mono_class_get_exception_for_failure (tmp->data);
-                               mono_array_setref (exl, i, exc);
-                       }
-                       g_list_free (list);
-                       list = NULL;
+               mono_loader_clear_error ();
 
-                       exc = mono_get_exception_reflection_type_load (res, exl);
-                       mono_raise_exception (exc);
+               MonoArray *exl = mono_array_new (domain, mono_defaults.exception_class, length);
+               for (i = 0, tmp = list; i < length; i++, tmp = tmp->next) {
+                       MonoException *exc = mono_class_get_exception_for_failure (tmp->data);
+                       mono_array_setref (exl, i, exc);
                }
+               g_list_free (list);
+               list = NULL;
+
+               exc = mono_get_exception_reflection_type_load (res, exl);
+               mono_raise_exception (exc);
        }
                
        return res;
@@ -5380,7 +5471,7 @@ ves_icall_Remoting_RealProxy_GetTransparentProxy (MonoObject *this, MonoString *
        res = mono_object_new (domain, mono_defaults.transparent_proxy_class);
        tp = (MonoTransparentProxy*) res;
        
-       tp->rp = rp;
+       MONO_OBJECT_SETREF (tp, rp, rp);
        type = ((MonoReflectionType *)rp->class_to_proxy)->type;
        klass = mono_class_from_mono_type (type);
 
@@ -5519,6 +5610,36 @@ ves_icall_System_Environment_GetEnvironmentVariableNames (void)
        return names;
 }
 
+/*
+ * If your platform lacks setenv/unsetenv, you must upgrade your glib.
+ */
+#if !GLIB_CHECK_VERSION(2,4,0)
+#define g_setenv(a,b,c)   setenv(a,b,c)
+#define g_unsetenv(a) unsetenv(a)
+#endif
+
+static void
+ves_icall_System_Environment_InternalSetEnvironmentVariable (MonoString *name, MonoString *value)
+{
+       gchar *utf8_name, *utf8_value;
+
+       MONO_ARCH_SAVE_REGS;
+
+       utf8_name = mono_string_to_utf8 (name); /* FIXME: this should be ascii */
+
+       if ((value == NULL) || (mono_string_length (value) == 0) || (mono_string_chars (value)[0] == 0)) {
+               g_unsetenv (utf8_name);
+               return;
+       }
+
+       utf8_value = mono_string_to_utf8 (value);
+
+       g_setenv (utf8_name, utf8_value, TRUE);
+
+       g_free (utf8_name);
+       g_free (utf8_value);
+}
+
 /*
  * Returns: the number of milliseconds elapsed since the system started.
  */
@@ -6453,6 +6574,7 @@ static const IcallEntry environment_icalls [] = {
        {"GetMachineConfigPath", ves_icall_System_Configuration_DefaultConfig_get_machine_config_path},
        {"GetOSVersionString", ves_icall_System_Environment_GetOSVersionString},
        {"GetWindowsFolderPath", ves_icall_System_Environment_GetWindowsFolderPath},
+       {"InternalSetEnvironmentVariable", ves_icall_System_Environment_InternalSetEnvironmentVariable},
        {"get_ExitCode", mono_environment_exitcode_get},
        {"get_HasShutdownStarted", ves_icall_System_Environment_get_HasShutdownStarted},
        {"get_MachineName", ves_icall_System_Environment_get_MachineName},
@@ -6533,7 +6655,7 @@ static const IcallEntry monoio_icalls [] = {
        {"GetTempPath(string&)", ves_icall_System_IO_MonoIO_GetTempPath},
        {"Lock(intptr,long,long,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Lock},
        {"MoveFile(string,string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_MoveFile},
-       {"Open(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,bool,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Open},
+       {"Open(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,System.IO.FileOptions,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Open},
        {"Read(intptr,byte[],int,int,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Read},
        {"RemoveDirectory(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_RemoveDirectory},
        {"Seek(intptr,long,System.IO.SeekOrigin,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Seek},
@@ -6856,8 +6978,10 @@ static const IcallEntry marshal_icalls [] = {
        {"AllocCoTaskMem", ves_icall_System_Runtime_InteropServices_Marshal_AllocCoTaskMem},
        {"AllocHGlobal", ves_icall_System_Runtime_InteropServices_Marshal_AllocHGlobal},
        {"DestroyStructure", ves_icall_System_Runtime_InteropServices_Marshal_DestroyStructure},
+       {"FreeBSTR", ves_icall_System_Runtime_InteropServices_Marshal_FreeBSTR},
        {"FreeCoTaskMem", ves_icall_System_Runtime_InteropServices_Marshal_FreeCoTaskMem},
        {"FreeHGlobal", ves_icall_System_Runtime_InteropServices_Marshal_FreeHGlobal},
+       {"GetComSlotForMethodInfoInternal", ves_icall_System_Runtime_InteropServices_Marshal_GetComSlotForMethodInfoInternal},
        {"GetDelegateForFunctionPointerInternal", ves_icall_System_Runtime_InteropServices_Marshal_GetDelegateForFunctionPointerInternal},
        {"GetFunctionPointerForDelegateInternal", mono_delegate_to_ftnptr},
        {"GetLastWin32Error", ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error},
@@ -6880,6 +7004,7 @@ static const IcallEntry marshal_icalls [] = {
        {"ReadInt64", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt64},
        {"ReadIntPtr", ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr},
        {"SizeOf", ves_icall_System_Runtime_InteropServices_Marshal_SizeOf},
+       {"StringToBSTR", ves_icall_System_Runtime_InteropServices_Marshal_StringToBSTR},
        {"StringToHGlobalAnsi", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi},
        {"StringToHGlobalAuto", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi},
        {"StringToHGlobalUni", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalUni},