Skip to content

Commit 75fd864

Browse files
authored
jextract/ffm: Support throwing funcs in FFM mode (#658)
* jextract/ffm: support throwing functions in ffm * script to jextract parts of SwiftKit, like SwiftJavaError * jextract/ffm: initial SwiftJavaError support in FFM mode Fix misleading Java output path in regeneration script The script echoed ${TYPE}.java but the generator may remap the Java class name (e.g. SwiftJavaError -> SwiftJavaErrorException). Print the output directory instead of guessing the filename. cleanups * re-run the ./scripts/swiftkit-ffm-generate-bindings.sh * fix formatting and license
1 parent aee3122 commit 75fd864

30 files changed

Lines changed: 1431 additions & 296 deletions

Samples/SwiftJavaExtractFFMSampleApp/Sources/MySwiftLibrary/MySwiftLibrary.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,33 @@ public func globalStringIdentity(string: String) -> String {
125125
string
126126
}
127127

128+
// ==== -----------------------------------------------------------------------
129+
// MARK: Throwing functions
130+
131+
public struct SwiftExampleError: Error {
132+
public let message: String
133+
}
134+
135+
public func globalThrowingVoid(doThrow: Bool) throws {
136+
if doThrow {
137+
throw SwiftExampleError(message: "expected error in globalThrowingVoid")
138+
}
139+
}
140+
141+
public func globalThrowingReturn(doThrow: Bool) throws -> Int {
142+
if doThrow {
143+
throw SwiftExampleError(message: "expected error in globalThrowingReturn")
144+
}
145+
return 42
146+
}
147+
148+
public func globalThrowingString(doThrow: Bool) throws -> String {
149+
if doThrow {
150+
throw SwiftExampleError(message: "expected error in globalThrowingString")
151+
}
152+
return "Hello from throwing Swift!"
153+
}
154+
128155
// ==== -----------------------------------------------------------------------
129156
// MARK: Overloaded functions
130157

Samples/SwiftJavaExtractFFMSampleApp/src/test/java/com/example/swift/MySwiftLibraryTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import org.junit.jupiter.api.Disabled;
1818
import org.junit.jupiter.api.Test;
19+
import org.swift.swiftkit.ffm.generated.SwiftJavaErrorException;
1920

2021
import java.util.concurrent.CountDownLatch;
2122

@@ -95,4 +96,56 @@ public void apply() {
9596
assertEquals(0, countDownLatch.getCount());
9697
}
9798

99+
// ==== ----------------------------------------------------------------
100+
// Throwing functions
101+
102+
@Test
103+
void call_globalThrowingVoid_noThrow() throws SwiftJavaErrorException {
104+
MySwiftLibrary.globalThrowingVoid(false);
105+
}
106+
107+
@Test
108+
void call_globalThrowingVoid_throws() {
109+
assertThrows(SwiftJavaErrorException.class, () -> {
110+
MySwiftLibrary.globalThrowingVoid(true);
111+
});
112+
}
113+
114+
@Test
115+
void call_globalThrowingReturn_noThrow() throws SwiftJavaErrorException {
116+
long result = MySwiftLibrary.globalThrowingReturn(false);
117+
assertEquals(42, result);
118+
}
119+
120+
@Test
121+
void call_globalThrowingReturn_throws() {
122+
assertThrows(SwiftJavaErrorException.class, () -> {
123+
MySwiftLibrary.globalThrowingReturn(true);
124+
});
125+
}
126+
127+
@Test
128+
void call_globalThrowingString_noThrow() throws SwiftJavaErrorException {
129+
String result = MySwiftLibrary.globalThrowingString(false);
130+
assertEquals("Hello from throwing Swift!", result);
131+
}
132+
133+
@Test
134+
void call_globalThrowingString_throws() {
135+
assertThrows(SwiftJavaErrorException.class, () -> {
136+
MySwiftLibrary.globalThrowingString(true);
137+
});
138+
}
139+
140+
@Test
141+
void call_globalThrowingString_throws_checkMessage() {
142+
SwiftJavaErrorException error = assertThrows(SwiftJavaErrorException.class, () -> {
143+
MySwiftLibrary.globalThrowingString(true);
144+
});
145+
assertEquals(
146+
"org.swift.swiftkit.ffm.generated.SwiftJavaErrorException: SwiftExampleError(message: \"expected error in globalThrowingString\")",
147+
error.toString()
148+
);
149+
}
150+
98151
}

Sources/CodePrinting/CodePrinter.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ public struct CodePrinter {
107107
print("}", .sloc, function: function, file: file, line: line)
108108
}
109109

110+
public mutating func printIfBlock(
111+
_ condition: Any,
112+
function: String = #function,
113+
file: String = #fileID,
114+
line: UInt = #line,
115+
body: (inout CodePrinter) throws -> Void
116+
) rethrows {
117+
try printBraceBlock("if (\(condition))", function: function, file: file, line: line, body: body)
118+
}
119+
110120
public mutating func printParts(
111121
_ parts: String...,
112122
terminator: PrinterTerminator = .newLine,

0 commit comments

Comments
 (0)