linera_wasmer/
engine.rs

1use core::ops::Deref;
2
3#[cfg(feature = "sys")]
4use crate::sys::engine as engine_imp;
5#[cfg(feature = "sys")]
6pub(crate) use crate::sys::engine::default_engine;
7#[cfg(feature = "sys")]
8use crate::IntoBytes;
9#[cfg(feature = "sys")]
10use shared_buffer::OwnedBuffer;
11#[cfg(feature = "sys")]
12use std::path::Path;
13#[cfg(feature = "sys")]
14use std::sync::Arc;
15#[cfg(feature = "sys")]
16#[allow(unused_imports)]
17pub use wasmer_compiler::{Artifact, CompilerConfig, EngineInner, Features, Tunables};
18#[cfg(feature = "sys")]
19use wasmer_types::DeserializeError;
20
21#[cfg(feature = "js")]
22use crate::js::engine as engine_imp;
23#[cfg(feature = "js")]
24pub(crate) use crate::js::engine::default_engine;
25
26#[cfg(feature = "jsc")]
27use crate::jsc::engine as engine_imp;
28#[cfg(feature = "jsc")]
29pub(crate) use crate::jsc::engine::default_engine;
30
31/// The engine type
32#[derive(Clone, Debug)]
33pub struct Engine(pub(crate) engine_imp::Engine);
34
35impl Engine {
36    /// Returns the deterministic id of this engine
37    pub fn deterministic_id(&self) -> &str {
38        self.0.deterministic_id()
39    }
40
41    #[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
42    /// Deserializes a WebAssembly module which was previously serialized with
43    /// `Module::serialize`.
44    ///
45    /// NOTE: you should almost always prefer [`Self::deserialize`].
46    ///
47    /// # Safety
48    /// See [`Artifact::deserialize_unchecked`].
49    pub unsafe fn deserialize_unchecked(
50        &self,
51        bytes: impl IntoBytes,
52    ) -> Result<Arc<Artifact>, DeserializeError> {
53        Ok(Arc::new(Artifact::deserialize_unchecked(
54            &self.0,
55            bytes.into_bytes().into(),
56        )?))
57    }
58
59    #[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
60    /// Deserializes a WebAssembly module which was previously serialized with
61    /// `Module::serialize`.
62    ///
63    /// # Safety
64    /// See [`Artifact::deserialize`].
65    pub unsafe fn deserialize(
66        &self,
67        bytes: impl IntoBytes,
68    ) -> Result<Arc<Artifact>, DeserializeError> {
69        Ok(Arc::new(Artifact::deserialize(
70            &self.0,
71            bytes.into_bytes().into(),
72        )?))
73    }
74
75    #[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
76    /// Load a serialized WebAssembly module from a file and deserialize it.
77    ///
78    /// NOTE: you should almost always prefer [`Self::deserialize_from_file`].
79    ///
80    /// # Safety
81    /// See [`Artifact::deserialize_unchecked`].
82    pub unsafe fn deserialize_from_file_unchecked(
83        &self,
84        file_ref: &Path,
85    ) -> Result<Arc<Artifact>, DeserializeError> {
86        let file = std::fs::File::open(file_ref)?;
87        Ok(Arc::new(Artifact::deserialize_unchecked(
88            &self.0,
89            OwnedBuffer::from_file(&file)
90                .map_err(|e| DeserializeError::Generic(format!("{e:?}")))?,
91        )?))
92    }
93
94    #[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
95    /// Load a serialized WebAssembly module from a file and deserialize it.
96    ///
97    /// # Safety
98    /// See [`Artifact::deserialize`].
99    pub unsafe fn deserialize_from_file(
100        &self,
101        file_ref: &Path,
102    ) -> Result<Arc<Artifact>, DeserializeError> {
103        let bytes = std::fs::read(file_ref)?;
104        Ok(Arc::new(Artifact::deserialize(&self.0, bytes.into())?))
105    }
106}
107
108impl AsEngineRef for Engine {
109    #[inline]
110    fn as_engine_ref(&self) -> EngineRef {
111        EngineRef { inner: self }
112    }
113}
114
115impl Default for Engine {
116    fn default() -> Self {
117        Self(default_engine())
118    }
119}
120
121impl<T: Into<engine_imp::Engine>> From<T> for Engine {
122    fn from(t: T) -> Self {
123        Self(t.into())
124    }
125}
126
127/// A temporary handle to an [`Engine`]
128/// EngineRef can be used to build a [`Module`][super::Module]
129/// It can be created directly with an [`Engine`]
130/// Or from anything implementing [`AsEngineRef`]
131/// like from [`Store`][super::Store] typicaly.
132pub struct EngineRef<'a> {
133    /// The inner engine
134    pub(crate) inner: &'a Engine,
135}
136
137impl<'a> EngineRef<'a> {
138    /// Get inner [`Engine`]
139    pub fn engine(&self) -> &Engine {
140        self.inner
141    }
142    /// Create an EngineRef from an Engine
143    pub fn new(engine: &'a Engine) -> Self {
144        EngineRef { inner: engine }
145    }
146}
147
148/// Helper trait for a value that is convertible to a [`EngineRef`].
149pub trait AsEngineRef {
150    /// Returns a `EngineRef` pointing to the underlying context.
151    fn as_engine_ref(&self) -> EngineRef<'_>;
152}
153
154impl AsEngineRef for EngineRef<'_> {
155    #[inline]
156    fn as_engine_ref(&self) -> EngineRef<'_> {
157        EngineRef { inner: self.inner }
158    }
159}
160
161impl<P> AsEngineRef for P
162where
163    P: Deref,
164    P::Target: AsEngineRef,
165{
166    #[inline]
167    fn as_engine_ref(&self) -> EngineRef<'_> {
168        (**self).as_engine_ref()
169    }
170}