-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Review projections of Swift generics in C# for CryptoKit dev template #2580
Comments
Should this be internal?
These can be just
Do we need to guarantee that |
Good point. Yes, we need to preserve it since |
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: