Skip to content

Commit f241b47

Browse files
committed
correct implementation, verify tests pass
1 parent 6c59fed commit f241b47

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

‎Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+NativeTranslation.swift‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,15 +1033,21 @@ extension JNISwift2JavaGenerator {
10331033
)
10341034

10351035
func printDo(printer: inout CodePrinter) {
1036-
// Generate proper 'try await' order: if placeholder already has 'try', remove it and add 'try await', otherwise just add 'try await'
1037-
let modifiersAndPlaceholder: String
1038-
if placeholder.hasPrefix("try ") {
1039-
let placeholderWithoutTry = placeholder.replacingOccurrences(of: "^try ", with: "", options: .regularExpression)
1040-
modifiersAndPlaceholder = "try await \(placeholderWithoutTry)"
1041-
} else {
1042-
modifiersAndPlaceholder = "try await \(placeholder)"
1043-
}
1044-
printer.print("let swiftResult$ = \(modifiersAndPlaceholder)")
1036+
// Make sure try/await are printed when necessary and avoid duplicate, or wrong-order, keywords (which would cause warnings)
1037+
let placeholderWithoutTry =
1038+
if placeholder.hasPrefix("try ") {
1039+
String(placeholder.dropFirst("try ".count))
1040+
} else {
1041+
placeholder
1042+
}
1043+
1044+
let tryAwaitString: String =
1045+
if isThrowing {
1046+
"try await"
1047+
} else {
1048+
"await"
1049+
}
1050+
printer.print("let swiftResult$ = \(tryAwaitString) \(placeholderWithoutTry)")
10451051
printer.print("environment = try! JavaVirtualMachine.shared().environment()")
10461052
let inner = nativeFunctionSignature.result.conversion.render(&printer, "swiftResult$")
10471053
if swiftFunctionResultType.isVoid {

‎Tests/JExtractSwiftTests/JNI/JNIAsyncTests.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ struct JNIAsyncTests {
144144
deferEnvironment.interface.DeleteGlobalRef(deferEnvironment, globalFuture)
145145
}
146146
do {
147-
let swiftResult$ = await try SwiftModule.async()
147+
let swiftResult$ = try await SwiftModule.async()
148148
environment = try! JavaVirtualMachine.shared().environment()
149149
_ = environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: nil)])
150150
}
@@ -164,7 +164,7 @@ struct JNIAsyncTests {
164164
deferEnvironment.interface.DeleteGlobalRef(deferEnvironment, globalFuture)
165165
}
166166
do {
167-
let swiftResult$ = await try SwiftModule.async()
167+
let swiftResult$ = try await SwiftModule.async()
168168
environment = try! JavaVirtualMachine.shared().environment()
169169
_ = environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: nil)])
170170
}

0 commit comments

Comments
 (0)