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
This proposal discusses the projection of Swift generics in C#. The primary goal is to validate the design of projections and integrate CryptoKit APIs for direct invocation from the dotnet/runtime repository. To limit the scope, this discussion specifically focuses on generics use-cases for the CryptoKit dev template. Protocols are considered as constraints within the Swift runtime, which should not affect functionality and they are not covered in this proposal.
Generics in Swift functions
In Swift, when a generic type is used as a parameter, its memory layout might not be known until runtime. To manage this, the generic parameter is represented by an opaque pointer. For object allocation, the runtime utilizes init functions from the value witness table using a metadata pointer. This metadata pointer is included as an implicit argument at the end of the function's signature, enabling the runtime to correctly allocate and manage generic types. When returning a generic type, the runtime utilizes indirect return result mechanism.
Here is a code snippet that illustrates how generics are handled in functions with the corresponding assembly code for arm64:
func returnData<T>(data:T)->T{return data
}
As the function returns a generic type, the register x8 is expected to contain the return buffer where the value should be stored. The first parameter x0 is an opaque pointer; the second parameter x1 is the metadata pointer of the type.
Next, x8 loads a value witness table pointer from -1 offset of the metadata pointer, and then loads the init function initializeBufferWithCopyOfBuffer. This function is invoked with the x0 return buffer , x1 opaque pointer, and x2 metadata pointer.
Do we need to guarantee that message is not collected by the GC after we extract messageMetadata from it? (e.g. via GC.KeepAlive after the end of the call)
Do we need to guarantee that message is not collected by the GC after we extract messageMetadata from it? (e.g. via GC.KeepAlive after the end of the call)
Good point. Yes, we need to preserve it since messagePtr is unmanaged reference.
Description
This proposal discusses the projection of Swift generics in C#. The primary goal is to validate the design of projections and integrate CryptoKit APIs for direct invocation from the dotnet/runtime repository. To limit the scope, this discussion specifically focuses on generics use-cases for the CryptoKit dev template. Protocols are considered as constraints within the Swift runtime, which should not affect functionality and they are not covered in this proposal.
Generics in Swift functions
In Swift, when a generic type is used as a parameter, its memory layout might not be known until runtime. To manage this, the generic parameter is represented by an opaque pointer. For object allocation, the runtime utilizes init functions from the value witness table using a metadata pointer. This metadata pointer is included as an implicit argument at the end of the function's signature, enabling the runtime to correctly allocate and manage generic types. When returning a generic type, the runtime utilizes indirect return result mechanism.
Here is a code snippet that illustrates how generics are handled in functions with the corresponding assembly code for arm64:
As the function returns a generic type, the register
x8
is expected to contain the return buffer where the value should be stored. The first parameterx0
is an opaque pointer; the second parameterx1
is the metadata pointer of the type.This block shuffles regs for an object allocation. At the end, the
x0
is the return buffer,x1
is the opaque pointer, andx2
is the metadata pointer.Next,
x8
loads a value witness table pointer from -1 offset of the metadata pointer, and then loads the init function initializeBufferWithCopyOfBuffer. This function is invoked with thex0
return buffer ,x1
opaque pointer, andx2
metadata pointer.CryptoKit bindings for encryption and decryption
To directly call into CryptoKit APIs, the tooling should surface methods for encryption and decryption with
Foundation.Data
type:Following the generics implementation described earlier, below are proposed CryptoKit APIs in C#:
/cc: @jkoritzinsky @AaronRobinsonMSFT @stephen-hawley @rolfbjarne @jkotas
The text was updated successfully, but these errors were encountered: