Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -1227,12 +1227,14 @@ extension JNISwift2JavaGenerator {
} else {
"await"
}
printer.print("let swiftResult$ = \(tryAwaitString) \(placeholderWithoutTry)")
printer.print("environment = try! JavaVirtualMachine.shared().environment()")
let inner = nativeFunctionSignature.result.conversion.render(&printer, "swiftResult$")
if swiftFunctionResultType.isVoid {
printer.print("\(tryAwaitString) \(placeholderWithoutTry)")
printer.print("environment = try! JavaVirtualMachine.shared().environment()")
printer.print("_ = environment.interface.CallBooleanMethodA(environment, globalFuture, \(completeMethodID), [jvalue(l: nil)])")
} else {
printer.print("let swiftResult$ = \(tryAwaitString) \(placeholderWithoutTry)")
printer.print("environment = try! JavaVirtualMachine.shared().environment()")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to move the environment initialization?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only difference is whether we write the if statement twice or not.
I was okay with a single line of duplication, but we could also write the if statement twice if preferred.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, you can keep the printer.print("environment = try! JavaVirtualMachine.shared().environment()") before the if entirely so we don't duplicate the string in both branches

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would be better not to change the timing of fetching environment as it might span across a suspention point (await).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... let's go with this, though arguably before might be safer tbh...

let inner = nativeFunctionSignature.result.conversion.render(&printer, "swiftResult$")
let result: String
if nativeFunctionSignature.result.javaType.requiresBoxing {
printer.print("let boxedResult$ = SwiftJavaRuntimeSupport._JNIBoxedConversions.box(\(inner), in: environment)")
Expand Down
8 changes: 4 additions & 4 deletions Tests/JExtractSwiftTests/JNI/JNIAsyncTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ struct JNIAsyncTests {
let deferEnvironment = try! JavaVirtualMachine.shared().environment()
deferEnvironment.interface.DeleteGlobalRef(deferEnvironment, globalFuture)
}
let swiftResult$ = await SwiftModule.asyncVoid()
await SwiftModule.asyncVoid()
environment = try! JavaVirtualMachine.shared().environment()
_ = environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: nil)])
}
Expand All @@ -82,7 +82,7 @@ struct JNIAsyncTests {
let deferEnvironment = try! JavaVirtualMachine.shared().environment()
deferEnvironment.interface.DeleteGlobalRef(deferEnvironment, globalFuture)
}
let swiftResult$ = await SwiftModule.asyncVoid()
await SwiftModule.asyncVoid()
environment = try! JavaVirtualMachine.shared().environment()
_ = environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: nil)])
}
Expand Down Expand Up @@ -144,7 +144,7 @@ struct JNIAsyncTests {
deferEnvironment.interface.DeleteGlobalRef(deferEnvironment, globalFuture)
}
do {
let swiftResult$ = try await SwiftModule.async()
try await SwiftModule.async()
environment = try! JavaVirtualMachine.shared().environment()
_ = environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: nil)])
}
Expand All @@ -164,7 +164,7 @@ struct JNIAsyncTests {
deferEnvironment.interface.DeleteGlobalRef(deferEnvironment, globalFuture)
}
do {
let swiftResult$ = try await SwiftModule.async()
try await SwiftModule.async()
environment = try! JavaVirtualMachine.shared().environment()
_ = environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: nil)])
}
Expand Down
Loading