swiftlang / swift-java

Apache License 2.0
636 stars 16 forks source link

jextract-swift support for Swift structs #26

Open DougGregor opened 4 days ago

DougGregor commented 4 days ago

At present, jextract-swift only exposes Swift classes to Java. We should also be able to expose Swift value types like structs and enums. We'll need to create a memory layout for the type on the Java side (which requires size/alignment) and will wrap the value type in a Java class. Destruction will require calls into the value witness table. If we want to implement the java.lang.Cloneable interface, we'll need to do so by calling the appropriate value witness to copy.

Anything we do here needs to start with getting access to the type metadata (we can use the mangled name of the witness table or its accessor for this), and the witness table from there.

ktoso commented 4 days ago

I suggest we split this up into struct separately from enum, as the enum case will be quite different source generation. Renaming this one to be the struct case specifically 👍

As a hint for the obtaining of type metadata, we have prepared already the:

    public static final String TYPE_METADATA_NAME = "....";
    static final MemorySegment TYPE_METADATA = SwiftKit.getTypeByMangledNameInEnvironment(TYPE_METADATA_NAME);

in the class generation, and that type metadata can be passed to Swift methods which need it 👍