You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Research improving finding the constructor to call to create a managed NSObject instance in MSR #18358
partialclassMyObject{class__RegistrarHelper_{[UnmanagedCallersOnly("__MyObject_DoSomething__")]staticvoidDoSomething(IntPtrhandle,IntPtrselector,IntPtrarg1){// Runtime.GetNSObject:// https://github.com/xamarin/xamarin-macios/blob/3dd412daa6b5522029014b0be298f27d751c5f45/src/ObjCRuntime/Runtime.cs#L1516-L1519// basically something like this:// if we already have a managed instance for the handle 'handle', return that// otherwise find the constructor that takes an IntPtr (using reflection), and call that constructorvararg1Obj=Runtime.GetNSObject();MyObject.DoSomething(arg1Obj);}}}
This is in some ways very similar to Filip's INativeObject/Selector case from above, except a bit more complex, because Runtime.GetNSObject will look up the actual runtime type of the Objective-C handle, find the closest matching C# type, and create an instance of that type. This might very well be a subclass of the type the caller expects (and this is why we can't just do a new NSString (handle) in the generated code).
Potential solution: we already generate a table to map Objective-C class -> managed type (which we use to find the closest matching C# type for a given Objective-C classr), we could add the token for the IntPtr ctor to table, so that when we find a managed type, we'll find the ctor to call too (or maybe even generate code to call the ctor directly, because calling a method given a token in C# is rather obnoxious)
I experimented with a C#-only solution that could potentially work with roslyn source generators in the future. I came up with a PoC which isn't very different from the current solution that uses reflection but uses the recently introduced zero-overhead unsafe accessors and a "trick" to make it impossible for NativeAOT to generate extra metadata:
partialclassCustomClass:NSObject{privateint_x;[Export("initWithX:")]publicCustomClass(intx)=>_x=x;}// generated:partialclassCustomClass{staticclass__Registrar_Callbacks__{[UnmanagedCallersOnly(EntryPoint="_callback_CustomClass__ctor")]privatestaticIntPtrcallback_CustomClass__ctor(IntPtrhandle,intx){varobj=NSObject.CreateNSObject<CustomClass>(handle);ctor(obj,x);returnRuntime.AllocGCHandle(obj);[UnsafeAccessor(UnsafeAccessorKind.Method,Name=".ctor")]externstaticvoidctor(CustomClass@this,intx);}}}// xamarin runtime:classNSObject{[UnconditionalSuppressMessage("ReflectionAnalysis","IL2087:UnrecognizedReflectionPattern",MessageId="We make sure there are hard references to the type's constructor(s) through the UnsafeAccessor "+"and ILC will find it and include it.")]publicstaticTCreateNSObject<T>(IntPtrhandle)whereT:NSObject{varobj=(T)RuntimeHelpers.GetUninitializedObject(typeof(T));// ILC won't generate extra metadata since we don't use typeof(CustomClass) directlyrefIntPtr_handle=refGetHandle(obj);_handle=handle;returnobj;}[UnsafeAccessor(UnsafeAccessorKind.Field,Name="_handle")]externstaticrefIntPtrGetHandle(NSObject@this);}
Activity
simonrozsival commentedon Aug 10, 2023
I experimented with a C#-only solution that could potentially work with roslyn source generators in the future. I came up with a PoC which isn't very different from the current solution that uses reflection but uses the recently introduced zero-overhead unsafe accessors and a "trick" to make it impossible for NativeAOT to generate extra metadata:
Source of a working PoC is here: https://gist.github.com/simonrozsival/4c7d16a2b277145fe7ea0a73ea61357d
Ref #17693
[registrar] Create instances of NSObjects and INativeObjects without …