Skip to content

jextract/jni: Support escaping closures through @JavaInterface callbacks#813

Merged
ktoso merged 1 commit into
swiftlang:mainfrom
ktoso:wip-escaping-closures-via-synthetic-protocols
Jul 3, 2026
Merged

jextract/jni: Support escaping closures through @JavaInterface callbacks#813
ktoso merged 1 commit into
swiftlang:mainfrom
ktoso:wip-escaping-closures-via-synthetic-protocols

Conversation

@ktoso

@ktoso ktoso commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

This is safe because we keep the java side object alive by capturing the JavaObjectHolder to it un the swift closure; so as long as Swift retains that holder, the Java object remains alive as well -- we cannot call on a freed java object.

Reuses the existing JavaInterfaceProtocolWrapperGenerator without going through the whole wrap-java route, since we know both sides and what to generate for it. This means that this works even with the sandbox enabled.

The java side just get a functional interface they can invoke, and rather than upcalling into it directly, we use the java interface Swift wrapper to make the upcall.

resolves #328

cc @madsodgaard

This is safe because we keep the java side object alive by capturing the
JavaObjectHolder to it un the swift closure; so as long as Swift retains
that holder, the Java object remains alive as well -- we cannot call on
a freed java object.

Reuses the existing JavaInterfaceProtocolWrapperGenerator without going
through the whole wrap-java route, since we know both sides and what to
generate for it. This means that this works even with the sandbox
enabled.

The java side just get a functional interface they can invoke, and
rather than upcalling into it directly, we use the java interface Swift
wrapper to make the upcall.

resolves swiftlang#328
@ktoso ktoso changed the title jextract/jni: Support escaping closures through WrapJava callbacks jextract/jni: Support escaping closures through @JavaInterface callbacks Jul 2, 2026
@ktoso ktoso marked this pull request as draft July 2, 2026 10:10
@ktoso

ktoso commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator Author

Meh, still some things I'm not happy with that I missed, marking as draft

@ktoso ktoso marked this pull request as ready for review July 3, 2026 03:53
@ktoso ktoso merged commit dda9e32 into swiftlang:main Jul 3, 2026
42 checks passed
@ktoso ktoso deleted the wip-escaping-closures-via-synthetic-protocols branch July 3, 2026 07:02
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.

Add support for escaping Swift closures

1 participant