Fix for building mono in VS, and implement reading/writing USER/MACHINE level environ...
[mono.git] / mono / metadata / icall.c
index a64cc0160317dae19b8cbb2bdfe77a6c44a21d51..544978af5e12878ad3f51999331c468518719826 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)
 {
@@ -3058,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;
@@ -3070,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))) {
@@ -3102,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;
 }
 
@@ -3120,7 +3136,6 @@ 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;
@@ -3154,6 +3169,9 @@ ves_icall_Type_GetMethodsByName (MonoReflectionType *type, MonoString *name, gui
                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;
@@ -3198,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;
 }
 
@@ -3222,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;
@@ -3239,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;
@@ -3266,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;
 }
 
@@ -3286,7 +3323,6 @@ 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;
@@ -3325,6 +3361,9 @@ ves_icall_Type_GetPropertiesByName (MonoReflectionType *type, MonoString *name,
                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;
@@ -3377,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;
 }
 
@@ -3454,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;
@@ -3474,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))) {
@@ -3514,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;
 }
 
@@ -3575,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;
@@ -3590,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;
@@ -3603,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;
 }
 
@@ -4396,8 +4462,6 @@ ves_icall_System_Reflection_Assembly_InternalGetAssemblyName (MonoString *fname,
                mono_raise_exception (exc);
        }
 
-       /* So we can call mono_image_close () later */
-       mono_image_addref (image);
        res = mono_assembly_fill_assembly_name (image, &name);
        if (!res) {
                mono_image_close (image);
@@ -4441,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;
@@ -4468,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++;
                }
@@ -4484,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;
 
@@ -4569,42 +4635,42 @@ 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;
+               MonoArray *exl = 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);
+               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;
@@ -5556,23 +5622,43 @@ ves_icall_System_Environment_GetEnvironmentVariableNames (void)
 static void
 ves_icall_System_Environment_InternalSetEnvironmentVariable (MonoString *name, MonoString *value)
 {
+#ifdef PLATFORM_WIN32
+       gunichar2 *utf16_name, *utf16_value;
+#else
        gchar *utf8_name, *utf8_value;
+#endif
 
        MONO_ARCH_SAVE_REGS;
+       
+#ifdef PLATFORM_WIN32
+       utf16_name = mono_string_to_utf16 (name);
+       if ((value == NULL) || (mono_string_length (value) == 0) || (mono_string_chars (value)[0] == 0)) {
+               SetEnvironmentVariable (utf16_name, NULL);
+               g_free (utf16_name);
+               return;
+       }
 
+       utf16_value = mono_string_to_utf16 (value);
+
+       SetEnvironmentVariable (utf16_name, utf16_value);
+
+       g_free (utf16_name);
+       g_free (utf16_value);
+#else
        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);
+               g_free (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);
+#endif
 }
 
 /*
@@ -6916,6 +7002,7 @@ static const IcallEntry marshal_icalls [] = {
        {"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},