diff --git a/src/ObjCRuntime/Exceptions.cs b/src/ObjCRuntime/Exceptions.cs
index 88e12b9c389c..91d7dbf6313c 100644
--- a/src/ObjCRuntime/Exceptions.cs
+++ b/src/ObjCRuntime/Exceptions.cs
@@ -14,7 +14,7 @@ namespace ObjCRuntime {
/// Always null.
/// The exception data for the Objective-C exception.
/// Exception marshaling
- public delegate void MarshalObjectiveCExceptionHandler (object sender, MarshalObjectiveCExceptionEventArgs args);
+ public delegate void MarshalObjectiveCExceptionHandler (object? sender, MarshalObjectiveCExceptionEventArgs args);
/// The event args for the event.
/// Exception marshaling
@@ -47,7 +47,7 @@ public MarshalObjectiveCExceptionEventArgs (NSException exception, MarshalObject
/// Always null.
/// The exception data for the managed exception.
/// Exception marshaling
- public delegate void MarshalManagedExceptionHandler (object sender, MarshalManagedExceptionEventArgs args);
+ public delegate void MarshalManagedExceptionHandler (object? sender, MarshalManagedExceptionEventArgs args);
/// The event args for the event.
/// Exception marshaling
diff --git a/src/ObjCRuntime/Registrar.cs b/src/ObjCRuntime/Registrar.cs
index 740ea4335044..3e7c62a06ecb 100644
--- a/src/ObjCRuntime/Registrar.cs
+++ b/src/ObjCRuntime/Registrar.cs
@@ -56,7 +56,7 @@ namespace ObjCRuntime {
/// To be added.
/// To be added.
/// To be added.
- public delegate void AssemblyRegistrationHandler (object sender, AssemblyRegistrationEventArgs args);
+ public delegate void AssemblyRegistrationHandler (object? sender, AssemblyRegistrationEventArgs args);
/// To be added.
/// To be added.
diff --git a/src/ObjCRuntime/Runtime.cs b/src/ObjCRuntime/Runtime.cs
index b1daec22eac9..d4e11c5bc69c 100644
--- a/src/ObjCRuntime/Runtime.cs
+++ b/src/ObjCRuntime/Runtime.cs
@@ -25,16 +25,10 @@
using AppKit;
#endif
-// Disable until we get around to enable + fix any issues.
-#nullable disable
-#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
-
namespace ObjCRuntime {
- /// Provides information about the Xamarin.iOS Runtime.
- ///
- ///
- /// SysSound
+ /// Provides information about the runtime.
+ /// SysSound
public partial class Runtime {
#if !COREBUILD
#pragma warning disable 8618 // "Non-nullable field '...' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.": we make sure through other means that these will never be null
@@ -258,11 +252,11 @@ internal unsafe static bool IsManagedStaticRegistrar {
}
/// If dynamic registration is supported.
- /// If dynamic registration is supported.
- ///
- /// At build time the managed linker can in some cases determine whether dynamic registration is required or not, and if not, it can optimize away the code to support dynamic registration (to minimize app size). If support for dynamic registration has been removed by the linker, this property will return false.
- ///
- /// Build optimizations (removal of the dynamic registrar)
+ /// If dynamic registration is supported.
+ ///
+ /// At build time the managed linker can in some cases determine whether dynamic registration is required or not, and if not, it can optimize away the code to support dynamic registration (to minimize app size). If support for dynamic registration has been removed by the linker, this property will return false.
+ ///
+ /// Build optimizations (removal of the dynamic registrar)
[BindingImpl (BindingImplOptions.Optimizable)]
public static bool DynamicRegistrationSupported {
get {
@@ -396,6 +390,8 @@ static MarshalObjectiveCExceptionMode OnMarshalObjectiveCException (IntPtr excep
if (MarshalObjectiveCException is not null) {
var exception = GetNSObject (exception_handle);
+ if (exception is null)
+ return objc_exception_mode;
var args = new MarshalObjectiveCExceptionEventArgs (
exception,
(throwManagedAsDefault != 0) ? MarshalObjectiveCExceptionMode.ThrowManagedException : objc_exception_mode
@@ -410,10 +406,11 @@ static MarshalObjectiveCExceptionMode OnMarshalObjectiveCException (IntPtr excep
static MarshalManagedExceptionMode OnMarshalManagedException (IntPtr exception_handle)
{
if (MarshalManagedException is not null) {
- var exception = GCHandle.FromIntPtr (exception_handle).Target as Exception;
- var args = new MarshalManagedExceptionEventArgs (exception, managed_exception_mode);
- MarshalManagedException (null, args);
- return args.ExceptionMode;
+ if (GCHandle.FromIntPtr (exception_handle).Target is Exception exception) {
+ var args = new MarshalManagedExceptionEventArgs (exception, managed_exception_mode);
+ MarshalManagedException (null, args);
+ return args.ExceptionMode;
+ }
}
return managed_exception_mode;
}
@@ -430,8 +427,7 @@ static IntPtr ConvertSmartEnumToNSString (IntPtr value_handle)
{
var value = GetGCHandleTarget (value_handle)!;
var smart_type = value.GetType ();
- MethodBase getConstantMethod, getValueMethod;
- if (!Registrar.IsSmartEnum (smart_type, out getConstantMethod, out getValueMethod))
+ if (!Registrar.IsSmartEnum (smart_type, out var getConstantMethod, out var getValueMethod))
throw ErrorHelper.CreateError (8024, $"Could not find a valid extension type for the smart enum '{smart_type.FullName}'. Please file a bug at https://github.com/dotnet/macios/issues/new.");
var rv = (NSString?) ((MethodInfo) getConstantMethod).Invoke (null, new object [] { value });
if (rv is null)
@@ -449,8 +445,7 @@ static IntPtr ConvertNSStringToSmartEnum (IntPtr value, IntPtr type)
{
var smart_type = (Type) GetGCHandleTarget (type)!;
var str = GetNSObject (value)!;
- MethodBase getConstantMethod, getValueMethod;
- if (!Registrar.IsSmartEnum (smart_type, out getConstantMethod, out getValueMethod))
+ if (!Registrar.IsSmartEnum (smart_type, out var getConstantMethod, out var getValueMethod))
throw ErrorHelper.CreateError (8024, $"Could not find a valid extension type for the smart enum '{smart_type.FullName}'. Please file a bug at https://github.com/dotnet/macios/issues/new.");
var rv = ((MethodInfo) getValueMethod).Invoke (null, new object [] { str });
return AllocGCHandle (rv);
@@ -658,10 +653,10 @@ internal static string ComputeSignature (MethodInfo method, bool isBlockSignatur
}
/// The assembly to process.
- /// Registers all of the classes in the specified assembly.
- ///
- /// This iterates over all the types that derive from NSObject in the specified assembly and registers them with the runtime.
- ///
+ /// Registers all of the classes in the specified assembly.
+ ///
+ /// This iterates over all the types that derive from in the specified assembly and registers them with the runtime.
+ ///
[BindingImpl (BindingImplOptions.Optimizable)]
public static void RegisterAssembly (Assembly a)
{
@@ -1601,12 +1596,8 @@ static IntPtr CreateNSObject (IntPtr type_gchandle, IntPtr handle, NSObject.Flag
}
/// A pointer to an unmanaged NSObject or any class that derives from the Objective-C NSObject class.
- /// Looks up an existing wrapper object for an unmanaged IntPtr.
- /// If a managed wrapper exists for the specified IntPtr, that wrapper is returned, otherwise null.
- ///
- ///
- ///
- ///
+ /// Looks up an existing wrapper object for an unmanaged .
+ /// If a managed wrapper exists for the specified , that wrapper is returned, otherwise null.
public static NSObject? TryGetNSObject (IntPtr ptr)
{
return TryGetNSObject (ptr, evenInFinalizerQueue: false);
@@ -1663,11 +1654,11 @@ static IntPtr CreateNSObject (IntPtr type_gchandle, IntPtr handle, NSObject.Flag
}
/// A pointer to an unmanaged NSObject or any class that derives from the Objective-C NSObject class.
- /// Wraps an unmanaged IntPtr into a fully typed NSObject, or returns an existing wrapper object if one already exists.
- /// An instance of a class that derives from Foundation.NSObject.
- ///
- /// The runtime create an instance of the most derived class.
- ///
+ /// Wraps an unmanaged IntPtr into a fully typed NSObject, or returns an existing wrapper object if one already exists.
+ /// An instance of a class that derives from Foundation.NSObject.
+ ///
+ /// The runtime create an instance of the most derived class.
+ ///
public static NSObject? GetNSObject (IntPtr ptr)
{
return GetNSObject (ptr, MissingCtorResolution.ThrowConstructor1NotFound);
@@ -1776,23 +1767,21 @@ static IntPtr CreateNSObject (IntPtr type_gchandle, IntPtr handle, NSObject.Flag
obj = ConstructNSObject (ptr, target_type, MissingCtorResolution.ThrowConstructor1NotFound, sel, method_handle);
if (obj is T o2)
return o2;
- throw new InvalidCastException ($"Unable to cast object of type '{obj.GetType ().FullName}' to type '{typeof (T).FullName}'.");
+ throw new InvalidCastException ($"Unable to cast object of type '{obj?.GetType ().FullName}' to type '{typeof (T).FullName}'.");
}
return ConstructNSObject (ptr, target_type, MissingCtorResolution.ThrowConstructor1NotFound, sel, method_handle);
}
/// Type to wrap the native object as.
- /// A pointer to an unmanaged NSObject or any class that derives from the Objective-C NSObject class.
- /// Pass true if the caller has a reference to the native object, and wants to give it to the managed wrapper instance. Otherwise pass false (and the native object will be retained).
- /// Wraps an unmanaged IntPtr into a fully typed NSObject, or returns an existing wrapper object if one already exists.
- /// An instance of the T class.
- ///
- /// Returns an instance of the T class even if the native object is not in the class hierarchy of T (no type checks).
- ///
- ///
- /// This method will fail if there already is a managed wrapper of a different (and incompatible) type for the native object.
- ///
+ /// A pointer to an unmanaged NSObject or any class that derives from the Objective-C NSObject class.
+ /// Pass true if the caller has a reference to the native object, and wants to give it to the managed wrapper instance. Otherwise pass false (and the native object will be retained).
+ /// Wraps an unmanaged IntPtr into a fully typed NSObject, or returns an existing wrapper object if one already exists.
+ /// An instance of the T class.
+ ///
+ /// Returns an instance of the T class even if the native object is not in the class hierarchy of T (no type checks).
+ /// This method will fail if there already is a managed wrapper of a different (and incompatible) type for the native object.
+ ///
static public T? GetNSObject (IntPtr ptr, bool owns) where T : NSObject
{
var obj = GetNSObject (ptr);
@@ -1912,12 +1901,12 @@ static Type LookupINativeObjectImplementation (IntPtr ptr, Type target_type, Typ
return implementation!;
}
- /// To be added.
- /// To be added.
- /// To be added.
- /// To be added.
- /// To be added.
- /// To be added.
+ /// Wraps a native object pointer in a managed object of the specified type.
+ /// A pointer to the native object.
+ /// Pass if the caller transfers ownership of a native reference to the managed wrapper; otherwise pass .
+ /// The managed type the native object should be wrapped as.
+ /// A managed object implementing , or if is .
+ /// This method may return an existing wrapper if one has already been created for the native object.
public static INativeObject? GetINativeObject (IntPtr ptr, bool owns, Type target_type)
{
return GetINativeObject (ptr, owns, target_type, null);
@@ -1971,13 +1960,13 @@ static Type LookupINativeObjectImplementation (IntPtr ptr, Type target_type, Typ
// this method is identical in behavior to the non-generic one.
/// The type of the object to return. This can also be an interface corresponding to an Objective-C protocol.
- /// A pointer to a native object.
- /// Pass true if the caller has a reference to the native object, and wants to give it to the managed wrapper instance. Otherwise pass false (and the native object will be retained).
- /// Wraps an native IntPtr with a managed object of the specified type.
- /// An instance of a class implementing the specified type.
- ///
- /// Returns an instance of the specified type even if the native object is not in the class hierarchy of type (there are no type checks).
- ///
+ /// A pointer to a native object.
+ /// Pass true if the caller has a reference to the native object, and wants to give it to the managed wrapper instance. Otherwise pass false (and the native object will be retained).
+ /// Wraps an native IntPtr with a managed object of the specified type.
+ /// An instance of a class implementing the specified type.
+ ///
+ /// Returns an instance of the specified type even if the native object is not in the class hierarchy of type (there are no type checks).
+ ///
public static T? GetINativeObject (IntPtr ptr, bool owns) where T : INativeObject
{
return GetINativeObject (ptr, false, owns);
@@ -2104,11 +2093,11 @@ static void TryReleaseINativeObject (INativeObject? obj)
extern static uint xamarin_find_protocol_wrapper_type (uint token_ref);
/// Name of the Objective-C protocol.
- /// Returns the handle of the Objective-C protocol descriptor for the given protocol name.
- /// The protocol handle for the given protocol name.
- ///
- /// This is the equivalent of the objc_getProtocol function call.
- ///
+ /// Returns the handle of the Objective-C protocol descriptor for the given protocol name.
+ /// The protocol handle for the given protocol name.
+ ///
+ /// This is the equivalent of the objc_getProtocol function call.
+ ///
public static IntPtr GetProtocol (string protocol)
{
return Protocol.objc_getProtocol (protocol);
@@ -2186,13 +2175,13 @@ static bool SlowIsUserType (IntPtr cls)
}
/// Connect to the selector on this type.
- /// Method that will be called when Objective-C sends a message to the specified selector.
- /// Selector to connect to.
- /// This call allows the specified method in this method to respond to message invocations on the specified selector.
- ///
- /// The method must be declared on an NSObject-derived class.
- /// Developers can use this method to dynamically reconfigure which methods on a class should respond to which Objective-C selectors.
- ///
+ /// Method that will be called when Objective-C sends a message to the specified selector.
+ /// Selector to connect to.
+ /// This call allows the specified method in this method to respond to message invocations on the specified selector.
+ ///
+ /// The method must be declared on an NSObject-derived class.
+ /// Developers can use this method to dynamically reconfigure which methods on a class should respond to which Objective-C selectors.
+ ///
public static void ConnectMethod (Type type, MethodInfo method, Selector selector)
{
if (selector is null)
@@ -2202,13 +2191,13 @@ public static void ConnectMethod (Type type, MethodInfo method, Selector selecto
}
/// Connect to the selector on this type.
- /// Method that will be called when Objective-C sends a message to the specified selector.
- /// An export attribute that specifies the selector to connect to.
- /// This call allows the specified method in this method to respond to message invocations on the specified selector.
- ///
- /// The method must be declared on an NSObject-derived class.
- /// Developers can use this method to dynamically reconfigure which methods on a class should respond to which Objective-C selectors.
- ///
+ /// Method that will be called when Objective-C sends a message to the specified selector.
+ /// An export attribute that specifies the selector to connect to.
+ /// This call allows the specified method in this method to respond to message invocations on the specified selector.
+ ///
+ /// The method must be declared on an NSObject-derived class.
+ /// Developers can use this method to dynamically reconfigure which methods on a class should respond to which Objective-C selectors.
+ ///
[BindingImpl (BindingImplOptions.Optimizable)]
public static void ConnectMethod (Type type, MethodInfo method, ExportAttribute export)
{
@@ -2228,12 +2217,12 @@ public static void ConnectMethod (Type type, MethodInfo method, ExportAttribute
}
/// Method that will be called when Objective-C sends a message to the specified selector.
- /// Selector to connect to.
- /// This call allows the specified method in this method to respond to message invocations on the specified selector.
- ///
- /// The method must be declared on an NSObject-derived class.
- /// Developers can use this method to dynamically reconfigure which methods on a class should respond to which Objective-C selectors.
- ///
+ /// Selector to connect to.
+ /// This call allows the specified method in this method to respond to message invocations on the specified selector.
+ ///
+ /// The method must be declared on an NSObject-derived class.
+ /// Developers can use this method to dynamically reconfigure which methods on a class should respond to which Objective-C selectors.
+ ///
public static void ConnectMethod (MethodInfo method, Selector selector)
{
if (method is null)
@@ -2527,10 +2516,10 @@ static void GCCollect ()
}
/// The block to release.
- /// Calls _Block_release on the specified block on the main thread.
- ///
- /// Developers should not call this method, it's called by generated binding code.
- ///
+ /// Calls _Block_release on the specified block on the main thread.
+ ///
+ /// Developers should not call this method, it's called by generated binding code.
+ ///
[EditorBrowsable (EditorBrowsableState.Never)]
[DllImport ("__Internal", EntryPoint = "xamarin_release_block_on_main_thread")]
public static extern void ReleaseBlockOnMainThread (IntPtr block);
@@ -2579,8 +2568,8 @@ public string Description {
[DllImport (Constants.libSystemLibrary)]
static unsafe extern NXArchInfo* NXGetLocalArchInfo ();
- /// To be added.
- /// To be added.
+ /// Gets whether the current process uses ARM64 calling conventions.
+ /// This is initialized during runtime startup.
public static bool IsARM64CallingConvention;
[BindingImpl (BindingImplOptions.Optimizable)]
@@ -2620,7 +2609,7 @@ internal static string iOSSupportVersion {
using var dict = NSMutableDictionary.FromFile ("/System/Library/CoreServices/SystemVersion.plist");
using var str = (NSString) "iOSSupportVersion";
using var obj = dict.ObjectForKey (str);
- _iOSSupportVersion = obj.ToString ();
+ _iOSSupportVersion = obj?.ToString () ?? "";
}
return _iOSSupportVersion;
}
@@ -2669,9 +2658,9 @@ public static nuint ConvertManagedEnumValueToNative (ulong value)
return (nuint) value;
}
- /// To be added.
- /// To be added.
- /// To be added.
+ /// Gets the original working directory used when the application process was launched.
+ /// The original working directory, or if unavailable.
+ /// This value is provided by the native runtime and may differ from the current working directory.
public static string? OriginalWorkingDirectory {
get {
return Marshal.PtrToStringUTF8 (xamarin_get_original_working_directory_path ());