#[cfg(feature = "core")]
use crate::alloc::borrow::Cow;
use crate::js::lib::std::string::String;
use crate::js::trap::RuntimeError;
#[cfg(feature = "std")]
use std::borrow::Cow;
#[cfg(feature = "std")]
use thiserror::Error;
use wasmer_types::ImportError;
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum CompileError {
#[cfg_attr(feature = "std", error("WebAssembly translation error: {0}"))]
Wasm(WasmError),
#[cfg_attr(feature = "std", error("Compilation error: {0}"))]
Codegen(String),
#[cfg_attr(feature = "std", error("Validation error: {0}"))]
Validate(String),
#[cfg_attr(feature = "std", error("Feature {0} is not yet supported"))]
UnsupportedFeature(String),
#[cfg_attr(feature = "std", error("The target {0} is not yet supported (see https://docs.wasmer.io/ecosystem/wasmer/wasmer-features)"))]
UnsupportedTarget(String),
#[cfg_attr(feature = "std", error("Insufficient resources: {0}"))]
Resource(String),
}
#[cfg(feature = "core")]
impl std::fmt::Display for CompileError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "CompileError")
}
}
impl From<WasmError> for CompileError {
fn from(original: WasmError) -> Self {
Self::Wasm(original)
}
}
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum WasmError {
#[cfg_attr(
feature = "std",
error("Invalid input WebAssembly code at offset {offset}: {message}")
)]
InvalidWebAssembly {
message: String,
offset: usize,
},
#[cfg_attr(feature = "std", error("Unsupported feature: {0}"))]
Unsupported(String),
#[cfg_attr(feature = "std", error("{0} doesn't match js value type {1}"))]
TypeMismatch(Cow<'static, str>, Cow<'static, str>),
#[cfg_attr(feature = "std", error("{0}"))]
Generic(String),
}
impl From<wasm_bindgen::JsValue> for WasmError {
fn from(err: wasm_bindgen::JsValue) -> Self {
Self::Generic(
if err.is_string() && err.as_string().filter(|s| !s.is_empty()).is_some() {
err.as_string().unwrap_or_default()
} else {
format!("Unexpected Javascript error: {:?}", err)
},
)
}
}
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum SerializeError {
#[cfg_attr(feature = "std", error(transparent))]
Io(#[cfg_attr(feature = "std", from)] std::io::Error),
#[cfg_attr(feature = "std", error("{0}"))]
Generic(String),
}
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum DeserializeError {
#[cfg_attr(feature = "std", error(transparent))]
Io(#[cfg_attr(feature = "std", from)] std::io::Error),
#[cfg_attr(feature = "std", error("{0}"))]
Generic(String),
#[cfg_attr(feature = "std", error("incompatible binary: {0}"))]
Incompatible(String),
#[cfg_attr(feature = "std", error("corrupted binary: {0}"))]
CorruptedBinary(String),
#[cfg_attr(feature = "std", error(transparent))]
Compiler(CompileError),
}
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(Error))]
#[cfg_attr(feature = "std", error("Link error: {0}"))]
pub enum LinkError {
#[cfg_attr(feature = "std", error("Error while importing {0:?}.{1:?}: {2}"))]
Import(String, String, ImportError),
#[cfg(not(target_arch = "wasm32"))]
#[cfg_attr(feature = "std", error("RuntimeError occurred during linking: {0}"))]
Trap(#[source] RuntimeError),
#[cfg_attr(feature = "std", error("Insufficient resources: {0}"))]
Resource(String),
}
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum InstantiationError {
#[cfg_attr(feature = "std", error(transparent))]
Link(LinkError),
#[cfg_attr(feature = "std", error(transparent))]
Start(RuntimeError),
#[cfg_attr(feature = "std", error("missing required CPU features: {0:?}"))]
CpuFeature(String),
#[cfg_attr(feature = "std", error("cannot mix imports from different stores"))]
DifferentStores,
#[cfg_attr(feature = "std", error(transparent))]
Wasm(WasmError),
#[cfg_attr(feature = "std", error("Can't get {0} from the instance exports"))]
NotInExports(String),
}
impl From<WasmError> for InstantiationError {
fn from(original: WasmError) -> Self {
Self::Wasm(original)
}
}
#[cfg(feature = "core")]
impl std::fmt::Display for InstantiationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "InstantiationError")
}
}