Skip to content
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

[Discussion] Generate C# bindings for Foundation framework #2584

Closed
1 task
kotlarmilos opened this issue May 10, 2024 · 1 comment
Closed
1 task

[Discussion] Generate C# bindings for Foundation framework #2584

kotlarmilos opened this issue May 10, 2024 · 1 comment
Assignees
Labels
area-SwiftBindings Swift bindings for .NET

Comments

@kotlarmilos
Copy link
Member

kotlarmilos commented May 10, 2024

Objective

Our goal is to generate Foundation APIs bindings that can be consumed by other frameworks like CryptoKit and SwiftUI. This issue analyzes the implementation of the Foundation framework which should help us identify requirements for the projection tooling and runtime.

Numbers, data, and basic values

Primitive Swift types along with unsafe pointers are projected into C# types as outlined in this table.

The Foundation.Data is a frozen struct utilized within the CryptoKit framework and serves as a byte buffer in memory. This struct should be mapped to a C# struct that contains a sequence of 16-byte primitives. The representation depends on the length of the byte buffer. For buffers with a length of up to 15 bytes, the data is stored directly within the struct, with the last byte allocated for its length. If the buffer exceeds the struct’s capacity but is still small enough to fit a storage pointer + range in two words, it is represented by a slice: Range<HalfInt> and a storage: __DataStorage. If the buffer range is too large to fit in a single word, t is represented by a slice: RangeReference and storage: __DataStorage.

The Swift implementation of data representation is as follows:

internal enum _Representation : Sendable {
        case empty
        case inline(InlineData)
        case slice(InlineSlice)
        case large(LargeSlice)
}

In C#, enums can't hold values in addition to cases, and thus are projected as structs. Type Foundation.Data is heavily used across the frameworks, and can be projected as a blob of 16 bytes. This can be implemented as C# struct of fixed array of bytes:

[StructLayout(LayoutKind.Sequential, Size = 16)]
public unsafe struct Data {
    public fixed byte bytes[16];
}

Strings and text

TBD

Collections: Array, dictionary, and set

TBD

Tasks

List of tasks to support identified bindings:

@kotlarmilos kotlarmilos added the area-SwiftBindings Swift bindings for .NET label May 10, 2024
@kotlarmilos kotlarmilos self-assigned this May 10, 2024
@kotlarmilos kotlarmilos changed the title Generate C# bindings for Foundation framework [Discussion] Generate C# bindings for Foundation framework May 13, 2024
@kotlarmilos
Copy link
Member Author

Experimental Swift bindings for Foundation required for the CryptoKit: #2704.

We will continue this effort in .NET 10 but with focus on APIs required for the use-cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-SwiftBindings Swift bindings for .NET
Projects
None yet
Development

No branches or pull requests

1 participant