linera_wasmer/
extern_ref.rs

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