gwasmi/module/instantiate/
error.rs

1use crate::{
2    errors::{MemoryError, TableError},
3    global::GlobalError,
4    Extern,
5    ExternType,
6    FuncType,
7    Table,
8};
9use core::{fmt, fmt::Display};
10
11/// An error that may occur upon instantiation of a Wasm module.
12#[derive(Debug)]
13pub enum InstantiationError {
14    /// Caused when the number of required imports does not match
15    /// the number of given externals upon module instantiation.
16    ImportsExternalsLenMismatch,
17    /// Caused when a given external value does not match the
18    /// type of the required import for module instantiation.
19    ImportsExternalsMismatch {
20        /// The expected external value for the module import.
21        expected: ExternType,
22        /// The actually found external value for the module import.
23        actual: Extern,
24    },
25    /// Caused when a function has a mismatching signature.
26    SignatureMismatch {
27        /// The expected function signature for the function import.
28        expected: FuncType,
29        /// The actual function signature for the function import.
30        actual: FuncType,
31    },
32    /// Occurs when an imported table does not satisfy the required table type.
33    Table(TableError),
34    /// Occurs when an imported memory does not satisfy the required memory type.
35    Memory(MemoryError),
36    /// Occurs when an imported global variable does not satisfy the required global type.
37    Global(GlobalError),
38    /// Caused when an element segment does not fit into the specified table instance.
39    ElementSegmentDoesNotFit {
40        /// The table of the element segment.
41        table: Table,
42        /// The offset to store the `amount` of elements into the table.
43        offset: u32,
44        /// The amount of elements with which the table is initialized at the `offset`.
45        amount: u32,
46    },
47    /// Caused when the `start` function was unexpectedly found in the instantiated module.
48    FoundStartFn {
49        /// The index of the found `start` function.
50        index: u32,
51    },
52}
53
54#[cfg(feature = "std")]
55impl std::error::Error for InstantiationError {}
56
57impl Display for InstantiationError {
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        match self {
60            Self::ImportsExternalsLenMismatch => write!(
61                f,
62                "encountered mismatch between number of given externals and module imports",
63            ),
64            Self::ImportsExternalsMismatch { expected, actual } => write!(
65                f,
66                "expected {expected:?} external for import but found {actual:?}",
67            ),
68            Self::SignatureMismatch { expected, actual } => {
69                write!(
70                    f,
71                    "expected {expected:?} function signature but found {actual:?}",
72                )
73            }
74            Self::ElementSegmentDoesNotFit {
75                table,
76                offset,
77                amount,
78            } => write!(
79                f,
80                "out of bounds table access: {table:?} does not fit {amount} elements starting from offset {offset}",
81            ),
82            Self::FoundStartFn { index } => {
83                write!(f, "found an unexpected start function with index {index}")
84            }
85            Self::Table(error) => Display::fmt(error, f),
86            Self::Memory(error) => Display::fmt(error, f),
87            Self::Global(error) => Display::fmt(error, f),
88        }
89    }
90}
91
92impl From<TableError> for InstantiationError {
93    fn from(error: TableError) -> Self {
94        Self::Table(error)
95    }
96}
97
98impl From<MemoryError> for InstantiationError {
99    fn from(error: MemoryError) -> Self {
100        Self::Memory(error)
101    }
102}
103
104impl From<GlobalError> for InstantiationError {
105    fn from(error: GlobalError) -> Self {
106        Self::Global(error)
107    }
108}