diff --git a/lib/api/src/sys/function_env.rs b/lib/api/src/function_env.rs similarity index 88% rename from lib/api/src/sys/function_env.rs rename to lib/api/src/function_env.rs index 6ef7b48a3..c5f65a640 100644 --- a/lib/api/src/sys/function_env.rs +++ b/lib/api/src/function_env.rs @@ -1,8 +1,11 @@ use std::{any::Any, marker::PhantomData}; -use wasmer_vm::{StoreHandle, StoreObjects, VMFunctionEnvironment}; +#[cfg(feature = "js")] +use crate::js::vm::VMFunctionEnvironment; +#[cfg(feature = "sys")] +use wasmer_vm::VMFunctionEnvironment; -use crate::{AsStoreMut, AsStoreRef, StoreMut, StoreRef}; +use crate::store::{AsStoreMut, AsStoreRef, StoreHandle, StoreMut, StoreObjects, StoreRef}; #[derive(Debug)] #[repr(transparent)] @@ -40,6 +43,14 @@ impl FunctionEnv { .unwrap() } + #[allow(dead_code)] // This function is only used in js + pub(crate) fn from_handle(handle: StoreHandle) -> Self { + Self { + handle, + marker: PhantomData, + } + } + /// Get the data as mutable pub fn as_mut<'a>(&self, store: &'a mut impl AsStoreMut) -> &'a mut T where diff --git a/lib/api/src/js/externals/function.rs b/lib/api/src/js/externals/function.rs index 24236ec02..0bba3cbb1 100644 --- a/lib/api/src/js/externals/function.rs +++ b/lib/api/src/js/externals/function.rs @@ -1,13 +1,13 @@ pub use self::inner::{FromToNativeWasmType, HostFunction, WasmTypeList, WithEnv, WithoutEnv}; +use crate::function_env::{FunctionEnv, FunctionEnvMut}; use crate::js::exports::{ExportError, Exportable}; use crate::js::externals::Extern; -use crate::js::function_env::FunctionEnvMut; use crate::js::types::{param_from_js, AsJs}; /* ValFuncRef */ use crate::js::vm::VMExtern; +use crate::js::FunctionType; use crate::js::RuntimeError; use crate::js::TypedFunction; use crate::js::Value; -use crate::js::{FunctionEnv, FunctionType}; use crate::store::{AsStoreMut, AsStoreRef, StoreMut}; use js_sys::{Array, Function as JSFunction}; use std::iter::FromIterator; @@ -629,9 +629,9 @@ impl fmt::Debug for Function { mod inner { use super::RuntimeError; use super::VMFunctionBody; - use crate::js::function_env::{FunctionEnvMut, VMFunctionEnvironment}; + use crate::function_env::{FunctionEnv, FunctionEnvMut}; use crate::js::store::{InternalStoreHandle, StoreHandle}; - use crate::js::FunctionEnv; + use crate::js::vm::VMFunctionEnvironment; use crate::js::NativeWasmTypeInto; use crate::store::{AsStoreMut, AsStoreRef, StoreMut}; use std::array::TryFromSliceError; diff --git a/lib/api/src/js/function_env.rs b/lib/api/src/js/function_env.rs deleted file mode 100644 index 6eca9771a..000000000 --- a/lib/api/src/js/function_env.rs +++ /dev/null @@ -1,174 +0,0 @@ -use std::{any::Any, marker::PhantomData}; - -use crate::js::store::{StoreHandle, StoreObjects}; - -use crate::store::{AsStoreMut, AsStoreRef, StoreMut, StoreRef}; - -#[derive(Debug)] -#[repr(transparent)] -/// An opaque reference to a function environment. -/// The function environment data is owned by the `Store`. -pub struct FunctionEnv { - pub(crate) handle: StoreHandle, - marker: PhantomData, -} - -impl FunctionEnv { - /// Make a new extern reference - pub fn new(store: &mut impl AsStoreMut, value: T) -> Self - where - T: Any + Send + 'static + Sized, - { - Self { - handle: StoreHandle::new( - store.as_store_mut().objects_mut(), - VMFunctionEnvironment::new(value), - ), - marker: PhantomData, - } - } - - pub(crate) fn from_handle(handle: StoreHandle) -> Self { - Self { - handle, - marker: PhantomData, - } - } - - /// Get the data as reference - pub fn as_ref<'a>(&self, store: &'a impl AsStoreRef) -> &'a T - where - T: Any + Send + 'static + Sized, - { - self.handle - .get(store.as_store_ref().objects()) - .as_ref() - .downcast_ref::() - .unwrap() - } - - /// Get the data as mutable - pub fn as_mut<'a>(&self, store: &'a mut impl AsStoreMut) -> &'a mut T - where - T: Any + Send + 'static + Sized, - { - self.handle - .get_mut(store.objects_mut()) - .as_mut() - .downcast_mut::() - .unwrap() - } - - /// Convert it into a `FunctionEnvMut` - pub fn into_mut(self, store: &mut impl AsStoreMut) -> FunctionEnvMut - where - T: Any + Send + 'static + Sized, - { - FunctionEnvMut { - store_mut: store.as_store_mut(), - func_env: self, - } - } -} - -impl PartialEq for FunctionEnv { - fn eq(&self, other: &Self) -> bool { - self.handle == other.handle - } -} - -impl Eq for FunctionEnv {} - -impl std::hash::Hash for FunctionEnv { - fn hash(&self, state: &mut H) { - self.handle.hash(state); - self.marker.hash(state); - } -} - -impl Clone for FunctionEnv { - fn clone(&self) -> Self { - Self { - handle: self.handle.clone(), - marker: self.marker, - } - } -} - -/// A temporary handle to a [`Context`]. -pub struct FunctionEnvMut<'a, T: 'a> { - pub(crate) store_mut: StoreMut<'a>, - pub(crate) func_env: FunctionEnv, -} - -impl FunctionEnvMut<'_, T> { - /// Returns a reference to the host state in this context. - pub fn data(&self) -> &T { - self.func_env.as_ref(&self.store_mut) - } - - /// Returns a mutable- reference to the host state in this context. - pub fn data_mut<'a>(&'a mut self) -> &'a mut T { - self.func_env.as_mut(&mut self.store_mut) - } - - /// Borrows a new immmutable reference - pub fn as_ref(&self) -> FunctionEnv { - self.func_env.clone() - } - - /// Borrows a new mutable reference - pub fn as_mut<'a>(&'a mut self) -> FunctionEnvMut<'a, T> { - FunctionEnvMut { - store_mut: self.store_mut.as_store_mut(), - func_env: self.func_env.clone(), - } - } -} - -impl AsStoreRef for FunctionEnvMut<'_, T> { - fn as_store_ref(&self) -> StoreRef<'_> { - StoreRef { - inner: self.store_mut.inner, - } - } -} - -impl AsStoreMut for FunctionEnvMut<'_, T> { - fn as_store_mut(&mut self) -> StoreMut<'_> { - StoreMut { - inner: self.store_mut.inner, - } - } - #[inline] - fn objects_mut(&mut self) -> &mut StoreObjects { - &mut self.store_mut.inner.objects - } -} - -/// Underlying FunctionEnvironment used by a `VMFunction`. -#[derive(Debug)] -pub struct VMFunctionEnvironment { - contents: Box, -} - -impl VMFunctionEnvironment { - /// Wraps the given value to expose it to Wasm code as a function context. - pub fn new(val: impl Any + Send + 'static) -> Self { - Self { - contents: Box::new(val), - } - } - - #[allow(clippy::should_implement_trait)] - /// Returns a reference to the underlying value. - pub fn as_ref(&self) -> &(dyn Any + Send + 'static) { - &*self.contents - } - - #[allow(clippy::should_implement_trait)] - /// Returns a mutable reference to the underlying value. - pub fn as_mut(&mut self) -> &mut (dyn Any + Send + 'static) { - &mut *self.contents - } -} diff --git a/lib/api/src/js/mod.rs b/lib/api/src/js/mod.rs index 6f2584f98..424fae92d 100644 --- a/lib/api/src/js/mod.rs +++ b/lib/api/src/js/mod.rs @@ -27,7 +27,6 @@ pub(crate) mod engine; pub(crate) mod error; mod exports; mod externals; -mod function_env; mod imports; mod instance; mod mem_access; @@ -41,7 +40,7 @@ pub(crate) mod store; mod trap; mod types; mod value; -mod vm; +pub(crate) mod vm; mod wasm_bindgen_polyfill; pub use crate::js::engine::Engine; @@ -51,7 +50,6 @@ pub use crate::js::externals::{ Extern, FromToNativeWasmType, Function, Global, HostFunction, Memory, MemoryError, MemoryView, Table, WasmTypeList, }; -pub use crate::js::function_env::{FunctionEnv, FunctionEnvMut}; pub use crate::js::imports::Imports; pub use crate::js::instance::Instance; pub use crate::js::mem_access::{MemoryAccessError, WasmRef, WasmSlice, WasmSliceIter}; diff --git a/lib/api/src/js/store.rs b/lib/api/src/js/store.rs index d4cd48e06..eb20de275 100644 --- a/lib/api/src/js/store.rs +++ b/lib/api/src/js/store.rs @@ -4,7 +4,7 @@ pub use objects::{StoreHandle, StoreObjects}; mod objects { use wasm_bindgen::JsValue; - use crate::js::{function_env::VMFunctionEnvironment, vm::VMGlobal}; + use crate::js::vm::{VMFunctionEnvironment, VMGlobal}; use std::{fmt, marker::PhantomData, num::NonZeroUsize}; pub use wasmer_types::StoreId; diff --git a/lib/api/src/js/vm.rs b/lib/api/src/js/vm.rs index 5804a5471..26705ee38 100644 --- a/lib/api/src/js/vm.rs +++ b/lib/api/src/js/vm.rs @@ -15,6 +15,7 @@ use js_sys::WebAssembly; use js_sys::WebAssembly::{Memory, Table}; use js_sys::WebAssembly::{Memory as JsMemory, Table as JsTable}; use serde::{Deserialize, Serialize}; +use std::any::Any; use std::fmt; #[cfg(feature = "tracing")] use tracing::trace; @@ -255,3 +256,30 @@ impl VMExtern { } pub type VMInstance = WebAssembly::Instance; + +/// Underlying FunctionEnvironment used by a `VMFunction`. +#[derive(Debug)] +pub struct VMFunctionEnvironment { + contents: Box, +} + +impl VMFunctionEnvironment { + /// Wraps the given value to expose it to Wasm code as a function context. + pub fn new(val: impl Any + Send + 'static) -> Self { + Self { + contents: Box::new(val), + } + } + + #[allow(clippy::should_implement_trait)] + /// Returns a reference to the underlying value. + pub fn as_ref(&self) -> &(dyn Any + Send + 'static) { + &*self.contents + } + + #[allow(clippy::should_implement_trait)] + /// Returns a mutable reference to the underlying value. + pub fn as_mut(&mut self) -> &mut (dyn Any + Send + 'static) { + &mut *self.contents + } +} diff --git a/lib/api/src/lib.rs b/lib/api/src/lib.rs index 3c5efb889..f1a11d23a 100644 --- a/lib/api/src/lib.rs +++ b/lib/api/src/lib.rs @@ -430,6 +430,7 @@ compile_error!( ); mod engine; +mod function_env; mod module; mod store; @@ -446,6 +447,7 @@ mod js; pub use js::*; pub use engine::{AsEngineRef, Engine}; +pub use function_env::{FunctionEnv, FunctionEnvMut}; pub use module::{IoCompileError, Module}; pub use store::{OnCalledHandler, Store, StoreId}; #[cfg(feature = "sys")] diff --git a/lib/api/src/store.rs b/lib/api/src/store.rs index c8c34a35a..f32dd85a5 100644 --- a/lib/api/src/store.rs +++ b/lib/api/src/store.rs @@ -10,10 +10,10 @@ use wasmer_vm::init_traps; pub use wasmer_vm::TrapHandlerFn; #[cfg(feature = "sys")] -pub use wasmer_vm::StoreObjects; +pub use wasmer_vm::{StoreHandle, StoreObjects}; #[cfg(feature = "js")] -pub use crate::js::store::StoreObjects; +pub use crate::js::store::{StoreHandle, StoreObjects}; /// Call handler for a store. // TODO: better documentation! diff --git a/lib/api/src/sys/externals/function.rs b/lib/api/src/sys/externals/function.rs index e9a93a877..431dc8760 100644 --- a/lib/api/src/sys/externals/function.rs +++ b/lib/api/src/sys/externals/function.rs @@ -856,7 +856,7 @@ mod inner { use std::panic::{self, AssertUnwindSafe}; use wasmer_vm::{on_host_stack, VMContext, VMTrampoline}; - use crate::sys::function_env::FunctionEnvMut; + use crate::function_env::FunctionEnvMut; use wasmer_types::{NativeWasmType, RawValue, Type}; use wasmer_vm::{raise_user_trap, resume_panic, VMFunctionBody}; diff --git a/lib/api/src/sys/mod.rs b/lib/api/src/sys/mod.rs index 77ddacecf..037e30c65 100644 --- a/lib/api/src/sys/mod.rs +++ b/lib/api/src/sys/mod.rs @@ -2,7 +2,6 @@ pub(crate) mod engine; mod exports; mod extern_ref; mod externals; -mod function_env; mod imports; mod instance; mod mem_access; @@ -21,7 +20,6 @@ pub use crate::sys::externals::{ Extern, FromToNativeWasmType, Function, Global, HostFunction, Memory, MemoryView, Table, WasmTypeList, }; -pub use crate::sys::function_env::{FunctionEnv, FunctionEnvMut}; pub use crate::sys::imports::Imports; pub use crate::sys::instance::{Instance, InstantiationError}; pub use crate::sys::mem_access::{MemoryAccessError, WasmRef, WasmSlice, WasmSliceIter};