Skip to content
Open
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 @@ -116,6 +116,14 @@ public func globalCallMeIntToLongFunction(run: (Int32) -> Int64) -> Int64 {
run(1)
}

public func globalCallMeIntToDoubleFunction(run: (Int32) -> Double) -> Double {
run(1)
}

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

// ==== Internal helpers

func p(_ msg: String, file: String = #fileID, line: UInt = #line, function: String = #function) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,16 @@ void call_globalCallMeIntToLongFunction_noThrow() {
long result = MySwiftLibrary.globalCallMeIntToLongFunction((int a) -> { return (long) a; });
assertEquals(1L, result);
}

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

@Test
void call_globalCallMeLongToDoubleFunction_noThrow() {
double result = MySwiftLibrary.globalCallMeLongToDoubleFunction((long a) -> { return (double) a; });
assertEquals(1.0, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ public func globalCallMeIntToLongFunction(run: (Int32) -> Int64) -> Int64 {
run(1)
}

public func globalCallMeIntToDoubleFunction(run: (Int32) -> Double) -> Double {
run(1)
}

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

public func globalReceiveRawBuffer(buf: UnsafeRawBufferPointer) -> Int {
buf.count
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,16 @@ void call_globalCallMeIntToLongFunction_noThrow() {
long result = MySwiftLibrary.globalCallMeIntToLongFunction((int a) -> { return (long) a; });
assertEquals(1L, result);
}

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

@Test
void call_globalCallMeLongToDoubleFunction_noThrow() {
double result = MySwiftLibrary.globalCallMeLongToDoubleFunction((long a) -> { return (double) a; });
assertEquals(1.0, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ public func globalCallMeIntToLongFunction(run: (Int32) -> Int64) -> Int64 {
run(1)
}

public func globalCallMeIntToDoubleFunction(run: (Int32) -> Double) -> Double {
run(1)
}

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

public func closureMultipleArguments(
input1: Int64,
input2: Int64,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,16 @@ void globalCallMeIntToLongFunction() {
long result = MySwiftLibrary.globalCallMeIntToLongFunction((int a) -> { return (long) a; });
assertEquals(1L, result);
}

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

@Test
void globalCallMeLongToDoubleFunction() {
double result = MySwiftLibrary.globalCallMeLongToDoubleFunction((long a) -> { return (double) a; });
assertEquals(1.0, result);
}
}
8 changes: 8 additions & 0 deletions Sources/ExampleSwiftLibrary/MySwiftLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ public func globalCallMeIntToLongFunction(run: (Int32) -> Int64) -> Int64 {
run(1)
}

public func globalCallMeIntToDoubleFunction(run: (Int32) -> Double) -> Double {
run(1)
}

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

public func globalReceiveRawBuffer(buf: UnsafeRawBufferPointer) -> Int {
buf.count
}
Expand Down
10 changes: 10 additions & 0 deletions Sources/JExtractSwiftLib/JavaTypes/JavaType+JDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ extension JavaType {
.class(package: "java.util.function", name: "IntToLongFunction")
}

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

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

/// The description of the type java.lang.Class.
static var javaLangClass: JavaType {
.class(package: "java.lang", name: "Class")
Expand Down
29 changes: 29 additions & 0 deletions Sources/JExtractSwiftLib/KnownFunctionalInterfaces.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ struct KnownJavaFunctionalInterface: Sendable {
result: .long
)

static let intToDoubleFunction = KnownJavaFunctionalInterface(
JavaType.javaUtilFunctionIntToDoubleFunction,
method: "applyAsDouble",
parameters: [.int],
result: .double
)

static let longToDoubleFunction = KnownJavaFunctionalInterface(
JavaType.javaUtilFunctionLongToDoubleFunction,
method: "applyAsDouble",
parameters: [.long],
result: .double
)

static let all: [KnownJavaFunctionalInterface] = [
.runnable,
.booleanSupplier,
Expand All @@ -191,6 +205,8 @@ struct KnownJavaFunctionalInterface: Sendable {
.longToIntFunction,
.doubleToLongFunction,
.intToLongFunction,
.intToDoubleFunction,
.longToDoubleFunction,
]

static func find(parameters: [JavaType], result: JavaType) -> KnownJavaFunctionalInterface? {
Expand Down Expand Up @@ -306,6 +322,19 @@ struct KnownJavaFunctionalInterface: Sendable {
}
}

// To double functions
if parameters.count == 1 && result.isDouble {
let parameter = parameters[0].type
return switch () {
case _ where parameter.isInt32:
intToDoubleFunction
case _ where parameter.isInt64:
longToDoubleFunction
default:
nil
}
}

// Binary operators
if parameters.count == 2 && parameters[0].type == result && parameters[1].type == result {
let parameter = parameters[0].type
Expand Down
89 changes: 89 additions & 0 deletions Tests/JExtractSwiftTests/FuncCallbackImportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ final class FuncCallbackImportTests {
public func callMeDoubleToLongFunction(callback: (Double) -> Int64)
public func callMeIntToLongFunction(callback: (Int32) -> Int64)

public func callMeIntToDoubleFunction(callback: (Int32) -> Double)
public func callMeLongToDoubleFunction(callback: (Int64) -> Double)

public func callMeIntUnaryOperator(callback: (Int32) -> Int32)
public func callMeLongUnaryOperator(callback: (Int64) -> Int64)
public func callMeDoubleUnaryOperator(callback: (Double) -> Double)
Expand Down Expand Up @@ -1032,6 +1035,92 @@ final class FuncCallbackImportTests {
)
}

@Test("Import: public func callMeIntToDoubleFunction(callback: (Int32) -> Double)")
func func_callMecallMeIntToDoubleFunctionFunc_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 == "callMeIntToDoubleFunction" }!

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

@Test("Import: public func callMeLongToDoubleFunction(callback: (Int64) -> Double)")
func func_callMecallMeLongToDoubleFunctionFunc_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 == "callMeLongToDoubleFunction" }!

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

@Test("Import: public func callMecallMeIntBinaryOperatorFunc(callback: (Int32, Int32) -> Int32)")
func func_callMecallMeIntBinaryOperatorFunc_callback() throws {
var config = Configuration()
Expand Down
Loading
Loading