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
use {
crate::{Mutex, UnaryOp, Value, Vm},
koto_bytecode::Chunk,
koto_parser::format_error_with_excerpt,
std::{
sync::Arc,
{error, fmt},
},
};
#[derive(Clone, Debug)]
pub struct ErrorFrame {
chunk: Arc<Chunk>,
instruction: usize,
}
#[derive(Clone, Debug)]
pub enum RuntimeErrorType {
StringError(String),
KotoError {
thrown_value: Value,
vm: Option<Arc<Mutex<Vm>>>,
},
}
#[derive(Clone, Debug)]
pub struct RuntimeError {
pub error: RuntimeErrorType,
pub trace: Vec<ErrorFrame>,
}
impl RuntimeError {
pub fn new(error: RuntimeErrorType) -> Self {
Self {
error,
trace: Vec::new(),
}
}
pub fn from_koto_value(thrown_value: Value, vm: Vm) -> Self {
Self::new(RuntimeErrorType::KotoError {
thrown_value,
vm: Some(Arc::new(Mutex::new(vm))),
})
}
pub fn with_prefix(mut self, prefix: &str) -> Self {
use RuntimeErrorType::StringError;
self.error = match self.error {
StringError(message) => StringError(format!("{}: {}", prefix, message)),
other => other,
};
self
}
pub fn extend_trace(&mut self, chunk: Arc<Chunk>, instruction: usize) {
self.trace.push(ErrorFrame { chunk, instruction });
}
}
impl From<String> for RuntimeError {
fn from(error: String) -> Self {
Self::new(RuntimeErrorType::StringError(error))
}
}
impl From<&str> for RuntimeError {
fn from(error: &str) -> Self {
Self::new(RuntimeErrorType::StringError(error.into()))
}
}
impl fmt::Display for RuntimeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use {RuntimeErrorType::*, Value::*};
let message = match &self.error {
StringError(s) => s.clone(),
KotoError { thrown_value, vm } => match (&thrown_value, vm) {
(Str(message), _) => message.to_string(),
(Map(_), Some(vm)) => match vm
.lock()
.run_unary_op(UnaryOp::Display, thrown_value.clone())
{
Ok(Str(message)) => message.to_string(),
Ok(other) => format!(
"Error while getting error message, expected string, found '{}'",
other.type_as_string()
),
Err(_) => "Unable to get error message".to_string(),
},
_ => "Unable to get error message".to_string(),
},
};
if f.alternate() {
f.write_str(&message)
} else {
let mut first_frame = true;
for frame in self.trace.iter() {
let frame_message = if first_frame {
first_frame = false;
Some(message.as_str())
} else {
None
};
match frame.chunk.debug_info.get_source_span(frame.instruction) {
Some(span) => f.write_str(&format_error_with_excerpt(
frame_message,
&frame.chunk.source_path,
&frame.chunk.debug_info.source,
span.start,
span.end,
))?,
None => write!(
f,
"Runtime error at instruction {}: {}",
frame.instruction, message
)?,
};
}
Ok(())
}
}
}
impl error::Error for RuntimeError {}
pub type RuntimeResult = Result<Value, RuntimeError>;
#[macro_export]
macro_rules! make_runtime_error {
($message:expr) => {{
#[cfg(panic_on_runtime_error)]
{
panic!($message);
}
$crate::RuntimeError::from($message)
}};
}
#[macro_export]
macro_rules! runtime_error {
($error:expr) => {
Err($crate::make_runtime_error!($error))
};
($error:expr, $($y:expr),+ $(,)?) => {
Err($crate::make_runtime_error!(format!($error, $($y),+)))
};
}