#[cfg(feature = "js")]
use crate::js::externals::memory as memory_impl;
#[cfg(feature = "jsc")]
use crate::jsc::externals::memory as memory_impl;
#[cfg(feature = "sys")]
use crate::sys::externals::memory as memory_impl;
use super::memory_view::MemoryView;
use crate::exports::{ExportError, Exportable};
use crate::store::{AsStoreMut, AsStoreRef};
use crate::vm::{VMExtern, VMExternMemory, VMMemory};
use crate::Extern;
use crate::MemoryAccessError;
use crate::MemoryType;
use std::mem::MaybeUninit;
use wasmer_types::{MemoryError, Pages};
#[derive(Debug, Clone, PartialEq)]
pub struct Memory(pub(crate) memory_impl::Memory);
impl Memory {
pub fn new(store: &mut impl AsStoreMut, ty: MemoryType) -> Result<Self, MemoryError> {
Ok(Self(memory_impl::Memory::new(store, ty)?))
}
pub fn new_from_existing(new_store: &mut impl AsStoreMut, memory: VMMemory) -> Self {
Self(memory_impl::Memory::new_from_existing(new_store, memory))
}
pub fn ty(&self, store: &impl AsStoreRef) -> MemoryType {
self.0.ty(store)
}
pub fn view<'a>(&self, store: &'a (impl AsStoreRef + ?Sized)) -> MemoryView<'a> {
MemoryView::new(self, store)
}
pub fn grow<IntoPages>(
&self,
store: &mut impl AsStoreMut,
delta: IntoPages,
) -> Result<Pages, MemoryError>
where
IntoPages: Into<Pages>,
{
self.0.grow(store, delta)
}
pub fn copy_to_store(
&self,
store: &impl AsStoreRef,
new_store: &mut impl AsStoreMut,
) -> Result<Self, MemoryError> {
if !self.ty(store).shared {
return Err(MemoryError::InvalidMemory {
reason: "memory is not a shared memory type".to_string(),
});
}
self.0
.try_copy(&store)
.map(|new_memory| Self::new_from_existing(new_store, new_memory.into()))
}
pub(crate) fn from_vm_extern(store: &mut impl AsStoreMut, vm_extern: VMExternMemory) -> Self {
Self(memory_impl::Memory::from_vm_extern(store, vm_extern))
}
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
self.0.is_from_store(store)
}
pub fn try_clone(&self, store: &impl AsStoreRef) -> Result<VMMemory, MemoryError> {
self.0.try_clone(store)
}
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::InvalidMemory {
reason: "memory is not a shared memory type".to_string(),
});
}
self.0
.try_clone(&store)
.map(|new_memory| Self::new_from_existing(new_store, new_memory))
}
pub(crate) fn to_vm_extern(&self) -> VMExtern {
self.0.to_vm_extern()
}
}
impl std::cmp::Eq for Memory {}
impl<'a> Exportable<'a> for Memory {
fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
match _extern {
Extern::Memory(memory) => Ok(memory),
_ => Err(ExportError::IncompatibleType),
}
}
}
#[derive(Debug, Copy, Clone)]
pub(crate) struct MemoryBuffer<'a>(pub(crate) memory_impl::MemoryBuffer<'a>);
impl<'a> MemoryBuffer<'a> {
#[allow(unused)]
pub(crate) fn read(&self, offset: u64, buf: &mut [u8]) -> Result<(), MemoryAccessError> {
self.0.read(offset, buf)
}
#[allow(unused)]
pub(crate) fn read_uninit<'b>(
&self,
offset: u64,
buf: &'b mut [MaybeUninit<u8>],
) -> Result<&'b mut [u8], MemoryAccessError> {
self.0.read_uninit(offset, buf)
}
#[allow(unused)]
pub(crate) fn write(&self, offset: u64, data: &[u8]) -> Result<(), MemoryAccessError> {
self.0.write(offset, data)
}
}