mod inner;
mod store_ref;
pub use store_ref::*;
mod obj;
pub use obj::*;
use crate::{AsEngineRef, BackendEngine, Engine, EngineRef};
pub(crate) use inner::*;
use wasmer_types::StoreId;
#[cfg(feature = "sys")]
use wasmer_vm::TrapHandlerFn;
pub struct Store {
pub(crate) inner: Box<StoreInner>,
}
impl Store {
pub fn new(engine: impl Into<Engine>) -> Self {
let engine: Engine = engine.into();
let store = match engine.be {
#[cfg(feature = "sys")]
BackendEngine::Sys(_) => {
BackendStore::Sys(crate::backend::sys::entities::store::Store::new(engine))
}
#[cfg(feature = "wamr")]
BackendEngine::Wamr(_) => {
BackendStore::Wamr(crate::backend::wamr::entities::store::Store::new(engine))
}
#[cfg(feature = "wasmi")]
BackendEngine::Wasmi(_) => {
BackendStore::Wasmi(crate::backend::wasmi::entities::store::Store::new(engine))
}
#[cfg(feature = "v8")]
BackendEngine::V8(_) => {
BackendStore::V8(crate::backend::v8::entities::store::Store::new(engine))
}
#[cfg(feature = "js")]
BackendEngine::Js(_) => {
BackendStore::Js(crate::backend::js::entities::store::Store::new(engine))
}
#[cfg(feature = "jsc")]
BackendEngine::Jsc(_) => {
BackendStore::Jsc(crate::backend::jsc::entities::store::Store::new(engine))
}
};
Self {
inner: Box::new(StoreInner {
objects: StoreObjects::from_store_ref(&store),
on_called: None,
store,
}),
}
}
#[cfg(feature = "sys")]
pub fn set_trap_handler(&mut self, handler: Option<Box<TrapHandlerFn<'static>>>) {
use crate::backend::sys::entities::store::NativeStoreExt;
#[allow(irrefutable_let_patterns)]
if let BackendStore::Sys(ref mut s) = self.inner.store {
s.set_trap_handler(handler)
}
}
pub fn engine(&self) -> &Engine {
self.inner.store.engine()
}
pub fn engine_mut(&mut self) -> &mut Engine {
self.inner.store.engine_mut()
}
pub fn same(a: &Self, b: &Self) -> bool {
a.id() == b.id()
}
pub fn id(&self) -> StoreId {
self.inner.objects.id()
}
}
impl PartialEq for Store {
fn eq(&self, other: &Self) -> bool {
Self::same(self, other)
}
}
unsafe impl Send for Store {}
unsafe impl Sync for Store {}
impl Default for Store {
fn default() -> Self {
Self::new(Engine::default())
}
}
impl std::fmt::Debug for Store {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("Store").finish()
}
}
impl AsEngineRef for Store {
fn as_engine_ref(&self) -> EngineRef<'_> {
self.inner.store.as_engine_ref()
}
fn maybe_as_store(&self) -> Option<StoreRef<'_>> {
Some(self.as_store_ref())
}
}
impl AsStoreRef for Store {
fn as_store_ref(&self) -> StoreRef<'_> {
StoreRef { inner: &self.inner }
}
}
impl AsStoreMut for Store {
fn as_store_mut(&mut self) -> StoreMut<'_> {
StoreMut {
inner: &mut self.inner,
}
}
fn objects_mut(&mut self) -> &mut StoreObjects {
&mut self.inner.objects
}
}