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 = "v8")]
13    /// Store objects for the `v8` runtime.
14    V8(crate::backend::v8::store::StoreObjects),
15
16    #[cfg(feature = "js")]
17    /// Store objects for the `js` runtime.
18    Js(crate::backend::js::store::StoreObjects),
19}
20
21impl StoreObjects {
22    /// Checks whether two stores are identical. A store is considered
23    /// equal to another store if both have the same engine.
24    #[inline]
25    pub fn same(a: &Self, b: &Self) -> bool {
26        match (a, b) {
27            #[cfg(feature = "sys")]
28            (Self::Sys(a), Self::Sys(b)) => a.id() == b.id(),
29            #[cfg(feature = "v8")]
30            (Self::V8(a), Self::V8(b)) => a.id() == b.id(),
31            #[cfg(feature = "js")]
32            (Self::Js(a), Self::Js(b)) => a.id() == b.id(),
33
34            _ => panic!(
35                "Incompatible `StoreObjects` instance: {}, {}!",
36                a.id(),
37                b.id()
38            ),
39        }
40    }
41
42    /// Returns the ID of this store
43    #[inline]
44    pub fn id(&self) -> StoreId {
45        match_rt!(on self => s {
46            s.id()
47        })
48    }
49
50    #[inline]
51    pub(crate) fn from_store_ref(store: &BackendStore) -> Self {
52        match store {
53            #[cfg(feature = "sys")]
54            BackendStore::Sys(_) => Self::Sys(Default::default()),
55            #[cfg(feature = "v8")]
56            BackendStore::V8(_) => Self::V8(Default::default()),
57            #[cfg(feature = "js")]
58            BackendStore::Js(_) => Self::Js(Default::default()),
59        }
60    }
61
62    /// Return a vector of all globals and converted to u128
63    #[inline]
64    pub fn as_u128_globals(&self) -> Vec<u128> {
65        match_rt!(on self => s {
66            s.as_u128_globals()
67        })
68    }
69
70    /// Set a global, at index idx. Will panic if idx is out of range
71    /// Safety: the caller should check that the raw value is compatible
72    /// with destination VMGlobal type
73    #[inline]
74    pub fn set_global_unchecked(&self, idx: usize, val: u128) {
75        match_rt!(on self => s {
76            s.set_global_unchecked(idx, val)
77        })
78    }
79
80    #[cfg(all(unix, feature = "experimental-host-interrupt"))]
81    /// Builds an [`Interrupter`] for this store
82    pub fn interrupter(&self) -> Interrupter {
83        match self {
84            #[cfg(feature = "sys")]
85            Self::Sys(s) => Interrupter(InterrupterInner::Sys(
86                crate::backend::sys::store::Interrupter::new(s.id()),
87            )),
88            _ => panic!("Interrupters can only be built for stores from the sys backend"),
89        }
90    }
91}
92
93/// Allows embedders to interrupt a running WASM instance.
94#[cfg(all(unix, feature = "experimental-host-interrupt"))]
95#[derive(Clone)]
96pub struct Interrupter(InterrupterInner);
97
98#[cfg(all(unix, feature = "experimental-host-interrupt"))]
99impl Interrupter {
100    /// Interrupts running WASM instances from the owning [`Store`](crate::Store).
101    pub fn interrupt(&self) {
102        self.0.interrupt()
103    }
104}
105
106/// Allows embedders to interrupt a running WASM instance.
107#[cfg(all(unix, feature = "experimental-host-interrupt"))]
108#[derive(Clone)]
109pub enum InterrupterInner {
110    #[cfg(feature = "sys")]
111    /// Interrupter for the `sys` runtime.
112    Sys(crate::backend::sys::store::Interrupter),
113}
114
115#[cfg(all(unix, feature = "experimental-host-interrupt"))]
116impl InterrupterInner {
117    /// Interrupts running WASM instances from the owning [`Store`](crate::Store).
118    pub fn interrupt(&self) {
119        match self {
120            #[cfg(feature = "sys")]
121            Self::Sys(i) => i.interrupt(),
122        }
123    }
124}