Skip to content

Commit 57e8ce6

Browse files
committed
fix throwing functions
1 parent a6db058 commit 57e8ce6

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/CallbackProtcol.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public protocol CallbackProtocol {
3131
func withInt64Array(_ input: [Int64]) -> [Int64]
3232
func withStringArray(_ input: [String]) -> [String]
3333
func withObjectArray(_ input: [MySwiftClass]) -> [MySwiftClass]
34+
func successfulThrowingFunction() throws
35+
func throwingFunction() throws
3436
}
3537

3638
public struct CallbackOutput {
@@ -51,6 +53,18 @@ public struct CallbackOutput {
5153
public let objectArray: [MySwiftClass]
5254
}
5355

56+
public func callProtocolVoid(_ callbacks: some CallbackProtocol) {
57+
callbacks.withVoid();
58+
}
59+
60+
public func callProtocolWithFailedThrowingFunction(_ callbacks: some CallbackProtocol) throws {
61+
try callbacks.throwingFunction();
62+
}
63+
64+
public func callProtocolWithSuccessfulThrowingFunction(_ callbacks: some CallbackProtocol) throws {
65+
try callbacks.successfulThrowingFunction();
66+
}
67+
5468
public func outputCallbacks(
5569
_ callbacks: some CallbackProtocol,
5670
bool: Bool,

Samples/SwiftJavaExtractJNISampleApp/src/test/java/com/example/swift/ProtocolCallbacksTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,35 @@ public String[] withStringArray(String[] input) {
102102
public MySwiftClass[] withObjectArray(MySwiftClass[] input, SwiftArena swiftArena$) {
103103
return input;
104104
}
105+
106+
@Override
107+
public void throwingFunction() throws Exception {
108+
throw new Exception("Failed in Java");
109+
}
110+
111+
@Override
112+
public void successfulThrowingFunction() throws Exception {
113+
114+
}
115+
}
116+
117+
@Test
118+
void voidTest() {
119+
JavaCallbacks callbacks = new JavaCallbacks();
120+
MySwiftLibrary.callProtocolVoid(callbacks);
121+
}
122+
123+
@Test
124+
void throwingFunction_thatDoesNotThrow() {
125+
JavaCallbacks callbacks = new JavaCallbacks();
126+
assertDoesNotThrow(() -> MySwiftLibrary.callProtocolWithSuccessfulThrowingFunction(callbacks));
127+
}
128+
129+
@Test
130+
void throwingFunction_thatThrows() {
131+
JavaCallbacks callbacks = new JavaCallbacks();
132+
Exception exception = assertThrows(Exception.class, () -> MySwiftLibrary.callProtocolWithFailedThrowingFunction(callbacks));
133+
assertEquals("Failed in Java", exception.getMessage());
105134
}
106135

107136
@Test

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ extension JNISwift2JavaGenerator {
153153
conversion.render(&printer, param.parameterName!)
154154
}
155155

156-
let javaUpcall = "\(wrapper.javaInterfaceVariableName).\(function.swiftFunctionName)(\(upcallArguments.joined(separator: ", ")))"
156+
let tryClause = function.originalFunctionSignature.isThrowing ? "try " : ""
157+
let javaUpcall = "\(tryClause)\(wrapper.javaInterfaceVariableName).\(function.swiftFunctionName)(\(upcallArguments.joined(separator: ", ")))"
157158

158159
let resultType = function.originalFunctionSignature.result.type
159160
let result = function.resultConversion.render(&printer, javaUpcall)

0 commit comments

Comments
 (0)