Skip to content

Commit

Permalink
Simplified api Memory
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed May 1, 2021
1 parent da8e6b4 commit 165152d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
33 changes: 17 additions & 16 deletions lib/api/src/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::slice;
use std::sync::Arc;
use wasmer_engine::Export;
use wasmer_types::{Pages, ValueType};
use wasmer_vm::{Memory as RuntimeMemory, MemoryError, VMMemory};
use wasmer_vm::{MemoryError, VMMemory};

/// A WebAssembly `memory` instance.
///
Expand All @@ -27,7 +27,7 @@ use wasmer_vm::{Memory as RuntimeMemory, MemoryError, VMMemory};
#[derive(Debug, Clone, MemoryUsage)]
pub struct Memory {
store: Store,
memory: Arc<dyn RuntimeMemory>,
vm_memory: VMMemory,
}

impl Memory {
Expand All @@ -51,7 +51,12 @@ impl Memory {

Ok(Self {
store: store.clone(),
memory,
vm_memory: VMMemory {
from: memory,
// We are creating it from the host, and therefore there is no
// associated instance with this memory
instance_ref: None
},
})
}

Expand All @@ -69,7 +74,7 @@ impl Memory {
/// assert_eq!(m.ty(), &mt);
/// ```
pub fn ty(&self) -> &MemoryType {
self.memory.ty()
self.vm_memory.from.ty()
}

/// Returns the [`Store`] where the `Memory` belongs.
Expand Down Expand Up @@ -110,21 +115,21 @@ impl Memory {
/// by resizing this Memory.
#[allow(clippy::mut_from_ref)]
pub unsafe fn data_unchecked_mut(&self) -> &mut [u8] {
let definition = self.memory.vmmemory();
let definition = self.vm_memory.from.vmmemory();
let def = definition.as_ref();
slice::from_raw_parts_mut(def.base, def.current_length.try_into().unwrap())
}

/// Returns the pointer to the raw bytes of the `Memory`.
pub fn data_ptr(&self) -> *mut u8 {
let definition = self.memory.vmmemory();
let definition = self.vm_memory.from.vmmemory();
let def = unsafe { definition.as_ref() };
def.base
}

/// Returns the size (in bytes) of the `Memory`.
pub fn data_size(&self) -> u64 {
let definition = self.memory.vmmemory();
let definition = self.vm_memory.from.vmmemory();
let def = unsafe { definition.as_ref() };
def.current_length.into()
}
Expand All @@ -142,7 +147,7 @@ impl Memory {
/// assert_eq!(m.size(), Pages(1));
/// ```
pub fn size(&self) -> Pages {
self.memory.size()
self.vm_memory.from.size()
}

/// Grow memory by the specified amount of WebAssembly [`Pages`] and return
Expand Down Expand Up @@ -179,7 +184,7 @@ impl Memory {
where
IntoPages: Into<Pages>,
{
self.memory.grow(delta.into())
self.vm_memory.from.grow(delta.into())
}

/// Return a "view" of the currently accessible memory. By
Expand Down Expand Up @@ -224,7 +229,7 @@ impl Memory {
pub(crate) fn from_vm_export(store: &Store, vm_memory: VMMemory) -> Self {
Self {
store: store.clone(),
memory: vm_memory.from,
vm_memory,
}
}

Expand All @@ -241,17 +246,13 @@ impl Memory {
/// assert!(m.same(&m));
/// ```
pub fn same(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.memory, &other.memory)
Arc::ptr_eq(&self.vm_memory.from, &other.vm_memory.from)
}
}

impl<'a> Exportable<'a> for Memory {
fn to_export(&self) -> Export {
VMMemory {
from: self.memory.clone(),
instance_ref: None,
}
.into()
self.vm_memory.clone().into()
}

fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
Expand Down
2 changes: 1 addition & 1 deletion lib/vm/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl From<VMTable> for VMExport {
}

/// A memory export value.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, MemoryUsage)]
pub struct VMMemory {
/// Pointer to the containing `Memory`.
pub from: Arc<dyn Memory>,
Expand Down

0 comments on commit 165152d

Please sign in to comment.