Open kotlarmilos opened 1 month ago
We will need a binding for UnsafeMutablePointer<T>
but we need to be careful that it follows the Swift conventions. An UMP can be in any of the following states:
This is important because T
could be a Swift type that is reference counted. Here is an example of code generated by BTfS which follows all of the states:
public func next() -> Optional<T0>
{
let vt: SwiftIteratorProtocol_xam_vtable = getSwiftIteratorProtocol_xam_vtable(T0.self)!;
// creates an UMP which is now allocated (but not initialized)
let retval = UnsafeMutablePointer<Swift.Optional<T0>>.allocate(capacity: 1);
// C# code gets called which will set the payload of the UMP which will initialize it, and in the process will bump any
// reference counts. You can't simply blit the type into the allocated space. It is best to use the types value witness table
// to copy it UNLESS the type is frozen and is blittable.
vt.func0!(retval, toIntPtr(value: self));
// Move uses the value witness table to pull the contents of the pointer out into a variable and in the process changes
// the state from initialized to deinitialized
let actualRetval = retval.move();
// deallocate should only be called on a pointer that in the allocated or deinitialized states.
retval.deallocate();
return actualRetval;
}
Therefore we should cleave as close as possible to the API of UMP to make it harder to use it incorrectly and we should document it usage thoroughly. Apple documents the states briefly here
@stephen-hawley I was going through the docs and I started wondering how would the ISwiftTyped
interface proposal handle getting metadata for primitive types e.g. ints? Will we have to create new types on C# side e.g. SwiftInt : ISwiftTyped
?
Overview
This issue tracks the the existing design proposals of Swift runtime features required for the interop.
Metadata
The existing implementation from the CryptoKit example:
Proposal based on measurements:
Consider creating a new type for the metadata - a struct containing either an
IntPtr
, aNativeHandle
, or avoid*
. There will be sufficient functions that will have metadata as arguments that we are likely to have signature conflicts.Protocol conformance descriptor
The existing implementation from the CryptoKit example: