wasmer/entities/exception/
mod.rs

1pub(crate) mod inner;
2pub(crate) use inner::*;
3use wasmer_types::{TagType, Type};
4
5use crate::{
6    AsStoreMut, AsStoreRef, ExportError, Exportable, Extern, Tag, Value,
7    vm::{VMExceptionRef, VMExtern, VMExternTag},
8};
9
10/// A WebAssembly `exception` instance.
11///
12/// An exception is an internal construct in WebAssembly that represents a runtime object that can
13/// be thrown. A WebAssembly exception consists of an exception tag and its runtime arguments.
14///
15/// Spec: <https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/Exceptions.md#exceptions>
16#[derive(Debug, Clone, PartialEq, Eq, derive_more::From)]
17#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
18pub struct Exception(pub(crate) BackendException);
19
20impl Exception {
21    /// Create a new exception with the given tag and payload, and also creates
22    /// a reference to it, returning the reference.
23    pub fn new(store: &mut impl AsStoreMut, tag: &Tag, payload: &[Value]) -> Self {
24        Self(BackendException::new(store, tag, payload))
25    }
26
27    /// Check whether this `Exception` comes from the given store.
28    pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
29        self.0.is_from_store(store)
30    }
31
32    /// Get the exception tag.
33    pub fn tag(&self, store: &impl AsStoreRef) -> Tag {
34        self.0.tag(store)
35    }
36
37    /// Get the exception payload values.
38    pub fn payload(&self, store: &mut impl AsStoreMut) -> Vec<Value> {
39        self.0.payload(store)
40    }
41
42    /// Get the `VMExceptionRef` corresponding to this `Exception`.
43    pub fn vm_exceptionref(&self) -> VMExceptionRef {
44        self.0.vm_exceptionref()
45    }
46
47    /// Create an `Exception` from a `VMExceptionRef`.
48    pub fn from_vm_exceptionref(exnref: VMExceptionRef) -> Self {
49        Self(BackendException::from_vm_exceptionref(exnref))
50    }
51}