Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1958,15 +1958,20 @@ extension JNISwift2JavaGenerator {
"""
)

var selfCaptures: [(sendable: String, original: String)] = []
Comment thread
Flatout73 marked this conversation as resolved.
Outdated
if let selfParameter = nativeFunctionSignature.selfParameter {
for parameter in selfParameter.parameters {
printer.print("nonisolated(unsafe) let \(parameter.name)Sendable$ = \(parameter.name)$")
if case .extractSwiftProtocolValue = selfParameter.conversion {
for parameter in selfParameter.parameters {
selfCaptures.append(("\(parameter.name)ExistentialSendable$", "\(parameter.name)Existential$"))
Comment thread
Flatout73 marked this conversation as resolved.
Outdated
}
} else {
for parameter in selfParameter.parameters {
selfCaptures.append(("\(parameter.name)Sendable$", "\(parameter.name)$"))
Comment thread
Flatout73 marked this conversation as resolved.
Outdated
}
}
}
if let selfTypeParameter = nativeFunctionSignature.selfTypeParameter {
for parameter in selfTypeParameter.parameters {
printer.print("nonisolated(unsafe) let \(parameter.name)Sendable$ = \(parameter.name)$")
}
for capture in selfCaptures {
printer.print("nonisolated(unsafe) let \(capture.sendable) = \(capture.original)")
}

func printDo(printer: inout SwiftPrinter) {
Expand Down Expand Up @@ -2011,15 +2016,8 @@ extension JNISwift2JavaGenerator {
}

func printTaskBody(printer: inout SwiftPrinter) {
if let selfParameter = nativeFunctionSignature.selfParameter {
for parameter in selfParameter.parameters {
printer.print("let \(parameter.name)$ = \(parameter.name)Sendable$")
}
}
if let selfTypeParameter = nativeFunctionSignature.selfTypeParameter {
for parameter in selfTypeParameter.parameters {
printer.print("let \(parameter.name)$ = \(parameter.name)Sendable$")
}
for capture in selfCaptures {
printer.print("let \(capture.original) = \(capture.sendable)")
}
printer.printBraceBlock("defer") { printer in
// Defer might on any thread, so we need to attach environment.
Expand Down
109 changes: 109 additions & 0 deletions Tests/JExtractSwiftTests/JNI/JNIClassAsyncSelfCaptureTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import JExtractSwiftLib
import SwiftJavaConfigurationShared
import Testing

@Suite
struct JNIAsyncSelfCaptureTests {

@Test("Import: class async method captures converted self pointer (Swift)")
func classAsyncMethod_swift() throws {
try assertOutput(
input: """
public class MyClass {
public func compute() async -> Int64 { 42 }
}
""",
.jni,
.swift,
detectChunkByInitialLines: 1,
expectedChunks: [
"""
@_cdecl("Java_com_example_swift_MyClass__00024compute__JLjava_util_concurrent_CompletableFuture_2")
...
nonisolated(unsafe) let selfPointerSendable$ = selfPointer$
...
task = Task.immediate {
...
let selfPointer$ = selfPointerSendable$
...
let swiftResult$ = await selfPointer$.pointee.compute()
"""
]
)
}

@Test("Import: protocol box async method captures loaded existential (Swift)")
func protocolBoxAsyncMethod_swift() throws {
try assertOutput(
input: """
public protocol Worker {
func work() async -> Int64
}
""",
.jni,
.swift,
detectChunkByInitialLines: 1,
expectedChunks: [
"""
@_cdecl("Java_com_example_swift_WorkerBox__00024work__JJLjava_util_concurrent_CompletableFuture_2")
...
nonisolated(unsafe) let selfPointerExistentialSendable$ = selfPointerExistential$

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.

Heh, re-reading these now I guess we should be naming them selfPointerExistentialUnsafeSendable$ tbh; It's just generated code but it'll be better to get the point across also for us reading the code that these are not actually safe "Sendable" -- I know this term was here before, would you mind changing those names to UnsafeSendable though please?

...
task = Task.immediate {
...
let selfPointerExistential$ = selfPointerExistentialSendable$
...
let swiftResult$ = await selfPointerExistential$.work()
"""
],
notExpectedChunks: [
"nonisolated(unsafe) let selfPointerSendable$",
"nonisolated(unsafe) let selfTypePointerSendable$",
]
)
}

@Test("Import: generic class async method captures only self pointer (Swift)")
func genericClassAsyncMethod_swift() throws {
try assertOutput(
input: """
public class Box<T> {
public func compute() async -> Int64 { 42 }
}
""",
.jni,
.swift,
detectChunkByInitialLines: 1,
expectedChunks: [
"""
extension Box: _SwiftModule_Box_opener {
...
nonisolated(unsafe) let selfPointerSendable$ = selfPointer$
...
task = Task.immediate {
...
let selfPointer$ = selfPointerSendable$
...
let swiftResult$ = await selfPointer$.pointee.compute()
"""
],
notExpectedChunks: [
"selfTypePointerSendable$",
]
)
}
}
Loading