| 1 |
// A handful of classes and functions to support the generated data structures. |
| 2 |
// This would be a good candidate for isolating in its own ffi-support lib. |
| 3 |
|
| 4 |
internal const val UNIFFI_CALL_SUCCESS = 0.toByte() |
| 5 |
internal const val UNIFFI_CALL_ERROR = 1.toByte() |
| 6 |
internal const val UNIFFI_CALL_UNEXPECTED_ERROR = 2.toByte() |
| 7 |
|
| 8 |
@Structure.FieldOrder("code", "error_buf") |
| 9 |
internal open class UniffiRustCallStatus : Structure() { |
| 10 |
@JvmField var code: Byte = 0 |
| 11 |
@JvmField var error_buf: RustBuffer.ByValue = RustBuffer.ByValue() |
| 12 |
|
| 13 |
class ByValue: UniffiRustCallStatus(), Structure.ByValue |
| 14 |
|
| 15 |
fun isSuccess(): Boolean { |
| 16 |
return code == UNIFFI_CALL_SUCCESS |
| 17 |
} |
| 18 |
|
| 19 |
fun isError(): Boolean { |
| 20 |
return code == UNIFFI_CALL_ERROR |
| 21 |
} |
| 22 |
|
| 23 |
fun isPanic(): Boolean { |
| 24 |
return code == UNIFFI_CALL_UNEXPECTED_ERROR |
| 25 |
} |
| 26 |
|
| 27 |
companion object { |
| 28 |
fun create(code: Byte, errorBuf: RustBuffer.ByValue): UniffiRustCallStatus.ByValue { |
| 29 |
val callStatus = UniffiRustCallStatus.ByValue() |
| 30 |
callStatus.code = code |
| 31 |
callStatus.error_buf = errorBuf |
| 32 |
return callStatus |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
class InternalException(message: String) : kotlin.Exception(message) |
| 38 |
|
| 39 |
/** |
| 40 |
* Each top-level error class has a companion object that can lift the error from the call status's rust buffer |
| 41 |
* |
| 42 |
* @suppress |
| 43 |
*/ |
| 44 |
interface UniffiRustCallStatusErrorHandler<E> { |
| 45 |
fun lift(error_buf: RustBuffer.ByValue): E; |
| 46 |
} |
| 47 |
|
| 48 |
// Helpers for calling Rust |
| 49 |
// In practice we usually need to be synchronized to call this safely, so it doesn't |
| 50 |
// synchronize itself |
| 51 |
|
| 52 |
// Call a rust function that returns a Result<>. Pass in the Error class companion that corresponds to the Err |
| 53 |
private inline fun <U, E: kotlin.Exception> uniffiRustCallWithError(errorHandler: UniffiRustCallStatusErrorHandler<E>, callback: (UniffiRustCallStatus) -> U): U { |
| 54 |
var status = UniffiRustCallStatus() |
| 55 |
val return_value = callback(status) |
| 56 |
uniffiCheckCallStatus(errorHandler, status) |
| 57 |
return return_value |
| 58 |
} |
| 59 |
|
| 60 |
// Check UniffiRustCallStatus and throw an error if the call wasn't successful |
| 61 |
private fun<E: kotlin.Exception> uniffiCheckCallStatus(errorHandler: UniffiRustCallStatusErrorHandler<E>, status: UniffiRustCallStatus) { |
| 62 |
if (status.isSuccess()) { |
| 63 |
return |
| 64 |
} else if (status.isError()) { |
| 65 |
throw errorHandler.lift(status.error_buf) |
| 66 |
} else if (status.isPanic()) { |
| 67 |
// when the rust code sees a panic, it tries to construct a rustbuffer |
| 68 |
// with the message. but if that code panics, then it just sends back |
| 69 |
// an empty buffer. |
| 70 |
if (status.error_buf.len > 0) { |
| 71 |
throw InternalException({{ Type::String.borrow()|lift_fn }}(status.error_buf)) |
| 72 |
} else { |
| 73 |
throw InternalException("Rust panic") |
| 74 |
} |
| 75 |
} else { |
| 76 |
throw InternalException("Unknown rust call status: $status.code") |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* UniffiRustCallStatusErrorHandler implementation for times when we don't expect a CALL_ERROR |
| 82 |
* |
| 83 |
* @suppress |
| 84 |
*/ |
| 85 |
object UniffiNullRustCallStatusErrorHandler: UniffiRustCallStatusErrorHandler<InternalException> { |
| 86 |
override fun lift(error_buf: RustBuffer.ByValue): InternalException { |
| 87 |
RustBuffer.free(error_buf) |
| 88 |
return InternalException("Unexpected CALL_ERROR") |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// Call a rust function that returns a plain value |
| 93 |
private inline fun <U> uniffiRustCall(callback: (UniffiRustCallStatus) -> U): U { |
| 94 |
return uniffiRustCallWithError(UniffiNullRustCallStatusErrorHandler, callback) |
| 95 |
} |
| 96 |
|
| 97 |
internal inline fun<T> uniffiTraitInterfaceCall( |
| 98 |
callStatus: UniffiRustCallStatus, |
| 99 |
makeCall: () -> T, |
| 100 |
writeReturn: (T) -> Unit, |
| 101 |
) { |
| 102 |
try { |
| 103 |
writeReturn(makeCall()) |
| 104 |
} catch(e: kotlin.Exception) { |
| 105 |
callStatus.code = UNIFFI_CALL_UNEXPECTED_ERROR |
| 106 |
callStatus.error_buf = {{ Type::String.borrow()|lower_fn }}(e.toString()) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
internal inline fun<T, reified E: Throwable> uniffiTraitInterfaceCallWithError( |
| 111 |
callStatus: UniffiRustCallStatus, |
| 112 |
makeCall: () -> T, |
| 113 |
writeReturn: (T) -> Unit, |
| 114 |
lowerError: (E) -> RustBuffer.ByValue |
| 115 |
) { |
| 116 |
try { |
| 117 |
writeReturn(makeCall()) |
| 118 |
} catch(e: kotlin.Exception) { |
| 119 |
if (e is E) { |
| 120 |
callStatus.code = UNIFFI_CALL_ERROR |
| 121 |
callStatus.error_buf = lowerError(e) |
| 122 |
} else { |
| 123 |
callStatus.code = UNIFFI_CALL_UNEXPECTED_ERROR |
| 124 |
callStatus.error_buf = {{ Type::String.borrow()|lower_fn }}(e.toString()) |
| 125 |
} |
| 126 |
} |
| 127 |
} |