use std::fmt;
use crate::bytecode::NativeIdx;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeCallError {
NoNativeRight,
IndexNotAllowlisted(NativeIdx),
IndexOutOfRange(NativeIdx),
Revoked,
}
impl fmt::Display for NativeCallError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
NativeCallError::NoNativeRight => f.write_str("CALL_NATIVE: flow lacks NATIVE right"),
NativeCallError::IndexNotAllowlisted(idx) => {
write!(f, "CALL_NATIVE: index {idx} not on allowlist")
}
NativeCallError::IndexOutOfRange(idx) => {
write!(f, "CALL_NATIVE: index {idx} out of range")
}
NativeCallError::Revoked => f.write_str("CALL_NATIVE: capability revoked"),
}
}
}
impl std::error::Error for NativeCallError {}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Fault {
DivideByZero,
RegisterOutOfRange {
reg: u8,
frame_size: u8,
},
RegisterIndexOverflow {
base: u8,
offset: u8,
},
BadConstant {
index: u32,
pool_size: u32,
},
BadFunction {
index: u32,
table_size: u32,
},
CallStackOverflow {
depth: usize,
},
TypeMismatch {
expected: &'static str,
got: &'static str,
},
BadNative {
index: u32,
table_size: u32,
},
NativeError(String),
NativeDenied(NativeCallError),
Explicit(i32),
Invariant(&'static str),
QuotaExceeded(String),
BadJump {
target: usize,
code_len: usize,
},
}
impl fmt::Display for Fault {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Fault::DivideByZero => write!(f, "division by zero"),
Fault::RegisterOutOfRange { reg, frame_size } => {
write!(
f,
"register r{reg} out of range (frame has {frame_size} registers)"
)
}
Fault::BadConstant { index, pool_size } => {
write!(
f,
"constant index {index} out of range (pool size {pool_size})"
)
}
Fault::BadFunction { index, table_size } => {
write!(
f,
"function index {index} out of range (table size {table_size})"
)
}
Fault::RegisterIndexOverflow { base, offset } => write!(
f,
"register index r{base}+{offset} overflows the register index space (max r255)"
),
Fault::CallStackOverflow { depth } => write!(f, "call stack overflow at depth {depth}"),
Fault::TypeMismatch { expected, got } => {
write!(f, "type mismatch: expected {expected}, got {got}")
}
Fault::BadNative { index, table_size } => {
write!(
f,
"native function index {index} out of range (table size {table_size})"
)
}
Fault::NativeError(msg) => write!(f, "native function error: {msg}"),
Fault::NativeDenied(err) => write!(f, "{err}"),
Fault::Explicit(code) => write!(f, "explicit trap (code {code})"),
Fault::Invariant(msg) => write!(f, "vm invariant broken: {msg}"),
Fault::QuotaExceeded(msg) => write!(f, "{msg}"),
Fault::BadJump { target, code_len } => {
write!(
f,
"jump target {target} out of range (code length {code_len})"
)
}
}
}
}
impl std::error::Error for Fault {}