Skip to content

Conversation

@li-feng-sc
Copy link
Contributor

@li-feng-sc li-feng-sc commented Jul 12, 2024

This adds first class Swift support without going through ObjC bridge.

New command line switches

  • --swift-out
    The Swift code output directory.
  • --swift-module
    The Swift module name. This is used to generate “import” statements that references this module.
  • --swiftxx-out
    The C++ bridge code output directory.
  • swiftxx-include-cpp-prefix
    The prefix for #includes of the main header files from Swift C++ files
  • swiftxx-base-lib-include-prefix
    Swift C++ interop support library relative path
  • --ident-swiftxx-class
    The naming convention of generated C++ bridge classes. For example, “SwiftFooBar”.
  • --ident-swiftxx-file
    The naming convention of generated C++ files. For example, “SwiftFooBar”.

The generated code

The generated code in the output directories has 3 parts:

  • Swift API
    These are “*.swift” files without “+Private” suffix under the output directory specified by “--swift-out”. They contain Swift definitions for Djinni enums, records, and interfaces.
  • Swift bridge
    These are “*+Private.swift” files in the same directory as Swift API module files specified by “--swift-out”. They contain:
    • The code responsible for converting Swift objects to/from an intermediate form
    • Proxy classes that implements the protocols defined in the API module
  • C++ bridge
    These are “.h” and “.cpp” files under the output directory specified by “--swiftxx-out”. They contain:
    • The code responsible for converting the intermediate form to/from C++ objects.
    • C++ helper functions used by the Swift proxy classes that forward Swift method calls to C++ implementations.

Notable improvements over ObjC bridge

  • Djinni records are Swift structs instead of classes. This means most data can be allocated on the stack instead of heap, and without the overhead of reference counting.
  • Djinni interfaces are always Swift protocols. This gives user code more flexibility in how they would implement the interfaces with classes.
  • There is no boxing needed for composite types. Elements get to retain their precise type signature and a lot less objects need to be created on the heap when passing containers across the boundary.
  • Correct nullability attributes are retained in nested types. “list<optional>” now intuitively maps to “[T?]”.
  • Maps “future<>” to a Swift type that supports the Swift "await" syntax.
  • Pure Swift implementation simplifies integration with Swift code.

@paulocoutinhox
Copy link
Contributor

o.o
it has support for Swift direct call C++?

li-feng-sc and others added 3 commits July 12, 2024 14:11
When non-nullable pointers are set, externally defined
interface types were not being treated the same as file-local
interface types, causing missing import headers and incorrect
pointer types to be used in C++ generated code.
@paulocoutinhox
Copy link
Contributor

What Swift version is required?

@li-feng-sc
Copy link
Contributor Author

What Swift version is required?

5.9+ i think as it uses the c++ interop

@paulocoutinhox
Copy link
Contributor

What Swift version is required?

5.9+ i think as it uses the c++ interop

I check here, you are correct, thanks.

@li-feng-sc li-feng-sc merged commit 32b6985 into experimental-record May 5, 2025
1 check passed
@li-feng-sc li-feng-sc deleted the lf/swift-support branch May 5, 2025 23:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants