1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use crate::{
Extern,
ExternType,
FuncType,
GlobalType,
MemoryType,
Table,
TableType,
errors::{MemoryError, TableError},
module::ImportName,
};
use core::{
error::Error,
fmt::{self, Display},
};
/// An error that may occur upon instantiation of a Wasm module.
#[derive(Debug)]
pub enum InstantiationError {
/// Returned when the number of imports does not match the required amount.
MismatchedNumberOfImports {
/// The expected number of imports required by the Wasm module.
expected: usize,
/// The actual number of imports given to the Wasm module.
actual: usize,
},
/// Returned when an imported item has a complete type mismatch.
ImportTypeMismatch {
/// The name of the import.
name: ImportName,
/// The expected external value for the module import.
expected: ExternType,
/// The actually found external value for the module import.
actual: Extern,
},
/// Returned when an imported global has a mismatching type.
GlobalTypeMismatch {
/// Name and module of the import.
name: ImportName,
/// The expected global type of the import.
expected: GlobalType,
/// The actual global type of the import.
actual: GlobalType,
},
/// Returned when an imported function has a mismatching type.
FuncTypeMismatch {
/// Name and module of the import.
name: ImportName,
/// The expected type of the import.
expected: FuncType,
/// The actual type of the import.
actual: FuncType,
},
/// Returned when an imported table has a mismatching type.
TableTypeMismatch {
/// Name and module of the import.
name: ImportName,
/// The expected table type of the import.
expected: TableType,
/// The actual type of the import.
actual: TableType,
},
/// Returned when an imported linear memory has a mismatching type.
MemoryTypeMismatch {
/// Name and module of the import.
name: ImportName,
/// The expected memory type of the import.
expected: MemoryType,
/// The actual memory type of the import.
actual: MemoryType,
},
/// Caused when an element segment does not fit into the specified table instance.
ElementSegmentDoesNotFit {
/// The table of the element segment.
table: Table,
/// The offset to store the `amount` of elements into the table.
table_index: u64,
/// The amount of elements with which the table is initialized at the `offset`.
len: u32,
},
/// Caused when the `start` function was unexpectedly found in the instantiated module.
UnexpectedStartFn {
/// The index of the found `start` function.
index: u32,
},
/// When trying to instantiate more instances than supported by Wasmi.
TooManyInstances,
/// When trying to instantiate more tables than supported by Wasmi.
TooManyTables,
/// When trying to instantiate more linear memories than supported by Wasmi.
TooManyMemories,
/// Encountered when failing to instantiate a linear memory.
FailedToInstantiateMemory(MemoryError),
/// Encountered when failing to instantiate a table.
FailedToInstantiateTable(TableError),
}
impl InstantiationError {
/// Creates a new [`Self::MismatchedNumberOfImports`] from its parts.
#[cold]
pub fn mismatched_number_of_imports(expected: usize, actual: usize) -> Self {
Self::MismatchedNumberOfImports { expected, actual }
}
/// Creates a new [`Self::ImportTypeMismatch`] from its parts.
#[cold]
pub fn import_type_mismatch(name: &ImportName, expected: &ExternType, actual: &Extern) -> Self {
Self::ImportTypeMismatch {
name: name.clone(),
expected: expected.clone(),
actual: *actual,
}
}
/// Creates a new [`Self::GlobalTypeMismatch`] from its parts.
#[cold]
pub fn global_type_mismatch(
name: &ImportName,
expected: &GlobalType,
actual: &GlobalType,
) -> Self {
Self::GlobalTypeMismatch {
name: name.clone(),
expected: *expected,
actual: *actual,
}
}
/// Creates a new [`Self::FuncTypeMismatch`] from its parts.
#[cold]
pub fn func_type_mismatch(name: &ImportName, expected: &FuncType, actual: &FuncType) -> Self {
Self::FuncTypeMismatch {
name: name.clone(),
expected: expected.clone(),
actual: actual.clone(),
}
}
/// Creates a new [`Self::TableTypeMismatch`] from its parts.
#[cold]
pub fn table_type_mismatch(
name: &ImportName,
expected: &TableType,
actual: &TableType,
) -> Self {
Self::TableTypeMismatch {
name: name.clone(),
expected: *expected,
actual: *actual,
}
}
/// Creates a new [`Self::MemoryTypeMismatch`] from its parts.
#[cold]
pub fn memory_type_mismatch(
name: &ImportName,
expected: &MemoryType,
actual: &MemoryType,
) -> Self {
Self::MemoryTypeMismatch {
name: name.clone(),
expected: *expected,
actual: *actual,
}
}
}
impl Error for InstantiationError {}
impl Display for InstantiationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::MismatchedNumberOfImports { expected, actual } => write!(
f,
"mismatched number of imports: expected {expected} but found {actual}",
),
Self::ImportTypeMismatch {
name,
expected,
actual,
} => write!(
f,
"mismatched {name} import type: expected {expected:?} but found {actual:?}",
),
Self::GlobalTypeMismatch {
name,
expected,
actual,
} => write!(
f,
"imported global {name} type mismatch. expected {expected:?} but found {actual:?}"
),
Self::FuncTypeMismatch {
name,
expected,
actual,
} => write!(
f,
"imported function {name} type mismatch. expected {expected:?} but found {actual:?}",
),
Self::TableTypeMismatch {
name,
expected,
actual,
} => write!(
f,
"imported table {name} type mismatch. expected {expected:?} but found {actual:?}"
),
Self::MemoryTypeMismatch {
name,
expected,
actual,
} => write!(
f,
"imported memory {name} type mismatch. expected {expected:?} but found {actual:?}"
),
Self::ElementSegmentDoesNotFit {
table,
table_index: offset,
len: amount,
} => write!(
f,
"out of bounds table access: {table:?} does not fit {amount} elements starting from offset {offset}",
),
Self::UnexpectedStartFn { index } => {
write!(f, "found an unexpected start function with index {index}")
}
Self::TooManyInstances => write!(f, "tried to instantiate too many instances"),
Self::TooManyTables => write!(f, "tried to instantiate too many tables"),
Self::TooManyMemories => write!(f, "tried to instantiate too many linear memories"),
Self::FailedToInstantiateMemory(error) => {
write!(f, "failed to instantiate memory: {error}")
}
Self::FailedToInstantiateTable(error) => {
write!(f, "failed to instantiate table: {error}")
}
}
}
}