Skip to content

Commit

Permalink
Mirror Wasmtime APIs more closely (#613)
Browse files Browse the repository at this point in the history
* add Wasmtime-like methods to StoreContext[Mut]

* rename Store::state[_mut] to data[_mut]

This new naming scheme mirrors the names from Wasmtime.

* rename Store::into_state -> into_data

* add missing docs to ModuleError
  • Loading branch information
Robbepop authored Jan 16, 2023
1 parent 1a5891a commit 7c16e77
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
4 changes: 2 additions & 2 deletions crates/wasmi/src/func/caller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ impl<'a, T> Caller<'a, T> {

/// Returns a shared reference to the host provided data.
pub fn host_data(&self) -> &T {
self.store.store.state()
self.store.store.data()
}

/// Returns an exclusive reference to the host provided data.
pub fn host_data_mut(&mut self) -> &mut T {
self.store.store.state_mut()
self.store.store.data_mut()
}

/// Returns a shared reference to the used [`Engine`].
Expand Down
1 change: 1 addition & 0 deletions crates/wasmi/src/module/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use core::{
};
use wasmparser::BinaryReaderError as ParserError;

/// Errors that may occur upon reading, parsing and translating Wasm modules.
#[derive(Debug)]
pub enum ModuleError {
/// Encountered when there is a problem with the Wasm input stream.
Expand Down
67 changes: 51 additions & 16 deletions crates/wasmi/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ pub struct Store<T> {
pub(crate) inner: StoreInner,
/// Stored Wasm or host functions.
funcs: Arena<FuncIdx, FuncEntity<T>>,
/// User provided state.
user_state: T,
/// User provided host data owned by the [`Store`].
data: T,
}

/// The inner store that owns all data not associated to the host state.
Expand Down Expand Up @@ -392,11 +392,11 @@ impl StoreInner {

impl<T> Store<T> {
/// Creates a new store.
pub fn new(engine: &Engine, user_state: T) -> Self {
pub fn new(engine: &Engine, data: T) -> Self {
Self {
inner: StoreInner::new(engine),
funcs: Arena::new(),
user_state,
data,
}
}

Expand All @@ -405,19 +405,19 @@ impl<T> Store<T> {
self.inner.engine()
}

/// Returns a shared reference to the user provided state.
pub fn state(&self) -> &T {
&self.user_state
/// Returns a shared reference to the user provided data owned by this [`Store`].
pub fn data(&self) -> &T {
&self.data
}

/// Returns a shared reference to the user provided state.
pub fn state_mut(&mut self) -> &mut T {
&mut self.user_state
/// Returns an exclusive reference to the user provided data owned by this [`Store`].
pub fn data_mut(&mut self) -> &mut T {
&mut self.data
}

/// Consumes `self` and returns its user provided state.
pub fn into_state(self) -> T {
self.user_state
/// Consumes `self` and returns its user provided data.
pub fn into_data(self) -> T {
self.data
}

/// Wraps an entitiy `Idx` (index type) as a [`Stored<Idx>`] type.
Expand Down Expand Up @@ -599,7 +599,7 @@ impl<T> Store<T> {
&mut self,
memory: Memory,
) -> (&mut MemoryEntity, &mut T) {
(self.inner.resolve_memory_mut(memory), &mut self.user_state)
(self.inner.resolve_memory_mut(memory), &mut self.data)
}

/// Returns a shared reference to the associated entity of the Wasm or host function.
Expand Down Expand Up @@ -641,7 +641,7 @@ pub trait AsContextMut: AsContext {
fn as_context_mut(&mut self) -> StoreContextMut<Self::UserState>;
}

/// A temporary handle to a `&Store<T>`.
/// A temporary handle to a [`&Store<T>`][`Store`].
///
/// This type is suitable for [`AsContext`] trait bounds on methods if desired.
/// For more information, see [`Store`].
Expand All @@ -651,6 +651,20 @@ pub struct StoreContext<'a, T> {
pub(super) store: &'a Store<T>,
}

impl<'a, T> StoreContext<'a, T> {
/// Returns the underlying [`Engine`] this store is connected to.
pub fn engine(&self) -> &Engine {
self.store.engine()
}

/// Access the underlying data owned by this store.
///
/// Same as [`Store::data`].
pub fn data(&self) -> &T {
self.store.data()
}
}

impl<'a, T: AsContext> From<&'a T> for StoreContext<'a, T::UserState> {
#[inline]
fn from(ctx: &'a T) -> Self {
Expand All @@ -672,7 +686,7 @@ impl<'a, T: AsContextMut> From<&'a mut T> for StoreContextMut<'a, T::UserState>
}
}

/// A temporary handle to a `&mut Store<T>`.
/// A temporary handle to a [`&mut Store<T>`][`Store`].
///
/// This type is suitable for [`AsContextMut`] or [`AsContext`] trait bounds on methods if desired.
/// For more information, see [`Store`].
Expand All @@ -682,6 +696,27 @@ pub struct StoreContextMut<'a, T> {
pub(super) store: &'a mut Store<T>,
}

impl<'a, T> StoreContextMut<'a, T> {
/// Returns the underlying [`Engine`] this store is connected to.
pub fn engine(&self) -> &Engine {
self.store.engine()
}

/// Access the underlying data owned by this store.
///
/// Same as [`Store::data`].
pub fn data(&self) -> &T {
self.store.data()
}

/// Access the underlying data owned by this store.
///
/// Same as [`Store::data_mut`].
pub fn data_mut(&mut self) -> &mut T {
self.store.data_mut()
}
}

impl<T> AsContext for &'_ T
where
T: AsContext,
Expand Down

0 comments on commit 7c16e77

Please sign in to comment.