Skip to content

Commit

Permalink
Fix Kotlin error handling in async functions (#1614)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhammond committed Jun 21, 2023
1 parent 01e700f commit 68107e3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
10 changes: 10 additions & 0 deletions fixtures/futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ pub async fn fallible_me(do_fail: bool) -> Result<u8, MyError> {
}
}

// An async function returning a struct that can throw.
#[uniffi::export]
pub async fn fallible_struct(do_fail: bool) -> Result<Arc<Megaphone>, MyError> {
if do_fail {
Err(MyError::Foo)
} else {
Ok(new_megaphone())
}
}

/// Sync function that generates a new `Megaphone`.
///
/// It builds a `Megaphone` which has async methods on it.
Expand Down
8 changes: 8 additions & 0 deletions fixtures/futures/tests/bindings/test_futures.kts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ runBlocking {

print("fallible method (with exception): ${time4}ms")
assert(time4 < 100)

fallibleStruct(false)
try {
fallibleStruct(true)
assert(false) // should never be reached
} catch (exception: MyException) {
assert(true)
}
println(" ... ok")
}

Expand Down
15 changes: 14 additions & 1 deletion fixtures/futures/tests/bindings/test_futures.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from uniffi_futures import always_ready, void, sleep, say_after, new_megaphone, say_after_with_tokio, fallible_me, MyError, MyRecord, new_my_record
from uniffi_futures import always_ready, void, sleep, say_after, new_megaphone, say_after_with_tokio, fallible_me, fallible_struct, MyError, MyRecord, new_my_record
import unittest
from datetime import datetime
import asyncio
Expand Down Expand Up @@ -110,6 +110,19 @@ async def test():

asyncio.run(test())

def test_fallible_struct(self):
async def test():
megaphone = await fallible_struct(False)
self.assertEqual(await megaphone.fallible_me(False), 42)

try:
await fallible_struct(True)
self.assertTrue(False) # should never be reached
except MyError as exception:
pass

asyncio.run(test())

def test_record(self):
async def test():
result = await new_my_record("foo", 42)
Expand Down
16 changes: 16 additions & 0 deletions fixtures/futures/tests/bindings/test_futures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ Task {
counter.leave()
}

Task {
let m = try await fallibleStruct(doFail: false)
let result = try await m.fallibleMe(doFail: false)
assert(result == 42)
}

counter.enter()

Task {
Expand Down Expand Up @@ -193,6 +199,16 @@ Task {
counter.leave()
}

Task {
do {
let _ = try await fallibleStruct(doFail: true)
} catch MyError.Foo {
assert(true)
} catch {
assert(false)
}
}

counter.enter()

Task {
Expand Down
6 changes: 3 additions & 3 deletions uniffi_bindgen/src/bindings/kotlin/templates/AsyncTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
internal interface UniFfiFutureCallback{{ callback_param|ffi_type_name }} : com.sun.jna.Callback {
// Note: callbackData is always 0. We could pass Rust a pointer/usize to represent the
// continuation, but with JNA it's easier to just store it in the callback handler.
fun invoke(_callbackData: USize, returnValue: {{ callback_param|ffi_type_name_by_value }}, callStatus: RustCallStatus.ByValue);
fun invoke(_callbackData: USize, returnValue: {{ callback_param|ffi_type_name_by_value }}?, callStatus: RustCallStatus.ByValue);
}
{%- endfor %}

Expand All @@ -26,12 +26,12 @@ internal interface UniFfiFutureCallback{{ callback_param|ffi_type_name }} : com.

internal class {{ result_type|future_callback_handler }}(val continuation: {{ result_type|future_continuation_type }})
: UniFfiFutureCallback{{ callback_param|ffi_type_name }} {
override fun invoke(_callbackData: USize, returnValue: {{ callback_param|ffi_type_name_by_value }}, callStatus: RustCallStatus.ByValue) {
override fun invoke(_callbackData: USize, returnValue: {{ callback_param|ffi_type_name_by_value }}?, callStatus: RustCallStatus.ByValue) {
try {
checkCallStatus({{ result_type|error_handler }}, callStatus)
{%- match result_type.return_type %}
{%- when Some(return_type) %}
continuation.resume({{ return_type|lift_fn }}(returnValue))
continuation.resume({{ return_type|lift_fn }}(returnValue!!))
{%- when None %}
continuation.resume(Unit)
{%- endmatch %}
Expand Down

0 comments on commit 68107e3

Please sign in to comment.