Merge pull request #1267 from ramtinkermani/master
[mono.git] / mono / metadata / icall.c
index 98b09a50c0490e8bb3c24d75e339fa3412b5569d..763b2f638bff16a7463b9ef207bf0ce2e33a2706 100644 (file)
@@ -907,11 +907,12 @@ ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunClassConstructor (Mo
 ICALL_EXPORT void
 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunModuleConstructor (MonoImage *image)
 {
-       MONO_ARCH_SAVE_REGS;
+       MonoError error;
 
        mono_image_check_for_module_cctor (image);
        if (image->has_module_cctor) {
-               MonoClass *module_klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | 1);
+               MonoClass *module_klass = mono_class_get_checked (image, MONO_TOKEN_TYPE_DEF | 1, &error);
+               mono_error_raise_exception (&error);
                /*It's fine to raise the exception here*/
                mono_runtime_class_init (mono_class_vtable_full (mono_domain_get (), module_klass, TRUE));
        }
@@ -1673,8 +1674,7 @@ ves_icall_System_Reflection_FieldInfo_GetTypeModifiers (MonoReflectionField *fie
 {
        MonoError error;
        MonoType *type = mono_field_get_type_checked (field->field, &error);
-       if (!mono_error_ok (&error))
-               mono_error_raise_exception (&error);
+       mono_error_raise_exception (&error);
 
        return type_array_from_modifiers (field->field->parent->image, type, optional);
 }
@@ -5161,21 +5161,16 @@ mono_module_get_types (MonoDomain *domain, MonoImage *image, MonoArray **excepti
                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);
+                       MonoError error;
+                       klass = mono_class_get_checked (image, (i + 1) | MONO_TOKEN_TYPE_DEF, &error);
+                       g_assert (!mono_loader_get_last_error ()); /* Plug any leaks */
+                       
                        if (klass) {
                                mono_array_setref (res, count, mono_type_get_object (domain, &klass->byval_arg));
                        } else {
-                               MonoLoaderError *error;
-                               MonoException *ex;
-                               
-                               error = mono_loader_get_last_error ();
-                               g_assert (error != NULL);
-       
-                               ex = mono_loader_error_prepare_exception (error);
+                               MonoException *ex = mono_error_convert_to_exception (&error);
                                mono_array_setref (*exceptions, count, ex);
                        }
-                       if (mono_loader_get_last_error ())
-                               mono_loader_clear_error ();
                        count++;
                }
        }
@@ -5266,7 +5261,7 @@ ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly,
                mono_loader_clear_error ();
 
                exl = mono_array_new (domain, mono_defaults.exception_class, length);
-               /* Types for which mono_class_get () succeeded */
+               /* Types for which mono_class_get_checked () succeeded */
                for (i = 0, tmp = list; tmp; i++, tmp = tmp->next) {
                        MonoException *exc = mono_class_get_exception_for_failure (tmp->data);
                        mono_array_setref (exl, i, exc);
@@ -5321,6 +5316,7 @@ ves_icall_System_Reflection_AssemblyName_ParseName (MonoReflectionAssemblyName *
 ICALL_EXPORT MonoReflectionType*
 ves_icall_System_Reflection_Module_GetGlobalType (MonoReflectionModule *module)
 {
+       MonoError error;
        MonoDomain *domain = mono_object_domain (module); 
        MonoClass *klass;
 
@@ -5332,7 +5328,8 @@ ves_icall_System_Reflection_Module_GetGlobalType (MonoReflectionModule *module)
                /* These images do not have a global type */
                return NULL;
 
-       klass = mono_class_get (module->image, 1 | MONO_TOKEN_TYPE_DEF);
+       klass = mono_class_get_checked (module->image, 1 | MONO_TOKEN_TYPE_DEF, &error);
+       mono_error_raise_exception (&error);
        return mono_type_get_object (domain, &klass->byval_arg);
 }
 
@@ -5442,19 +5439,20 @@ init_generic_context_from_args (MonoGenericContext *context, MonoArray *type_arg
 }
 
 ICALL_EXPORT MonoType*
-ves_icall_System_Reflection_Module_ResolveTypeToken (MonoImage *image, guint32 token, MonoArray *type_args, MonoArray *method_args, MonoResolveTokenError *error)
+ves_icall_System_Reflection_Module_ResolveTypeToken (MonoImage *image, guint32 token, MonoArray *type_args, MonoArray *method_args, MonoResolveTokenError *resolve_error)
 {
        MonoClass *klass;
        int table = mono_metadata_token_table (token);
        int index = mono_metadata_token_index (token);
        MonoGenericContext context;
+       MonoError error;
 
-       *error = ResolveTokenError_Other;
+       *resolve_error = ResolveTokenError_Other;
 
        /* Validate token */
        if ((table != MONO_TABLE_TYPEDEF) && (table != MONO_TABLE_TYPEREF) && 
                (table != MONO_TABLE_TYPESPEC)) {
-               *error = ResolveTokenError_BadTable;
+               *resolve_error = ResolveTokenError_BadTable;
                return NULL;
        }
 
@@ -5470,15 +5468,15 @@ ves_icall_System_Reflection_Module_ResolveTypeToken (MonoImage *image, guint32 t
        }
 
        if ((index <= 0) || (index > image->tables [table].rows)) {
-               *error = ResolveTokenError_OutOfRange;
+               *resolve_error = ResolveTokenError_OutOfRange;
                return NULL;
        }
 
        init_generic_context_from_args (&context, type_args, method_args);
-       klass = mono_class_get_full (image, token, &context);
-
-       if (mono_loader_get_last_error ())
-               mono_raise_exception (mono_loader_error_prepare_exception (mono_loader_get_last_error ()));
+       klass = mono_class_get_checked (image, token, &error);
+       if (klass)
+               klass = mono_class_inflate_generic_class_checked (klass, &context, &error);
+       mono_error_raise_exception (&error);
 
        if (klass)
                return &klass->byval_arg;
@@ -7412,15 +7410,11 @@ ves_icall_System_Char_GetDataTablePointers (int category_data_version,
        *to_upper_data_high = ToUpperDataHigh;
 }
 
-ICALL_EXPORT gint32
-ves_icall_MonoDebugger_GetMethodToken (MonoReflectionMethod *method)
-{
-       return method->method->token;
-}
-
 /*
  * We return NULL for no modifiers so the corlib code can return Type.EmptyTypes
  * and avoid useless allocations.
+ * 
+ * MAY THROW
  */
 static MonoArray*
 type_array_from_modifiers (MonoImage *image, MonoType *type, int optional)
@@ -7437,7 +7431,9 @@ type_array_from_modifiers (MonoImage *image, MonoType *type, int optional)
        count = 0;
        for (i = 0; i < type->num_mods; ++i) {
                if ((optional && !type->modifiers [i].required) || (!optional && type->modifiers [i].required)) {
-                       MonoClass *klass = mono_class_get (image, type->modifiers [i].token);
+                       MonoError error;
+                       MonoClass *klass = mono_class_get_checked (image, type->modifiers [i].token, &error);
+                       mono_error_raise_exception (&error); /* this is safe, no cleanup needed on callers */ 
                        mono_array_setref (res, count, mono_type_get_object (mono_domain_get (), &klass->byval_arg));
                        count++;
                }