Skip to content

feat: Create SwiftConverter#1157

Draft
mrousavy wants to merge 64 commits intomainfrom
feat/swift-converter
Draft

feat: Create SwiftConverter#1157
mrousavy wants to merge 64 commits intomainfrom
feat/swift-converter

Conversation

@mrousavy
Copy link
Copy Markdown
Owner

@mrousavy mrousavy commented Jan 15, 2026

Concept

The idea is simple; currently we pass C++ STL types directly to Swift (std::string -> std.string), and also convert them inside Swift - currently this looks like this;

Swift:

func mySwiftFunc(string: std.string) {
  // 1. Convert C++ STL type (here: std::string) to Swift type (here: String)
  let swiftString = String(string)
  // 2. Call the actual Swift implementation (which obviously accepts a Swift String)
  actualSwiftImplementation.mySwiftFunc(convertedToSwift)
}

C++:

void mySwiftFunc(const std::string& string) {
  // 1. Pass std::string directly to Swift
  swiftClass.mySwiftFunc(string);
}

To allow for extending external types, I had the idea to convert C++ STL types to Swift types while we're still in C++ land, so this will look like this:

Swift:

func mySwiftFunc(string: String) {
  // 1. Pass Swift String directly to our implementation
  actualSwiftImplementation.mySwiftFunc(string)
}

C++:

void mySwiftFunc(const std::string& string) {
  // 1. Convert C++ STL type (here: std::string) to Swift type (here: swift::String)
  auto swiftString = SwiftConverter<std::string>::toSwift(string);
  // 2. Call the Swift method which now takes swift::String directly
  swiftClass.mySwiftFunc(swiftString);
}

Implementation

As already hinted by the code above, this introduces a new template class: SwiftConverter<T>.

This SwiftConverter<T> allows to convert any C++ STL type to a Swift type and back using the Swift <> C++ interop.

This is quite tricky now, since the Swift types (swift::String, swift::Optional<T>, swift::Array<T>, ...) are generated per module (so each Pod/Framework has it's own definition of swift::String, even though they are identical).
I first tried to simply forward declare it, and only NitroModules module/Pod defines it inside it's .cpp file so that we don't need to define those templates multiple times - but this only works for fixed types like swift::String, and not for templated types like swift::Optional<T> or swift::Array<T>, since those have to be known at the time when I define the interface/hpp file.
So now it all lives inside the .hpp file.

Progress

Currently I have implemented SwiftConverter<T> for double, std::string, std::optional<T> and std::vector<T>.
I am now stuck at std::function<...> since that requires hybrid state- one implementation has to live in Swift, one in C++, and I cannot define it just once because Swift does not have variadic generics (or at least only after iOS 17, and C++ cannot interop with that yet).
I am not sure how to implement that properly tbh.

image

@vercel
Copy link
Copy Markdown

vercel Bot commented Jan 15, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Review Updated (UTC)
nitro-docs Skipped Skipped Jan 26, 2026 0:28am

Request Review

* feat: Support Swift functions in `SwiftConverter<T>`

* remoev

* fix cxx

* fix: Generate old code again to keep it working

* only generate (Double) -> Void for now.

* ok it builds

* ok that now works!

* holy shit it runs

* make it static
* feat: Implement `SwiftConverter<T>` for structs

* Add all imports

* use param for escaping

* generate a whole bunch of funcs

* fix: Fix weird func names

* no name info in converter

* don't do any result bridging for now

* remove a lot of c++ bridges

* move `call` to impl

* remove a lot more

* fix: Add back functions for Promise (resolver/rejecter)

* Rewrite `AnyMap` to use Swift implementation...?

* fix HybridObject downcast

* Ok create `SwiftAnyMap` wrapper again

* Import `SwiftAnyMap`

* Handle array

* Move Swift calls from .hpp into .cpp

* Swift part can be `std::shared_ptr` to avoid including it

* fix some
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.

1 participant