First set of licensing changes
[mono.git] / mono / metadata / appdomain.c
index 961ff53416584106dd9672445f82853a423e5c2d..2df1eeda959f4111ea31fae4651e86e19aa5bd11 100644 (file)
@@ -9,6 +9,7 @@
  * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  * Copyright 2012 Xamarin Inc
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 #undef ASSEMBLY_LOAD_DEBUG
 #include <config.h>
@@ -80,7 +81,7 @@
  * Changes which are already detected at runtime, like the addition
  * of icalls, do not require an increment.
  */
-#define MONO_CORLIB_VERSION 142
+#define MONO_CORLIB_VERSION 143
 
 typedef struct
 {
@@ -118,6 +119,9 @@ get_shadow_assembly_location_base (MonoDomain *domain, MonoError *error);
 
 static MonoLoadFunc load_function = NULL;
 
+/* Lazy class loading functions */
+static GENERATE_GET_CLASS_WITH_CACHE (assembly, System.Reflection, Assembly)
+
 void
 mono_install_runtime_load (MonoLoadFunc func)
 {
@@ -177,7 +181,9 @@ create_domain_objects (MonoDomain *domain)
        string_vt = mono_class_vtable (domain, mono_defaults.string_class);
        string_empty_fld = mono_class_get_field_from_name (mono_defaults.string_class, "Empty");
        g_assert (string_empty_fld);
-       mono_field_static_set_value (string_vt, string_empty_fld, mono_string_intern (mono_string_new (domain, "")));
+       MonoString *empty_str = mono_string_intern_checked (mono_string_new (domain, ""), &error);
+       mono_error_assert_ok (&error);
+       mono_field_static_set_value (string_vt, string_empty_fld, empty_str);
 
        /*
         * Create an instance early since we can't do it when there is no memory.
@@ -256,11 +262,11 @@ mono_runtime_init_checked (MonoDomain *domain, MonoThreadStartCB start_cb, MonoT
 
        mono_thread_init (start_cb, attach_cb);
 
-       klass = mono_class_from_name (mono_defaults.corlib, "System", "AppDomainSetup");
+       klass = mono_class_load_from_name (mono_defaults.corlib, "System", "AppDomainSetup");
        setup = (MonoAppDomainSetup *) mono_object_new_pinned (domain, klass, error);
        return_if_nok (error);
 
-       klass = mono_class_from_name (mono_defaults.corlib, "System", "AppDomain");
+       klass = mono_class_load_from_name (mono_defaults.corlib, "System", "AppDomain");
 
        ad = (MonoAppDomain *) mono_object_new_pinned (domain, klass, error);
        return_if_nok (error);
@@ -302,18 +308,20 @@ mono_runtime_init_checked (MonoDomain *domain, MonoThreadStartCB start_cb, MonoT
 static int
 mono_get_corlib_version (void)
 {
+       MonoError error;
        MonoClass *klass;
        MonoClassField *field;
        MonoObject *value;
 
-       klass = mono_class_from_name (mono_defaults.corlib, "System", "Environment");
+       klass = mono_class_load_from_name (mono_defaults.corlib, "System", "Environment");
        mono_class_init (klass);
        field = mono_class_get_field_from_name (klass, "mono_corlib_version");
        if (!field)
                return -1;
        if (! (field->type->attrs & FIELD_ATTRIBUTE_STATIC))
                return -1;
-       value = mono_field_get_value_object (mono_domain_get (), field, NULL);
+       value = mono_field_get_value_object_checked (mono_domain_get (), field, NULL, &error);
+       mono_error_raise_exception (&error); /* FIXME don't raise here */
        return *(gint32*)((gchar*)value + sizeof (MonoObject));
 }
 
@@ -357,8 +365,7 @@ mono_context_init_checked (MonoDomain *domain, MonoError *error)
 
        mono_error_init (error);
 
-       klass = mono_class_from_name (mono_defaults.corlib, "System.Runtime.Remoting.Contexts", "Context");
-
+       klass = mono_class_load_from_name (mono_defaults.corlib, "System.Runtime.Remoting.Contexts", "Context");
        context = (MonoAppContext *) mono_object_new_pinned (domain, klass, error);
        return_if_nok (error);
 
@@ -427,7 +434,7 @@ mono_domain_create_appdomain (char *friendly_name, char *configuration_file)
        MonoAppDomainSetup *setup;
        MonoClass *klass;
 
-       klass = mono_class_from_name (mono_defaults.corlib, "System", "AppDomainSetup");
+       klass = mono_class_load_from_name (mono_defaults.corlib, "System", "AppDomainSetup");
        setup = (MonoAppDomainSetup *) mono_object_new_checked (mono_domain_get (), klass, &error);
        mono_error_raise_exception (&error); /* FIXME don't raise here */
        setup->configuration_file = configuration_file != NULL ? mono_string_new (mono_domain_get (), configuration_file) : NULL;
@@ -467,7 +474,7 @@ copy_app_domain_setup (MonoDomain *domain, MonoAppDomainSetup *setup, MonoError
        mono_error_init (error);
 
        caller_domain = mono_domain_get ();
-       ads_class = mono_class_from_name (mono_defaults.corlib, "System", "AppDomainSetup");
+       ads_class = mono_class_load_from_name (mono_defaults.corlib, "System", "AppDomainSetup");
 
        copy = (MonoAppDomainSetup*)mono_object_new_checked (domain, ads_class, error);
        return_val_if_nok (error, NULL);
@@ -510,7 +517,7 @@ mono_domain_create_appdomain_internal (char *friendly_name, MonoAppDomainSetup *
 
        mono_error_init (error);
 
-       adclass = mono_class_from_name (mono_defaults.corlib, "System", "AppDomain");
+       adclass = mono_class_load_from_name (mono_defaults.corlib, "System", "AppDomain");
 
        /* FIXME: pin all those objects */
        data = mono_domain_create();
@@ -957,17 +964,12 @@ ves_icall_System_AppDomain_GetAssemblies (MonoAppDomain *ad, MonoBoolean refonly
        MonoError error;
        MonoDomain *domain = ad->data; 
        MonoAssembly* ass;
-       static MonoClass *System_Reflection_Assembly;
        MonoArray *res;
        GSList *tmp;
        int i;
        GPtrArray *assemblies;
 
        mono_error_init (&error);
-       
-       if (!System_Reflection_Assembly)
-               System_Reflection_Assembly = mono_class_from_name (
-                       mono_defaults.corlib, "System.Reflection", "Assembly");
 
        /* 
         * Make a copy of the list of assemblies because we can't hold the assemblies
@@ -986,7 +988,7 @@ ves_icall_System_AppDomain_GetAssemblies (MonoAppDomain *ad, MonoBoolean refonly
        }
        mono_domain_assemblies_unlock (domain);
 
-       res = mono_array_new (domain, System_Reflection_Assembly, assemblies->len);
+       res = mono_array_new (domain, mono_class_get_assembly_class (), assemblies->len);
        for (i = 0; i < assemblies->len; ++i) {
                ass = (MonoAssembly *)g_ptr_array_index (assemblies, i);
                MonoReflectionAssembly *ass_obj = mono_assembly_get_object_checked (domain, ass, &error);
@@ -1765,7 +1767,7 @@ mono_make_shadow_copy (const char *filename, MonoError *oerror)
        if (!mono_error_ok (&error)) {
                mono_error_cleanup (&error);
                g_free (dir_name);
-               mono_error_set_generic_error (oerror, "System", "ExecutionEngineException", "Failed to create shadow copy (invalid characters in shadow directory name).");
+               mono_error_set_execution_engine (oerror, "Failed to create shadow copy (invalid characters in shadow directory name).");
                return NULL;
        }
 
@@ -1780,13 +1782,13 @@ mono_make_shadow_copy (const char *filename, MonoError *oerror)
        shadow = get_shadow_assembly_location (filename, &error);
        if (!mono_error_ok (&error)) {
                mono_error_cleanup (&error);
-               mono_error_set_generic_error (oerror, "System", "ExecutionEngineException", "Failed to create shadow copy (invalid characters in file name).");
+               mono_error_set_execution_engine (oerror, "Failed to create shadow copy (invalid characters in file name).");
                return NULL;
        }
 
        if (ensure_directory_exists (shadow) == FALSE) {
                g_free (shadow);
-               mono_error_set_generic_error (oerror, "System", "ExecutionEngineException", "Failed to create shadow copy (ensure directory exists).");
+               mono_error_set_execution_engine (oerror, "Failed to create shadow copy (ensure directory exists).");
                return NULL;
        }       
 
@@ -1823,7 +1825,7 @@ mono_make_shadow_copy (const char *filename, MonoError *oerror)
                if (GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_PATH_NOT_FOUND)
                        return NULL; /* file not found, shadow copy failed */
 
-               mono_error_set_generic_error (oerror, "System", "ExecutionEngineException", "Failed to create shadow copy (CopyFile).");
+               mono_error_set_execution_engine (oerror, "Failed to create shadow copy (CopyFile).");
                return NULL;
        }
 
@@ -1842,14 +1844,14 @@ mono_make_shadow_copy (const char *filename, MonoError *oerror)
        
        if (copy_result == FALSE)  {
                g_free (shadow);
-               mono_error_set_generic_error (oerror, "System", "ExecutionEngineException", "Failed to create shadow copy of sibling data (CopyFile).");
+               mono_error_set_execution_engine (oerror, "Failed to create shadow copy of sibling data (CopyFile).");
                return NULL;
        }
 
        /* Create a .ini file containing the original assembly location */
        if (!shadow_copy_create_ini (shadow, filename)) {
                g_free (shadow);
-               mono_error_set_generic_error (oerror, "System", "ExecutionEngineException", "Failed to create shadow copy .ini file.");
+               mono_error_set_execution_engine (oerror, "Failed to create shadow copy .ini file.");
                return NULL;
        }
 
@@ -2145,10 +2147,11 @@ ves_icall_System_AppDomain_LoadAssembly (MonoAppDomain *ad,  MonoString *assRef,
 void
 ves_icall_System_AppDomain_InternalUnload (gint32 domain_id)
 {
+       MonoException *exc = NULL;
        MonoDomain * domain = mono_domain_get_by_id (domain_id);
 
        if (NULL == domain) {
-               MonoException *exc = mono_get_exception_execution_engine ("Failed to unload domain, domain id not found");
+               mono_get_exception_execution_engine ("Failed to unload domain, domain id not found");
                mono_set_pending_exception (exc);
                return;
        }
@@ -2168,7 +2171,9 @@ ves_icall_System_AppDomain_InternalUnload (gint32 domain_id)
        return;
 #endif
 
-       mono_domain_unload (domain);
+       mono_domain_try_unload (domain, (MonoObject**)&exc);
+       if (exc)
+               mono_set_pending_exception (exc);
 }
 
 gboolean
@@ -2515,8 +2520,6 @@ mono_domain_unload (MonoDomain *domain)
 {
        MonoObject *exc = NULL;
        mono_domain_try_unload (domain, &exc);
-       if (exc)
-               mono_raise_exception ((MonoException*)exc);
 }
 
 static guint32