Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Memory::data_and_store_mut #448

Merged
merged 3 commits into from
Sep 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions wasmi_v1/src/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,20 @@ impl Memory {
ctx.into().store.resolve_memory_mut(*self).data_mut()
}

/// Returns an exclusive slice to the bytes underlying to the byte buffer, and a shared
/// reference to the user provided state.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Returns an exclusive slice to the bytes underlying to the byte buffer, and a shared
/// reference to the user provided state.
/// Returns an exclusive slice to the bytes underlying to 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 a shared
/// reference to the user provided state.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Returns an exclusive reference to the associated entity of the linear memory and a shared
/// reference to the user provided state.
/// 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