wasmer/entities/exception/
inner.rs

1use crate::vm::VMExceptionRef;
2use crate::{
3    AsStoreMut, AsStoreRef, BackendTag, ExportError, Exportable, Extern, Tag, Value,
4    macros::backend::{gen_rt_ty, match_rt},
5    vm::{VMExtern, VMExternTag},
6};
7
8/// A WebAssembly `global` instance.
9///
10/// A global instance is the runtime representation of a global variable.
11/// It consists of an individual value and a flag indicating whether it is mutable.
12///
13/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#global-instances>
14gen_rt_ty! {
15    #[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
16    #[derive(Debug, Clone, PartialEq, Eq, derive_more::From)]
17    pub(crate) BackendException(entities::exception::Exception);
18}
19
20impl BackendException {
21    /// Create a new exception with the given tag type and payload.
22    #[inline]
23    #[allow(irrefutable_let_patterns)]
24    pub fn new(store: &mut impl AsStoreMut, tag: &Tag, payload: &[Value]) -> Self {
25        match &store.as_store_mut().inner.store {
26            #[cfg(feature = "sys")]
27            crate::BackendStore::Sys(_) => {
28                let BackendTag::Sys(tag) = &tag.0 else {
29                    panic!("cannot create Exception with Tag from another backend");
30                };
31
32                Self::Sys(crate::backend::sys::exception::Exception::new(
33                    store, tag, payload,
34                ))
35            }
36            _ => unimplemented!("new is only implemented for the sys backend"),
37        }
38    }
39
40    /// Checks whether this `Exception` can be used with the given store.
41    #[inline]
42    pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
43        match self {
44            #[cfg(feature = "sys")]
45            Self::Sys(s) => s.is_from_store(store),
46            _ => unimplemented!("is_from_store is only implemented for the sys backend"),
47        }
48    }
49
50    /// Gets the exception tag.
51    #[inline]
52    pub fn tag(&self, store: &impl AsStoreRef) -> Tag {
53        match self {
54            #[cfg(feature = "sys")]
55            Self::Sys(s) => Tag(BackendTag::Sys(s.tag(store))),
56            _ => unimplemented!("tag is only implemented for the sys backend"),
57        }
58    }
59
60    /// Gets the exception payload values.
61    #[inline]
62    pub fn payload(&self, store: &mut impl AsStoreMut) -> Vec<Value> {
63        match self {
64            #[cfg(feature = "sys")]
65            Self::Sys(s) => s.payload(store),
66            _ => unimplemented!("payload is only implemented for the sys backend"),
67        }
68    }
69
70    /// Get the `VMExceptionRef` corresponding to this `Exception`.
71    #[inline]
72    pub fn vm_exceptionref(&self) -> VMExceptionRef {
73        match self {
74            #[cfg(feature = "sys")]
75            Self::Sys(s) => VMExceptionRef::Sys(s.exnref()),
76            _ => unimplemented!("vm_exceptionref is only implemented for the sys backend"),
77        }
78    }
79
80    /// Creates a new `Exception` from a `VMExceptionRef`.
81    #[inline]
82    pub fn from_vm_exceptionref(exnref: VMExceptionRef) -> Self {
83        match exnref {
84            #[cfg(feature = "sys")]
85            VMExceptionRef::Sys(s) => {
86                Self::Sys(crate::backend::sys::exception::Exception::from_exnref(s))
87            }
88            _ => unimplemented!("from_vm_exceptionref is only implemented for the sys backend"),
89        }
90    }
91}