use core::{
    error::Error,
    fmt::{self, Display},
};

/// An error that may occur upon parsing, validating and translating Wasm.
#[derive(Debug)]
pub enum TranslationError {
    /// Encountered an unsupported Wasm block type.
    UnsupportedBlockType(wasmparser::BlockType),
    /// Encountered an unsupported Wasm value type.
    UnsupportedValueType(wasmparser::ValType),
    /// When using too many branch table targets.
    BranchTableTargetsOutOfBounds,
    /// Branching offset out of bounds.
    BranchOffsetOutOfBounds,
    /// Fuel required for a block is out of bounds.
    BlockFuelOutOfBounds,
    /// Tried to allocate more registers than possible.
    AllocatedTooManySlots,
    /// Tried to access a slot that is out of bounds.
    SlotAccessOutOfBounds,
    /// Tried to use an out of bounds register index.
    SlotOutOfBounds,
    /// Pushed too many values on the emulated value stack during translation.
    EmulatedValueStackOverflow,
    /// Tried to allocate too many or large provider slices.
    ProviderSliceOverflow,
    /// Tried to allocate too many function local constant values.
    TooManyFuncLocalConstValues,
    /// Tried to define a function with too many function results.
    TooManyFunctionResults,
    /// Tried to define a function with too many function parameters.
    TooManyFunctionParams,
    /// Tried to define a function with too many local variables.
    TooManyLocalVariables,
    /// The function failed to compiled lazily.
    LazyCompilationFailed,
    /// Ran out of system memory during translation.
    OutOfSystemMemory,
}

impl TranslationError {
    /// Creates a new error indicating an unsupported Wasm block type.
    pub fn unsupported_block_type(block_type: wasmparser::BlockType) -> Self {
        Self::UnsupportedBlockType(block_type)
    }

    /// Creates a new error indicating an unsupported Wasm value 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)
    }
}

Homonyms

soft3/mir/src/error.rs
soft3/tru/rs/error.rs
warriors/trisha/cli/error.rs
neural/rs/darwin-sys/src/error.rs
soft3/radio/iroh-car/src/error.rs
soft3/radio/iroh-ffi/src/error.rs
cyb/prysm/molecules/rs/error.rs
soft3/radio/cyber-bao/src/io/error.rs
soft3/radio/iroh-willow/src/session/error.rs
cyb/evy/forks/naga/src/error.rs
cyb/wysm/crates/fuzz/src/error.rs
cyb/evy/crates/evy_engine_tasks/src/error.rs
soft3/radio/iroh-dns-server/src/http/error.rs
cyb/wysm/crates/ir/src/error.rs
soft3/radio/iroh-blobs/src/get/error.rs
cyb/wysm/crates/c_api/src/error.rs
cyb/wysm/crates/wasmi/src/error.rs
cyb/evy/crates/evy_engine_dispatch/src/error.rs
cyb/wysm/crates/core/src/table/error.rs
bootloader/go-cyber/cw/contracts/graph-filter/src/error.rs
cyb/wysm/crates/collections/src/arena/error.rs
cyb/wysm/crates/core/src/memory/error.rs
cyb/evy/forks/bevy_ecs/src/schedule/error.rs
bootloader/go-cyber/cw/contracts/std-test/src/error.rs
cyb/evy/forks/bevy_ecs/src/world/error.rs
cyb/wysm/crates/wasmi/src/func/error.rs
cyb/evy/forks/bevy_ecs/src/query/error.rs
cyb/wysm/crates/wasmi/src/store/error.rs
cyb/evy/forks/naga/src/front/wgsl/error.rs
cyb/evy/forks/naga/src/front/spv/error.rs
cyb/evy/forks/naga/src/front/glsl/error.rs
cyb/wysm/crates/wasmi/src/module/instantiate/error.rs

Graph