Skip to content

Commit

Permalink
Add Memory::data_and_store_mut (#448)
Browse files Browse the repository at this point in the history
* Add `Memory::data_and_store_mut`

* Fix doc comments

* Fix comments for `data*` methods on `Memory`
  • Loading branch information
AldaronLau committed Sep 17, 2022
1 parent e908d52 commit 753b05d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
18 changes: 16 additions & 2 deletions wasmi_v1/src/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ impl Memory {
.grow(additional)
}

/// Returns a shared slice to the bytes underlying to the byte buffer.
/// Returns a shared slice to the bytes underlying the [`Memory`].
///
/// # Panics
///
Expand All @@ -320,7 +320,7 @@ impl Memory {
ctx.into().store.resolve_memory(*self).data()
}

/// Returns an exclusive slice to the bytes underlying to the byte buffer.
/// Returns an exclusive slice to the bytes underlying the [`Memory`].
///
/// # Panics
///
Expand All @@ -329,6 +329,20 @@ impl Memory {
ctx.into().store.resolve_memory_mut(*self).data_mut()
}

/// Returns an exclusive slice to the bytes underlying the [`Memory`], and an exclusive
/// reference to the user provided state.
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Memory`].
pub fn data_and_store_mut<'a, T: 'a>(
&self,
ctx: impl Into<StoreContextMut<'a, T>>,
) -> (&'a mut [u8], &'a mut T) {
let (memory, store) = ctx.into().store.resolve_memory_and_state_mut(*self);
(memory.data_mut(), store)
}

/// Reads `n` bytes from `memory[offset..offset+n]` into `buffer`
/// where `n` is the length of `buffer`.
///
Expand Down
18 changes: 18 additions & 0 deletions wasmi_v1/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,24 @@ impl<T> Store<T> {
.unwrap_or_else(|| panic!("failed to resolve stored linear memory: {:?}", entity_index))
}

/// Returns an exclusive reference to the associated entity of the linear memory and an
/// exclusive reference to the user provided state.
///
/// # Panics
///
/// - If the linear memory does not originate from this store.
/// - If the linear memory cannot be resolved to its entity.
pub(super) fn resolve_memory_and_state_mut(
&mut self,
memory: Memory,
) -> (&mut MemoryEntity, &mut T) {
let entity_index = self.unwrap_index(memory.into_inner());
let memory_entity = self.memories.get_mut(entity_index).unwrap_or_else(|| {
panic!("failed to resolve stored linear memory: {:?}", entity_index)
});
(memory_entity, &mut self.user_state)
}

/// Returns a shared reference to the associated entity of the Wasm or host function.
///
/// # Panics
Expand Down

0 comments on commit 753b05d

Please sign in to comment.