From 8e9f86aad892b846cd9e9510d766324e138949b3 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Fri, 30 Apr 2021 07:53:30 +0200 Subject: [PATCH] [runtime] Implement mono_method_get_class for CoreCLR. (#11389) --- runtime/coreclr-bridge.m | 8 ++++++++ runtime/delegates.t4 | 7 +++++++ runtime/exports.t4 | 4 +++- runtime/mono-runtime.h.t4 | 5 +++++ runtime/runtime.m | 4 +++- runtime/trampolines-invoke.m | 5 ++++- src/ObjCRuntime/Runtime.CoreCLR.cs | 20 ++++++++++++++++++++ tools/common/StaticRegistrar.cs | 5 ++++- 8 files changed, 54 insertions(+), 4 deletions(-) diff --git a/runtime/coreclr-bridge.m b/runtime/coreclr-bridge.m index 7487e52eb4..ab38a051ec 100644 --- a/runtime/coreclr-bridge.m +++ b/runtime/coreclr-bridge.m @@ -231,4 +231,12 @@ mono_jit_exec (MonoDomain * domain, MonoAssembly * assembly, int argc, const cha return (int) exitCode; } +MonoClass * +mono_method_get_class (MonoMethod * method) +{ + MonoClass *rv = xamarin_bridge_get_method_declaring_type (method); + LOG_CORECLR (stderr, "%s (%p) => %p\n", __func__, method, rv); + return rv; +} + #endif // CORECLR_RUNTIME diff --git a/runtime/delegates.t4 b/runtime/delegates.t4 index 91093985ee..3af5265d4c 100644 --- a/runtime/delegates.t4 +++ b/runtime/delegates.t4 @@ -341,6 +341,13 @@ OnlyCoreCLR = true, }, + new XDelegate ("MonoObject *", "IntPtr", "xamarin_bridge_get_method_declaring_type", + "MonoObject *", "IntPtr", "gchandle" + ) { + WrappedManagedFunction = "GetMethodDeclaringType", + OnlyDynamicUsage = false, + OnlyCoreCLR = true, + }, }; delegates.CalculateLengths (); #><#+ diff --git a/runtime/exports.t4 b/runtime/exports.t4 index 4d06b4c22e..28cac17e41 100644 --- a/runtime/exports.t4 +++ b/runtime/exports.t4 @@ -329,7 +329,9 @@ new Export ("MonoClass *", "mono_method_get_class", "MonoMethod *", "method" - ), + ) { + HasCoreCLRBridgeFunction = true, + }, new Export ("void", "mono_dllmap_insert", "MonoImage *", "assembly", diff --git a/runtime/mono-runtime.h.t4 b/runtime/mono-runtime.h.t4 index 754c2c1ea8..7200e5d6a9 100644 --- a/runtime/mono-runtime.h.t4 +++ b/runtime/mono-runtime.h.t4 @@ -82,7 +82,12 @@ typedef enum { } MonoImageOpenStatus; /* metadata/metadata.h */ +#if defined (CORECLR_RUNTIME) +// In Mono, MonoClass is not related to MonoObject at all, but for the CoreCLR bridge we use the same struct representation for both types. +typedef struct _MonoObject MonoClass; +#else typedef struct _MonoClass MonoClass; +#endif typedef struct _MonoDomain MonoDomain; #if defined (CORECLR_RUNTIME) // In Mono, MonoMethod is not related to MonoObject at all, but for the CoreCLR bridge we use the same struct representation for both types. diff --git a/runtime/runtime.m b/runtime/runtime.m index c9120d3bcd..cf5d7c4491 100644 --- a/runtime/runtime.m +++ b/runtime/runtime.m @@ -716,7 +716,9 @@ xamarin_check_for_gced_object (MonoObject *obj, SEL sel, id self, MonoMethod *me if (obj != NULL) { #if DEBUG - verify_cast (mono_method_get_class (method), obj, [self class], sel, method, exception_gchandle); + MonoClass *declaring_type = mono_method_get_class (method); + verify_cast (declaring_type, obj, [self class], sel, method, exception_gchandle); + xamarin_mono_object_release (&declaring_type); #endif return; } diff --git a/runtime/trampolines-invoke.m b/runtime/trampolines-invoke.m index 1f0e77b968..18e570c254 100644 --- a/runtime/trampolines-invoke.m +++ b/runtime/trampolines-invoke.m @@ -543,7 +543,10 @@ xamarin_invoke_trampoline (enum TrampolineType type, id self, SEL sel, iterator_ * This problem is documented in the following bug: * https://bugzilla.xamarin.com/show_bug.cgi?id=6556 */ - retval = xamarin_new_nsobject (self, mono_method_get_class (method), &exception_gchandle); + MonoClass *declaring_type = mono_method_get_class (method); + ADD_TO_MONOOBJECT_RELEASE_LIST (declaring_type); + + retval = xamarin_new_nsobject (self, declaring_type, &exception_gchandle); if (exception_gchandle != INVALID_GCHANDLE) goto exception_handling; ADD_TO_MONOOBJECT_RELEASE_LIST (retval); diff --git a/src/ObjCRuntime/Runtime.CoreCLR.cs b/src/ObjCRuntime/Runtime.CoreCLR.cs index cf12bccd62..031b2b564c 100644 --- a/src/ObjCRuntime/Runtime.CoreCLR.cs +++ b/src/ObjCRuntime/Runtime.CoreCLR.cs @@ -15,6 +15,8 @@ using System.Runtime.InteropServices; using Foundation; +using MonoObjectPtr=System.IntPtr; + namespace ObjCRuntime { public partial class Runtime { @@ -107,6 +109,17 @@ namespace ObjCRuntime { return rv; } + static object GetMonoObjectTarget (MonoObjectPtr mobj) + { + if (mobj == IntPtr.Zero) + return null; + + unsafe { + MonoObject *monoobj = (MonoObject *) mobj; + return GetGCHandleTarget (monoobj->GCHandle); + } + } + static IntPtr MarshalStructure (T value) where T: struct { var rv = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (T))); @@ -139,6 +152,13 @@ namespace ObjCRuntime { var obj = (NSObject) GetGCHandleTarget (gchandle); return (byte) obj.FlagsInternal; } + + static IntPtr GetMethodDeclaringType (MonoObjectPtr mobj) + { + var method = (MethodBase) GetMonoObjectTarget (mobj); + return GetMonoObject (method.DeclaringType); + } + } } diff --git a/tools/common/StaticRegistrar.cs b/tools/common/StaticRegistrar.cs index f0b31a3fa4..898cf5343f 100644 --- a/tools/common/StaticRegistrar.cs +++ b/tools/common/StaticRegistrar.cs @@ -3756,7 +3756,10 @@ namespace Registrar { // the actual invoke if (isCtor) { - invoke.AppendLine ("mthis = xamarin_new_nsobject (self, mono_method_get_class (managed_method), &exception_gchandle);"); + body_setup.AppendLine ("MonoClass *declaring_type = NULL;"); + invoke.AppendLine ("declaring_type = mono_method_get_class (managed_method);"); + invoke.AppendLine ("mthis = xamarin_new_nsobject (self, declaring_type, &exception_gchandle);"); + invoke.AppendLine ("xamarin_mono_object_release (&declaring_type);"); cleanup.AppendLine ("xamarin_mono_object_release (&mthis);"); invoke.AppendLine ("if (exception_gchandle != INVALID_GCHANDLE) goto exception_handling;"); }