wasmer/entities/engine/
inner.rs

1use bytes::Bytes;
2use std::{path::Path, sync::Arc};
3use wasmer_types::{DeserializeError, Features, target::Target};
4
5#[cfg(feature = "sys")]
6use wasmer_compiler::Artifact;
7#[cfg(feature = "compiler")]
8use wasmer_compiler::CompilerConfig;
9
10use crate::{
11    BackendKind, IntoBytes, Store,
12    macros::backend::{gen_rt_ty, match_rt},
13};
14
15gen_rt_ty!(Engine @derives Debug, Clone);
16
17impl BackendEngine {
18    /// Returns the deterministic id of this engine.
19    #[inline]
20    pub fn deterministic_id(&self) -> String {
21        match_rt!(on self  => s {
22            s.deterministic_id()
23        })
24    }
25
26    #[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
27    /// Deserializes a WebAssembly module which was previously serialized with
28    /// `Module::serialize`,
29    ///
30    /// # Note
31    /// You should almost always prefer [`Self::deserialize`].
32    ///
33    /// # Errors
34    /// Not every implementer supports serializing and deserializing modules.
35    /// Currently, only the `sys` engines support it, and only when the target
36    /// architecture is not `wasm32`.
37    ///
38    /// # Safety
39    /// See [`Artifact::deserialize_unchecked`].
40    #[inline]
41    pub(crate) unsafe fn deserialize_unchecked(
42        &self,
43        bytes: impl IntoBytes,
44    ) -> Result<Arc<Artifact>, DeserializeError> {
45        match self {
46            #[cfg(feature = "sys")]
47            Self::Sys(s) => unsafe {
48                s.deserialize_unchecked(bytes.into_bytes().to_owned().into())
49            },
50            _ => Err(DeserializeError::Generic(
51                "The selected runtime does not support `deserialize_unchecked`".into(),
52            )),
53        }
54    }
55
56    #[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
57    /// Deserializes a WebAssembly module which was previously serialized with
58    /// `Module::serialize`,
59    ///
60    /// # Errors
61    /// Not every implementer supports serializing and deserializing modules.
62    /// Currently, only the `sys` engines support it, and only when the target
63    /// architecture is not `wasm32`.
64    #[inline]
65    pub(crate) unsafe fn deserialize(
66        &self,
67        bytes: impl IntoBytes,
68    ) -> Result<Arc<Artifact>, DeserializeError> {
69        match self {
70            #[cfg(feature = "sys")]
71            Self::Sys(s) => unsafe { s.deserialize(bytes.into_bytes().to_owned().into()) },
72            _ => Err(DeserializeError::Generic(
73                "The selected runtime does not support `deserialize`".into(),
74            )),
75        }
76    }
77
78    #[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
79    /// Load a serialized WebAssembly module from a file and deserialize it.
80    ///
81    /// # Note
82    /// You should almost always prefer [`Self::deserialize_from_file`].
83    ///
84    /// # Errors
85    /// Not every implementer supports serializing and deserializing modules.
86    /// Currently, only the `sys` engines support it, and only when the target
87    /// architecture is not `wasm32`.
88    ///
89    /// # Safety
90    /// See [`Artifact::deserialize_unchecked`].
91    #[inline]
92    pub(crate) unsafe fn deserialize_from_file_unchecked(
93        &self,
94        file_ref: &Path,
95    ) -> Result<Arc<Artifact>, DeserializeError> {
96        match self {
97            #[cfg(feature = "sys")]
98            Self::Sys(s) => unsafe { s.deserialize_from_file_unchecked(file_ref) },
99            _ => Err(DeserializeError::Generic(
100                "The selected runtime does not support `deserialize_from_file_unchecked`".into(),
101            )),
102        }
103    }
104
105    #[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
106    /// Load a serialized WebAssembly module from a file and deserialize it.
107    ///
108    /// # Errors
109    /// Not every implementer supports serializing and deserializing modules.
110    /// Currently, only the `sys` engines support it, and only when the target
111    /// architecture is not `wasm32`.
112    ///
113    /// # Safety
114    /// See [`Artifact::deserialize`].
115    #[inline]
116    pub(crate) unsafe fn deserialize_from_file(
117        &self,
118        file_ref: &Path,
119    ) -> Result<Arc<Artifact>, DeserializeError> {
120        match self {
121            #[cfg(feature = "sys")]
122            Self::Sys(s) => unsafe { s.deserialize_from_file(file_ref) },
123            _ => Err(DeserializeError::Generic(
124                "The selected runtime does not support `deserialize_from_file`".into(),
125            )),
126        }
127    }
128}
129
130impl Default for BackendEngine {
131    #[allow(unreachable_code)]
132    #[inline]
133    fn default() -> Self {
134        #[cfg(feature = "sys-default")]
135        {
136            return Self::Sys(crate::backend::sys::entities::engine::default_engine());
137        }
138
139        #[cfg(feature = "wamr-default")]
140        {
141            return Self::Wamr(crate::backend::wamr::entities::engine::default_engine());
142        }
143
144        #[cfg(feature = "wasmi-default")]
145        {
146            return Self::Wasmi(crate::backend::wasmi::entities::engine::default_engine());
147        }
148
149        #[cfg(feature = "v8-default")]
150        {
151            return Self::V8(crate::backend::v8::entities::engine::default_engine());
152        }
153
154        #[cfg(feature = "js-default")]
155        {
156            return Self::Js(crate::backend::js::entities::engine::default_engine());
157        }
158
159        #[cfg(feature = "jsc-default")]
160        {
161            return Self::Jsc(crate::backend::jsc::entities::engine::default_engine());
162        }
163
164        #[cfg(feature = "sys")]
165        {
166            return Self::Sys(crate::backend::sys::entities::engine::default_engine());
167        }
168
169        #[cfg(feature = "wamr")]
170        {
171            return Self::Wamr(crate::backend::wamr::entities::engine::default_engine());
172        }
173
174        #[cfg(feature = "wasmi")]
175        {
176            return Self::Wasmi(crate::backend::wasmi::entities::engine::default_engine());
177        }
178
179        #[cfg(feature = "v8")]
180        {
181            return Self::V8(crate::backend::v8::entities::engine::default_engine());
182        }
183
184        #[cfg(feature = "js")]
185        {
186            return Self::Js(crate::backend::js::entities::engine::default_engine());
187        }
188
189        #[cfg(feature = "jsc")]
190        {
191            return Self::Jsc(crate::backend::jsc::entities::engine::default_engine());
192        }
193
194        panic!("No runtime enabled!")
195    }
196}