wasmer/entities/external/extref/
mod.rs

1use std::any::Any;
2
3use crate::StoreRef;
4use crate::entities::store::{AsStoreMut, AsStoreRef};
5use crate::vm::VMExternRef;
6
7pub(crate) mod inner;
8pub(crate) use inner::*;
9
10#[derive(Debug, Clone, derive_more::From)]
11/// An opaque reference to some data. This reference can be passed through Wasm.
12pub struct ExternRef(pub(crate) BackendExternRef);
13
14impl ExternRef {
15    /// Make a new extern reference
16    pub fn new<T>(store: &mut impl AsStoreMut, value: T) -> Self
17    where
18        T: Any + Send + Sync + 'static + Sized,
19    {
20        Self(BackendExternRef::new(store, value))
21    }
22
23    /// Try to downcast to the given value.
24    pub fn downcast<'a, T>(&self, store: &'a impl AsStoreRef) -> Option<&'a T>
25    where
26        T: Any + Send + Sync + 'static + Sized,
27    {
28        self.0.downcast(store)
29    }
30
31    /// Create a [`VMExternRef`] from [`Self`].
32    pub(crate) fn vm_externref(&self) -> VMExternRef {
33        self.0.vm_externref()
34    }
35
36    /// Create an instance of [`Self`] from a [`VMExternRef`].
37    pub(crate) unsafe fn from_vm_externref(
38        store: &mut impl AsStoreMut,
39        vm_externref: VMExternRef,
40    ) -> Self {
41        Self(unsafe { BackendExternRef::from_vm_externref(store, vm_externref) })
42    }
43
44    /// Checks whether this `ExternRef` can be used with the given context.
45    ///
46    /// Primitive (`i32`, `i64`, etc) and null funcref/externref values are not
47    /// tied to a context and can be freely shared between contexts.
48    ///
49    /// Externref and funcref values are tied to a context and can only be used
50    /// with that context.
51    pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
52        self.0.is_from_store(store)
53    }
54}