Skip to content

Commit d693691

Browse files
committed
add "future" mode
1 parent adfdea8 commit d693691

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaTranslation.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,12 +496,29 @@ extension JNISwift2JavaGenerator {
496496
annotations: result.annotations,
497497
outParameters: result.outParameters,
498498
conversion: .method(.constant("java.util.concurrent.CompletableFuture"), function: "supplyAsync", arguments: [
499-
.lambda(body: supplyAsyncBodyConversion)
499+
.lambda(body: supplyAsyncBodyConversion),
500+
.constant("SwiftAsync.SWIFT_ASYNC_EXECUTOR")
500501
])
501502
)
502503

503504
case .future:
504-
fatalError("TODO")
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+
])
521+
)
505522
}
506523
}
507524

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+NativeTranslation.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ extension JNISwift2JavaGenerator {
521521
mode: JExtractAsyncMode
522522
) -> NativeResult {
523523
switch mode {
524-
case .completableFuture:
524+
case .completableFuture, .future:
525525
return NativeResult(
526526
javaType: result.javaType,
527527
conversion: .asyncBlocking(
@@ -531,9 +531,6 @@ extension JNISwift2JavaGenerator {
531531
),
532532
outParameters: result.outParameters
533533
)
534-
535-
case .future:
536-
fatalError("TODO")
537534
}
538535
}
539536
}

Sources/SwiftJavaTool/Commands/JExtractCommand.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ extension SwiftJava {
7878
"""
7979
)
8080
var dependsOn: [String] = []
81+
82+
@Option(help: "The mode to use for extracting asynchronous Swift code. By default async methods are extracted as Java functions returning CompletableFuture.")
83+
var asyncMode: JExtractAsyncMode = .default
8184
}
8285
}
8386

@@ -98,6 +101,7 @@ extension SwiftJava.JExtractCommand {
98101
config.unsignedNumbersMode = unsignedNumbers
99102
config.minimumInputAccessLevelMode = minimumInputAccessLevel
100103
config.memoryManagementMode = memoryManagementMode
104+
config.asyncMode = asyncMode
101105

102106
try checkModeCompatibility()
103107

@@ -162,3 +166,4 @@ extension JExtractGenerationMode: ExpressibleByArgument {}
162166
extension JExtractUnsignedIntegerMode: ExpressibleByArgument {}
163167
extension JExtractMinimumAccessLevelMode: ExpressibleByArgument {}
164168
extension JExtractMemoryManagementMode: ExpressibleByArgument {}
169+
extension JExtractAsyncMode: ExpressibleByArgument {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
package org.swift.swiftkit.core;
16+
17+
import java.util.concurrent.ExecutorService;
18+
import java.util.concurrent.Executors;
19+
20+
public final class SwiftAsync {
21+
public static final ExecutorService SWIFT_ASYNC_EXECUTOR = Executors.newCachedThreadPool();
22+
}

0 commit comments

Comments
 (0)