use std::{any::Any, error::Error, fmt::Debug};
#[cfg(feature = "sys")]
use crate::BackendException;
use crate::{Exception, RuntimeError, macros::backend::match_rt};
#[derive(Debug, derive_more::From)]
pub enum BackendTrap {
#[cfg(feature = "sys")]
Sys(crate::backend::sys::vm::Trap),
#[cfg(feature = "wamr")]
Wamr(crate::backend::wamr::vm::Trap),
#[cfg(feature = "wasmi")]
Wasmi(crate::backend::wasmi::vm::Trap),
#[cfg(feature = "v8")]
V8(crate::backend::v8::vm::Trap),
#[cfg(feature = "js")]
Js(crate::backend::js::vm::Trap),
#[cfg(feature = "jsc")]
Jsc(crate::backend::jsc::vm::Trap),
}
impl BackendTrap {
pub fn user(err: Box<dyn Error + Send + Sync>) -> RuntimeError {
#[cfg(feature = "sys")]
{
return crate::backend::sys::vm::Trap::user(err).into();
}
#[cfg(feature = "wamr")]
{
return crate::backend::wamr::vm::Trap::user(err).into();
}
#[cfg(feature = "wasmi")]
{
return crate::backend::wasmi::vm::Trap::user(err).into();
}
#[cfg(feature = "v8")]
{
return crate::backend::v8::vm::Trap::user(err).into();
}
#[cfg(feature = "js")]
{
return crate::backend::js::vm::Trap::user(err).into();
}
#[cfg(feature = "jsc")]
{
return crate::backend::jsc::vm::Trap::user(err).into();
}
panic!("No runtime enabled!")
}
#[inline]
pub fn downcast<T: Error + 'static>(self) -> Result<T, Self> {
match_rt!(on self => s {
s.downcast::<T>().map_err(Into::into)
})
}
#[inline]
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
match_rt!(on self => s {
s.downcast_ref::<T>()
})
}
#[inline]
pub fn is<T: Error + 'static>(&self) -> bool {
match_rt!(on self => s {
s.is::<T>()
})
}
#[inline]
pub fn is_exception(&self) -> bool {
match_rt!(on self => s {
s.is_exception()
})
}
#[inline]
pub fn to_exception(&self) -> Option<Exception> {
match self {
#[cfg(feature = "sys")]
Self::Sys(s) => s
.to_exception_ref()
.map(|e| Exception::from_vm_exceptionref(crate::vm::VMExceptionRef::Sys(e))),
#[cfg(feature = "wamr")]
Self::Wamr(s) => s
.to_exception_ref()
.map(|e| Exception::from_vm_exceptionref(crate::vm::VMExceptionRef::Wamr(e))),
#[cfg(feature = "wasmi")]
Self::Wasmi(s) => s
.to_exception_ref()
.map(|e| Exception::from_vm_exceptionref(crate::vm::VMExceptionRef::Wasmi(e))),
#[cfg(feature = "v8")]
Self::V8(s) => s
.to_exception_ref()
.map(|e| Exception::from_vm_exceptionref(crate::vm::VMExceptionRef::V8(e))),
#[cfg(feature = "js")]
Self::Js(s) => s
.to_exception_ref()
.map(|e| Exception::from_vm_exceptionref(crate::vm::VMExceptionRef::Js(e))),
#[cfg(feature = "jsc")]
Self::Jsc(s) => s
.to_exception_ref()
.map(|e| Exception::from_vm_exceptionref(crate::vm::VMExceptionRef::Jsc(e))),
}
}
}
impl std::fmt::Display for BackendTrap {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match_rt!(on self => s {
(s as &dyn std::fmt::Display).fmt(f)
})
}
}
impl std::error::Error for BackendTrap {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match_rt!(on self => s {
s.source()
})
}
}