use crate::BackendStore;
use crate::entities::engine::{AsEngineRef, Engine, EngineRef};
use wasmer_vm::TrapHandlerFn;
use wasmer_vm::init_traps;
pub use wasmer_vm::{StoreHandle, StoreObjects};
mod obj;
pub use obj::*;
pub struct Store {
pub(crate) engine: Engine,
pub(crate) trap_handler: Option<Box<TrapHandlerFn<'static>>>,
}
impl std::fmt::Debug for Store {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Store")
.field("engine", &self.engine)
.finish()
}
}
impl Store {
pub(crate) fn new(engine: Engine) -> Self {
init_traps();
Self {
engine,
trap_handler: None,
}
}
pub(crate) fn engine(&self) -> &Engine {
&self.engine
}
pub(crate) fn engine_mut(&mut self) -> &mut Engine {
&mut self.engine
}
}
impl AsEngineRef for Store {
fn as_engine_ref(&self) -> EngineRef<'_> {
EngineRef::new(&self.engine)
}
}
pub trait NativeStoreExt {
fn set_trap_handler(&mut self, handler: Option<Box<TrapHandlerFn<'static>>>);
fn signal_handler(&self) -> Option<*const TrapHandlerFn<'static>>;
}
impl NativeStoreExt for Store {
fn set_trap_handler(&mut self, handler: Option<Box<TrapHandlerFn<'static>>>) {
self.trap_handler = handler;
}
#[inline]
fn signal_handler(&self) -> Option<*const TrapHandlerFn<'static>> {
self.trap_handler
.as_ref()
.map(|handler| handler.as_ref() as *const _)
}
}
impl crate::BackendStore {
pub fn into_sys(self) -> crate::backend::sys::store::Store {
match self {
Self::Sys(s) => s,
_ => panic!("Not a `sys` store!"),
}
}
pub fn as_sys(&self) -> &crate::backend::sys::store::Store {
match self {
Self::Sys(s) => s,
_ => panic!("Not a `sys` store!"),
}
}
pub fn as_sys_mut(&mut self) -> &mut crate::backend::sys::store::Store {
match self {
Self::Sys(s) => s,
_ => panic!("Not a `sys` store!"),
}
}
pub fn is_sys(&self) -> bool {
matches!(self, Self::Sys(_))
}
}