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::{CompileError, DeserializeError, FunctionType, Target};
#[cfg(feature = "sys")]
use wasmer_vm::VMSharedSignatureIndex;
#[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;
#[cfg(feature = "sys")]
type EngineId = str;
#[derive(Clone, Debug)]
pub struct Engine(pub(crate) engine_imp::Engine);
impl Engine {
#[deprecated(
since = "3.2.0",
note = "engine.cloned() has been deprecated in favor of engine.clone()"
)]
pub fn cloned(&self) -> Self {
self.clone()
}
pub fn deterministic_id(&self) -> &str {
self.0.deterministic_id()
}
#[deprecated(since = "3.2.0")]
#[cfg(all(feature = "compiler", feature = "sys"))]
pub fn new(
compiler_config: Box<dyn CompilerConfig>,
target: Target,
features: Features,
) -> Self {
Self(engine_imp::Engine::new(compiler_config, target, features))
}
#[cfg(feature = "sys")]
pub fn headless() -> Self {
Self(engine_imp::Engine::headless())
}
#[deprecated(since = "3.2.0")]
#[cfg(feature = "sys")]
pub fn inner(&self) -> std::sync::MutexGuard<'_, EngineInner> {
self.0.inner()
}
#[deprecated(since = "3.2.0")]
#[cfg(feature = "sys")]
pub fn inner_mut(&self) -> std::sync::MutexGuard<'_, EngineInner> {
self.0.inner_mut()
}
#[cfg(feature = "sys")]
pub fn target(&self) -> &Target {
self.0.target()
}
#[deprecated(since = "3.2.0")]
#[cfg(feature = "sys")]
#[cfg(not(target_arch = "wasm32"))]
pub fn register_signature(&self, func_type: &FunctionType) -> VMSharedSignatureIndex {
let compiler = self.0.inner();
compiler.signatures().register(func_type)
}
#[deprecated(since = "3.2.0")]
#[cfg(feature = "sys")]
#[cfg(not(target_arch = "wasm32"))]
pub fn lookup_signature(&self, sig: VMSharedSignatureIndex) -> Option<FunctionType> {
let compiler = self.0.inner();
compiler.signatures().lookup(sig)
}
#[deprecated(since = "3.2.0")]
#[cfg(feature = "sys")]
#[cfg(feature = "compiler")]
pub fn validate(&self, binary: &[u8]) -> Result<(), CompileError> {
self.0.inner().validate(binary)
}
#[deprecated(since = "3.2.0")]
#[cfg(all(feature = "sys", feature = "compiler"))]
#[cfg(not(target_arch = "wasm32"))]
pub fn compile(&self, binary: &[u8]) -> Result<Arc<Artifact>, CompileError> {
Ok(Arc::new(Artifact::new(&self.0, binary, self.0.tunables())?))
}
#[deprecated(since = "3.2.0")]
#[cfg(all(feature = "sys", not(feature = "compiler")))]
#[cfg(not(target_arch = "wasm32"))]
pub fn compile(
&self,
_binary: &[u8],
_tunables: &dyn Tunables,
) -> Result<Arc<Artifact>, CompileError> {
Err(CompileError::Codegen(
"The Engine is operating in headless mode, so it can not compile Modules.".to_string(),
))
}
#[deprecated(since = "3.2.0")]
#[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)?))
}
#[deprecated(since = "3.2.0")]
#[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())?))
}
#[deprecated(since = "3.2.0", note = "Use Engine::deterministic_id()")]
#[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
pub fn id(&self) -> &EngineId {
self.deterministic_id()
}
#[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
pub fn set_tunables(&mut self, tunables: impl Tunables + Send + Sync + 'static) {
self.0.set_tunables(tunables);
}
#[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
pub fn tunables(&self) -> &dyn Tunables {
self.0.tunables()
}
}
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()
}
}