use core::{
error::Error,
fmt::{self, Display},
};
#[derive(Debug)]
pub enum TranslationError {
UnsupportedBlockType(wasmparser::BlockType),
UnsupportedValueType(wasmparser::ValType),
BranchTableTargetsOutOfBounds,
BranchOffsetOutOfBounds,
BlockFuelOutOfBounds,
AllocatedTooManySlots,
SlotOutOfBounds,
EmulatedValueStackOverflow,
ProviderSliceOverflow,
TooManyFuncLocalConstValues,
TooManyFunctionResults,
TooManyFunctionParams,
TooManyLocalVariables,
LazyCompilationFailed,
}
impl TranslationError {
pub fn unsupported_block_type(block_type: wasmparser::BlockType) -> Self {
Self::UnsupportedBlockType(block_type)
}
pub fn unsupported_value_type(value_type: wasmparser::ValType) -> Self {
Self::UnsupportedValueType(value_type)
}
}
impl Error for TranslationError {}
impl Display for TranslationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnsupportedBlockType(error) => {
write!(f, "encountered unsupported Wasm block type: {error:?}")
}
Self::UnsupportedValueType(error) => {
write!(f, "encountered unsupported Wasm value type: {error:?}")
}
Self::BranchTableTargetsOutOfBounds => {
write!(
f,
"branch table targets are out of bounds for wasmi bytecode"
)
}
Self::BranchOffsetOutOfBounds => {
write!(f, "branching offset is out of bounds for wasmi bytecode")
}
Self::BlockFuelOutOfBounds => {
write!(
f,
"fuel required to execute a block is out of bounds for wasmi bytecode"
)
}
Self::AllocatedTooManySlots => {
write!(
f,
"translation requires more registers for a function than available"
)
}
Self::SlotOutOfBounds => {
write!(f, "tried to access out of bounds register index")
}
Self::EmulatedValueStackOverflow => {
write!(f, "function requires value stack with out of bounds depth")
}
Self::ProviderSliceOverflow => {
write!(f, "tried to allocate too many or too large provider slices")
}
Self::TooManyFuncLocalConstValues => {
write!(
f,
"tried to allocate too many function local constant values"
)
}
Self::TooManyFunctionResults => {
write!(f, "encountered function with too many function results")
}
Self::TooManyFunctionParams => {
write!(f, "encountered function with too many function parameters")
}
Self::TooManyLocalVariables => {
write!(f, "encountered function with too many local variables")
}
Self::LazyCompilationFailed => {
write!(
f,
"lazy function compilation encountered a Wasm validation or translation error"
)
}
}
}
}