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! {
16    #[derive(Debug, Clone)]
17    pub(crate) BackendEngine(entities::engine::Engine);
18}
19
20impl BackendEngine {
21    /// Returns the deterministic id of this engine.
22    #[inline]
23    pub fn deterministic_id(&self) -> String {
24        match_rt!(on self  => s {
25            s.deterministic_id()
26        })
27    }
28
29    #[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
30    /// Deserializes a WebAssembly module which was previously serialized with
31    /// `Module::serialize`,
32    ///
33    /// # Note
34    /// You should almost always prefer [`Self::deserialize`].
35    ///
36    /// # Errors
37    /// Not every implementer supports serializing and deserializing modules.
38    /// Currently, only the `sys` engines support it, and only when the target
39    /// architecture is not `wasm32`.
40    ///
41    /// # Safety
42    /// See [`Artifact::deserialize_unchecked`].
43    #[inline]
44    pub(crate) unsafe fn deserialize_unchecked(
45        &self,
46        bytes: impl IntoBytes,
47    ) -> Result<Arc<Artifact>, DeserializeError> {
48        match self {
49            #[cfg(feature = "sys")]
50            Self::Sys(s) => unsafe {
51                s.deserialize_unchecked(bytes.into_bytes().to_owned().into())
52            },
53            _ => Err(DeserializeError::Generic(
54                "The selected runtime does not support `deserialize_unchecked`".into(),
55            )),
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    /// # Errors
64    /// Not every implementer supports serializing and deserializing modules.
65    /// Currently, only the `sys` engines support it, and only when the target
66    /// architecture is not `wasm32`.
67    #[inline]
68    pub(crate) unsafe fn deserialize(
69        &self,
70        bytes: impl IntoBytes,
71    ) -> Result<Arc<Artifact>, DeserializeError> {
72        match self {
73            #[cfg(feature = "sys")]
74            Self::Sys(s) => unsafe { s.deserialize(bytes.into_bytes().to_owned().into()) },
75            _ => Err(DeserializeError::Generic(
76                "The selected runtime does not support `deserialize`".into(),
77            )),
78        }
79    }
80
81    #[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
82    /// Load a serialized WebAssembly module from a file and deserialize it.
83    ///
84    /// # Note
85    /// You should almost always prefer [`Self::deserialize_from_file`].
86    ///
87    /// # Errors
88    /// Not every implementer supports serializing and deserializing modules.
89    /// Currently, only the `sys` engines support it, and only when the target
90    /// architecture is not `wasm32`.
91    ///
92    /// # Safety
93    /// See [`Artifact::deserialize_unchecked`].
94    #[inline]
95    pub(crate) unsafe fn deserialize_from_file_unchecked(
96        &self,
97        file_ref: &Path,
98    ) -> Result<Arc<Artifact>, DeserializeError> {
99        match self {
100            #[cfg(feature = "sys")]
101            Self::Sys(s) => unsafe { s.deserialize_from_file_unchecked(file_ref) },
102            _ => Err(DeserializeError::Generic(
103                "The selected runtime does not support `deserialize_from_file_unchecked`".into(),
104            )),
105        }
106    }
107
108    #[cfg(all(feature = "sys", not(target_arch = "wasm32")))]
109    /// Load a serialized WebAssembly module from a file and deserialize it.
110    ///
111    /// # Errors
112    /// Not every implementer supports serializing and deserializing modules.
113    /// Currently, only the `sys` engines support it, and only when the target
114    /// architecture is not `wasm32`.
115    ///
116    /// # Safety
117    /// See [`Artifact::deserialize`].
118    #[inline]
119    pub(crate) unsafe fn deserialize_from_file(
120        &self,
121        file_ref: &Path,
122    ) -> Result<Arc<Artifact>, DeserializeError> {
123        match self {
124            #[cfg(feature = "sys")]
125            Self::Sys(s) => unsafe { s.deserialize_from_file(file_ref) },
126            _ => Err(DeserializeError::Generic(
127                "The selected runtime does not support `deserialize_from_file`".into(),
128            )),
129        }
130    }
131}
132
133impl Default for BackendEngine {
134    #[allow(unreachable_code)]
135    #[inline]
136    fn default() -> Self {
137        #[cfg(feature = "sys-default")]
138        {
139            return Self::Sys(crate::backend::sys::entities::engine::default_engine());
140        }
141
142        #[cfg(feature = "v8-default")]
143        {
144            return Self::V8(crate::backend::v8::entities::engine::default_engine());
145        }
146
147        #[cfg(feature = "js-default")]
148        {
149            return Self::Js(crate::backend::js::entities::engine::default_engine());
150        }
151
152        #[cfg(feature = "sys")]
153        {
154            return Self::Sys(crate::backend::sys::entities::engine::default_engine());
155        }
156
157        #[cfg(feature = "v8")]
158        {
159            return Self::V8(crate::backend::v8::entities::engine::default_engine());
160        }
161
162        #[cfg(feature = "js")]
163        {
164            return Self::Js(crate::backend::js::entities::engine::default_engine());
165        }
166
167        panic!("No runtime enabled!")
168    }
169}