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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! Runtime errors of the VM.
use super::*;
/// The kind of runtime error.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VmErrorKind {
/// Exceeded the limit on the number of instructions.
StepLimitExceeded,
/// An interrupt was requested through the token ([`crate::vm::InterruptHandle`]).
Interrupted,
/// The depth of calls (nesting of `call`) exceeded the limit.
CallDepthExceeded,
/// A value (a string, `list`, `struct`, and so on) exceeded the size limit.
ValueTooLarge,
/// The total size of values held in variables exceeded the limit (`VmConfig::max_total_bytes`).
MemoryLimitExceeded,
/// An index into a `list` is out of range.
IndexOutOfRange {
/// The index given.
index: i64,
/// The length of the `list`.
len: i64,
},
/// Read a key missing from a `map` by index (`m[k]`), or tried to update it (`m[k] += 1`, `m[k].x = 1`).
/// `m[k] = v` adds the key if missing, so it does not cause this.
KeyNotFound {
/// The key given (as the string written for `${}` interpolation).
key: String,
},
/// Integer overflow.
IntegerOverflow,
/// Divided by 0 (for both `int` and `float`).
DivisionByZero,
/// The result of a `float` operation became `NaN` or `±∞` (which never appear as values).
NonFiniteFloat,
/// The result of `to_int` does not fit in the range of `int`.
FloatOutOfRange,
/// Called `resume` or `save` while not stopped.
NotStopped,
/// Called `run` while stopped.
AlreadyStopped,
/// Called `run` after the end.
Ended,
/// None of the options of a `choice` are in the list (`if` was false, or already chosen with `once`).
NoChoiceAvailable,
/// The number of the choice passed to `resume` is out of the range of the given list.
ChoiceOutOfRange {
/// The number given.
index: i64,
/// The length of the list.
count: i64,
},
/// Tried to continue after a runtime error.
Poisoned,
/// The persistent data in `VmConfig::persistent` could not be read (`Vm::new`).
PersistentData(LoadErrorKind),
/// `min > max` in `random(min, max)`.
InvalidRandomRange {
/// The value given as the minimum.
min: i64,
/// The value given as the maximum.
max: i64,
},
/// A snapshot made with another program was passed to `restore`.
SnapshotMismatch,
/// The type of the input to `resume` does not match.
BadInput {
/// The expected type.
expected: Ty,
/// The type given.
found: Ty,
},
/// There is no start label.
StartLabelNotFound(String),
/// A command or variable used by the program is not in the registry.
CommandMissing(String),
/// A command function returned `Err`. Has a code decided by the package (`package.error_name`) and named arguments.
/// No message text (displaying it is the host's job).
CommandFailed {
/// The error code of the package (those returned by the core are `core.…`).
code: String,
/// Named arguments determined by each code.
args: Vec<(&'static str, DiagArg)>,
},
/// The variable named in `get_var` / `set_var` is not registered.
UnknownVariable(String),
/// A package's state slice could not be serialized (a bug in the implementation of `Package::State`).
SliceSerialize(String),
/// Tried to create a VM with a registry made from a declaration file (it has no functions to run commands).
DeclOnlyRegistry,
}
impl VmErrorKind {
/// The stable English name.
pub fn code(&self) -> &'static str {
match self {
VmErrorKind::StepLimitExceeded => "step_limit_exceeded",
VmErrorKind::Interrupted => "interrupted",
VmErrorKind::CallDepthExceeded => "call_depth_exceeded",
VmErrorKind::ValueTooLarge => "value_too_large",
VmErrorKind::MemoryLimitExceeded => "memory_limit_exceeded",
VmErrorKind::IndexOutOfRange { .. } => "index_out_of_range",
VmErrorKind::KeyNotFound { .. } => "key_not_found",
VmErrorKind::IntegerOverflow => "integer_overflow",
VmErrorKind::DivisionByZero => "division_by_zero",
VmErrorKind::NonFiniteFloat => "non_finite_float",
VmErrorKind::FloatOutOfRange => "float_out_of_range",
VmErrorKind::NotStopped => "not_stopped",
VmErrorKind::AlreadyStopped => "already_stopped",
VmErrorKind::Ended => "ended",
VmErrorKind::NoChoiceAvailable => "no_choice_available",
VmErrorKind::ChoiceOutOfRange { .. } => "choice_out_of_range",
VmErrorKind::Poisoned => "poisoned",
VmErrorKind::PersistentData(_) => "persistent_data",
VmErrorKind::InvalidRandomRange { .. } => "invalid_random_range",
VmErrorKind::SnapshotMismatch => "snapshot_mismatch",
VmErrorKind::BadInput { .. } => "bad_input",
VmErrorKind::StartLabelNotFound(_) => "start_label_not_found",
VmErrorKind::CommandMissing(_) => "command_missing",
VmErrorKind::CommandFailed { .. } => "command_failed",
VmErrorKind::UnknownVariable(_) => "unknown_variable",
VmErrorKind::SliceSerialize(_) => "slice_serialize",
VmErrorKind::DeclOnlyRegistry => "decl_only_registry",
}
}
/// For a command failure ([`VmErrorKind::CommandFailed`]), the package's error code and named arguments.
pub fn command_error(&self) -> Option<(&str, &[(&'static str, DiagArg)])> {
match self {
VmErrorKind::CommandFailed { code, args } => Some((code, args)),
_ => None,
}
}
}
/// A runtime error.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VmError {
/// Kind.
pub kind: VmErrorKind,
/// The name of the module (when known).
pub module: Option<String>,
/// The label (when known).
pub label: Option<String>,
/// The line (when known).
pub line: Option<u32>,
}
impl std::fmt::Display for VmError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.kind.code())?;
if let Some(l) = &self.label {
write!(f, " (label ")?;
if let Some(m) = &self.module {
write!(f, "{m}.")?;
}
write!(f, "{l}")?;
if let Some(n) = self.line {
write!(f, ", line {n}")?;
}
write!(f, ")")?;
}
Ok(())
}
}
impl std::error::Error for VmError {}