@@ -182,13 +182,13 @@ extension JNISwift2JavaGenerator {
182182 }
183183
184184 // Swift -> Java
185- let translatedFunctionSignature = try translate (
185+ var translatedFunctionSignature = try translate (
186186 functionSignature: decl. functionSignature,
187187 methodName: javaName,
188188 parentName: parentName
189189 )
190190 // Java -> Java (native)
191- let nativeFunctionSignature = try nativeTranslation. translate (
191+ var nativeFunctionSignature = try nativeTranslation. translate (
192192 functionSignature: decl. functionSignature,
193193 translatedFunctionSignature: translatedFunctionSignature,
194194 methodName: javaName,
@@ -213,6 +213,16 @@ extension JNISwift2JavaGenerator {
213213 }
214214 }
215215
216+ // Handle async methods
217+ if decl. functionSignature. isAsync {
218+ self . convertToAsync (
219+ translatedFunctionSignature: & translatedFunctionSignature,
220+ nativeFunctionSignature: & nativeFunctionSignature,
221+ originalFunctionSignature: decl. functionSignature,
222+ mode: config. effectiveAsyncFuncMode
223+ )
224+ }
225+
216226 return TranslatedFunctionDecl (
217227 name: javaName,
218228 isStatic: decl. isStatic || !decl. hasParent || decl. isInitializer,
@@ -281,11 +291,7 @@ extension JNISwift2JavaGenerator {
281291 genericRequirements: functionSignature. genericRequirements
282292 )
283293
284- var resultType = try translate ( swiftResult: functionSignature. result)
285-
286- if functionSignature. effectSpecifiers. contains ( . async) {
287- resultType = asyncResultConversion ( result: resultType, mode: config. effectiveAsyncMode)
288- }
294+ let resultType = try translate ( swiftResult: functionSignature. result)
289295
290296 return TranslatedFunctionSignature (
291297 selfParameter: selfParameter,
@@ -476,49 +482,58 @@ extension JNISwift2JavaGenerator {
476482 }
477483 }
478484
479- func asyncResultConversion(
480- result: TranslatedResult ,
481- mode: JExtractAsyncMode
482- ) -> TranslatedResult {
485+ func convertToAsync(
486+ translatedFunctionSignature: inout TranslatedFunctionSignature ,
487+ nativeFunctionSignature: inout NativeFunctionSignature ,
488+ originalFunctionSignature: SwiftFunctionSignature ,
489+ mode: JExtractAsyncFuncMode
490+ ) {
483491 switch mode {
484492 case . completableFuture:
485- let supplyAsyncBodyConversion : JavaNativeConversionStep = if result. javaType. isVoid {
486- . aggregate( [
487- . print( result. conversion) ,
488- . null
489- ] )
490- } else {
491- result. conversion
492- }
493+ // let supplyAsyncBodyConversion: JavaNativeConversionStep = if result.javaType.isVoid {
494+ // .aggregate([
495+ // .print(result.conversion),
496+ // .null
497+ // ])
498+ // } else {
499+ // result.conversion
500+ // }
501+
502+ // Update translated function
503+
504+ let nativeFutureType = JavaType . completableFuture ( nativeFunctionSignature. result. javaType)
505+
506+ let futureOutParameter = OutParameter (
507+ name: " $future " ,
508+ type: nativeFutureType,
509+ allocation: . new
510+ )
493511
494- return TranslatedResult (
495- javaType: . class( package : " java.util.concurrent " , name: " CompletableFuture< \( result. javaType. wrapperClassIfNeeded) > " ) ,
512+ let result = translatedFunctionSignature. resultType
513+ translatedFunctionSignature. resultType = TranslatedResult (
514+ javaType: . completableFuture( translatedFunctionSignature. resultType. javaType) ,
496515 annotations: result. annotations,
497- outParameters: result. outParameters,
498- conversion: . method( . constant( " java.util.concurrent.CompletableFuture " ) , function: " supplyAsync " , arguments: [
499- . lambda( body: supplyAsyncBodyConversion) ,
500- . constant( " SwiftAsync.SWIFT_ASYNC_EXECUTOR " )
516+ outParameters: result. outParameters + [ futureOutParameter] ,
517+ conversion: . aggregate( variable: nil , [
518+ . print( . placeholder) , // Make the downcall
519+ . method( . constant( " $future " ) , function: " thenApply " , arguments: [
520+ . lambda( args: [ " futureResult$ " ] , body: . replacingPlaceholder( result. conversion, placeholder: " futureResult$ " ) )
521+ ] )
501522 ] )
502523 )
503524
504- case . future:
505- let asyncBodyConversion : JavaNativeConversionStep = if result. javaType. isVoid {
506- . aggregate( [
507- . print( result. conversion) ,
508- . null
509- ] )
510- } else {
511- result. conversion
512- }
513-
514- return TranslatedResult (
515- javaType: . class( package : " java.util.concurrent " , name: " Future< \( result. javaType. wrapperClassIfNeeded) > " ) ,
516- annotations: result. annotations,
517- outParameters: result. outParameters,
518- conversion: . method( . constant( " SwiftAsync.SWIFT_ASYNC_EXECUTOR " ) , function: " submit " , arguments: [
519- . lambda( body: asyncBodyConversion)
520- ] )
525+ // Update native function
526+ nativeFunctionSignature. result. javaType = . void
527+ nativeFunctionSignature. result. conversion = . asyncCompleteFuture(
528+ nativeFunctionSignature. result. conversion,
529+ swiftFunctionResultType: originalFunctionSignature. result. type,
530+ nativeReturnType: nativeFunctionSignature. result. javaType,
531+ outParameters: nativeFunctionSignature. result. outParameters
521532 )
533+ nativeFunctionSignature. result. outParameters. append ( . init( name: " result_future " , type: nativeFutureType) )
534+
535+ case . future:
536+ fatalError ( )
522537 }
523538 }
524539
@@ -866,11 +881,15 @@ extension JNISwift2JavaGenerator {
866881 struct OutParameter {
867882 enum Allocation {
868883 case newArray( JavaType , size: Int )
884+ case new
869885
870- func render( ) -> String {
886+ func render( type : JavaType ) -> String {
871887 switch self {
872888 case . newArray( let javaType, let size) :
873889 " new \( javaType) [ \( size) ] "
890+
891+ case . new:
892+ " new \( type) () "
874893 }
875894 }
876895 }
@@ -969,8 +988,8 @@ extension JNISwift2JavaGenerator {
969988 /// `return value`
970989 indirect case `return`( JavaNativeConversionStep )
971990
972- /// `() -> { return body; }`
973- indirect case lambda( body: JavaNativeConversionStep )
991+ /// `(args ) -> { return body; }`
992+ indirect case lambda( args : [ String ] = [ ] , body: JavaNativeConversionStep )
974993
975994 case null
976995
@@ -1094,9 +1113,9 @@ extension JNISwift2JavaGenerator {
10941113 let inner = inner. render ( & printer, placeholder)
10951114 return " return \( inner) ; "
10961115
1097- case . lambda( let body) :
1116+ case . lambda( let args , let body) :
10981117 var printer = CodePrinter ( )
1099- printer. printBraceBlock ( " () -> " ) { printer in
1118+ printer. printBraceBlock ( " ( \( args . joined ( separator : " , " ) ) ) -> " ) { printer in
11001119 let body = body. render ( & printer, placeholder)
11011120 if !body. isEmpty {
11021121 printer. print ( " return \( body) ; " )
@@ -1167,7 +1186,7 @@ extension JNISwift2JavaGenerator {
11671186 case . return( let inner) :
11681187 return inner. requiresSwiftArena
11691188
1170- case . lambda( let body) :
1189+ case . lambda( _ , let body) :
11711190 return body. requiresSwiftArena
11721191
11731192 case . print( let inner) :
0 commit comments