Skip to main content

sim_lib_control/
protected.rs

1use sim_kernel::{Args, Cx, Result, Value};
2
3/// Result of invoking a callable through a protected boundary.
4///
5/// Protected calls turn ordinary kernel call failures into language-neutral
6/// raised values supplied by the caller's error mapper.
7#[derive(Clone, Debug)]
8pub enum ProtectedOutcome {
9    /// The callable returned normally.
10    Returned(Vec<Value>),
11    /// The callable raised a mapped error value.
12    Raised(Value),
13}
14
15/// Calls `function` and maps kernel errors into a returned protected outcome.
16///
17/// The kernel callable surface returns one value per call. The protected result
18/// stores successful values in a vector so language layers with multi-value
19/// returns can reuse the same outcome type at their boundary.
20pub fn protected_call(
21    cx: &mut Cx,
22    function: Value,
23    args: Args,
24    map_error: impl FnOnce(&mut Cx, sim_kernel::Error) -> Result<Value>,
25) -> Result<ProtectedOutcome> {
26    match cx.call_value(function, args) {
27        Ok(value) => Ok(ProtectedOutcome::Returned(vec![value])),
28        Err(error) => Ok(ProtectedOutcome::Raised(map_error(cx, error)?)),
29    }
30}