rune/runtime/
vm_halt.rs

1use core::fmt;
2
3use crate::runtime::{Awaited, InstAddress, Output, VmCall};
4
5/// The reason why the virtual machine execution stopped.
6#[derive(Debug)]
7pub(crate) enum VmHalt {
8    /// The virtual machine exited by running out of call frames, returning the given value.
9    Exited(Option<InstAddress>),
10    /// The virtual machine exited because it ran out of execution quota.
11    Limited,
12    /// The virtual machine yielded.
13    Yielded(Option<InstAddress>, Output),
14    /// The virtual machine awaited on the given future.
15    Awaited(Awaited),
16    /// Call into a new virtual machine.
17    VmCall(VmCall),
18}
19
20impl VmHalt {
21    /// Convert into cheap info enum which only described the reason.
22    pub(crate) fn into_info(self) -> VmHaltInfo {
23        match self {
24            Self::Exited(..) => VmHaltInfo::Exited,
25            Self::Limited => VmHaltInfo::Limited,
26            Self::Yielded(..) => VmHaltInfo::Yielded,
27            Self::Awaited(..) => VmHaltInfo::Awaited,
28            Self::VmCall(..) => VmHaltInfo::VmCall,
29        }
30    }
31}
32
33/// The reason why the virtual machine execution stopped.
34#[derive(Debug, Clone, Copy, PartialEq)]
35pub(crate) enum VmHaltInfo {
36    /// The virtual machine exited by running out of call frames.
37    Exited,
38    /// The virtual machine exited because it ran out of execution quota.
39    Limited,
40    /// The virtual machine yielded.
41    Yielded,
42    /// The virtual machine awaited on the given future.
43    Awaited,
44    /// Received instruction to push the inner virtual machine.
45    VmCall,
46}
47
48impl fmt::Display for VmHaltInfo {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        match self {
51            Self::Exited => write!(f, "exited"),
52            Self::Limited => write!(f, "limited"),
53            Self::Yielded => write!(f, "yielded"),
54            Self::Awaited => write!(f, "awaited"),
55            Self::VmCall => write!(f, "calling into other vm"),
56        }
57    }
58}