Skip to content

Commit ad5b111

Browse files
committed
Adjust tests for new if in source gen
1 parent 49114a4 commit ad5b111

File tree

3 files changed

+88
-64
lines changed

3 files changed

+88
-64
lines changed

Sources/JExtractSwiftLib/CodePrinter.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ public struct CodePrinter {
7070
}
7171
}
7272

73+
public mutating func printHashIfBlock(
74+
_ header: Any,
75+
function: String = #function,
76+
file: String = #fileID,
77+
line: UInt = #line,
78+
body: (inout CodePrinter) throws -> ()
79+
) rethrows {
80+
print("#if \(header)")
81+
indent()
82+
try body(&self)
83+
outdent()
84+
print("#endif // end of \(header)", .sloc, function: function, file: file, line: line)
85+
}
86+
7387
public mutating func printBraceBlock(
7488
_ header: Any,
7589
function: String = #function,

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+NativeTranslation.swift

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ extension JNISwift2JavaGenerator {
839839
}
840840
}
841841

842-
func printTask(printer: inout CodePrinter) {
842+
func printTaskBody(printer: inout CodePrinter) {
843843
printer.printBraceBlock("defer") { printer in
844844
// Defer might on any thread, so we need to attach environment.
845845
printer.print("let deferEnvironment = try! JavaVirtualMachine.shared().environment()")
@@ -867,24 +867,22 @@ extension JNISwift2JavaGenerator {
867867
}
868868
}
869869

870-
printer.print("var taskInitialized = false") // FIXME: horrible hack so we can keep having CI for Linux with 6.1.2 until build issues with 6.2 are resovled
871-
printer.print("#if swift(>=6.2)")
872-
printer.printBraceBlock("if #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *)") { printer in
873-
printer.print("taskInitialized = true")
874-
printer.printBraceBlock("Task.immediate") { printer in
875-
// Immediate runs on the caller thread, so we don't need to attach the environment again.
876-
printer.print("var environment = environment!") // this is to ensure we always use the same environment name, even though we are rebinding it.
877-
printTask(printer: &printer)
870+
printer.print("var task: Task<Void, Never>? = nil")
871+
printer.printHashIfBlock("swift(>=6.2)") { printer in
872+
printer.printBraceBlock("if #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *)") { printer in
873+
printer.printBraceBlock("task = Task.immediate") { printer in
874+
// Immediate runs on the caller thread, so we don't need to attach the environment again.
875+
printer.print("var environment = environment!") // this is to ensure we always use the same environment name, even though we are rebinding it.
876+
printTaskBody(printer: &printer)
877+
}
878878
}
879879
}
880-
printer.print("#endif // canImport(Darwin)")
881880

882-
// printer.printBraceBlock("else") { printer in
883-
printer.printBraceBlock("if !taskInitialized") { printer in
884-
printer.printBraceBlock("Task") { printer in
881+
printer.printBraceBlock("if task == nil") { printer in
882+
printer.printBraceBlock("task = Task") { printer in
885883
// We can be on any thread, so we need to attach the thread.
886884
printer.print("var environment = try! JavaVirtualMachine.shared().environment()")
887-
printTask(printer: &printer)
885+
printTaskBody(printer: &printer)
888886
}
889887
}
890888

Tests/JExtractSwiftTests/JNI/JNIAsyncTests.swift

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,23 @@ struct JNIAsyncTests {
5959
@_cdecl("Java_com_example_swift_SwiftModule__00024asyncVoid__Ljava_util_concurrent_CompletableFuture_2")
6060
func Java_com_example_swift_SwiftModule__00024asyncVoid__Ljava_util_concurrent_CompletableFuture_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, result_future: jobject?) {
6161
let globalFuture = environment.interface.NewGlobalRef(environment, result_future)
62-
if #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *) {
63-
Task.immediate {
64-
var environment = environment!
65-
defer {
66-
let deferEnvironment = try! JavaVirtualMachine.shared().environment()
67-
environment.interface.DeleteGlobalRef(deferEnvironment, globalFuture)
62+
var task: Task<Void, Never>? = nil
63+
#if swift(>=6.2)
64+
if #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *) {
65+
task = Task.immediate {
66+
var environment = environment!
67+
defer {
68+
let deferEnvironment = try! JavaVirtualMachine.shared().environment()
69+
environment.interface.DeleteGlobalRef(deferEnvironment, globalFuture)
70+
}
71+
let swiftResult$ = await SwiftModule.asyncVoid()
72+
environment = try JavaVirtualMachine.shared().environment()
73+
environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: nil)])
6874
}
69-
let swiftResult$ = await SwiftModule.asyncVoid()
70-
environment = try JavaVirtualMachine.shared().environment()
71-
environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: nil)])
7275
}
73-
}
74-
else {
75-
Task {
76+
#endif
77+
if task == nil {
78+
task = Task {
7679
var environment = try! JavaVirtualMachine.shared().environment()
7780
defer {
7881
let deferEnvironment = try! JavaVirtualMachine.shared().environment()
@@ -130,27 +133,30 @@ struct JNIAsyncTests {
130133
@_cdecl("Java_com_example_swift_SwiftModule__00024async__Ljava_util_concurrent_CompletableFuture_2")
131134
func Java_com_example_swift_SwiftModule__00024async__Ljava_util_concurrent_CompletableFuture_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, result_future: jobject?) {
132135
let globalFuture = environment.interface.NewGlobalRef(environment, result_future)
133-
if #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *) {
134-
Task.immediate {
135-
var environment = environment!
136-
defer {
137-
let deferEnvironment = try! JavaVirtualMachine.shared().environment()
138-
environment.interface.DeleteGlobalRef(deferEnvironment, globalFuture)
139-
}
140-
do {
141-
let swiftResult$ = await try SwiftModule.async()
142-
environment = try JavaVirtualMachine.shared().environment()
143-
environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: nil)])
144-
}
145-
catch {
146-
let catchEnvironment = try! JavaVirtualMachine.shared().environment()
147-
let exception = catchEnvironment.interface.NewObjectA(catchEnvironment, _JNIMethodIDCache.Exception.class, _JNIMethodIDCache.Exception.constructWithMessage, [String(describing: error).getJValue(in: catchEnvironment)])
148-
catchEnvironment.interface.CallBooleanMethodA(catchEnvironment, globalFuture, _JNIMethodIDCache.CompletableFuture.completeExceptionally, [jvalue(l: exception)])
136+
var task: Task<Void, Never>? = nil
137+
#if swift(>=6.2)
138+
if #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *) {
139+
task = Task.immediate {
140+
var environment = environment!
141+
defer {
142+
let deferEnvironment = try! JavaVirtualMachine.shared().environment()
143+
environment.interface.DeleteGlobalRef(deferEnvironment, globalFuture)
144+
}
145+
do {
146+
let swiftResult$ = await try SwiftModule.async()
147+
environment = try JavaVirtualMachine.shared().environment()
148+
environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: nil)])
149+
}
150+
catch {
151+
let catchEnvironment = try! JavaVirtualMachine.shared().environment()
152+
let exception = catchEnvironment.interface.NewObjectA(catchEnvironment, _JNIMethodIDCache.Exception.class, _JNIMethodIDCache.Exception.constructWithMessage, [String(describing: error).getJValue(in: catchEnvironment)])
153+
catchEnvironment.interface.CallBooleanMethodA(catchEnvironment, globalFuture, _JNIMethodIDCache.CompletableFuture.completeExceptionally, [jvalue(l: exception)])
154+
}
149155
}
150156
}
151-
}
152-
else {
153-
Task {
157+
#endif
158+
if task == nil {
159+
task = Task {
154160
var environment = try! JavaVirtualMachine.shared().environment()
155161
defer {
156162
let deferEnvironment = try! JavaVirtualMachine.shared().environment()
@@ -215,8 +221,10 @@ struct JNIAsyncTests {
215221
@_cdecl("Java_com_example_swift_SwiftModule__00024async__JLjava_util_concurrent_CompletableFuture_2")
216222
func Java_com_example_swift_SwiftModule__00024async__JLjava_util_concurrent_CompletableFuture_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, i: jlong, result_future: jobject?) {
217223
let globalFuture = environment.interface.NewGlobalRef(environment, result_future)
224+
var task: Task<Void, Never>? = nil
225+
#if swift(>=6.2)
218226
if #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *) {
219-
Task.immediate {
227+
task = Task.immediate {
220228
var environment = environment!
221229
defer {
222230
let deferEnvironment = try! JavaVirtualMachine.shared().environment()
@@ -228,8 +236,9 @@ struct JNIAsyncTests {
228236
environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: boxedResult$)])
229237
}
230238
}
231-
else {
232-
Task {
239+
#endif // end of swift(>=6.2)
240+
if task == nil {
241+
task = Task {
233242
var environment = try! JavaVirtualMachine.shared().environment()
234243
defer {
235244
let deferEnvironment = try! JavaVirtualMachine.shared().environment()
@@ -303,24 +312,27 @@ struct JNIAsyncTests {
303312
fatalError("c memory address was null in call to \\(#function)!")
304313
}
305314
let globalFuture = environment.interface.NewGlobalRef(environment, result_future)
306-
if #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *) {
307-
Task.immediate {
308-
var environment = environment!
309-
defer {
310-
let deferEnvironment = try! JavaVirtualMachine.shared().environment()
311-
environment.interface.DeleteGlobalRef(deferEnvironment, globalFuture)
315+
var task: Task<Void, Never>? = nil
316+
#if swift(>=6.2)
317+
if #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *) {
318+
task = Task.immediate {
319+
var environment = environment!
320+
defer {
321+
let deferEnvironment = try! JavaVirtualMachine.shared().environment()
322+
environment.interface.DeleteGlobalRef(deferEnvironment, globalFuture)
323+
}
324+
let swiftResult$ = await SwiftModule.async(c: c$.pointee)
325+
environment = try JavaVirtualMachine.shared().environment()
326+
let result$ = UnsafeMutablePointer<MyClass>.allocate(capacity: 1)
327+
result$.initialize(to: swiftResult$)
328+
let resultBits$ = Int64(Int(bitPattern: result$))
329+
let boxedResult$ = SwiftJavaRuntimeSupport._JNIBoxedConversions.box(resultBits$.getJNIValue(in: environment), in: environment)
330+
environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: boxedResult$)])
312331
}
313-
let swiftResult$ = await SwiftModule.async(c: c$.pointee)
314-
environment = try JavaVirtualMachine.shared().environment()
315-
let result$ = UnsafeMutablePointer<MyClass>.allocate(capacity: 1)
316-
result$.initialize(to: swiftResult$)
317-
let resultBits$ = Int64(Int(bitPattern: result$))
318-
let boxedResult$ = SwiftJavaRuntimeSupport._JNIBoxedConversions.box(resultBits$.getJNIValue(in: environment), in: environment)
319-
environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: boxedResult$)])
320332
}
321-
}
322-
else {
323-
Task {
333+
#endif
334+
if task == nil {
335+
task = Task {
324336
var environment = try! JavaVirtualMachine.shared().environment()
325337
defer {
326338
let deferEnvironment = try! JavaVirtualMachine.shared().environment()

0 commit comments

Comments
 (0)