wasmer/entities/store/
obj.rs

1use wasmer_types::StoreId;
2
3use crate::{BackendStore, macros::backend::match_rt};
4
5/// Set of objects managed by a context.
6#[derive(Debug)]
7pub enum StoreObjects {
8    #[cfg(feature = "sys")]
9    /// Store objects for the `sys` runtime.
10    Sys(crate::backend::sys::store::StoreObjects),
11
12    #[cfg(feature = "wamr")]
13    /// Store objects for the `wamr` runtime.
14    Wamr(crate::backend::wamr::store::StoreObjects),
15
16    #[cfg(feature = "wasmi")]
17    /// Store objects for the `wasmi` runtime.
18    Wasmi(crate::backend::wasmi::store::StoreObjects),
19
20    #[cfg(feature = "v8")]
21    /// Store objects for the `v8` runtime.
22    V8(crate::backend::v8::store::StoreObjects),
23
24    #[cfg(feature = "js")]
25    /// Store objects for the `js` runtime.
26    Js(crate::backend::js::store::StoreObjects),
27
28    #[cfg(feature = "jsc")]
29    /// Store objects for the `jsc` runtime.
30    Jsc(crate::backend::jsc::store::StoreObjects),
31}
32
33impl StoreObjects {
34    /// Checks whether two stores are identical. A store is considered
35    /// equal to another store if both have the same engine.
36    #[inline]
37    pub fn same(a: &Self, b: &Self) -> bool {
38        match (a, b) {
39            #[cfg(feature = "sys")]
40            (Self::Sys(a), Self::Sys(b)) => a.id() == b.id(),
41            #[cfg(feature = "wamr")]
42            (Self::Wamr(a), Self::Wamr(b)) => a.id() == b.id(),
43            #[cfg(feature = "v8")]
44            (Self::V8(a), Self::V8(b)) => a.id() == b.id(),
45            #[cfg(feature = "js")]
46            (Self::Js(a), Self::Js(b)) => a.id() == b.id(),
47
48            #[cfg(feature = "jsc")]
49            (Self::Jsc(a), Self::Jsc(b)) => a.id() == b.id(),
50
51            _ => panic!(
52                "Incompatible `StoreObjects` instance: {}, {}!",
53                a.id(),
54                b.id()
55            ),
56        }
57    }
58
59    /// Returns the ID of this store
60    #[inline]
61    pub fn id(&self) -> StoreId {
62        match_rt!(on self => s {
63            s.id()
64        })
65    }
66
67    #[inline]
68    pub(crate) fn from_store_ref(store: &BackendStore) -> Self {
69        match store {
70            #[cfg(feature = "sys")]
71            BackendStore::Sys(_) => Self::Sys(Default::default()),
72            #[cfg(feature = "wamr")]
73            BackendStore::Wamr(_) => Self::Wamr(Default::default()),
74            #[cfg(feature = "wasmi")]
75            BackendStore::Wasmi(_) => Self::Wasmi(Default::default()),
76            #[cfg(feature = "v8")]
77            BackendStore::V8(_) => Self::V8(Default::default()),
78            #[cfg(feature = "js")]
79            BackendStore::Js(_) => Self::Js(Default::default()),
80            #[cfg(feature = "jsc")]
81            BackendStore::Jsc(_) => Self::Jsc(Default::default()),
82        }
83    }
84
85    /// Return a vector of all globals and converted to u128
86    #[inline]
87    pub fn as_u128_globals(&self) -> Vec<u128> {
88        match_rt!(on self => s {
89            s.as_u128_globals()
90        })
91    }
92
93    /// Set a global, at index idx. Will panic if idx is out of range
94    /// Safety: the caller should check taht the raw value is compatible
95    /// with destination VMGlobal type
96    #[inline]
97    pub fn set_global_unchecked(&self, idx: usize, val: u128) {
98        match_rt!(on self => s {
99            s.set_global_unchecked(idx, val)
100        })
101    }
102}