wasmer/entities/store/
mod.rs1mod inner;
6
7mod store_ref;
9pub use store_ref::*;
10
11mod obj;
12pub use obj::*;
13
14use crate::{AsEngineRef, BackendEngine, Engine, EngineRef};
15pub(crate) use inner::*;
16use wasmer_types::StoreId;
17
18#[cfg(feature = "sys")]
19use wasmer_vm::TrapHandlerFn;
20
21pub struct Store {
32 pub(crate) inner: Box<StoreInner>,
33}
34
35impl Store {
36 pub fn new(engine: impl Into<Engine>) -> Self {
38 let engine: Engine = engine.into();
39
40 let store = match engine.be {
41 #[cfg(feature = "sys")]
42 BackendEngine::Sys(_) => {
43 BackendStore::Sys(crate::backend::sys::entities::store::Store::new(engine))
44 }
45 #[cfg(feature = "wamr")]
46 BackendEngine::Wamr(_) => {
47 BackendStore::Wamr(crate::backend::wamr::entities::store::Store::new(engine))
48 }
49 #[cfg(feature = "wasmi")]
50 BackendEngine::Wasmi(_) => {
51 BackendStore::Wasmi(crate::backend::wasmi::entities::store::Store::new(engine))
52 }
53 #[cfg(feature = "v8")]
54 BackendEngine::V8(_) => {
55 BackendStore::V8(crate::backend::v8::entities::store::Store::new(engine))
56 }
57 #[cfg(feature = "js")]
58 BackendEngine::Js(_) => {
59 BackendStore::Js(crate::backend::js::entities::store::Store::new(engine))
60 }
61 #[cfg(feature = "jsc")]
62 BackendEngine::Jsc(_) => {
63 BackendStore::Jsc(crate::backend::jsc::entities::store::Store::new(engine))
64 }
65 };
66
67 Self {
68 inner: Box::new(StoreInner {
69 objects: StoreObjects::from_store_ref(&store),
70 on_called: None,
71 store,
72 }),
73 }
74 }
75
76 #[cfg(feature = "sys")]
77 pub fn set_trap_handler(&mut self, handler: Option<Box<TrapHandlerFn<'static>>>) {
84 use crate::backend::sys::entities::store::NativeStoreExt;
85 #[allow(irrefutable_let_patterns)]
86 if let BackendStore::Sys(ref mut s) = self.inner.store {
87 s.set_trap_handler(handler)
88 }
89 }
90
91 pub fn engine(&self) -> &Engine {
93 self.inner.store.engine()
94 }
95
96 pub fn engine_mut(&mut self) -> &mut Engine {
98 self.inner.store.engine_mut()
99 }
100
101 pub fn same(a: &Self, b: &Self) -> bool {
104 a.id() == b.id()
105 }
106
107 pub fn id(&self) -> StoreId {
109 self.inner.objects.id()
110 }
111}
112
113impl PartialEq for Store {
114 fn eq(&self, other: &Self) -> bool {
115 Self::same(self, other)
116 }
117}
118
119unsafe impl Send for Store {}
122unsafe impl Sync for Store {}
123
124impl Default for Store {
125 fn default() -> Self {
126 Self::new(Engine::default())
127 }
128}
129
130impl std::fmt::Debug for Store {
131 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
132 f.debug_struct("Store").finish()
133 }
134}
135
136impl AsEngineRef for Store {
137 fn as_engine_ref(&self) -> EngineRef<'_> {
138 self.inner.store.as_engine_ref()
139 }
140
141 fn maybe_as_store(&self) -> Option<StoreRef<'_>> {
142 Some(self.as_store_ref())
143 }
144}
145
146impl AsStoreRef for Store {
147 fn as_store_ref(&self) -> StoreRef<'_> {
148 StoreRef { inner: &self.inner }
149 }
150}
151impl AsStoreMut for Store {
152 fn as_store_mut(&mut self) -> StoreMut<'_> {
153 StoreMut {
154 inner: &mut self.inner,
155 }
156 }
157
158 fn objects_mut(&mut self) -> &mut StoreObjects {
159 &mut self.inner.objects
160 }
161}