[runtime] Implement mono_method_get_class for CoreCLR. (#11389)

This commit is contained in:
Rolf Bjarne Kvinge 2021-04-30 07:53:30 +02:00 коммит произвёл GitHub
Родитель 6f743fd292
Коммит 8e9f86aad8
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
8 изменённых файлов: 54 добавлений и 4 удалений

Просмотреть файл

@ -231,4 +231,12 @@ mono_jit_exec (MonoDomain * domain, MonoAssembly * assembly, int argc, const cha
return (int) exitCode; 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 #endif // CORECLR_RUNTIME

Просмотреть файл

@ -341,6 +341,13 @@
OnlyCoreCLR = true, OnlyCoreCLR = true,
}, },
new XDelegate ("MonoObject *", "IntPtr", "xamarin_bridge_get_method_declaring_type",
"MonoObject *", "IntPtr", "gchandle"
) {
WrappedManagedFunction = "GetMethodDeclaringType",
OnlyDynamicUsage = false,
OnlyCoreCLR = true,
},
}; };
delegates.CalculateLengths (); delegates.CalculateLengths ();
#><#+ #><#+

Просмотреть файл

@ -329,7 +329,9 @@
new Export ("MonoClass *", "mono_method_get_class", new Export ("MonoClass *", "mono_method_get_class",
"MonoMethod *", "method" "MonoMethod *", "method"
), ) {
HasCoreCLRBridgeFunction = true,
},
new Export ("void", "mono_dllmap_insert", new Export ("void", "mono_dllmap_insert",
"MonoImage *", "assembly", "MonoImage *", "assembly",

Просмотреть файл

@ -82,7 +82,12 @@ typedef enum {
} MonoImageOpenStatus; } MonoImageOpenStatus;
/* metadata/metadata.h */ /* 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; typedef struct _MonoClass MonoClass;
#endif
typedef struct _MonoDomain MonoDomain; typedef struct _MonoDomain MonoDomain;
#if defined (CORECLR_RUNTIME) #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. // In Mono, MonoMethod is not related to MonoObject at all, but for the CoreCLR bridge we use the same struct representation for both types.

Просмотреть файл

@ -716,7 +716,9 @@ xamarin_check_for_gced_object (MonoObject *obj, SEL sel, id self, MonoMethod *me
if (obj != NULL) { if (obj != NULL) {
#if DEBUG #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 #endif
return; return;
} }

Просмотреть файл

@ -543,7 +543,10 @@ xamarin_invoke_trampoline (enum TrampolineType type, id self, SEL sel, iterator_
* This problem is documented in the following bug: * This problem is documented in the following bug:
* https://bugzilla.xamarin.com/show_bug.cgi?id=6556 * 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) if (exception_gchandle != INVALID_GCHANDLE)
goto exception_handling; goto exception_handling;
ADD_TO_MONOOBJECT_RELEASE_LIST (retval); ADD_TO_MONOOBJECT_RELEASE_LIST (retval);

Просмотреть файл

@ -15,6 +15,8 @@ using System.Runtime.InteropServices;
using Foundation; using Foundation;
using MonoObjectPtr=System.IntPtr;
namespace ObjCRuntime { namespace ObjCRuntime {
public partial class Runtime { public partial class Runtime {
@ -107,6 +109,17 @@ namespace ObjCRuntime {
return rv; 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> (T value) where T: struct static IntPtr MarshalStructure<T> (T value) where T: struct
{ {
var rv = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (T))); var rv = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (T)));
@ -139,6 +152,13 @@ namespace ObjCRuntime {
var obj = (NSObject) GetGCHandleTarget (gchandle); var obj = (NSObject) GetGCHandleTarget (gchandle);
return (byte) obj.FlagsInternal; return (byte) obj.FlagsInternal;
} }
static IntPtr GetMethodDeclaringType (MonoObjectPtr mobj)
{
var method = (MethodBase) GetMonoObjectTarget (mobj);
return GetMonoObject (method.DeclaringType);
}
} }
} }

Просмотреть файл

@ -3756,7 +3756,10 @@ namespace Registrar {
// the actual invoke // the actual invoke
if (isCtor) { 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);"); cleanup.AppendLine ("xamarin_mono_object_release (&mthis);");
invoke.AppendLine ("if (exception_gchandle != INVALID_GCHANDLE) goto exception_handling;"); invoke.AppendLine ("if (exception_gchandle != INVALID_GCHANDLE) goto exception_handling;");
} }