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#[derive(Clone, Debug)]
33pub struct Engine(pub(crate) engine_imp::Engine);
34
35impl Engine {
36 pub fn deterministic_id(&self) -> &str {
38 self.0.deterministic_id()
39 }
40
41 #[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
42 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 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 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 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
127pub struct EngineRef<'a> {
133 pub(crate) inner: &'a Engine,
135}
136
137impl<'a> EngineRef<'a> {
138 pub fn engine(&self) -> &Engine {
140 self.inner
141 }
142 pub fn new(engine: &'a Engine) -> Self {
144 EngineRef { inner: engine }
145 }
146}
147
148pub trait AsEngineRef {
150 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}