From 126b8fa3cedaa8c13541f6ea4fdcf95f97316237 Mon Sep 17 00:00:00 2001 From: Vlad Brezae Date: Wed, 17 Aug 2022 05:00:11 +0300 Subject: [PATCH] [mono] Implement missing functionality for cctor invocation (#73955) * [mono] Implement missing functionality for cctor invocation * [mono] Re-enable test --- .../System.Reflection/tests/ConstructorInfoTests.cs | 1 - .../src/System/Reflection/RuntimeMethodInfo.Mono.cs | 9 ++++++--- src/mono/mono/metadata/icall-def.h | 1 + src/mono/mono/metadata/icall.c | 12 ++++++++++++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Reflection/tests/ConstructorInfoTests.cs b/src/libraries/System.Reflection/tests/ConstructorInfoTests.cs index e6d59b787cc56..5e41d80f197c4 100644 --- a/src/libraries/System.Reflection/tests/ConstructorInfoTests.cs +++ b/src/libraries/System.Reflection/tests/ConstructorInfoTests.cs @@ -69,7 +69,6 @@ public void Invoke_StaticConstructor_NullObject_NullParameters() } [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsInvokingStaticConstructorsSupported))] - [ActiveIssue("https://github.com/dotnet/runtime/issues/40351", TestRuntimes.Mono)] public void Invoke_StaticConstructorMultipleTimes() { ConstructorInfo[] constructors = GetConstructors(typeof(ClassWithStaticConstructorThatIsCalledMultipleTimesViaReflection)); diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs index 146739163199b..08f30f04fca82 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs @@ -802,10 +802,13 @@ internal RuntimeType[] ArgumentTypes } } - private static void InvokeClassConstructor() + [MethodImplAttribute(MethodImplOptions.InternalCall)] + internal static extern void InvokeClassConstructor(QCallTypeHandle type); + + private void InvokeClassConstructor() { - // [TODO] Mechanism for invoking class constructor - // See https://github.com/dotnet/runtime/issues/40351 + RuntimeType type = (RuntimeType)DeclaringType; + InvokeClassConstructor(new QCallTypeHandle(ref type)); } /* diff --git a/src/mono/mono/metadata/icall-def.h b/src/mono/mono/metadata/icall-def.h index 5f47166ab1a64..073590ef5a9f1 100644 --- a/src/mono/mono/metadata/icall-def.h +++ b/src/mono/mono/metadata/icall-def.h @@ -357,6 +357,7 @@ HANDLES(RASSEM_7, "InternalGetReferencedAssemblies", ves_icall_System_Reflection ICALL_TYPE(MCMETH, "System.Reflection.RuntimeConstructorInfo", MCMETH_1) HANDLES(MCMETH_1, "GetGenericMethodDefinition_impl", ves_icall_RuntimeMethodInfo_GetGenericMethodDefinition, MonoReflectionMethod, 1, (MonoReflectionMethod)) HANDLES(MCMETH_2, "InternalInvoke", ves_icall_InternalInvoke, MonoObject, 4, (MonoReflectionMethod, MonoObject, MonoSpanOfObjects_ref, MonoExceptionOut)) +HANDLES(MCMETH_5, "InvokeClassConstructor", ves_icall_InvokeClassConstructor, void, 1, (MonoQCallTypeHandle)) HANDLES_REUSE_WRAPPER(MCMETH_4, "get_metadata_token", ves_icall_reflection_get_token) ICALL_TYPE(CATTR_DATA, "System.Reflection.RuntimeCustomAttributeData", CATTR_DATA_1) diff --git a/src/mono/mono/metadata/icall.c b/src/mono/mono/metadata/icall.c index 2fbdb8f330fe7..df14926eb572f 100644 --- a/src/mono/mono/metadata/icall.c +++ b/src/mono/mono/metadata/icall.c @@ -2809,6 +2809,18 @@ ves_icall_RuntimeTypeHandle_IsComObject (MonoQCallTypeHandle type_handle, MonoEr return mono_class_is_com_object (klass); } +void +ves_icall_InvokeClassConstructor (MonoQCallTypeHandle type_handle, MonoError *error) +{ + MonoType *type = type_handle.type; + MonoClass *klass = mono_class_from_mono_type_internal (type); + + MonoVTable *vtable = mono_class_vtable_checked (klass, error); + return_if_nok (error); + + mono_runtime_class_init_full (vtable, error); +} + guint32 ves_icall_reflection_get_token (MonoObjectHandle obj, MonoError *error) {