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

Improved hashing/ids of function envs #3039

Merged
merged 2 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 19 additions & 4 deletions lib/api/src/js/function_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::js::{AsStoreMut, AsStoreRef, StoreMut, StoreRef};
/// The function environment data is owned by the `Store`.
pub struct FunctionEnv<T> {
pub(crate) handle: StoreHandle<VMFunctionEnvironment>,
_phantom: PhantomData<T>,
marker: PhantomData<T>,
}

impl<T> FunctionEnv<T> {
Expand All @@ -24,14 +24,14 @@ impl<T> FunctionEnv<T> {
store.as_store_mut().objects_mut(),
VMFunctionEnvironment::new(value),
),
_phantom: PhantomData,
marker: PhantomData,
}
}

pub(crate) fn from_handle(handle: StoreHandle<VMFunctionEnvironment>) -> Self {
Self {
handle,
_phantom: PhantomData,
marker: PhantomData,
}
}

Expand Down Expand Up @@ -71,11 +71,26 @@ impl<T> FunctionEnv<T> {
}
}

impl<T> PartialEq for FunctionEnv<T> {
fn eq(&self, other: &Self) -> bool {
self.handle == other.handle
}
}

impl<T> Eq for FunctionEnv<T> {}

impl<T> std::hash::Hash for FunctionEnv<T> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.handle.hash(state);
self.marker.hash(state);
}
}

impl<T> Clone for FunctionEnv<T> {
fn clone(&self) -> Self {
Self {
handle: self.handle.clone(),
_phantom: self._phantom,
marker: self.marker,
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion lib/api/src/js/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ mod objects {
/// Every handle to an object managed by a context also contains the ID of the
/// context. This is used to check that a handle is always used with the
/// correct context.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct StoreId(NonZeroU64);

impl Default for StoreId {
Expand Down Expand Up @@ -298,6 +298,14 @@ mod objects {
self.id == other.id
}
}

impl<T> std::hash::Hash for StoreHandle<T> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.id.hash(state);
self.internal.idx.hash(state);
}
}

impl<T> Clone for StoreHandle<T> {
fn clone(&self) -> Self {
Self {
Expand Down
23 changes: 19 additions & 4 deletions lib/api/src/sys/function_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use crate::{AsStoreMut, AsStoreRef, StoreMut, StoreRef};
/// The function environment data is owned by the `Store`.
pub struct FunctionEnv<T> {
pub(crate) handle: StoreHandle<VMFunctionEnvironment>,
_phantom: PhantomData<T>,
marker: PhantomData<T>,
}

impl<T> FunctionEnv<T> {
/// Make a new extern reference
/// Make a new FunctionEnv
pub fn new(store: &mut impl AsStoreMut, value: T) -> Self
where
T: Any + Send + 'static + Sized,
Expand All @@ -24,7 +24,7 @@ impl<T> FunctionEnv<T> {
store.as_store_mut().objects_mut(),
VMFunctionEnvironment::new(value),
),
_phantom: PhantomData,
marker: PhantomData,
}
}

Expand Down Expand Up @@ -64,11 +64,26 @@ impl<T> FunctionEnv<T> {
}
}

impl<T> PartialEq for FunctionEnv<T> {
fn eq(&self, other: &Self) -> bool {
self.handle == other.handle
}
}

impl<T> Eq for FunctionEnv<T> {}

impl<T> std::hash::Hash for FunctionEnv<T> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.handle.hash(state);
self.marker.hash(state);
}
}

impl<T> Clone for FunctionEnv<T> {
fn clone(&self) -> Self {
Self {
handle: self.handle.clone(),
_phantom: self._phantom,
marker: self.marker,
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion lib/vm/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{InstanceHandle, VMFunction, VMFunctionEnvironment, VMGlobal, VMMemor
/// Every handle to an object managed by a context also contains the ID of the
/// context. This is used to check that a handle is always used with the
/// correct context.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct StoreId(NonZeroU64);

impl Default for StoreId {
Expand Down Expand Up @@ -116,6 +116,13 @@ impl<T> Clone for StoreHandle<T> {
}
}

impl<T> std::hash::Hash for StoreHandle<T> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.id.hash(state);
self.internal.idx.hash(state);
}
}

impl<T: StoreObject> fmt::Debug for StoreHandle<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("StoreHandle")
Expand Down