1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use std::ops::{Deref, DerefMut};
use super::{StoreObjects, inner::StoreInner};
use crate::entities::engine::{AsEngineRef, Engine, EngineRef};
#[cfg(feature = "experimental-async")]
use crate::{AsStoreAsync, StoreAsync};
use wasmer_types::{ExternType, OnCalledAction};
//use wasmer_vm::{StoreObjects, TrapHandlerFn};
#[cfg(feature = "sys")]
use wasmer_vm::TrapHandlerFn;
/// A temporary handle to a [`crate::Store`].
#[derive(Debug)]
pub struct StoreRef<'a> {
pub(crate) inner: &'a StoreInner,
}
impl<'a> StoreRef<'a> {
pub(crate) fn objects(&self) -> &'a StoreObjects {
&self.inner.objects
}
/// Returns the [`Engine`].
pub fn engine(&self) -> &Engine {
self.inner.store.engine()
}
/// Checks whether two stores are identical. A store is considered
/// equal to another store if both have the same engine.
pub fn same(a: &Self, b: &Self) -> bool {
StoreObjects::same(&a.inner.objects, &b.inner.objects)
}
/// The signal handler
#[cfg(feature = "sys")]
#[inline]
pub fn signal_handler(&self) -> Option<*const TrapHandlerFn<'static>> {
use crate::backend::sys::entities::store::NativeStoreExt;
self.inner.store.as_sys().signal_handler()
}
}
/// A temporary handle to a [`crate::Store`].
pub struct StoreMut<'a> {
pub(crate) inner: &'a mut StoreInner,
}
impl StoreMut<'_> {
/// Returns the [`Engine`].
pub fn engine(&self) -> &Engine {
self.inner.store.engine()
}
/// Checks whether two stores are identical. A store is considered
/// equal to another store if both have the same engine.
pub fn same(a: &Self, b: &Self) -> bool {
StoreObjects::same(&a.inner.objects, &b.inner.objects)
}
#[allow(unused)]
pub(crate) fn as_raw(&self) -> *mut StoreInner {
self.inner as *const StoreInner as *mut StoreInner
}
#[allow(unused)]
pub(crate) unsafe fn from_raw(raw: *mut StoreInner) -> Self {
Self {
inner: unsafe { &mut *raw },
}
}
#[allow(unused)]
pub(crate) fn engine_and_objects_mut(&mut self) -> (&Engine, &mut StoreObjects) {
(self.inner.store.engine(), &mut self.inner.objects)
}
/// Parks this borrow of the store for the duration of `f`, lending the
/// store to code that runs inside `f` without reaching it through Rust:
/// [`Store::with_current`](crate::Store::with_current) hands the store back to any frame `f` reaches,
/// however many foreign frames deep.
///
/// This is how host code lends the store across an FFI boundary. An
/// embedded engine that calls back into the host — a JS engine's allocator
/// hook, say — cannot be handed a Rust reference through its own C frames,
/// and cannot be given one out of band either, because the imported
/// function it was called from is still holding the only borrow. Parking
/// that borrow, which the `&mut self` receiver here makes unreachable for
/// exactly as long as `f` runs, is what makes lending it sound rather than
/// aliasing.
///
/// Parks nest, and a store nobody parked stays unlendable:
/// [`Store::with_current`](crate::Store::with_current) returns `None`
/// unless every borrow on this thread's stack has been parked this way.
///
/// ```
/// use wasmer::{AsStoreMut, FunctionEnvMut, Store};
///
/// // A host function lending its store to code it calls into.
/// fn host_call(mut env: FunctionEnvMut<'_, ()>) {
/// // `env` holds the store, so nobody else may have it.
/// assert!(Store::with_current(|_| ()).is_none());
///
/// env.as_store_mut().parked(|| {
/// // ...but code reached from here can pick it back up.
/// assert!(Store::with_current(|_| ()).is_some());
/// });
/// }
/// ```
pub fn parked<R>(&mut self, f: impl FnOnce() -> R) -> R {
// This is the same thing `Function::call` does before entering Wasm:
// install this borrow as the store executing on the thread, so that
// code which reaches the store through the context — rather than
// through a reference it was handed — picks up a borrow derived from
// *this* one. The new entry starts unborrowed, which is what makes the
// store lendable for as long as it is on top.
let ptr: *mut StoreInner = &mut *self.inner;
// SAFETY: `ptr` comes from `&mut *self.inner`, which `&mut self` keeps
// alive and unreachable for every frame on this thread until `f`
// returns and the guard is dropped.
let _guard = unsafe { super::StoreContext::install(ptr) };
f()
}
// TODO: OnCalledAction is needed for asyncify. It will be refactored with https://github.com/wasmerio/wasmer/issues/3451
/// Sets the unwind callback which will be invoked when the call finishes
pub fn on_called<F>(&mut self, callback: F)
where
F: FnOnce(StoreMut<'_>) -> Result<OnCalledAction, Box<dyn std::error::Error + Send + Sync>>
+ Send
+ Sync
+ 'static,
{
self.inner.on_called.replace(Box::new(callback));
}
}
/// Helper trait for a value that is convertible to a [`StoreRef`].
pub trait AsStoreRef {
/// Returns a `StoreRef` pointing to the underlying context.
fn as_store_ref(&self) -> StoreRef<'_>;
/// Returns a [`StoreAsync`] if the current
/// context is asynchronous. The store will be locked since
/// it's already active in the current context, but can be used
/// to spawn new coroutines via
/// [`Function::call_async`](crate::Function::call_async).
#[cfg(feature = "experimental-async")]
fn as_store_async(&self) -> Option<impl AsStoreAsync + 'static> {
let id = self.as_store_ref().inner.objects.id();
StoreAsync::from_context(id)
}
}
/// Helper trait for a value that is convertible to a [`StoreMut`].
pub trait AsStoreMut: AsStoreRef {
/// Returns a `StoreMut` pointing to the underlying context.
fn as_store_mut(&mut self) -> StoreMut<'_>;
/// Returns the ObjectMutable
fn objects_mut(&mut self) -> &mut StoreObjects;
}
impl AsStoreRef for StoreRef<'_> {
fn as_store_ref(&self) -> StoreRef<'_> {
StoreRef { inner: self.inner }
}
}
impl AsEngineRef for StoreRef<'_> {
fn as_engine_ref(&self) -> EngineRef<'_> {
self.inner.store.as_engine_ref()
}
}
impl AsStoreRef for StoreMut<'_> {
fn as_store_ref(&self) -> StoreRef<'_> {
StoreRef { inner: self.inner }
}
}
impl AsStoreMut for StoreMut<'_> {
fn as_store_mut(&mut self) -> StoreMut<'_> {
StoreMut { inner: self.inner }
}
fn objects_mut(&mut self) -> &mut StoreObjects {
&mut self.inner.objects
}
}
impl<P> AsStoreRef for P
where
P: Deref,
P::Target: AsStoreRef,
{
fn as_store_ref(&self) -> StoreRef<'_> {
(**self).as_store_ref()
}
}
impl<P> AsStoreMut for P
where
P: DerefMut,
P::Target: AsStoreMut,
{
fn as_store_mut(&mut self) -> StoreMut<'_> {
(**self).as_store_mut()
}
fn objects_mut(&mut self) -> &mut StoreObjects {
(**self).objects_mut()
}
}
impl AsEngineRef for StoreMut<'_> {
fn as_engine_ref(&self) -> EngineRef<'_> {
self.inner.store.as_engine_ref()
}
}