wasmer/entities/store/
store_ref.rs1use std::ops::{Deref, DerefMut};
2
3use super::{StoreObjects, inner::StoreInner};
4use crate::entities::engine::{AsEngineRef, Engine, EngineRef};
5#[cfg(feature = "experimental-async")]
6use crate::{AsStoreAsync, StoreAsync};
7use wasmer_types::{ExternType, OnCalledAction};
8#[cfg(feature = "sys")]
11use wasmer_vm::TrapHandlerFn;
12
13#[derive(Debug)]
15pub struct StoreRef<'a> {
16 pub(crate) inner: &'a StoreInner,
17}
18
19impl<'a> StoreRef<'a> {
20 pub(crate) fn objects(&self) -> &'a StoreObjects {
21 &self.inner.objects
22 }
23
24 pub fn engine(&self) -> &Engine {
26 self.inner.store.engine()
27 }
28
29 pub fn same(a: &Self, b: &Self) -> bool {
32 StoreObjects::same(&a.inner.objects, &b.inner.objects)
33 }
34
35 #[cfg(feature = "sys")]
37 #[inline]
38 pub fn signal_handler(&self) -> Option<*const TrapHandlerFn<'static>> {
39 use crate::backend::sys::entities::store::NativeStoreExt;
40 self.inner.store.as_sys().signal_handler()
41 }
42}
43
44pub struct StoreMut<'a> {
46 pub(crate) inner: &'a mut StoreInner,
47}
48
49impl StoreMut<'_> {
50 pub fn engine(&self) -> &Engine {
52 self.inner.store.engine()
53 }
54
55 pub fn same(a: &Self, b: &Self) -> bool {
58 StoreObjects::same(&a.inner.objects, &b.inner.objects)
59 }
60
61 #[allow(unused)]
62 pub(crate) fn as_raw(&self) -> *mut StoreInner {
63 self.inner as *const StoreInner as *mut StoreInner
64 }
65
66 #[allow(unused)]
67 pub(crate) unsafe fn from_raw(raw: *mut StoreInner) -> Self {
68 Self {
69 inner: unsafe { &mut *raw },
70 }
71 }
72
73 #[allow(unused)]
74 pub(crate) fn engine_and_objects_mut(&mut self) -> (&Engine, &mut StoreObjects) {
75 (self.inner.store.engine(), &mut self.inner.objects)
76 }
77
78 pub fn on_called<F>(&mut self, callback: F)
81 where
82 F: FnOnce(StoreMut<'_>) -> Result<OnCalledAction, Box<dyn std::error::Error + Send + Sync>>
83 + Send
84 + Sync
85 + 'static,
86 {
87 self.inner.on_called.replace(Box::new(callback));
88 }
89}
90
91pub trait AsStoreRef {
93 fn as_store_ref(&self) -> StoreRef<'_>;
95
96 #[cfg(feature = "experimental-async")]
102 fn as_store_async(&self) -> Option<impl AsStoreAsync + 'static> {
103 let id = self.as_store_ref().inner.objects.id();
104 StoreAsync::from_context(id)
105 }
106}
107
108pub trait AsStoreMut: AsStoreRef {
110 fn as_store_mut(&mut self) -> StoreMut<'_>;
112
113 fn objects_mut(&mut self) -> &mut StoreObjects;
115}
116
117impl AsStoreRef for StoreRef<'_> {
118 fn as_store_ref(&self) -> StoreRef<'_> {
119 StoreRef { inner: self.inner }
120 }
121}
122
123impl AsEngineRef for StoreRef<'_> {
124 fn as_engine_ref(&self) -> EngineRef<'_> {
125 self.inner.store.as_engine_ref()
126 }
127}
128
129impl AsStoreRef for StoreMut<'_> {
130 fn as_store_ref(&self) -> StoreRef<'_> {
131 StoreRef { inner: self.inner }
132 }
133}
134impl AsStoreMut for StoreMut<'_> {
135 fn as_store_mut(&mut self) -> StoreMut<'_> {
136 StoreMut { inner: self.inner }
137 }
138
139 fn objects_mut(&mut self) -> &mut StoreObjects {
140 &mut self.inner.objects
141 }
142}
143
144impl<P> AsStoreRef for P
145where
146 P: Deref,
147 P::Target: AsStoreRef,
148{
149 fn as_store_ref(&self) -> StoreRef<'_> {
150 (**self).as_store_ref()
151 }
152}
153
154impl<P> AsStoreMut for P
155where
156 P: DerefMut,
157 P::Target: AsStoreMut,
158{
159 fn as_store_mut(&mut self) -> StoreMut<'_> {
160 (**self).as_store_mut()
161 }
162
163 fn objects_mut(&mut self) -> &mut StoreObjects {
164 (**self).objects_mut()
165 }
166}
167
168impl AsEngineRef for StoreMut<'_> {
169 fn as_engine_ref(&self) -> EngineRef<'_> {
170 self.inner.store.as_engine_ref()
171 }
172}