Skip to content
This repository was archived by the owner on Nov 10, 2026. It is now read-only.

Bridge closures with closure parameters in the remaining directions - #305

Merged
piercifani merged 1 commit into
mainfrom
fix/bridge-nested-closure-reverse
Oct 22, 2026
Merged

piercifani merged 1 commit into
mainfrom
fix/bridge-nested-closure-reverse

Conversation

@piercifani

Copy link
Copy Markdown
Contributor

#249 made it possible for Swift to call a Kotlin closure that takes closure parameters, through the @_cdecl path (skiptools/skip-bridge#109). Three other cases still sent the nested closure arguments through AnyBridging, which cannot convert between Swift closures and Kotlin functions:

Bridged type Closure provided by Called from Generated by Previous result
Swift type bridged to Kotlin Swift Kotlin, with a Kotlin lambda convertToCDecl Unable to bridge Kotlin/Java instance
Kotlin type bridged to Swift Swift (assigned to the property) Kotlin, with a Kotlin lambda convertToJava same
Kotlin type bridged to Swift Kotlin Swift, with a Swift closure convertClosureFromJava Unable to bridge Swift instance (Function)

For example, Kotlin calling public var onDeposit: @MainActor (@escaping @MainActor () -> Void) -> Void on a bridged Swift class crashed. This is the reverse of skiptools/skip-bridge#101.

Change

  • A new convertClosureToJava wraps a Swift closure whose parameters include closures. Those parameters arrive as JavaObjectPointer? and are converted with convertClosureFromCDecl using their static types. It is used by both convertToCDecl and convertToJava:
    SwiftClosure2.javaObject(forMainActor: { @MainActor [_closure = c] (p0: Int, p1: JavaObjectPointer?) in _closure(p0, SwiftClosure0.closure(forJavaObject: p1, options: [])! as @MainActor () -> Void) }, options: [])!
    The capture list keeps the closure value from the time it is bridged, as before. Optional closures go through .map. throws and @MainActor are preserved.
  • convertClosureFromJava converts closure arguments with convertClosureToJava before calling JavaBackedClosure.invoke.
  • The Bridge closure properties whose parameters are closures #249 @_cdecl conversion has moved into convertClosureFromCDecl, and it now also uses convertClosureToJava for its nested arguments, so deeper nesting goes through the same code. The two existing nested-closure tests therefore now expect ! after the inner javaObject(...) call; it cannot fail for a non-optional closure.

As with #249, async closures and closures with more than 5 parameters are still not handled.

Testing

  • New codegen tests: BridgeToKotlinTests.testNestedClosureVar, testOptionalNestedClosureVar and BridgeToSwiftTests.testNestedClosureVar. All 253 BridgeTo* tests pass.
  • skip-bridge runtime tests that cover all three directions are in a companion skip-bridge PR, which needs a skipstone release containing this change. Against a local build of this branch, its Kotlin-compat (9/9) and BridgeToSwift (71/71) Robolectric suites pass. Against skipstone 1.9.16, the new Kotlin-to-Swift test crashes with Unable to bridge Kotlin/Java instance.

🤖 Generated with Claude Code

#249 handled a Kotlin closure with closure parameters that is called from
Swift through the @_cdecl path. Three cases still sent the nested closure
arguments through AnyBridging, which cannot convert between Swift closures
and Kotlin functions:

- a Swift closure exposed to Kotlin (convertToCDecl) and called from Kotlin
  with a Kotlin lambda
- a Swift closure assigned to a Kotlin-native property (convertToJava) and
  called from Kotlin with a Kotlin lambda
- a Kotlin-native closure (convertClosureFromJava) called from Swift with a
  Swift closure

Swift closures are now wrapped so that their function-typed parameters
arrive as Java objects and are converted using their static types. The
wrapper captures the closure value when it is bridged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@piercifani
piercifani merged commit 343eac0 into main Oct 22, 2026
16 of 17 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants