1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/// Helper to perform the try operation over [`VmResult`].
///
/// This can be used through [`rune::function`] by enabling the `vm_result`
/// option and suffixing an expression with `<expr>.vm?`.
///
/// [`rune::function`]: crate::function
/// [`VmResult`]: crate::runtime::VmResult
#[macro_export]
macro_rules! vm_try {
    ($expr:expr) => {
        match $crate::runtime::try_result($expr) {
            $crate::runtime::VmResult::Ok(value) => value,
            $crate::runtime::VmResult::Err(err) => {
                return $crate::runtime::VmResult::Err($crate::runtime::VmError::from(err));
            }
        }
    };
}

/// Helper to cause a panic.
///
/// This simply returns a [`VmResult`], but the macro is provided to play nicely
/// with [`rune::function`], since a regular return would otherwise be
/// transformed.
///
/// [`rune::function`]: crate::function
/// [`VmResult`]: crate::runtime::VmResult
///
/// # Examples
///
/// ```
/// use rune::vm_panic;
///
/// #[rune::function(vm_result)]
/// fn hello(panic: bool) {
///     if panic {
///        vm_panic!("I was told to panic");
///     }
/// }
/// ```
#[macro_export]
macro_rules! vm_panic {
    ($expr:expr) => {{
        return $crate::runtime::VmResult::panic($expr);
    }};
}

/// Helper macro to perform a `write!` in a context which errors with
/// [`VmResult`] and returns `VmResult<Result<_, E>>` on write errors.
///
/// [`VmResult`]: crate::runtime::VmResult
#[macro_export]
macro_rules! vm_write {
    ($($tt:tt)*) => {
        match core::write!($($tt)*) {
            Ok(()) => (),
            Err(err) => {
                return $crate::runtime::VmResult::Err($crate::runtime::VmError::from(err));
            }
        }
    };
}