Repository navigation
JNI: preserve MainActor isolation in synchronous thunks - #1
Merged
Merged
Conversation
Synchronous MainActor APIs currently produce nonisolated JNI entry points, so their generated bindings fail to compile when calling isolated Swift initializers or members. Annotate these entry points with MainActor and check the caller's executor before converting arguments or accessing Swift state. Keep the Java API synchronous, preserve isolation through generic openers, and leave nonisolated members and asynchronous downcalls on their existing paths. Isolating the thunk itself also supports reference-valued JNI results in Swift 6 without requiring JNI pointers to conform to Sendable. Add regression tests, JNI sample compilation fixtures, and documentation of the executor requirement for Java callers. Verify the regressions against the original generator, run the full Swift and JNI sample test suites, and compile the fixtures in Swift 6 language mode.
Collaborator
Author
|
submitted to upstream |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
JNI extraction preserves synchronous Java signatures for
@MainActorAPIs, but emits nonisolated Swift thunks. As a result, the generated bindings fail to compile when they call isolated initializers or methods, or access isolated properties and subscripts.For example, extracting this declaration generates a nonisolated thunk that directly calls
Counter.init, producing an actor-isolation diagnostic:Changes
@MainActorwhen isolation is explicitly specified on the declaration, its enclosing nominal type, or its extension. Recognize both@MainActorand@_Concurrency.MainActor.MainActor.preconditionIsolated()before the thunk converts arguments or accesses Swift state. Java callers must already execute on the main actor's executor; the thunk remains synchronous and does not schedule an executor hop.nonisolatedmembers and existing asynchronous downcall paths unchanged.Annotating the thunk itself allows reference-valued results such as
jstring?to remain in the synchronous call without requiring JNI pointers to conform toSendable.Validation
Tested on macOS 27.0.1 with Apple Swift 6.4 and JetBrains Runtime 25.0.2.
301e3da710ede7af93e798f1ce1c7950715a5748and pass with this change. Coverage includes initializers, instance/static methods, properties, subscripts, extensions, qualified attributes, generic openers, throwing calls, reference results,nonisolatedmembers, and async signatures.swift test --disable-experimental-prebuilts --disable-sandbox../gradlew :Samples:SwiftJavaExtractJNISampleApp:test --no-daemon.MainActorCounterandMainActorBoxin Swift 6 language mode, including string results and generic opener dispatch. The existing JNI sample also compiles these fixtures in Swift 5 language mode.swift-format lint --stricton all changed Swift files andgit diff --check.