[reflection] Use coop handles for MonoMethod icalls (#4272)
[mono.git] / mono / metadata / reflection.c
index fc88d2b7f55c18c22dce1cc84247aa8e7c765c3a..c6d637cb61c16ed0a280a1152fba4cc17f9dd623 100644 (file)
@@ -2442,50 +2442,74 @@ mono_class_bind_generic_parameters (MonoClass *klass, int type_argc, MonoType **
        return mono_generic_class_get_class (gclass);
 }
 
-static MonoReflectionMethod*
-reflection_bind_generic_method_parameters (MonoReflectionMethod *rmethod, MonoArray *types, MonoError *error)
+static MonoGenericInst*
+generic_inst_from_type_array_handle (MonoArrayHandle types, MonoError *error)
+{
+       HANDLE_FUNCTION_ENTER ();
+       mono_error_init (error);
+       MonoGenericInst *ginst = NULL;
+       int count = mono_array_handle_length (types);
+       MonoType **type_argv = g_new0 (MonoType *, count);
+       MonoReflectionTypeHandle garg = MONO_HANDLE_NEW (MonoReflectionType, NULL);
+       for (int i = 0; i < count; i++) {
+               MONO_HANDLE_ARRAY_GETREF (garg, types, i);
+               type_argv [i] = mono_reflection_type_handle_mono_type (garg, error);
+               if (!is_ok (error))
+                       goto leave;
+
+       }
+       ginst = mono_metadata_get_generic_inst (count, type_argv);
+leave:
+       g_free (type_argv);
+       HANDLE_FUNCTION_RETURN_VAL (ginst);
+}
+
+static MonoMethod*
+reflection_bind_generic_method_parameters (MonoMethod *method, MonoArrayHandle types, MonoError *error)
 {
        MonoClass *klass;
-       MonoMethod *method, *inflated;
-       MonoMethodInflated *imethod;
+       MonoMethod *inflated;
        MonoGenericContext tmp_context;
-       MonoGenericInst *ginst;
-       MonoType **type_argv;
-       int count, i;
 
        mono_error_init (error);
 
-       g_assert (strcmp (rmethod->object.vtable->klass->name, "MethodBuilder"));
-
-       method = rmethod->method;
-
        klass = method->klass;
 
        if (method->is_inflated)
                method = ((MonoMethodInflated *) method)->declaring;
 
-       count = mono_method_signature (method)->generic_param_count;
-       if (count != mono_array_length (types))
+       int count = mono_method_signature (method)->generic_param_count;
+       if (count != mono_array_handle_length (types)) {
+               mono_error_set_argument (error, "typeArguments", "Incorrect number of generic arguments");
                return NULL;
-
-       type_argv = g_new0 (MonoType *, count);
-       for (i = 0; i < count; i++) {
-               MonoReflectionType *garg = (MonoReflectionType *)mono_array_get (types, gpointer, i);
-               type_argv [i] = mono_reflection_type_get_handle (garg, error);
-               if (!is_ok (error)) {
-                       g_free (type_argv);
-                       return NULL;
-               }
        }
-       ginst = mono_metadata_get_generic_inst (count, type_argv);
-       g_free (type_argv);
+
+       MonoGenericInst *ginst = generic_inst_from_type_array_handle (types, error);
+       return_val_if_nok (error, NULL);
 
        tmp_context.class_inst = mono_class_is_ginst (klass) ? mono_class_get_generic_class (klass)->context.class_inst : NULL;
        tmp_context.method_inst = ginst;
 
        inflated = mono_class_inflate_generic_method_checked (method, &tmp_context, error);
        mono_error_assert_ok (error);
-       imethod = (MonoMethodInflated *) inflated;
+
+       if (!mono_verifier_is_method_valid_generic_instantiation (inflated)) {
+               mono_error_set_argument (error, "typeArguments", "Invalid generic arguments");
+               return NULL;
+       }
+
+       return inflated;
+}
+
+MonoReflectionMethodHandle
+ves_icall_MonoMethod_MakeGenericMethod_impl (MonoReflectionMethodHandle rmethod, MonoArrayHandle types, MonoError *error)
+{
+       mono_error_init (error);
+       g_assert (0 != strcmp (mono_handle_class (rmethod)->name, "MethodBuilder"));
+
+       MonoMethod *method = MONO_HANDLE_GETVAL (rmethod, method);
+       MonoMethod *imethod = reflection_bind_generic_method_parameters (method, types, error);
+       return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
 
        /*FIXME but I think this is no longer necessary*/
        if (image_is_dynamic (method->klass->image)) {
@@ -2495,25 +2519,11 @@ reflection_bind_generic_method_parameters (MonoReflectionMethod *rmethod, MonoAr
                 * to the reflection objects representing their generic definitions.
                 */
                mono_image_lock ((MonoImage*)image);
-               mono_g_hash_table_insert (image->generic_def_objects, imethod, rmethod);
+               mono_g_hash_table_insert (image->generic_def_objects, imethod, MONO_HANDLE_RAW (rmethod));
                mono_image_unlock ((MonoImage*)image);
        }
 
-       if (!mono_verifier_is_method_valid_generic_instantiation (inflated)) {
-               mono_error_set_argument (error, "typeArguments", "Invalid generic arguments");
-               return NULL;
-       }
-       
-       return mono_method_get_object_checked (mono_object_domain (rmethod), inflated, NULL, error);
-}
-
-MonoReflectionMethod*
-ves_icall_MonoMethod_MakeGenericMethod_impl (MonoReflectionMethod *rmethod, MonoArray *types)
-{
-       MonoError error;
-       MonoReflectionMethod *result = reflection_bind_generic_method_parameters (rmethod, types, &error);
-       mono_error_set_pending_exception (&error);
-       return result;
+       return mono_method_get_object_handle (MONO_HANDLE_DOMAIN (rmethod), imethod, NULL, error);
 }