Change Ctx::memory to Ctx::memory_mut and add Ctx::memory

This commit is contained in:
Lachlan Sneff
2019-01-23 16:06:42 -08:00
parent 0aa1687ca3
commit c06bd15bff

View File

@ -94,6 +94,44 @@ impl Ctx {
}
}
/// This exposes the specified memory of the WebAssembly instance
/// as a immutable slice.
///
/// WebAssembly will soon support multiple linear memories, so this
/// forces the user to specify.
///
/// # Usage:
///
/// ```
/// # use wasmer_runtime_core::{
/// # vm::Ctx,
/// # };
/// fn read_memory(ctx: &Ctx) -> u8 {
/// let first_memory = ctx.memory(0);
/// // Read the first byte of that linear memory.
/// first_memory[0]
/// }
/// ```
pub fn memory<'a>(&'a self, mem_index: u32) -> &'a [u8] {
let module = unsafe { &*self.module };
let mem_index = MemoryIndex::new(mem_index as usize);
match mem_index.local_or_import(module) {
LocalOrImport::Local(local_mem_index) => {
let local_backing = unsafe { &*self.local_backing };
&local_backing.memories[local_mem_index][..]
}
LocalOrImport::Import(import_mem_index) => {
let import_backing = unsafe { &mut *self.import_backing };
let vm_memory_import = import_backing.memories[import_mem_index].clone();
unsafe {
let memory = &*vm_memory_import.memory;
slice::from_raw_parts(memory.base, memory.size)
}
}
}
}
/// This exposes the specified memory of the WebAssembly instance
/// as a mutable slice.
///
@ -108,12 +146,12 @@ impl Ctx {
/// # error::Result,
/// # };
/// extern fn host_func(ctx: &mut Ctx) {
/// let first_memory = ctx.memory(0);
/// let first_memory = ctx.memory_mut(0);
/// // Set the first byte of that linear memory.
/// first_memory[0] = 42;
/// }
/// ```
pub fn memory<'a>(&'a mut self, mem_index: u32) -> &'a mut [u8] {
pub fn memory_mut<'a>(&'a mut self, mem_index: u32) -> &'a mut [u8] {
let module = unsafe { &*self.module };
let mem_index = MemoryIndex::new(mem_index as usize);
match mem_index.local_or_import(module) {