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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use super::{SharedMemory, shared_memory_detach_error, view::*};
use wasmer_types::{MemoryError, MemoryType, Pages};
use crate::{
AsStoreMut, AsStoreRef, ExportError, Exportable, Extern, StoreMut, StoreRef,
macros::backend::{gen_rt_ty, match_rt},
vm::{VMExtern, VMExternMemory, VMMemory},
};
gen_rt_ty! {
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
#[derive(Debug, Clone, PartialEq, Eq, derive_more::From)]
pub BackendMemory(entities::memory::Memory);
}
impl BackendMemory {
/// Creates a new host [`BackendMemory`] from the provided [`MemoryType`].
///
/// This function will construct the `Memory` using the store
/// `BaseTunables`.
///
/// # Example
///
/// ```
/// # use wasmer::{Memory, MemoryType, Pages, Store, Type, Value};
/// # let mut store = Store::default();
/// #
/// let m = Memory::new(&mut store, MemoryType::new(1, None, false)).unwrap();
/// ```
#[inline]
pub fn new(store: &mut impl AsStoreMut, ty: MemoryType) -> Result<Self, MemoryError> {
match &store.as_store_mut().inner.store {
#[cfg(feature = "sys")]
crate::BackendStore::Sys(s) => Ok(Self::Sys(
crate::backend::sys::entities::memory::Memory::new(store, ty)?,
)),
#[cfg(feature = "v8")]
crate::BackendStore::V8(s) => Ok(Self::V8(
crate::backend::v8::entities::memory::Memory::new(store, ty)?,
)),
#[cfg(feature = "js")]
crate::BackendStore::Js(s) => Ok(Self::Js(
crate::backend::js::entities::memory::Memory::new(store, ty)?,
)),
}
}
/// Create a memory object from an existing memory and attaches it to the store
#[inline]
pub fn new_from_existing(new_store: &mut impl AsStoreMut, memory: VMMemory) -> Self {
match new_store.as_store_mut().inner.store {
#[cfg(feature = "sys")]
crate::BackendStore::Sys(_) => Self::Sys(
crate::backend::sys::entities::memory::Memory::new_from_existing(
new_store,
memory.unwrap_sys(),
),
),
#[cfg(feature = "v8")]
crate::BackendStore::V8(_) => Self::V8(
crate::backend::v8::entities::memory::Memory::new_from_existing(
new_store,
memory.unwrap_v_8(),
),
),
#[cfg(feature = "js")]
crate::BackendStore::Js(_) => Self::Js(
crate::backend::js::entities::memory::Memory::new_from_existing(
new_store,
memory.unwrap_js(),
),
),
}
}
/// Returns the [`MemoryType`] of the [`BackendMemory`].
///
/// # Example
///
/// ```
/// # use wasmer::{Memory, MemoryType, Pages, Store, Type, Value};
/// # let mut store = Store::default();
/// #
/// let mt = MemoryType::new(1, None, false);
/// let m = Memory::new(&mut store, mt).unwrap();
///
/// assert_eq!(m.ty(&mut store), mt);
/// ```
#[inline]
pub fn ty(&self, store: &impl AsStoreRef) -> MemoryType {
match_rt!(on self => s {
s.ty(store)
})
}
/// Retrieve the size of the memory in pages.
pub fn size(&self, store: &impl AsStoreRef) -> Pages {
match_rt!(on self => s {
s.size(store)
})
}
/// Grow memory by the specified amount of WebAssembly [`Pages`] and return
/// the previous memory size.
///
/// # Example
///
/// ```
/// # use wasmer::{Memory, MemoryType, Pages, Store, Type, Value, WASM_MAX_PAGES};
/// # let mut store = Store::default();
/// #
/// let m = Memory::new(&mut store, MemoryType::new(1, Some(3), false)).unwrap();
/// let p = m.grow(&mut store, 2).unwrap();
///
/// assert_eq!(p, Pages(1));
/// assert_eq!(m.view(&mut store).size(), Pages(3));
/// ```
///
/// # Errors
///
/// Returns an error if memory can't be grown by the specified amount
/// of pages.
///
/// ```should_panic
/// # use wasmer::{Memory, MemoryType, Pages, Store, Type, Value, WASM_MAX_PAGES};
/// # use wasmer::FunctionEnv;
/// # let mut store = Store::default();
/// # let env = FunctionEnv::new(&mut store, ());
/// #
/// let m = Memory::new(&mut store, MemoryType::new(1, Some(1), false)).unwrap();
///
/// // This results in an error: `MemoryError::CouldNotGrow`.
/// let s = m.grow(&mut store, 1).unwrap();
/// ```
#[inline]
pub fn grow<IntoPages>(
&self,
store: &mut impl AsStoreMut,
delta: IntoPages,
) -> Result<Pages, MemoryError>
where
IntoPages: Into<Pages>,
{
match_rt!(on self => s {
s.grow(store, delta)
})
}
/// Grows the memory to at least a minimum size.
///
/// # Note
///
/// If the memory is already big enough for the min size this function does nothing.
#[inline]
pub fn grow_at_least(
&self,
store: &mut impl AsStoreMut,
min_size: u64,
) -> Result<(), MemoryError> {
match_rt!(on self => s {
s.grow_at_least(store, min_size)
})
}
/// Resets the memory back to zero length
#[inline]
pub fn reset(&self, store: &mut impl AsStoreMut) -> Result<(), MemoryError> {
match_rt!(on self => s {
s.reset(store)
})
}
/// Attempts to duplicate this memory in a new store with a byte-for-byte copy
#[inline]
#[deprecated(
since = "8.0.0",
note = "Since `Store` is no longer `Send + Sync`, this method cannot be used meaningfully. \
Use `copy`, then `attach` on the thread owning the other `Store` instead."
)]
pub fn copy_to_store(
&self,
store: &impl AsStoreRef,
new_store: &mut impl AsStoreMut,
) -> Result<Self, MemoryError> {
self.copy(store)
.map(|new_memory| new_memory.attach(new_store).0)
}
#[inline]
pub(crate) fn from_vm_extern(store: &mut impl AsStoreMut, vm_extern: VMExternMemory) -> Self {
match &store.as_store_mut().inner.store {
#[cfg(feature = "sys")]
crate::BackendStore::Sys(s) => Self::Sys(
crate::backend::sys::entities::memory::Memory::from_vm_extern(store, vm_extern),
),
#[cfg(feature = "v8")]
crate::BackendStore::V8(s) => Self::V8(
crate::backend::v8::entities::memory::Memory::from_vm_extern(store, vm_extern),
),
#[cfg(feature = "js")]
crate::BackendStore::Js(s) => Self::Js(
crate::backend::js::entities::memory::Memory::from_vm_extern(store, vm_extern),
),
}
}
/// Checks whether this `Memory` can be used with the given context.
#[inline]
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
match_rt!(on self => s {
s.is_from_store(store)
})
}
/// Attempts to create a detached copied memory handle that can later be
/// attached to a different store.
#[inline]
pub fn copy(&self, store: &impl AsStoreRef) -> Result<SharedMemory, MemoryError> {
match_rt!(on self => s {
s.copy(store)
})
}
/// Attempts to clone this memory (if its cloneable) in a new store
/// (cloned memory will be shared between those that clone it)
#[inline]
#[deprecated(
since = "8.0.0",
note = "Since `Store` is no longer `Send + Sync`, this method cannot be used meaningfully. \
Use `as_shared`, then `attach` on the thread owning the other `Store` instead."
)]
pub fn share_in_store(
&self,
store: &impl AsStoreRef,
new_store: &mut impl AsStoreMut,
) -> Result<Self, MemoryError> {
if !self.ty(store).shared {
return Err(MemoryError::MemoryNotShared);
}
self.as_shared(store)
.ok_or_else(shared_memory_detach_error)
.map(|new_memory| new_memory.attach(new_store).0)
}
/// Get a [`SharedMemory`].
///
/// Only returns `Some(_)` if the memory is shared, and if the target
/// backend supports shared memory operations.
///
/// See [`SharedMemory`] and its methods for more information.
#[inline]
pub fn as_shared(&self, store: &impl AsStoreRef) -> Option<SharedMemory> {
if !self.ty(store).shared {
return None;
}
match_rt!(on self => s {
s.as_shared(store).ok()
})
}
/// Create a [`VMExtern`] from self.
#[inline]
pub(crate) fn to_vm_extern(&self) -> VMExtern {
match_rt!(on self => s {
s.to_vm_extern()
})
}
}