wasmer_vm/
exception_ref.rs

1use std::any::Any;
2use wasmer_types::RawValue;
3
4use crate::store::InternalStoreHandle;
5
6/// Underlying object referenced by a `VMExceptionRef`.
7#[derive(Debug)]
8pub struct VMExceptionObj {
9    contents: Box<dyn Any + Send + Sync + 'static>,
10}
11
12impl VMExceptionObj {
13    /// Wraps the given value to expose it to Wasm code as an externref.
14    pub fn new(val: impl Any + Send + Sync + 'static) -> Self {
15        Self {
16            contents: Box::new(val),
17        }
18    }
19
20    #[allow(clippy::should_implement_trait)]
21    /// Returns a reference to the underlying value.
22    pub fn as_ref(&self) -> &(dyn Any + Send + Sync + 'static) {
23        &*self.contents
24    }
25}
26
27/// Represents an opaque reference to any data within WebAssembly.
28#[repr(transparent)]
29#[derive(Debug, Clone, Copy)]
30pub struct VMExceptionRef(pub InternalStoreHandle<VMExceptionObj>);
31
32impl VMExceptionRef {
33    /// Converts the [`VMExceptionRef`] into a `RawValue`.
34    pub fn into_raw(self) -> RawValue {
35        RawValue {
36            funcref: self.0.index(),
37        }
38    }
39
40    /// Extracts a `VMExceptionRef` from a `RawValue`.
41    ///
42    /// # Safety
43    /// `raw` must be a valid `VMExceptionRef` instance.
44    pub unsafe fn from_raw(raw: RawValue) -> Option<Self> {
45        InternalStoreHandle::from_index(raw.externref).map(Self)
46    }
47}