gwasmi/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}
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}