Skip to content

Commit ec6ae15

Browse files
authored
jextract: Support Java DoubleUnaryOperator func interface (#889)
1 parent a433ddb commit ec6ae15

11 files changed

Lines changed: 145 additions & 0 deletions

File tree

Samples/SwiftAndJavaJarFFMSampleLib/Sources/MySwiftLibrary/MySwiftLibrary.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public func globalCallMeLongUnaryOperator(run: (Int64) -> Int64) -> Int64 {
8484
run(1)
8585
}
8686

87+
public func globalCallMeDoubleUnaryOperator(run: (Double) -> Double) -> Double {
88+
run(1.0)
89+
}
90+
8791
public func globalCallMeIntBinaryOperator(run: (Int32, Int32) -> Int32) -> Int32 {
8892
run(1, 2)
8993
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ void call_globalCallMeLongUnaryOperator_noThrow() {
123123
assertEquals(1L, result);
124124
}
125125

126+
@Test
127+
void call_globalCallMeDoubleUnaryOperator_noThrow() {
128+
double result = MySwiftLibrary.globalCallMeDoubleBinaryOperator((double a) -> { return a; });
129+
assertEquals(1.0, result);
130+
}
131+
126132
@Test
127133
void call_globalCallMeIntBinaryOperator_noThrow() {
128134
int result = MySwiftLibrary.globalCallMeIntBinaryOperator((int a, int b) -> { return a + b; });

Samples/SwiftJavaExtractFFMSampleApp/Sources/MySwiftLibrary/MySwiftLibrary.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ public func globalCallMeLongUnaryOperator(run: (Int64) -> Int64) -> Int64 {
9696
run(1)
9797
}
9898

99+
public func globalCallMeDoubleUnaryOperator(run: (Double) -> Double) -> Double {
100+
run(1.0)
101+
}
102+
99103
public func globalCallMeIntBinaryOperator(run: (Int32, Int32) -> Int32) -> Int32 {
100104
run(1, 2)
101105
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ void call_globalCallMeLongUnaryOperator_noThrow() {
235235
assertEquals(1L, result);
236236
}
237237

238+
@Test
239+
void call_globalCallMeDoubleUnaryOperator_noThrow() {
240+
double result = MySwiftLibrary.globalCallMeDoubleUnaryOperator((double a) -> { return a; });
241+
assertEquals(1.0, result);
242+
}
243+
238244
@Test
239245
void call_globalCallMeIntBinaryOperator_noThrow() {
240246
int result = MySwiftLibrary.globalCallMeIntBinaryOperator((int a, int b) -> { return a + b; });

Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/Closures.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public func globalCallMeLongUnaryOperator(run: (Int64) -> Int64) -> Int64 {
6969
run(1)
7070
}
7171

72+
public func globalCallMeDoubleUnaryOperator(run: (Double) -> Double) -> Double {
73+
run(1.0)
74+
}
75+
7276
public func globalCallMeIntBinaryOperator(run: (Int32, Int32) -> Int32) -> Int32 {
7377
run(1, 2)
7478
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ void globalCallMeLongUnaryOperator() {
117117
assertEquals(1L, result);
118118
}
119119

120+
@Test
121+
void globalCallMeDoubleUnaryOperator() {
122+
double result = MySwiftLibrary.globalCallMeDoubleUnaryOperator((double a) -> { return a; });
123+
assertEquals(1.0, result);
124+
}
125+
120126
@Test
121127
void globalCallMeIntBinaryOperator() {
122128
int result = MySwiftLibrary.globalCallMeIntBinaryOperator((int a, int b) -> { return a + b; });

Sources/ExampleSwiftLibrary/MySwiftLibrary.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public func globalCallMeLongUnaryOperator(run: (Int64) -> Int64) -> Int64 {
9191
run(1)
9292
}
9393

94+
public func globalCallMeDoubleUnaryOperator(run: (Double) -> Double) -> Double {
95+
run(1.0)
96+
}
97+
9498
public func globalCallMeIntBinaryOperator(run: (Int32, Int32) -> Int32) -> Int32 {
9599
run(1, 2)
96100
}

Sources/JExtractSwiftLib/JavaTypes/JavaType+JDK.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ extension JavaType {
9090
.class(package: "java.util.function", name: "LongUnaryOperator")
9191
}
9292

93+
/// The description of the type java.util.function.DoubleUnaryOperator.
94+
static var javaUtilFunctionDoubleUnaryOperator: JavaType {
95+
.class(package: "java.util.function", name: "DoubleUnaryOperator")
96+
}
97+
9398
/// The description of the type java.util.function.IntBinaryOperator.
9499
static var javaUtilFunctionIntBinaryOperator: JavaType {
95100
.class(package: "java.util.function", name: "IntBinaryOperator")

Sources/JExtractSwiftLib/KnownFunctionalInterfaces.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ struct KnownJavaFunctionalInterface: Sendable {
113113
result: .long
114114
)
115115

116+
static let doubleUnaryOperator = KnownJavaFunctionalInterface(
117+
JavaType.javaUtilFunctionDoubleUnaryOperator,
118+
method: "applyAsDouble",
119+
parameters: [.double],
120+
result: .double
121+
)
122+
116123
static let intBinaryOperator = KnownJavaFunctionalInterface(
117124
JavaType.javaUtilFunctionIntBinaryOperator,
118125
method: "applyAsInt",
@@ -148,6 +155,7 @@ struct KnownJavaFunctionalInterface: Sendable {
148155
.doublePredicate,
149156
.intUnaryOperator,
150157
.longUnaryOperator,
158+
.doubleUnaryOperator,
151159
.intBinaryOperator,
152160
.longBinaryOperator,
153161
.doubleBinaryOperator,
@@ -233,6 +241,8 @@ struct KnownJavaFunctionalInterface: Sendable {
233241
intUnaryOperator
234242
case _ where parameter.isInt64:
235243
longUnaryOperator
244+
case _ where parameter.isDouble:
245+
doubleUnaryOperator
236246
default:
237247
nil
238248
}

Tests/JExtractSwiftTests/FuncCallbackImportTests.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ final class FuncCallbackImportTests {
4949
5050
public func callMeIntUnaryOperator(callback: (Int32) -> Int32)
5151
public func callMeLongUnaryOperator(callback: (Int64) -> Int64)
52+
public func callMeDoubleUnaryOperator(callback: (Double) -> Double)
5253
5354
public func callMeIntBinaryOperator(callback: (Int32, Int32) -> Int32)
5455
public func callMeLongBinaryOperator(callback: (Int64, Int64) -> Int64)
@@ -810,6 +811,49 @@ final class FuncCallbackImportTests {
810811
)
811812
}
812813

814+
@Test("Import: public func callMecallMeDoubleUnaryOperatorFunc(callback: (Double) -> Double)")
815+
func func_callMecallMeDoubleUnaryOperatorFunc_callback() throws {
816+
var config = Configuration()
817+
config.swiftModule = "__FakeModule"
818+
let st = makeSwiftJavaAnalyzer(config: config)
819+
st.log.logLevel = .error
820+
821+
try st.analyze(path: "Fake.swift", text: Self.class_interfaceFile)
822+
823+
let funcDecl = st.extractedGlobalFuncs.first { $0.name == "callMeDoubleUnaryOperator" }!
824+
825+
let generator = FFMSwift2JavaGenerator(
826+
config: config,
827+
translator: st,
828+
javaPackage: "com.example.swift",
829+
swiftOutputDirectory: "/fake",
830+
javaOutputDirectory: "/fake"
831+
)
832+
833+
let output = JavaPrinter.toString { printer in
834+
generator.printFunctionDowncallMethods(&printer, funcDecl)
835+
}
836+
837+
assertOutput(
838+
output,
839+
expectedChunks: [
840+
"""
841+
/**
842+
* Downcall to Swift:
843+
* {@snippet lang=swift :
844+
* public func callMeDoubleUnaryOperator(callback: (Double) -> Double)
845+
* }
846+
*/
847+
public static void callMeDoubleUnaryOperator(java.util.function.DoubleUnaryOperator callback) {
848+
try(var arena$ = Arena.ofConfined()) {
849+
swiftjava___FakeModule_callMeDoubleUnaryOperator_callback.call(callMeDoubleUnaryOperator.$toUpcallStub(callback, arena$));
850+
}
851+
}
852+
"""
853+
]
854+
)
855+
}
856+
813857
@Test("Import: public func callMecallMeIntBinaryOperatorFunc(callback: (Int32, Int32) -> Int32)")
814858
func func_callMecallMeIntBinaryOperatorFunc_callback() throws {
815859
var config = Configuration()

0 commit comments

Comments
 (0)