Skip to content

Commit 2a23a14

Browse files
committed
Tweak
1 parent 55af486 commit 2a23a14

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

Samples/SwiftKitSampleApp/src/test/java/com/example/swift/MySwiftLibraryTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ void call_writeString_jni() {
6464
void call_globalCallMeRunnable() {
6565
CountDownLatch countDownLatch = new CountDownLatch(3);
6666

67-
MySwiftLibrary.globalCallMeRunnable.run fn = () -> {};
68-
MySwiftLibrary.globalCallMeRunnable(() -> {
69-
countDownLatch.countDown();
67+
MySwiftLibrary.globalCallMeRunnable(new MySwiftLibrary.globalCallMeRunnable.run() {
68+
@Override
69+
public void apply() {
70+
countDownLatch.countDown();
71+
}
7072
});
7173
assertEquals(2, countDownLatch.getCount());
7274

Sources/JExtractSwiftLib/CDeclLowering/Swift2JavaTranslator+FunctionLowering.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ struct CdeclLowering {
116116
)
117117
}
118118

119-
/// Lower a Swift function parameter type to cdecl parameters.
119+
/// Lower a Swift function parameter to cdecl parameters.
120120
///
121121
/// For example, Swift parameter `arg value: inout Int` can be lowered with
122122
/// `lowerParameter(intTy, .inout, "value")`.
@@ -294,6 +294,10 @@ struct CdeclLowering {
294294
}
295295
}
296296

297+
/// Lower a Swift function type (i.e. closure) to cdecl function type.
298+
///
299+
/// - Parameters:
300+
/// - fn: the Swift function type to lower.
297301
func lowerFunctionType(
298302
_ fn: SwiftFunctionType
299303
) throws -> (type: SwiftType, conversion: ConversionStep) {

Sources/JExtractSwiftLib/Swift2JavaTranslator+JavaBindingsPrinting.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ extension Swift2JavaTranslator {
128128
)
129129
}
130130

131+
/// Print required helper classes/interfaces for describing the CFunction.
132+
///
133+
/// * function pointer parameter as a function interface.
134+
/// * Unnamed-struct parameter as a record. (unimplemented)
131135
func printParameterDescriptorClasses(
132136
_ printer: inout CodePrinter,
133137
_ cFunc: CFunction
@@ -143,13 +147,14 @@ extension Swift2JavaTranslator {
143147
}
144148
}
145149

146-
/// Print a class describing a closure parameter type.
147-
/// ```
148-
/// class <paramter-name> {
150+
/// Print a class describing a function pointer parameter type.
151+
///
152+
/// ```java
153+
/// class $<parameter-name> {
149154
/// interface Function {
150155
/// <return-type> apply(<parameters>);
151156
/// }
152-
/// static final MethodDescriptor DESC = FunctionDescriptor.of(...);s
157+
/// static final MethodDescriptor DESC = FunctionDescriptor.of(...);
153158
/// static final MethodHandle HANDLE = SwiftKit.upcallHandle(Function.class, "apply", DESC);
154159
/// static MemorySegment toUpcallStub(Function fi, Arena arena) {
155160
/// return Linker.nativeLinker().upcallStub(HANDLE.bindTo(fi), DESC, arena);
@@ -199,6 +204,9 @@ extension Swift2JavaTranslator {
199204
}
200205
}
201206

207+
/// Print the helper type container for a user-facing Java API.
208+
///
209+
/// * User-facing function interfaces.
202210
func printJavaBindingWrapperHelperClass(
203211
_ printer: inout CodePrinter,
204212
_ decl: ImportedFunc
@@ -220,13 +228,16 @@ extension Swift2JavaTranslator {
220228
}
221229
}
222230

231+
/// Print "wrapper" function interface representing a Swift closure type.
223232
func printJavaBindingWrapperFunctionTypeHelper(
224233
_ printer: inout CodePrinter,
225234
_ functionType: TranslatedFunctionType,
226235
_ bindingDescriptorName: String
227236
) {
228237
let cdeclDescritor = "\(bindingDescriptorName).$\(functionType.name)"
229238
if functionType.isTrivial {
239+
// If the usser-facing function interface is C-compatible, just extend the
240+
// lowered function pointer parameter interface.
230241
printer.print(
231242
"""
232243
public interface \(functionType.name) extends \(cdeclDescritor).Function {
@@ -237,6 +248,7 @@ extension Swift2JavaTranslator {
237248
"""
238249
)
239250
} else {
251+
// Otherwise, the lambda must be wrapped with the lowered function instance.
240252
assertionFailure("should be unreachable at this point")
241253
let apiParams = functionType.parameters.flatMap {
242254
$0.javaParameters.map { param in "\(param.type) \(param.name)" }

Sources/JExtractSwiftLib/Swift2JavaTranslator+JavaTranslation.swift

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ extension Swift2JavaTranslator {
2424

2525
let translated: TranslatedFunctionDecl?
2626
do {
27-
translated = try self.translate(decl)
27+
let translation = JavaTranslation(swiftStdlibTypes: self.swiftStdlibTypes)
28+
translated = try translation.translate(decl)
2829
} catch {
2930
self.log.info("Failed to translate: '\(decl.swiftDecl.qualifiedNameForDebug)'; \(error)")
3031
translated = nil
@@ -33,11 +34,6 @@ extension Swift2JavaTranslator {
3334
translatedDecls[decl] = translated
3435
return translated
3536
}
36-
37-
private func translate(_ decl: ImportedFunc) throws -> TranslatedFunctionDecl {
38-
let translation = JavaTranslation(swiftStdlibTypes: self.swiftStdlibTypes)
39-
return try translation.translate(decl)
40-
}
4137
}
4238

4339
/// Represent a parameter in Java code.
@@ -83,18 +79,27 @@ struct TranslatedResult {
8379
var conversion: JavaConversionStep
8480
}
8581

82+
83+
/// Translated Java API representing a Swift API.
84+
///
85+
/// Since this holds the lowered signature, and the original `SwiftFunctionSignature`
86+
/// in it, this contains all the API information (except the name) to generate the
87+
/// cdecl thunk, Java binding, and the Java wrapper function.
8688
struct TranslatedFunctionDecl {
89+
/// Java function name.
8790
let name: String
91+
92+
/// Function interfaces (i.e. lambda types) required for the Java method.
8893
let functionTypes: [TranslatedFunctionType]
94+
95+
/// Function signature.
8996
let translatedSignature: TranslatedFunctionSignature
97+
98+
/// Cdecl lowerd signature.
9099
let loweredSignature: LoweredFunctionSignature
91100
}
92101

93-
/// Translated function signature representing a Swift API.
94-
///
95-
/// Since this holds the lowered signature, and the original `SwiftFunctionSignature`
96-
/// in it, this contains all the API information (except the name) to generate the
97-
/// cdecl thunk, Java binding, and the Java wrapper function.
102+
/// Function signature for a Java API.
98103
struct TranslatedFunctionSignature {
99104
var selfParameter: TranslatedParameter?
100105
var parameters: [TranslatedParameter]
@@ -219,7 +224,7 @@ struct JavaTranslation {
219224
)
220225
}
221226

222-
/// Translate Swift API to user-facing Java API.
227+
/// Translate a Swift API signature to the user-facing Java API signature.
223228
///
224229
/// Note that the result signature is for the high-level Java API, not the
225230
/// low-level FFM down-calling interface.
@@ -268,7 +273,7 @@ struct JavaTranslation {
268273
)
269274
}
270275

271-
/// Translate
276+
/// Translate a Swift API parameter to the user-facing Java API parameter.
272277
func translate(
273278
swiftParam: SwiftParameter,
274279
loweredParam: LoweredParameter,
@@ -368,6 +373,7 @@ struct JavaTranslation {
368373
}
369374
}
370375

376+
/// Translate a Swift API result to the user-facing Java API result.
371377
func translate(
372378
swiftResult: SwiftResult,
373379
loweredResult: LoweredResult

0 commit comments

Comments
 (0)