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 InvalidNumberOfImports {
17 required: usize,
19 given: usize,
21 },
22 ImportsExternalsMismatch {
25 expected: ExternType,
27 actual: Extern,
29 },
30 SignatureMismatch {
32 expected: FuncType,
34 actual: FuncType,
36 },
37 Table(TableError),
39 Memory(MemoryError),
41 Global(GlobalError),
43 ElementSegmentDoesNotFit {
45 table: Table,
47 table_index: u64,
49 len: u32,
51 },
52 FoundStartFn {
54 index: u32,
56 },
57 TooManyInstances,
58}
59
60#[cfg(feature = "std")]
61impl std::error::Error for InstantiationError {}
62
63impl Display for InstantiationError {
64 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65 match self {
66 Self::InvalidNumberOfImports { required, given } => write!(
67 f,
68 "invalid number of imports: required = {required}, given = {given}",
69 ),
70 Self::ImportsExternalsMismatch { expected, actual } => write!(
71 f,
72 "expected {expected:?} external for import but found {actual:?}",
73 ),
74 Self::SignatureMismatch { expected, actual } => {
75 write!(
76 f,
77 "expected {expected:?} function signature but found {actual:?}",
78 )
79 }
80 Self::ElementSegmentDoesNotFit {
81 table,
82 table_index: offset,
83 len: amount,
84 } => write!(
85 f,
86 "out of bounds table access: {table:?} does not fit {amount} elements starting from offset {offset}",
87 ),
88 Self::FoundStartFn { index } => {
89 write!(f, "found an unexpected start function with index {index}")
90 }
91 Self::Table(error) => Display::fmt(error, f),
92 Self::Memory(error) => Display::fmt(error, f),
93 Self::Global(error) => Display::fmt(error, f),
94 Self::TooManyInstances => write!(f, "too many instances")
95 }
96 }
97}
98
99impl From<TableError> for InstantiationError {
100 fn from(error: TableError) -> Self {
101 Self::Table(error)
102 }
103}
104
105impl From<MemoryError> for InstantiationError {
106 fn from(error: MemoryError) -> Self {
107 Self::Memory(error)
108 }
109}
110
111impl From<GlobalError> for InstantiationError {
112 fn from(error: GlobalError) -> Self {
113 Self::Global(error)
114 }
115}