wasmi/module/instantiate/
error.rs1use crate::{
2 errors::{MemoryError, TableError},
3 global::GlobalError,
4 Extern,
5 ExternType,
6 FuncType,
7 Table,
8};
9use core::{fmt, fmt::Display};
10
11#[derive(Debug)]
13pub enum InstantiationError {
14 ImportsExternalsLenMismatch,
17 ImportsExternalsMismatch {
20 expected: ExternType,
22 actual: Extern,
24 },
25 SignatureMismatch {
27 expected: FuncType,
29 actual: FuncType,
31 },
32 Table(TableError),
34 Memory(MemoryError),
36 Global(GlobalError),
38 ElementSegmentDoesNotFit {
40 table: Table,
42 offset: u32,
44 amount: u32,
46 },
47 FoundStartFn {
49 index: u32,
51 },
52 TooManyInstances,
53}
54
55#[cfg(feature = "std")]
56impl std::error::Error for InstantiationError {}
57
58impl Display for InstantiationError {
59 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60 match self {
61 Self::ImportsExternalsLenMismatch => write!(
62 f,
63 "encountered mismatch between number of given externals and module imports",
64 ),
65 Self::ImportsExternalsMismatch { expected, actual } => write!(
66 f,
67 "expected {expected:?} external for import but found {actual:?}",
68 ),
69 Self::SignatureMismatch { expected, actual } => {
70 write!(
71 f,
72 "expected {expected:?} function signature but found {actual:?}",
73 )
74 }
75 Self::ElementSegmentDoesNotFit {
76 table,
77 offset,
78 amount,
79 } => write!(
80 f,
81 "out of bounds table access: {table:?} does not fit {amount} elements starting from offset {offset}",
82 ),
83 Self::FoundStartFn { index } => {
84 write!(f, "found an unexpected start function with index {index}")
85 }
86 Self::Table(error) => Display::fmt(error, f),
87 Self::Memory(error) => Display::fmt(error, f),
88 Self::Global(error) => Display::fmt(error, f),
89 Self::TooManyInstances => write!(f, "too many instances")
90 }
91 }
92}
93
94impl From<TableError> for InstantiationError {
95 fn from(error: TableError) -> Self {
96 Self::Table(error)
97 }
98}
99
100impl From<MemoryError> for InstantiationError {
101 fn from(error: MemoryError) -> Self {
102 Self::Memory(error)
103 }
104}
105
106impl From<GlobalError> for InstantiationError {
107 fn from(error: GlobalError) -> Self {
108 Self::Global(error)
109 }
110}