Skip to content

jextract: Move Foundation support into a common package#684

Open
sidepelican wants to merge 13 commits intoswiftlang:mainfrom
sidepelican:swiftkit_foundation
Open

jextract: Move Foundation support into a common package#684
sidepelican wants to merge 13 commits intoswiftlang:mainfrom
sidepelican:swiftkit_foundation

Conversation

@sidepelican
Copy link
Copy Markdown
Contributor

Instead of generating Date, Data, and DataProtocol implementations within each user module, I will bundle these as part of SwiftJava's standard library.

Fixed implementations are now located under org.swift.swiftkit.core.foundation and org.swift.swiftkit.ffm.foundation.
Removed printSpecificTypeHelpers from the code generation process.

This ensures that when multiple modules utilize swift-java, they all share a single, unified set of Foundation classes.

@sidepelican sidepelican requested a review from ktoso as a code owner April 8, 2026 01:49
import java.util.concurrent.atomic.AtomicBoolean;
import org.swift.swiftkit.core.annotations.*;

public final class Data implements JNISwiftInstance, DataProtocol {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, let's add some docs onto it that this is Foundation.Data and link to https://developer.apple.com/documentation/foundation/data

Copy link
Copy Markdown
Collaborator

@ktoso ktoso Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm on the other hand, shouldn't this be generated? We can use the jextract --single-type mode perhaps and point it at https://github.com/swiftlang/swift-foundation/blob/main/Sources/FoundationEssentials/Data/Data.swift

We should have some script in scripts/ (there's a few, maybe one of them) that does that and generates this file perhaps? We can use the filterInclude to pick which exact pieces of Data we want etc

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah damn we can't because we're adding helper methods to it...

We'd need an extra feature to "add extra methods to imported type"... that would be great though 🤔

import java.lang.invoke.*;
import java.util.*;

public final class Data extends FFMSwiftInstance implements SwiftValue {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as on the other type; can we find a way to generate those using a script and not just by hand paste the generated code?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually tried that approach at first, but I ran into quite a few minor issues.
It needs more custom logics into the generater.
Since Date and Data had their own dedicated code generation logic (printSpecificTypeHelpers), I thought it would be easier to manage the source code manually rather than dealing with the maintenance overhead of the generator.

@sidepelican
Copy link
Copy Markdown
Contributor Author

I'll look into code generation a bit more

@ktoso
Copy link
Copy Markdown
Collaborator

ktoso commented Apr 8, 2026

Thank you!

I think we maybe we need some extra setting that is "sourceGenAdditionalMembers" { "MyType": "Sources/Something/Data+Extras.java" } } or something...? And we'd just "plop" those sources into the generated java type maybe? (silly names, we can think of better ones)

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.

2 participants