Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public func globalCallMeLongUnaryOperator(run: (Int64) -> Int64) -> Int64 {
run(1)
}

public func globalCallMeDoubleUnaryOperator(run: (Double) -> Double) -> Double {
run(1.0)
}

public func globalCallMeIntBinaryOperator(run: (Int32, Int32) -> Int32) -> Int32 {
run(1, 2)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ void call_globalCallMeLongUnaryOperator_noThrow() {
assertEquals(1L, result);
}

@Test
void call_globalCallMeDoubleUnaryOperator_noThrow() {
double result = MySwiftLibrary.globalCallMeDoubleBinaryOperator((double a) -> { return a; });
assertEquals(1.0, result);
}

@Test
void call_globalCallMeIntBinaryOperator_noThrow() {
int result = MySwiftLibrary.globalCallMeIntBinaryOperator((int a, int b) -> { return a + b; });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ public func globalCallMeLongUnaryOperator(run: (Int64) -> Int64) -> Int64 {
run(1)
}

public func globalCallMeDoubleUnaryOperator(run: (Double) -> Double) -> Double {
run(1.0)
}

public func globalCallMeIntBinaryOperator(run: (Int32, Int32) -> Int32) -> Int32 {
run(1, 2)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ void call_globalCallMeLongUnaryOperator_noThrow() {
assertEquals(1L, result);
}

@Test
void call_globalCallMeDoubleUnaryOperator_noThrow() {
double result = MySwiftLibrary.globalCallMeDoubleUnaryOperator((double a) -> { return a; });
assertEquals(1.0, result);
}

@Test
void call_globalCallMeIntBinaryOperator_noThrow() {
int result = MySwiftLibrary.globalCallMeIntBinaryOperator((int a, int b) -> { return a + b; });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public func globalCallMeLongUnaryOperator(run: (Int64) -> Int64) -> Int64 {
run(1)
}

public func globalCallMeDoubleUnaryOperator(run: (Double) -> Double) -> Double {
run(1.0)
}

public func globalCallMeIntBinaryOperator(run: (Int32, Int32) -> Int32) -> Int32 {
run(1, 2)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ void globalCallMeLongUnaryOperator() {
assertEquals(1L, result);
}

@Test
void globalCallMeDoubleUnaryOperator() {
double result = MySwiftLibrary.globalCallMeDoubleUnaryOperator((double a) -> { return a; });
assertEquals(1.0, result);
}

@Test
void globalCallMeIntBinaryOperator() {
int result = MySwiftLibrary.globalCallMeIntBinaryOperator((int a, int b) -> { return a + b; });
Expand Down
4 changes: 4 additions & 0 deletions Sources/ExampleSwiftLibrary/MySwiftLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public func globalCallMeLongUnaryOperator(run: (Int64) -> Int64) -> Int64 {
run(1)
}

public func globalCallMeDoubleUnaryOperator(run: (Double) -> Double) -> Double {
run(1.0)
}

public func globalCallMeIntBinaryOperator(run: (Int32, Int32) -> Int32) -> Int32 {
run(1, 2)
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/JExtractSwiftLib/JavaTypes/JavaType+JDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ extension JavaType {
.class(package: "java.util.function", name: "LongUnaryOperator")
}

/// The description of the type java.util.function.DoubleUnaryOperator.
static var javaUtilFunctionDoubleUnaryOperator: JavaType {
.class(package: "java.util.function", name: "DoubleUnaryOperator")
}

/// The description of the type java.util.function.IntBinaryOperator.
static var javaUtilFunctionIntBinaryOperator: JavaType {
.class(package: "java.util.function", name: "IntBinaryOperator")
Expand Down
10 changes: 10 additions & 0 deletions Sources/JExtractSwiftLib/KnownFunctionalInterfaces.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ struct KnownJavaFunctionalInterface: Sendable {
result: .long
)

static let doubleUnaryOperator = KnownJavaFunctionalInterface(
JavaType.javaUtilFunctionDoubleUnaryOperator,
method: "applyAsDouble",
parameters: [.double],
result: .double
)

static let intBinaryOperator = KnownJavaFunctionalInterface(
JavaType.javaUtilFunctionIntBinaryOperator,
method: "applyAsInt",
Expand Down Expand Up @@ -148,6 +155,7 @@ struct KnownJavaFunctionalInterface: Sendable {
.doublePredicate,
.intUnaryOperator,
.longUnaryOperator,
.doubleUnaryOperator,
.intBinaryOperator,
.longBinaryOperator,
.doubleBinaryOperator,
Expand Down Expand Up @@ -233,6 +241,8 @@ struct KnownJavaFunctionalInterface: Sendable {
intUnaryOperator
case _ where parameter.isInt64:
longUnaryOperator
case _ where parameter.isDouble:
doubleUnaryOperator
default:
nil
}
Expand Down
44 changes: 44 additions & 0 deletions Tests/JExtractSwiftTests/FuncCallbackImportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ final class FuncCallbackImportTests {

public func callMeIntUnaryOperator(callback: (Int32) -> Int32)
public func callMeLongUnaryOperator(callback: (Int64) -> Int64)
public func callMeDoubleUnaryOperator(callback: (Double) -> Double)

public func callMeIntBinaryOperator(callback: (Int32, Int32) -> Int32)
public func callMeLongBinaryOperator(callback: (Int64, Int64) -> Int64)
Expand Down Expand Up @@ -810,6 +811,49 @@ final class FuncCallbackImportTests {
)
}

@Test("Import: public func callMecallMeDoubleUnaryOperatorFunc(callback: (Double) -> Double)")
func func_callMecallMeDoubleUnaryOperatorFunc_callback() throws {
var config = Configuration()
config.swiftModule = "__FakeModule"
let st = makeSwiftJavaAnalyzer(config: config)
st.log.logLevel = .error

try st.analyze(path: "Fake.swift", text: Self.class_interfaceFile)

let funcDecl = st.extractedGlobalFuncs.first { $0.name == "callMeDoubleUnaryOperator" }!

let generator = FFMSwift2JavaGenerator(
config: config,
translator: st,
javaPackage: "com.example.swift",
swiftOutputDirectory: "/fake",
javaOutputDirectory: "/fake"
)

let output = JavaPrinter.toString { printer in
generator.printFunctionDowncallMethods(&printer, funcDecl)
}

assertOutput(
output,
expectedChunks: [
"""
/**
* Downcall to Swift:
* {@snippet lang=swift :
* public func callMeDoubleUnaryOperator(callback: (Double) -> Double)
* }
*/
public static void callMeDoubleUnaryOperator(java.util.function.DoubleUnaryOperator callback) {
try(var arena$ = Arena.ofConfined()) {
swiftjava___FakeModule_callMeDoubleUnaryOperator_callback.call(callMeDoubleUnaryOperator.$toUpcallStub(callback, arena$));
}
}
"""
]
)
}

@Test("Import: public func callMecallMeIntBinaryOperatorFunc(callback: (Int32, Int32) -> Int32)")
func func_callMecallMeIntBinaryOperatorFunc_callback() throws {
var config = Configuration()
Expand Down
52 changes: 52 additions & 0 deletions Tests/JExtractSwiftTests/JNI/JNIClosureTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct JNIClosureTests {

public func closureIntUnaryOperator(closure: (Int32) -> Int32) {}
public func closureLongUnaryOperator(closure: (Int64) -> Int64) {}
public func closureDoubleUnaryOperator(closure: (Double) -> Double) {}

public func closureIntBinaryOperator(closure: (Int32, Int32) -> Int32) {}
public func closureLongBinaryOperator(closure: (Int64, Int64) -> Int64) {}
Expand Down Expand Up @@ -368,6 +369,32 @@ struct JNIClosureTests {
)
}


@Test
func closureDoubleUnaryOperator_javaBindings() throws {
try assertOutput(
input: source,
.jni,
.java,
expectedChunks: [
"""
/**
* Downcall to Swift:
* {@snippet lang=swift :
* public func closureDoubleUnaryOperator(closure: (Double) -> Double)
* }
*/
public static void closureDoubleUnaryOperator(java.util.function.DoubleUnaryOperator closure) {
SwiftModule.$closureDoubleUnaryOperator(closure);
}
""",
"""
private static native void $closureDoubleUnaryOperator(java.util.function.DoubleUnaryOperator closure);
""",
]
)
}

@Test
func closureIntBinaryOperator_javaBindings() throws {
try assertOutput(
Expand Down Expand Up @@ -768,6 +795,31 @@ struct JNIClosureTests {
)
}

@Test
func closureDoubleUnaryOperator_swiftThunks() throws {
try assertOutput(
input: source,
.jni,
.swift,
detectChunkByInitialLines: 1,
expectedChunks: [
"""
@_cdecl("Java_com_example_swift_SwiftModule__00024closureDoubleUnaryOperator__Ljava_util_function_DoubleUnaryOperator_2")
public func Java_com_example_swift_SwiftModule__00024closureDoubleUnaryOperator__Ljava_util_function_DoubleUnaryOperator_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, closure: jobject?) {
SwiftModule.closureDoubleUnaryOperator(closure: {
let class$ = environment.interface.GetObjectClass(environment, closure)
let methodID$ = environment.interface.GetMethodID(environment, class$, "applyAsDouble", "(D)D")!
environment.interface.DeleteLocalRef(environment, class$)
let arguments$: [jvalue] = [_0.getJValue(in: environment)]
return Double(fromJNI: environment.interface.CallDoubleMethodA(environment, closure, methodID$, arguments$), in: environment)
}
)
}
"""
]
)
}

@Test
func closureIntBinaryOperator_swiftThunks() throws {
try assertOutput(
Expand Down
Loading