Skip to content

Commit

Permalink
[runtime] Implement xamarin_get_[nsnumber|nsvalue]_type for CoreCLR. (x…
Browse files Browse the repository at this point in the history
…amarin#11552)

* Remove a few unused xamarin_get_*_class functions.
* Make the remaining two (xamarin_get_[nsnumber|nsvalue]_type) return a
  MonoType* instead of MonoClass* - that makes things slightly simpler for
  CoreCLR (the MonoClass* return values from the previous functions were
  always converted to MonoType*s anyway).
* Implement the xamarin_get_[nsnumber|nsvalue]_type functions.
* Make the existing mono_get_string_class use the new (and more generic)
  xamarin_bridge_lookup_class method instead of the specific
  xamarin_bridge_get_string_class (which can now be removed).
  • Loading branch information
rolfbjarne committed May 14, 2021
1 parent 42740a5 commit 9a013fd
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 50 deletions.
46 changes: 15 additions & 31 deletions runtime/coreclr-bridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -108,40 +108,24 @@
return method;
}

MonoClass *
xamarin_get_nsnumber_class ()
{
xamarin_assertion_message ("The method %s it not implemented yet for CoreCLR", __func__);
}

MonoClass *
xamarin_get_nsvalue_class ()
{
xamarin_assertion_message ("The method %s it not implemented yet for CoreCLR", __func__);
}

MonoClass *
xamarin_get_inativeobject_class ()
{
xamarin_assertion_message ("The method %s it not implemented yet for CoreCLR", __func__);
}

MonoClass *
xamarin_get_nsobject_class ()
{
xamarin_assertion_message ("The method %s it not implemented yet for CoreCLR", __func__);
}

MonoClass *
xamarin_get_nsstring_class ()
MonoType *
xamarin_get_nsnumber_type ()
{
xamarin_assertion_message ("The method %s it not implemented yet for CoreCLR", __func__);
// xamarin_bridge_lookup_class returns a MonoClass*, and this method returns a MonoType*,
// but they're interchangeable for CoreCLR (they're all just MonoObject*s), so this is fine.
MonoClass *rv = xamarin_bridge_lookup_class (XamarinLookupTypes_Foundation_NSNumber);
LOG_CORECLR (stderr, "%s () => %p\n", __func__, rv);
return rv;
}

MonoClass *
xamarin_get_runtime_class ()
MonoType *
xamarin_get_nsvalue_type ()
{
xamarin_assertion_message ("The method %s it not implemented yet for CoreCLR", __func__);
// xamarin_bridge_lookup_class returns a MonoClass*, and this method returns a MonoType*,
// but they're interchangeable for CoreCLR (they're all just MonoObject*s), so this is fine.
MonoClass *rv = xamarin_bridge_lookup_class (XamarinLookupTypes_Foundation_NSValue);
LOG_CORECLR (stderr, "%s () => %p\n", __func__, rv);
return rv;
}

void
Expand Down Expand Up @@ -529,7 +513,7 @@
MonoClass *
mono_get_string_class ()
{
MonoClass *rv = xamarin_bridge_get_string_class ();
MonoClass *rv = xamarin_bridge_lookup_class (XamarinLookupTypes_System_String);
LOG_CORECLR (stderr, "%s () => %p.\n", __func__, rv);
return rv;
}
Expand Down
6 changes: 4 additions & 2 deletions runtime/delegates.t4
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,10 @@
OnlyCoreCLR = true,
},

new XDelegate ("MonoObject *", "MonoObject *", "xamarin_bridge_get_string_class") {
WrappedManagedFunction = "GetStringClass",
new XDelegate ("MonoClass *", "MonoObject *", "xamarin_bridge_lookup_class",
"enum XamarinLookupTypes", "Runtime.TypeLookup", "type"
) {
WrappedManagedFunction = "LookupType",
OnlyDynamicUsage = false,
OnlyCoreCLR = true,
},
Expand Down
14 changes: 6 additions & 8 deletions runtime/monovm-bridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -172,20 +172,20 @@
return nsobject_class;
}

MonoClass *
xamarin_get_nsvalue_class ()
MonoType *
xamarin_get_nsvalue_type ()
{
if (nsvalue_class == NULL)
xamarin_assertion_message ("Internal consistency error, please file a bug (https://github.com/xamarin/xamarin-macios/issues/new). Additional data: can't get the %s class because it's been linked away.\n", "NSValue");
return nsvalue_class;
return mono_class_get_type (nsvalue_class);
}

MonoClass *
xamarin_get_nsnumber_class ()
MonoType *
xamarin_get_nsnumber_type ()
{
if (nsnumber_class == NULL)
xamarin_assertion_message ("Internal consistency error, please file a bug (https://github.com/xamarin/xamarin-macios/issues/new). Additional data: can't get the %s class because it's been linked away.\n", "NSNumber");
return nsnumber_class;
return mono_class_get_type (nsnumber_class);
}

MonoClass *
Expand Down Expand Up @@ -324,7 +324,6 @@
// COOP: Reading managed data, must be in UNSAFE mode
MONO_ASSERT_GC_UNSAFE;

MonoClass *nsnumber_class = xamarin_get_nsnumber_class ();
if (nsnumber_class == NULL)
return false;

Expand All @@ -337,7 +336,6 @@
// COOP: Reading managed data, must be in UNSAFE mode
MONO_ASSERT_GC_UNSAFE;

MonoClass *nsvalue_class = xamarin_get_nsvalue_class ();
if (nsvalue_class == NULL)
return false;

Expand Down
4 changes: 2 additions & 2 deletions runtime/trampolines.m
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@
func = xamarin_get_nsnumber_converter (baseClass, method, to_managed, exception_gchandle);
xamarin_mono_object_release (&baseClass);
} else {
MonoType *nsnumberType = mono_class_get_type (xamarin_get_nsnumber_class ());
MonoType *nsnumberType = xamarin_get_nsnumber_type ();
*exception_gchandle = xamarin_create_bindas_exception (mtype, nsnumberType, method);
xamarin_mono_object_release (&nsnumberType);
goto exception_handling;
Expand Down Expand Up @@ -1533,7 +1533,7 @@
#endif
} else {
MonoType *mType = mono_class_get_type (managedType);
MonoType *nsvalueType = mono_class_get_type (xamarin_get_nsvalue_class ());
MonoType *nsvalueType = xamarin_get_nsvalue_type ();
*exception_gchandle = xamarin_create_bindas_exception (mType, nsvalueType, method);
xamarin_mono_object_release (&mType);
xamarin_mono_object_release (&nsvalueType);
Expand Down
4 changes: 2 additions & 2 deletions runtime/xamarin/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ MonoException * xamarin_create_system_entry_point_not_found_exception (const cha
NSString * xamarin_print_all_exceptions (GCHandle handle);

id xamarin_invoke_objc_method_implementation (id self, SEL sel, IMP xamarin_impl);
MonoClass * xamarin_get_nsnumber_class ();
MonoClass * xamarin_get_nsvalue_class ();
MonoType * xamarin_get_nsnumber_type ();
MonoType * xamarin_get_nsvalue_type ();
MonoClass * xamarin_get_inativeobject_class ();
MonoClass * xamarin_get_nsobject_class ();
MonoClass * xamarin_get_nsstring_class ();
Expand Down
28 changes: 23 additions & 5 deletions src/ObjCRuntime/Runtime.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,29 @@ static bool IsClassOfType (Type type, TypeLookup match)
return rv;
}

static unsafe MonoObject* LookupType (TypeLookup type)
{
Type rv = null;

switch (type) {
case TypeLookup.Foundation_NSNumber:
rv = typeof (Foundation.NSNumber);
break;
case TypeLookup.Foundation_NSValue:
rv = typeof (Foundation.NSValue);
break;
case TypeLookup.System_String:
rv = typeof (string);
break;
default:
throw new ArgumentOutOfRangeException (nameof (type));
}

log_coreclr ($"LookupType ({type}) => {rv}");

return (MonoObject*) GetMonoObject (rv);
}

static unsafe MonoObject* GetElementClass (MonoObject* classobj)
{
var type = (Type) GetMonoObjectTarget (classobj);
Expand Down Expand Up @@ -663,11 +686,6 @@ static bool IsNullable (Type type)
return false;
}

static unsafe MonoObject* GetStringClass ()
{
return (MonoObject *) GetMonoObject (typeof (string));
}

unsafe static bool IsByRef (MonoObject *typeobj)
{
var type = (Type) GetMonoObjectTarget (typeobj);
Expand Down

0 comments on commit 9a013fd

Please sign in to comment.