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

[cxx-interop] Support using Swift protocols from C++ #75291

Open
mrousavy opened this issue Jul 17, 2024 · 0 comments
Open

[cxx-interop] Support using Swift protocols from C++ #75291

mrousavy opened this issue Jul 17, 2024 · 0 comments
Labels
c++ interop Feature: Interoperability with C++ feature A feature request or implementation swift to c++ Feature → c++ interop: swift to c++

Comments

@mrousavy
Copy link

Motivation

I'm building a framework where people can build modules using Swift. Those modules are then exposed to JavaScript, using a C++ based interface.

For example, if a developer wants to implement an Image editing module, they can declare the module definition like this:

protocol Image {
  var width: Int { get }
  var height: Int { get }
}

..and I now want to expose that to C++, by wrapping it in a C++ class.

class ImageCxx {
public:
  ImageCxx(Image swiftPart);

  int getWidth() { return _swiftPart.getWidth(); }
  int getHeight() { return _swiftPart.getHeight(); }

private:
  Image _swiftPart;
}

Unfortunately Image is not available in C++, as it is a protocol. I don't know how users are going to implement Image or if there are different implementations of Image (e.g. one that wraps UIImage, one that wraps CGImage, ...), so I need to stay at interface-level (or "protocol"-level).

I am not a compiler engineer nor do I understand the memory layout of protocols, so I am not even sure if it is possible to expose Swift protocols to C++, so that's why I'm creating this enhancement idea as a place for discussion.

Proposed solution

It would be cool if Swift protocols would be exposed to C++.

I have no idea how to represent protocols, if it's templated classes, compiler macros, or abstract/virtual classes, or if it's not possible at all.

Alternatives considered

I thought about using inheritance, but inheritance casting also doesn't work on Swift classes through C++.

Additional information

No response

@mrousavy mrousavy added feature A feature request or implementation triage needed This issue needs more specific labels labels Jul 17, 2024
@mrousavy mrousavy changed the title C++ Interop: Support protocols [cxx-interop] Support using Swift protocols from C++ Jul 18, 2024
@hborla hborla added c++ interop Feature: Interoperability with C++ swift to c++ Feature → c++ interop: swift to c++ and removed triage needed This issue needs more specific labels labels Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++ feature A feature request or implementation swift to c++ Feature → c++ interop: swift to c++
Projects
None yet
Development

No branches or pull requests

2 participants