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!(Exception
15    @cfg feature = "artifact-size" => derive(loupe::MemoryUsage)
16    @derives Debug, Clone, PartialEq, Eq, derive_more::From
17);
18
19impl BackendException {
20    /// Create a new exception with the given tag type and payload.
21    #[inline]
22    #[allow(irrefutable_let_patterns)]
23    pub fn new(store: &mut impl AsStoreMut, tag: &Tag, payload: &[Value]) -> Self {
24        match &store.as_store_mut().inner.store {
25            #[cfg(feature = "sys")]
26            crate::BackendStore::Sys(_) => {
27                let BackendTag::Sys(tag) = &tag.0 else {
28                    panic!("cannot create Exception with Tag from another backend");
29                };
30
31                Self::Sys(crate::backend::sys::exception::Exception::new(
32                    store, tag, payload,
33                ))
34            }
35            _ => unimplemented!("new is only implemented for the sys backend"),
36        }
37    }
38
39    /// Checks whether this `Exception` can be used with the given store.
40    #[inline]
41    pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
42        match self {
43            #[cfg(feature = "sys")]
44            Self::Sys(s) => s.is_from_store(store),
45            _ => unimplemented!("is_from_store is only implemented for the sys backend"),
46        }
47    }
48
49    /// Gets the exception tag.
50    #[inline]
51    pub fn tag(&self, store: &impl AsStoreRef) -> Tag {
52        match self {
53            #[cfg(feature = "sys")]
54            Self::Sys(s) => Tag(BackendTag::Sys(s.tag(store))),
55            _ => unimplemented!("tag is only implemented for the sys backend"),
56        }
57    }
58
59    /// Gets the exception payload values.
60    #[inline]
61    pub fn payload(&self, store: &mut impl AsStoreMut) -> Vec<Value> {
62        match self {
63            #[cfg(feature = "sys")]
64            Self::Sys(s) => s.payload(store),
65            _ => unimplemented!("payload is only implemented for the sys backend"),
66        }
67    }
68
69    /// Get the `VMExceptionRef` corresponding to this `Exception`.
70    #[inline]
71    pub fn vm_exceptionref(&self) -> VMExceptionRef {
72        match self {
73            #[cfg(feature = "sys")]
74            Self::Sys(s) => VMExceptionRef::Sys(s.exnref()),
75            _ => unimplemented!("vm_exceptionref is only implemented for the sys backend"),
76        }
77    }
78
79    /// Creates a new `Exception` from a `VMExceptionRef`.
80    #[inline]
81    pub fn from_vm_exceptionref(exnref: VMExceptionRef) -> Self {
82        match exnref {
83            #[cfg(feature = "sys")]
84            VMExceptionRef::Sys(s) => {
85                Self::Sys(crate::backend::sys::exception::Exception::from_exnref(s))
86            }
87            _ => unimplemented!("from_vm_exceptionref is only implemented for the sys backend"),
88        }
89    }
90}