1
//! Raising a catchable `$nomi_error` condition from a host fn.
2

            
3
use std::convert::Infallible;
4

            
5
use wasmtime::{AnyRef, AsContextMut, Caller, ExnRef, ExnRefPre, ExnType, Rooted, Val};
6

            
7
use super::abi::alloc_string_ref;
8
use super::decode::EvalValue;
9
use super::encode::alloc_value;
10

            
11
/// Throws a `$nomi_error` carrying `code` (upper-cased, as `handler-case` compares it),
12
/// `message` and the structured `detail`, so the calling script's `handler-case` can catch it
13
/// and read the detail with `error-detail`. Return the result from the host fn:
14
/// `return Err(throw_condition(&mut caller, code, message, &detail).await)`. Any failure while
15
/// building the condition is returned in its place.
16
10814
pub async fn throw_condition<T: Send>(
17
10814
    caller: &mut Caller<'_, T>,
18
10814
    code: &str,
19
10814
    message: &str,
20
10814
    detail: &EvalValue,
21
10814
) -> wasmtime::Error {
22
10814
    let exn = match build_exception(caller, &code.to_uppercase(), message, detail).await {
23
10809
        Ok(exn) => exn,
24
5
        Err(err) => return err,
25
    };
26
10809
    match caller.as_context_mut().throw::<Infallible>(exn) {
27
10809
        Err(thrown) => thrown,
28
    }
29
10814
}
30

            
31
10814
async fn build_exception<T: Send>(
32
10814
    caller: &mut Caller<'_, T>,
33
10814
    code: &str,
34
10814
    message: &str,
35
10814
    detail: &EvalValue,
36
10814
) -> wasmtime::Result<Rooted<ExnRef>> {
37
10814
    let tag = caller
38
10814
        .get_export("nomi_error")
39
10814
        .and_then(wasmtime::Extern::into_tag)
40
2
        .ok_or_else(|| wasmtime::Error::msg("module does not export `nomi_error`"))?;
41
10812
    let condition = condition_new(caller, code, message, detail).await?;
42
10809
    let tag_type = tag.ty(&*caller);
43
10809
    let pre = ExnRefPre::new(caller.as_context_mut(), ExnType::from_tag_type(&tag_type)?);
44
10809
    ExnRef::new_async(
45
10809
        caller.as_context_mut(),
46
10809
        &pre,
47
10809
        &tag,
48
10809
        &[Val::AnyRef(Some(condition))],
49
10809
    )
50
10809
    .await
51
10814
}
52

            
53
10812
async fn condition_new<T: Send>(
54
10812
    caller: &mut Caller<'_, T>,
55
10812
    code: &str,
56
10812
    message: &str,
57
10812
    detail: &EvalValue,
58
10812
) -> wasmtime::Result<Rooted<AnyRef>> {
59
10812
    let constructor = caller
60
10812
        .get_export("condition_new")
61
10812
        .and_then(wasmtime::Extern::into_func)
62
1
        .ok_or_else(|| wasmtime::Error::msg("module does not export `condition_new`"))?;
63
10811
    let code = alloc_string_ref(caller, code.as_bytes())?;
64
10811
    let message = alloc_string_ref(caller, message.as_bytes())?;
65
10811
    let detail = alloc_value(caller, detail).await?;
66
10810
    let mut results = [Val::AnyRef(None)];
67
10810
    constructor
68
10810
        .call_async(
69
10810
            caller.as_context_mut(),
70
10810
            &[
71
10810
                Val::AnyRef(Some(code.to_anyref())),
72
10810
                Val::AnyRef(Some(message.to_anyref())),
73
10810
                Val::AnyRef(detail),
74
10810
            ],
75
10810
            &mut results,
76
10810
        )
77
10810
        .await?;
78
10810
    match results {
79
10809
        [Val::AnyRef(Some(condition))] => Ok(condition),
80
1
        _ => Err(wasmtime::Error::msg("condition_new returned no condition")),
81
    }
82
10812
}