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 @@ -80,6 +80,10 @@ public func globalCallMeIntUnaryOperator(run: (Int32) -> Int32) -> Int32 {
run(1)
}

public func globalCallMeLongUnaryOperator(run: (Int64) -> Int64) -> Int64 {
run(1)
}

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 call_globalCallMeIntUnaryOperator_noThrow() {
assertEquals(1, result);
}

@Test
void call_globalCallMeLongUnaryOperator_noThrow() {
long result = MySwiftLibrary.globalCallMeLongBinaryOperator((long a) -> { return a; });
assertEquals(1L, 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 @@ -92,6 +92,10 @@ public func globalCallMeIntUnaryOperator(run: (Int32) -> Int32) -> Int32 {
run(1)
}

public func globalCallMeLongUnaryOperator(run: (Int64) -> Int64) -> Int64 {
run(1)
}

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 @@ -229,6 +229,12 @@ void call_globalCallMeIntUnaryOperator_noThrow() {
assertEquals(1, result);
}

@Test
void call_globalCallMeLongUnaryOperator_noThrow() {
long result = MySwiftLibrary.globalCallMeLongUnaryOperator((long a) -> { return a; });
assertEquals(1L, 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 @@ -65,6 +65,10 @@ public func globalCallMeIntUnaryOperator(run: (Int32) -> Int32) -> Int32 {
run(1)
}

public func globalCallMeLongUnaryOperator(run: (Int64) -> Int64) -> Int64 {
run(1)
}

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 @@ -111,6 +111,12 @@ void globalCallMeIntUnaryOperator() {
assertEquals(1, result);
}

@Test
void globalCallMeLongUnaryOperator() {
long result = MySwiftLibrary.globalCallMeLongUnaryOperator((long a) -> { return a; });
assertEquals(1L, 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 @@ -87,6 +87,10 @@ public func globalCallMeIntUnaryOperator(run: (Int32) -> Int32) -> Int32 {
run(1)
}

public func globalCallMeLongUnaryOperator(run: (Int64) -> Int64) -> Int64 {
run(1)
}

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 @@ -85,6 +85,11 @@ extension JavaType {
.class(package: "java.util.function", name: "IntUnaryOperator")
}

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

/// 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 @@ -106,6 +106,13 @@ struct KnownJavaFunctionalInterface: Sendable {
result: .int
)

static let longUnaryOperator = KnownJavaFunctionalInterface(
JavaType.javaUtilFunctionLongUnaryOperator,
method: "applyAsLong",
parameters: [.long],
result: .long
)

static let intBinaryOperator = KnownJavaFunctionalInterface(
JavaType.javaUtilFunctionIntBinaryOperator,
method: "applyAsInt",
Expand Down Expand Up @@ -140,6 +147,7 @@ struct KnownJavaFunctionalInterface: Sendable {
.longPredicate,
.doublePredicate,
.intUnaryOperator,
.longUnaryOperator,
.intBinaryOperator,
.longBinaryOperator,
.doubleBinaryOperator,
Expand Down Expand Up @@ -223,6 +231,8 @@ struct KnownJavaFunctionalInterface: Sendable {
return switch () {
case _ where parameter.isInt32:
intUnaryOperator
case _ where parameter.isInt64:
longUnaryOperator
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 @@ -48,6 +48,7 @@ final class FuncCallbackImportTests {
public func callMeDoublePredicate(callback: (Double) -> Bool)

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

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

@Test("Import: public func callMecallMeLongUnaryOperatorFunc(callback: (Int64) -> Int64)")
func func_callMecallMeLongUnaryOperatorFunc_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 == "callMeLongUnaryOperator" }!

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 callMeLongUnaryOperator(callback: (Int64) -> Int64)
* }
*/
public static void callMeLongUnaryOperator(java.util.function.LongUnaryOperator callback) {
try(var arena$ = Arena.ofConfined()) {
swiftjava___FakeModule_callMeLongUnaryOperator_callback.call(callMeLongUnaryOperator.$toUpcallStub(callback, arena$));
}
}
"""
]
)
}

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

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

public func closureIntBinaryOperator(closure: (Int32, Int32) -> Int32) {}
public func closureLongBinaryOperator(closure: (Int64, Int64) -> Int64) {}
Expand Down Expand Up @@ -342,6 +343,31 @@ struct JNIClosureTests {
)
}

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

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

@Test
func closureLongUnaryOperator_swiftThunks() throws {
try assertOutput(
input: source,
.jni,
.swift,
detectChunkByInitialLines: 1,
expectedChunks: [
"""
@_cdecl("Java_com_example_swift_SwiftModule__00024closureLongUnaryOperator__Ljava_util_function_LongUnaryOperator_2")
public func Java_com_example_swift_SwiftModule__00024closureLongUnaryOperator__Ljava_util_function_LongUnaryOperator_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, closure: jobject?) {
SwiftModule.closureLongUnaryOperator(closure: {
let class$ = environment.interface.GetObjectClass(environment, closure)
let methodID$ = environment.interface.GetMethodID(environment, class$, "applyAsLong", "(J)J")!
environment.interface.DeleteLocalRef(environment, class$)
let arguments$: [jvalue] = [_0.getJValue(in: environment)]
return Int64(fromJNI: environment.interface.CallLongMethodA(environment, closure, methodID$, arguments$), in: environment)
}
)
}
"""
]
)
}

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