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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use crate::bytecode::DebugSymbol;
use codespan::FileId;
use codespan::Files;
use codespan_reporting::diagnostic::{Diagnostic, Label};
use std::borrow::Cow;
use std::cell;
use std::fmt;
use std::sync;
use sys_info;
use opener;
pub type RuntimeResult<T> = std::result::Result<T, RuntimeError>;
#[derive(Clone, Debug)]
pub struct RuntimeError {
pub err: RuntimeErrorType,
pub help: String,
pub symbols: Vec<DebugSymbol>,
}
impl fmt::Display for RuntimeError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "{:?}: {}", self.err, self.help)
}
}
#[derive(Copy, Clone, Debug)]
pub enum RuntimeErrorType {
TypeError,
IntegerTooBigError,
AttributeError,
InternalError,
IndexOutOfBounds,
ChildProcessError,
VariableUndefinedError,
IOError,
SysInfoError,
PoisonError,
BorrowError,
BorrowMutError,
MutateImmutableError,
KeyError,
OpenError,
AssertionError,
}
impl From<sys_info::Error> for RuntimeError {
fn from(error: sys_info::Error) -> Self {
RuntimeError {
err: RuntimeErrorType::SysInfoError,
help: error.to_string(),
symbols: vec![],
}
}
}
impl From<std::io::Error> for RuntimeError {
fn from(error: std::io::Error) -> Self {
RuntimeError {
err: RuntimeErrorType::IOError,
help: error.to_string(),
symbols: vec![],
}
}
}
impl<T> From<sync::PoisonError<T>> for RuntimeError {
fn from(error: sync::PoisonError<T>) -> Self {
RuntimeError {
err: RuntimeErrorType::PoisonError,
help: error.to_string(),
symbols: vec![],
}
}
}
impl From<cell::BorrowError> for RuntimeError {
fn from(error: cell::BorrowError) -> Self {
RuntimeError {
err: RuntimeErrorType::BorrowError,
help: error.to_string(),
symbols: vec![],
}
}
}
impl From<cell::BorrowMutError> for RuntimeError {
fn from(error: cell::BorrowMutError) -> Self {
RuntimeError {
err: RuntimeErrorType::BorrowMutError,
help: error.to_string(),
symbols: vec![],
}
}
}
impl From<mlrefcell::BorrowMutError> for RuntimeError {
fn from(error: mlrefcell::BorrowMutError) -> Self {
match error {
mlrefcell::BorrowMutError::AlreadyBorrowed => RuntimeError {
err: RuntimeErrorType::BorrowMutError,
help: "tried to mutate and read from the same object".to_string(),
symbols: vec![],
},
mlrefcell::BorrowMutError::Locked => RuntimeError {
err: RuntimeErrorType::MutateImmutableError,
help: "tried to mutate value that was locked".to_string(),
symbols: vec![],
},
}
}
}
impl From<opener::OpenError> for RuntimeError {
fn from(error: opener::OpenError) -> Self {
RuntimeError {
err: RuntimeErrorType::OpenError,
help: error.to_string(),
symbols: vec![],
}
}
}
impl RuntimeError {
pub fn type_error<S: ToString>(message: S) -> Self {
RuntimeError {
err: RuntimeErrorType::TypeError,
help: message.to_string(),
symbols: vec![],
}
}
pub fn attribute_error<S: ToString>(message: S) -> Self {
RuntimeError {
err: RuntimeErrorType::AttributeError,
help: message.to_string(),
symbols: vec![],
}
}
pub fn key_error<S: ToString>(message: S) -> Self {
RuntimeError {
err: RuntimeErrorType::KeyError,
help: message.to_string(),
symbols: vec![],
}
}
pub fn internal_error<S: ToString>(message: S) -> Self {
RuntimeError {
err: RuntimeErrorType::InternalError,
help: message.to_string(),
symbols: vec![],
}
}
pub fn variable_undefined_error<S: ToString>(message: S) -> Self {
RuntimeError {
err: RuntimeErrorType::VariableUndefinedError,
help: message.to_string(),
symbols: vec![],
}
}
pub fn index_oob_error<S: ToString>(message: S) -> Self {
RuntimeError {
err: RuntimeErrorType::IndexOutOfBounds,
help: message.to_string(),
symbols: vec![],
}
}
pub fn index_too_big_error<S: ToString>(message: S) -> Self {
RuntimeError {
err: RuntimeErrorType::IntegerTooBigError,
help: message.to_string(),
symbols: vec![],
}
}
pub fn child_process_error<S: ToString>(message: S) -> Self {
RuntimeError {
err: RuntimeErrorType::ChildProcessError,
help: message.to_string(),
symbols: vec![],
}
}
pub fn assertion_error<S: ToString>(message: Option<S>) -> Self {
RuntimeError {
err: RuntimeErrorType::AssertionError,
help: message.map(|val| val.to_string()).unwrap_or_else(|| String::from("Assertion failed")),
symbols: vec![],
}
}
pub fn attach_span(mut self, debug: DebugSymbol) -> Self {
self.symbols.push(debug);
RuntimeError {
err: self.err,
help: self.help,
symbols: self.symbols,
}
}
pub fn as_diagnostic(&self) -> Diagnostic<FileId> {
match self.symbols.get(0) {
Some(&symbol) => Diagnostic::error()
.with_message(format!("Runtime Error: {:?}", self.err))
.with_labels(vec![
Label::primary(symbol.file_id, symbol.location).with_message(&self.help)
]),
None => Diagnostic::error().with_message(&self.help),
}
}
pub fn stack_trace(&self, files: &Files<Cow<'_, str>>) -> Vec<String> {
let mut res = vec![];
for symbol in self.symbols.iter() {
let slice = files
.source_slice(symbol.file_id, symbol.location)
.unwrap_or("<Unknown>");
let location = files
.location(symbol.file_id, symbol.location.start())
.map_or("??".to_string(), |location| {
format!(
"line {}, col {}",
location.line.number(),
location.column.number()
)
});
let fname = files.name(symbol.file_id).to_string_lossy();
res.push(format!("{} at {}: \"{}\"", fname, location, slice));
}
res
}
}