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
//! Error type for the `wamrx` high-level API.
use crate::value::ValType;
/// Errors returned by `wamrx` operations.
///
/// Implements [`std::error::Error`], so it converts cleanly into `anyhow::Error`
/// and other error-handling frameworks.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
/// Global WAMR runtime initialization failed.
RuntimeInit,
/// A module failed to load/validate. Carries WAMR's diagnostic message.
ModuleLoad(String),
/// A module failed to instantiate. Carries WAMR's diagnostic message.
Instantiate(String),
/// No exported function with the requested name was found.
FuncNotFound(String),
/// A call trapped or otherwise failed. Carries WAMR's exception string.
Trap(String),
/// The number of provided arguments/results did not match the callee.
SignatureMismatch {
/// What was expected (e.g. parameter count).
expected: usize,
/// What was actually provided.
provided: usize,
},
/// No exported global with the requested name was found.
GlobalNotFound(String),
/// Attempted to set an immutable (`const`) global.
GlobalImmutable,
/// A value's type did not match the expected type.
TypeMismatch {
/// The type that was required.
expected: ValType,
/// The type that was provided.
provided: ValType,
},
/// Encountered a Wasm value type these bindings do not model (only the four
/// numeric types `i32`/`i64`/`f32`/`f64` are supported).
UnsupportedType,
}
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::RuntimeInit => write!(f, "failed to initialize the WAMR runtime"),
Error::ModuleLoad(msg) => write!(f, "failed to load Wasm module: {msg}"),
Error::Instantiate(msg) => write!(f, "failed to instantiate Wasm module: {msg}"),
Error::FuncNotFound(name) => write!(f, "exported function not found: {name}"),
Error::Trap(msg) => write!(f, "Wasm trap: {msg}"),
Error::SignatureMismatch { expected, provided } => {
write!(
f,
"signature mismatch: expected {expected}, provided {provided}"
)
}
Error::GlobalNotFound(name) => write!(f, "exported global not found: {name}"),
Error::GlobalImmutable => write!(f, "cannot set an immutable global"),
Error::TypeMismatch { expected, provided } => {
write!(f, "type mismatch: expected {expected}, provided {provided}")
}
Error::UnsupportedType => {
write!(f, "unsupported Wasm value type (only i32/i64/f32/f64)")
}
}
}
}
impl std::error::Error for Error {}
/// Convenience result alias for `wamrx`.
pub type Result<T> = core::result::Result<T, Error>;