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
use std::fmt;
/// Result type returned by the MiraScript VM.
pub type Result<T> = std::result::Result<T, MiraError>;
/// An error produced while compiling, decoding, executing, or bridging values.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum MiraError {
/// Source compilation failed.
Compile {
/// Compiler diagnostics encoded by `mirascript-core`.
diagnostics: Vec<u32>,
},
/// The compiled bytecode chunk failed structural validation.
InvalidBytecode {
/// Byte offset at which validation failed.
offset: usize,
/// Human-readable validation failure.
reason: String,
},
/// Script execution failed.
Runtime {
/// Human-readable runtime failure.
message: String,
/// Function active at the failure site, when known.
function: Option<String>,
/// Bytecode offset of the failure site, when known.
offset: Option<usize>,
/// Names of active callers, ordered from root to leaf.
stack: Vec<String>,
},
/// A MiraScript value could not be converted to the requested Rust type.
Conversion {
/// Requested Rust-side value description.
expected: String,
/// MiraScript value type that was encountered.
actual: String,
/// Nested field or index path, when conversion added one.
path: Option<String>,
},
/// A host extern reported an error.
Extern {
/// Error message supplied by the extern.
message: String,
},
/// A live Rust value was already borrowed incompatibly.
BorrowConflict {
/// Operation that required the conflicting borrow.
operation: &'static str,
/// Type tag of the bridged Rust value.
tag: String,
},
/// Execution exceeded [`crate::RunOptions::timeout`].
Timeout,
/// Execution exceeded the configured call-depth limit.
MaxCallDepth {
/// Configured maximum call depth.
max: u32,
},
/// A script closure or script module attempted to outlive its execution.
EscapingClosure,
/// A previously captured script value was used after execution ended.
ExecutionEnded,
}
impl MiraError {
pub(crate) fn runtime(message: impl Into<String>) -> Self {
Self::Runtime {
message: message.into(),
function: None,
offset: None,
stack: Vec::new(),
}
}
pub(crate) fn conversion(expected: impl Into<String>, value: &crate::MiraAny) -> Self {
Self::Conversion {
expected: expected.into(),
actual: value.type_name().into(),
path: None,
}
}
/// Attach a nested field or index path to a [`MiraError::Conversion`].
///
/// Other error variants are returned unchanged.
pub fn at_path(mut self, path: impl Into<String>) -> Self {
if let Self::Conversion { path: slot, .. } = &mut self {
*slot = Some(path.into());
}
self
}
pub(crate) fn with_runtime_context(
mut self,
function: Option<String>,
offset: usize,
stack: Vec<String>,
) -> Self {
if let Self::Runtime {
function: error_function,
offset: error_offset,
stack: error_stack,
..
} = &mut self
&& error_function.is_none()
{
*error_function = function;
*error_offset = Some(offset);
*error_stack = stack;
}
self
}
}
impl fmt::Display for MiraError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Compile { diagnostics } => {
write!(
f,
"failed to compile MiraScript ({} diagnostic words)",
diagnostics.len()
)
}
Self::InvalidBytecode { offset, reason } => {
write!(f, "invalid bytecode at offset {offset}: {reason}")
}
Self::Runtime {
message,
function,
offset,
..
} => {
write!(f, "{message}")?;
if let Some(function) = function {
write!(f, " in {function}")?;
}
if let Some(offset) = offset {
write!(f, " at bytecode offset {offset}")?;
}
Ok(())
}
Self::Conversion {
expected,
actual,
path,
} => {
write!(f, "failed to convert {actual} to {expected}")?;
if let Some(path) = path {
write!(f, " at {path}")?;
}
Ok(())
}
Self::Extern { message } => write!(f, "extern error: {message}"),
Self::BorrowConflict { operation, tag } => {
write!(f, "borrow conflict while attempting to {operation} {tag}")
}
Self::Timeout => write!(f, "MiraScript execution timed out"),
Self::MaxCallDepth { max } => write!(f, "maximum call depth of {max} exceeded"),
Self::EscapingClosure => {
write!(f, "a MiraScript closure or module cannot escape run()")
}
Self::ExecutionEnded => write!(f, "MiraScript execution has ended"),
}
}
}
impl std::error::Error for MiraError {}