Skip to content

Conversation

@madsodgaard
Copy link
Contributor

If you had the following swift code:

protocol MyProtocol {
  func makeClass() -> MySwiftClass
}

class MySwiftClass: MyProtocol {
  // ...
}

we would previously generate the interface as

interface MyProtocol {
  MySwiftClass makeClass();
}

however, the class would be generated as:

final class MySwiftClass implements MyProtocol {
  MySwiftClass makeClass(SwiftArena swiftArena$) {
  }
}

that means that MySwiftClass does not implement MyProtocol, because we don't generate the arena parameter for interfaces.

The reason we do not do this, is because of #449 - where when Swift wants to call a Java interface, we don't pass a specific arena, but rather just use a global arena when we need it.

The change

Instead, we now generate interfaces with their full method signature, including arenas. And then we generate default methods that use the global arena, if we have enabled the java callbacks feature.

With this change, this also means you can make sure that the underlying classes actually use the arena you pass to the interface function:

SwiftArena arena = SwiftArena.ofAuto();
MyProtocol myProtocol = MySwiftClass.init(arena);
myProtocol.makeClass(arena); // the underlying impl can actually use the arena passed in.

@ktoso ktoso merged commit c6a56cd into swiftlang:main Dec 3, 2025
48 checks passed
ktoso added a commit to ktoso/swift-java that referenced this pull request Dec 4, 2025
Co-authored-by: pelekon <13712101+pelekon@users.noreply.github.com>
Co-authored-by: Konrad `ktoso` Malawski <konrad_malawski@apple.com>
Co-authored-by: Bartłomiej Bukowiecki <pelekon@users.noreply.github.com>
Fix `allowGlobalAutomatic` mode for interfaces (swiftlang#466)
Fix JNI caching for native threads (swiftlang#464)
fix extensions (swiftlang#473)
fix protocols that return java classes. (swiftlang#479)
ktoso added a commit that referenced this pull request Dec 4, 2025
* config: log-level should encode as string

* Properly handle nested directories of Swift Sources in jextract

* wip

* fix test and cleanup (#1)

Co-authored-by: pelekon <13712101+pelekon@users.noreply.github.com>
Co-authored-by: Konrad `ktoso` Malawski <konrad_malawski@apple.com>
Co-authored-by: Bartłomiej Bukowiecki <pelekon@users.noreply.github.com>
Fix `allowGlobalAutomatic` mode for interfaces (#466)
Fix JNI caching for native threads (#464)
fix extensions (#473)
fix protocols that return java classes. (#479)

---------

Co-authored-by: Mads Odgaard <mads@madsodgaard.com>
Co-authored-by: pelekon <13712101+pelekon@users.noreply.github.com>
Co-authored-by: Bartłomiej Bukowiecki <pelekon@users.noreply.github.com>
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