wasmi/module/instantiate/
error.rs1use crate::{
2 errors::{MemoryError, TableError},
3 Extern,
4 ExternType,
5 FuncType,
6 GlobalType,
7 MemoryType,
8 Table,
9 TableType,
10};
11use core::{
12 error::Error,
13 fmt::{self, Display},
14};
15
16#[derive(Debug)]
18pub enum InstantiationError {
19 InvalidNumberOfImports {
22 required: usize,
24 given: usize,
26 },
27 ImportsExternalsMismatch {
30 expected: ExternType,
32 actual: Extern,
34 },
35 GlobalTypeMismatch {
37 expected: GlobalType,
39 actual: GlobalType,
41 },
42 FuncTypeMismatch {
44 expected: FuncType,
46 actual: FuncType,
48 },
49 TableTypeMismatch {
51 expected: TableType,
53 actual: TableType,
55 },
56 MemoryTypeMismatch {
58 expected: MemoryType,
60 actual: MemoryType,
62 },
63 ElementSegmentDoesNotFit {
65 table: Table,
67 table_index: u64,
69 len: u32,
71 },
72 UnexpectedStartFn {
74 index: u32,
76 },
77 TooManyInstances,
79 TooManyTables,
81 TooManyMemories,
83 FailedToInstantiateMemory(MemoryError),
85 FailedToInstantiateTable(TableError),
87}
88
89impl Error for InstantiationError {}
90
91impl Display for InstantiationError {
92 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
93 match self {
94 Self::InvalidNumberOfImports { required, given } => write!(
95 f,
96 "invalid number of imports: required = {required}, given = {given}",
97 ),
98 Self::ImportsExternalsMismatch { expected, actual } => write!(
99 f,
100 "expected {expected:?} external for import but found {actual:?}",
101 ),
102 Self::GlobalTypeMismatch { expected, actual } => write!(f, "imported global type mismatch. expected {expected:?} but found {actual:?}"),
103 Self::FuncTypeMismatch { expected, actual } => write!(f, "imported function type mismatch. expected {expected:?} but found {actual:?}"),
104 Self::TableTypeMismatch { expected, actual } => write!(f, "imported table type mismatch. expected {expected:?} but found {actual:?}"),
105 Self::MemoryTypeMismatch { expected, actual } => write!(f, "imported memory type mismatch. expected {expected:?} but found {actual:?}"),
106 Self::ElementSegmentDoesNotFit {
107 table,
108 table_index: offset,
109 len: amount,
110 } => write!(
111 f,
112 "out of bounds table access: {table:?} does not fit {amount} elements starting from offset {offset}",
113 ),
114 Self::UnexpectedStartFn { index } => {
115 write!(f, "found an unexpected start function with index {index}")
116 }
117 Self::TooManyInstances => write!(f, "tried to instantiate too many instances"),
118 Self::TooManyTables => write!(f, "tried to instantiate too many tables"),
119 Self::TooManyMemories => write!(f, "tried to instantiate too many linear memories"),
120 Self::FailedToInstantiateMemory(error) => write!(f, "failed to instantiate memory: {error}"),
121 Self::FailedToInstantiateTable(error) => write!(f, "failed to instantiate table: {error}"),
122 }
123 }
124}