use core::ops::Deref;
#[cfg(feature = "sys")]
use crate::sys::engine as engine_imp;
#[cfg(feature = "sys")]
pub(crate) use crate::sys::engine::default_engine;
#[cfg(feature = "sys")]
use std::io::Read;
#[cfg(feature = "sys")]
use std::path::Path;
#[cfg(feature = "sys")]
use std::sync::Arc;
#[cfg(feature = "sys")]
pub use wasmer_compiler::{Artifact, CompilerConfig, EngineInner, Features, Tunables};
#[cfg(feature = "sys")]
use wasmer_types::DeserializeError;
#[cfg(feature = "js")]
use crate::js::engine as engine_imp;
#[cfg(feature = "js")]
pub(crate) use crate::js::engine::default_engine;
#[cfg(feature = "jsc")]
use crate::jsc::engine as engine_imp;
#[cfg(feature = "jsc")]
pub(crate) use crate::jsc::engine::default_engine;
#[derive(Clone, Debug)]
pub struct Engine(pub(crate) engine_imp::Engine);
impl Engine {
pub fn deterministic_id(&self) -> &str {
self.0.deterministic_id()
}
#[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
pub unsafe fn deserialize_unchecked(
&self,
bytes: &[u8],
) -> Result<Arc<Artifact>, DeserializeError> {
Ok(Arc::new(Artifact::deserialize_unchecked(&self.0, bytes)?))
}
#[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
pub unsafe fn deserialize(&self, bytes: &[u8]) -> Result<Arc<Artifact>, DeserializeError> {
Ok(Arc::new(Artifact::deserialize(&self.0, bytes)?))
}
#[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
pub unsafe fn deserialize_from_file_unchecked(
&self,
file_ref: &Path,
) -> Result<Arc<Artifact>, DeserializeError> {
let mut file = std::fs::File::open(file_ref)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
Ok(Arc::new(Artifact::deserialize_unchecked(
&self.0,
buffer.as_slice(),
)?))
}
#[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
pub unsafe fn deserialize_from_file(
&self,
file_ref: &Path,
) -> Result<Arc<Artifact>, DeserializeError> {
let mut file = std::fs::File::open(file_ref)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
Ok(Arc::new(Artifact::deserialize(&self.0, buffer.as_slice())?))
}
}
impl AsEngineRef for Engine {
#[inline]
fn as_engine_ref(&self) -> EngineRef {
EngineRef { inner: self }
}
}
impl Default for Engine {
fn default() -> Self {
Self(default_engine())
}
}
impl<T: Into<engine_imp::Engine>> From<T> for Engine {
fn from(t: T) -> Self {
Self(t.into())
}
}
pub struct EngineRef<'a> {
pub(crate) inner: &'a Engine,
}
impl<'a> EngineRef<'a> {
pub fn engine(&self) -> &Engine {
self.inner
}
pub fn new(engine: &'a Engine) -> Self {
EngineRef { inner: engine }
}
}
pub trait AsEngineRef {
fn as_engine_ref(&self) -> EngineRef<'_>;
}
impl AsEngineRef for EngineRef<'_> {
#[inline]
fn as_engine_ref(&self) -> EngineRef<'_> {
EngineRef { inner: self.inner }
}
}
impl<P> AsEngineRef for P
where
P: Deref,
P::Target: AsEngineRef,
{
#[inline]
fn as_engine_ref(&self) -> EngineRef<'_> {
(**self).as_engine_ref()
}
}