use core::{
error::Error,
fmt::{self, Display},
};
#[derive(Debug)]
pub enum TranslationError {
UnsupportedBlockType(wasmparser::BlockType),
UnsupportedValueType(wasmparser::ValType),
BranchTableTargetsOutOfBounds,
BranchOffsetOutOfBounds,
BlockFuelOutOfBounds,
AllocatedTooManySlots,
SlotAccessOutOfBounds,
SlotOutOfBounds,
EmulatedValueStackOverflow,
ProviderSliceOverflow,
TooManyFuncLocalConstValues,
TooManyFunctionResults,
TooManyFunctionParams,
TooManyLocalVariables,
LazyCompilationFailed,
OutOfSystemMemory,
}
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 {
let message = match self {
Self::UnsupportedBlockType(error) => {
return write!(f, "encountered unsupported Wasm block type: {error:?}");
}
Self::UnsupportedValueType(error) => {
return write!(f, "encountered unsupported Wasm value type: {error:?}");
}
Self::BranchTableTargetsOutOfBounds => {
"branch table targets are out of bounds for wasmi bytecode"
}
Self::BranchOffsetOutOfBounds => "branching offset is out of bounds for wasmi bytecode",
Self::BlockFuelOutOfBounds => {
"fuel required to execute a block is out of bounds for wasmi bytecode"
}
Self::AllocatedTooManySlots => {
"translation requires more registers for a function than available"
}
Self::SlotAccessOutOfBounds => "tried to access a slot that is out of bounds",
Self::SlotOutOfBounds => "tried to access out of bounds register index",
Self::EmulatedValueStackOverflow => {
"function requires value stack with out of bounds depth"
}
Self::ProviderSliceOverflow => {
"tried to allocate too many or too large provider slices"
}
Self::TooManyFuncLocalConstValues => {
"tried to allocate too many function local constant values"
}
Self::TooManyFunctionResults => "encountered function with too many function results",
Self::TooManyFunctionParams => "encountered function with too many function parameters",
Self::TooManyLocalVariables => "encountered function with too many local variables",
Self::LazyCompilationFailed => {
"lazy function compilation encountered a Wasm validation or translation error"
}
Self::OutOfSystemMemory => "ran out of system memory during translation",
};
f.write_str(message)
}
}