Skip to content

[JExtract] Import any C compatible closures #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025
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 @@ -43,9 +43,9 @@ void call_globalTakeInt() {
void call_globalCallMeRunnable() {
CountDownLatch countDownLatch = new CountDownLatch(3);

MySwiftLibrary.globalCallMeRunnable(new Runnable() {
MySwiftLibrary.globalCallMeRunnable(new MySwiftLibrary.globalCallMeRunnable.run() {
@Override
public void run() {
public void apply() {
countDownLatch.countDown();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public struct MySwiftStruct {
self.len
}

public func withCapLen(_ body: (Int, Int) -> Void) {
body(cap, len)
}

public mutating func increaseCap(by value: Int) -> Int {
precondition(value > 0)
self.cap += value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ static void examples() {

MySwiftStruct swiftValue = MySwiftStruct.init(2222, 1111, arena);
SwiftKit.trace("swiftValue.capacity = " + swiftValue.getCapacity());
swiftValue.withCapLen((cap, len) -> {
SwiftKit.trace("withCapLenCallback: cap=" + cap + ", len=" + len);
});
}

System.out.println("DONE.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ void call_writeString_jni() {
void call_globalCallMeRunnable() {
CountDownLatch countDownLatch = new CountDownLatch(3);

MySwiftLibrary.globalCallMeRunnable(new Runnable() {
MySwiftLibrary.globalCallMeRunnable(new MySwiftLibrary.globalCallMeRunnable.run() {
@Override
public void run() {
public void apply() {
countDownLatch.countDown();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ struct CdeclLowering {
)
}

/// Lower a Swift function parameter type to cdecl parameters.
/// Lower a Swift function parameter to cdecl parameters.
///
/// For example, Swift parameter `arg value: inout Int` can be lowered with
/// `lowerParameter(intTy, .inout, "value")`.
Expand Down Expand Up @@ -276,25 +276,63 @@ struct CdeclLowering {
}
return LoweredParameter(cdeclParameters: parameters, conversion: .tuplify(conversions))

case .function(let fn) where fn.parameters.isEmpty && fn.resultType.isVoid:
case .function(let fn):
let (loweredTy, conversion) = try lowerFunctionType(fn)
return LoweredParameter(
cdeclParameters: [
SwiftParameter(
convention: .byValue,
parameterName: parameterName,
type: .function(SwiftFunctionType(convention: .c, parameters: [], resultType: fn.resultType))
type: loweredTy
)
],
// '@convention(c) () -> ()' is compatible with '() -> Void'.
conversion: .placeholder
conversion: conversion
)

case .function, .optional:
// FIXME: Support other function types than '() -> Void'.
case .optional:
throw LoweringError.unhandledType(type)
}
}

/// Lower a Swift function type (i.e. closure) to cdecl function type.
///
/// - Parameters:
/// - fn: the Swift function type to lower.
func lowerFunctionType(
_ fn: SwiftFunctionType
) throws -> (type: SwiftType, conversion: ConversionStep) {
var parameters: [SwiftParameter] = []
var parameterConversions: [ConversionStep] = []

for parameter in fn.parameters {
if let _ = try? CType(cdeclType: parameter.type) {
parameters.append(SwiftParameter(convention: .byValue, type: parameter.type))
parameterConversions.append(.placeholder)
} else {
// Non-trivial types are not yet supported.
throw LoweringError.unhandledType(.function(fn))
}
}

let resultType: SwiftType
let resultConversion: ConversionStep
if let _ = try? CType(cdeclType: fn.resultType) {
resultType = fn.resultType
resultConversion = .placeholder
} else {
// Non-trivial types are not yet supported.
throw LoweringError.unhandledType(.function(fn))
}

// Ignore the conversions for now, since we don't support non-trivial types yet.
_ = (parameterConversions, resultConversion)

return (
type: .function(SwiftFunctionType(convention: .c, parameters: parameters, resultType: resultType)),
conversion: .placeholder
)
}

/// Lower a Swift result type to cdecl out parameters and return type.
///
/// - Parameters:
Expand Down
Loading