Skip to content

Commit 4e4a838

Browse files
committed
jextract: Support Java BooleanSupplier functional interface
1 parent 29464e2 commit 4e4a838

12 files changed

Lines changed: 194 additions & 4 deletions

File tree

‎Samples/SwiftAndJavaJarFFMSampleLib/Sources/MySwiftLibrary/MySwiftLibrary.swift‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public func globalCallMeRunnable(run: () -> Void) {
3636
run()
3737
}
3838

39+
public func globalCallMeBooleanSupplier(run: () -> Bool) -> Bool {
40+
return run()
41+
}
42+
3943
// ==== Internal helpers
4044

4145
func p(_ msg: String, file: String = #fileID, line: UInt = #line, function: String = #function) {

‎Samples/SwiftAndJavaJarFFMSampleLib/src/test/java/com/example/swift/MySwiftLibraryTest.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,9 @@ public void apply() {
5454
assertEquals(0, countDownLatch.getCount());
5555
}
5656

57+
@Test
58+
void call_globalCallMeBooleanSupplier_noThrow() {
59+
boolean result = MySwiftLibrary.globalCallMeBooleanSupplier(() -> { return true; });
60+
assertEquals(true, result);
61+
}
5762
}

‎Samples/SwiftJavaExtractFFMSampleApp/Sources/MySwiftLibrary/MySwiftLibrary.swift‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public func globalCallMeRunnable(run: () -> Void) {
4848
run()
4949
}
5050

51+
public func globalCallMeBooleanSupplier(run: () -> Bool) -> Bool {
52+
return run()
53+
}
54+
5155
public func globalReceiveRawBuffer(buf: UnsafeRawBufferPointer) -> Int {
5256
buf.count
5357
}

‎Samples/SwiftJavaExtractFFMSampleApp/src/test/java/com/example/swift/MySwiftLibraryTest.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ void call_globalStringIdentity_empty() {
7373
assertEquals("", result);
7474
}
7575

76-
77-
7876
@Test
7977
@Disabled("Upcalls not yet implemented in new scheme")
8078
@SuppressWarnings({"Convert2Lambda", "Convert2MethodRef"})
@@ -148,4 +146,9 @@ void call_globalThrowingString_throws_checkMessage() {
148146
);
149147
}
150148

149+
@Test
150+
void call_globalCallMeBooleanSupplier_noThrow() {
151+
boolean result = MySwiftLibrary.globalCallMeBooleanSupplier(() -> { return true; });
152+
assertEquals(true, result);
153+
}
151154
}

‎Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/Closures.swift‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public func closureWithInt(input: Int64, closure: (Int64) -> Int64) -> Int64 {
2020
closure(input)
2121
}
2222

23+
public func globalCallMeBooleanSupplier(run: () -> Bool) -> Bool {
24+
return run()
25+
}
26+
2327
public func closureMultipleArguments(
2428
input1: Int64,
2529
input2: Int64,

‎Samples/SwiftJavaExtractJNISampleApp/src/test/java/com/example/swift/ClosuresTest.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,10 @@ void closureMultipleArguments() {
4141
long result = MySwiftLibrary.closureMultipleArguments(5, 10, (a, b) -> a + b);
4242
assertEquals(15, result);
4343
}
44-
}
44+
45+
@Test
46+
void globalCallMeBooleanSupplier() {
47+
boolean result = MySwiftLibrary.globalCallMeBooleanSupplier(() -> true);
48+
assertEquals(true, result);
49+
}
50+
}

‎Sources/ExampleSwiftLibrary/MySwiftLibrary.swift‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public func globalCallMeRunnable(run: () -> Void) {
4343
run()
4444
}
4545

46+
public func globalCallMeBooleanSupplier(run: () -> Bool) -> Bool {
47+
return run()
48+
}
49+
4650
public func globalReceiveRawBuffer(buf: UnsafeRawBufferPointer) -> Int {
4751
buf.count
4852
}

‎Sources/JExtractSwiftLib/JavaTypes/JavaType+JDK.swift‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ extension JavaType {
3030
.class(package: "java.lang", name: "Runnable")
3131
}
3232

33+
/// The description of the type java.util.function.BooleanSupplier.
34+
static var javaFunctionBooleanSupplier: JavaType {
35+
.class(package: "java.util.function", name: "BooleanSupplier")
36+
}
37+
3338
/// The description of the type java.lang.Class.
3439
static var javaLangClass: JavaType {
3540
.class(package: "java.lang", name: "Class")

‎Sources/JExtractSwiftLib/KnownFunctionalInterfaces.swift‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,16 @@ struct KnownJavaFunctionalInterface: Sendable {
2929
result: .void
3030
)
3131

32+
static let booleanSupplier = KnownJavaFunctionalInterface(
33+
JavaType.javaFunctionBooleanSupplier,
34+
method: "getAsBoolean",
35+
parameters: [],
36+
result: .boolean
37+
)
38+
3239
static let all: [KnownJavaFunctionalInterface] = [
33-
.runnable
40+
.runnable,
41+
.booleanSupplier,
3442
]
3543

3644
static func find(parameters: [JavaType], result: JavaType) -> KnownJavaFunctionalInterface? {
@@ -59,6 +67,8 @@ struct KnownJavaFunctionalInterface: Sendable {
5967
return switch (parameters, result) {
6068
case ([], _) where result.isVoid:
6169
runnable
70+
case ([], _) where result.isBoolean:
71+
booleanSupplier
6272
default:
6373
nil
6474
}

‎Sources/SwiftExtract/SwiftTypes/SwiftType.swift‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ public enum SwiftType: Equatable {
9898
}
9999
}
100100

101+
public var isBoolean: Bool {
102+
if case let .nominal(nominal) = self {
103+
return nominal.nominalTypeDecl.knownTypeKind == .bool
104+
}
105+
return false
106+
}
107+
101108
/// Whether this is a pointer type. I.e 'Unsafe[Mutable][Raw]Pointer'
102109
public var isPointer: Bool {
103110
switch self {

0 commit comments

Comments
 (0)