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 @@ -108,6 +108,14 @@ public func globalCallMeLongToIntFunction(run: (Int64) -> Int32) -> Int32 {
run(1)
}

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

public func globalCallMeIntToLongFunction(run: (Int32) -> Int64) -> Int64 {
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 @@ -158,4 +158,16 @@ void call_globalCallMeLongToIntFunction_noThrow() {
int result = MySwiftLibrary.globalCallMeLongToIntFunction((long a) -> { return (int) a; });
assertEquals(1, result);
}

@Test
void call_globalCallMeDoubleToLongFunction_noThrow() {
long result = MySwiftLibrary.globalCallMeDoubleToLongFunction((double a) -> { return (long) a; });
assertEquals(1L, result);
}

@Test
void call_globalCallMeIntToLongFunction_noThrow() {
long result = MySwiftLibrary.globalCallMeIntToLongFunction((int a) -> { return (long) a; });
assertEquals(1L, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ public func globalCallMeLongToIntFunction(run: (Int64) -> Int32) -> Int32 {
run(1)
}

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

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

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

@Test
void call_globalCallMeDoubleToLongFunction_noThrow() {
long result = MySwiftLibrary.globalCallMeDoubleToLongFunction((double a) -> { return (long) a; });
assertEquals(1L, result);
}

@Test
void call_globalCallMeIntToLongFunction_noThrow() {
long result = MySwiftLibrary.globalCallMeIntToLongFunction((int a) -> { return (long) a; });
assertEquals(1L, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ public func globalCallMeLongToIntFunction(run: (Int64) -> Int32) -> Int32 {
run(1)
}

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

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

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

@Test
void globalCallMeDoubleToLongFunction() {
long result = MySwiftLibrary.globalCallMeDoubleToLongFunction((double a) -> { return (long) a; });
assertEquals(1L, result);
}

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

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

public func globalCallMeIntToLongFunction(run: (Int32) -> Int64) -> Int64 {
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 @@ -120,6 +120,16 @@ extension JavaType {
.class(package: "java.util.function", name: "LongToIntFunction")
}

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

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

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

static let doubleToLongFunction = KnownJavaFunctionalInterface(
JavaType.javaUtilFunctionDoubleToLongFunction,
method: "applyAsLong",
parameters: [.double],
result: .long
)

static let intToLongFunction = KnownJavaFunctionalInterface(
JavaType.javaUtilFunctionIntToLongFunction,
method: "applyAsLong",
parameters: [.int],
result: .long
)

static let all: [KnownJavaFunctionalInterface] = [
.runnable,
.booleanSupplier,
Expand All @@ -175,6 +189,8 @@ struct KnownJavaFunctionalInterface: Sendable {
.doubleBinaryOperator,
.doubleToIntFunction,
.longToIntFunction,
.doubleToLongFunction,
.intToLongFunction,
]

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

// To long functions
if parameters.count == 1 && result.isInt64 {
let parameter = parameters[0].type
return switch () {
case _ where parameter.isInt32:
intToLongFunction
case _ where parameter.isDouble:
doubleToLongFunction
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 @@ -50,6 +50,9 @@ final class FuncCallbackImportTests {
public func callMeDoubleToIntFunction(callback: (Double) -> Int32)
public func callMeLongToIntFunction(callback: (Int64) -> Int32)

public func callMeDoubleToLongFunction(callback: (Double) -> Int64)
public func callMeIntToLongFunction(callback: (Int32) -> Int64)

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

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

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

@Test("Import: public func callMeLongToIntFunction(callback: (Int32) -> Int64)")
func func_callMecallMeIntToLongFunctionFunc_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 == "callMeIntToLongFunction" }!

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

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