wasmer_engine/
executable.rs

1use crate::Engine;
2use enumset::EnumSet;
3use wasmer_compiler::{CompileError, CpuFeature, Features};
4use wasmer_types::FunctionIndex;
5use wasmer_vm::Artifact;
6
7mod private {
8    pub struct Internal(pub(super) ());
9}
10
11/// A WASM module built by some [`Engine`](crate::Engine).
12///
13/// Types implementing this trait are ready to be saved (to e.g. disk) for later use or loaded with
14/// the `Engine` to in order to produce an [`Artifact`](crate::Artifact).
15pub trait Executable {
16    /// Load this executable with the specified engine.
17    ///
18    /// TODO(0-copy): change error type here.
19    fn load(
20        &self,
21        engine: &(dyn Engine + 'static),
22    ) -> Result<std::sync::Arc<dyn Artifact>, CompileError>;
23
24    /// The features with which this `Executable` was built.
25    fn features(&self) -> Features;
26
27    /// The CPU features this `Executable` requires.
28    fn cpu_features(&self) -> EnumSet<CpuFeature>;
29
30    /// Serializes the artifact into bytes
31    fn serialize(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>>;
32
33    /// Obtain a best effort description for the function at the given function index.
34    ///
35    /// Implementations are not required to maintain symbol names, so this may always return None.
36    fn function_name(&self, index: FunctionIndex) -> Option<&str>;
37
38    /// Internal: support for downcasting `Executable`s.
39    #[doc(hidden)]
40    fn type_id(&self, _: private::Internal) -> std::any::TypeId
41    where
42        Self: 'static,
43    {
44        std::any::TypeId::of::<Self>()
45    }
46}
47
48impl dyn Executable {
49    /// Downcast a dynamic Executable object to a concrete implementation of the trait.
50    pub fn downcast_ref<T: Executable + 'static>(&self) -> Option<&T> {
51        if std::any::TypeId::of::<T>() == self.type_id(private::Internal(())) {
52            unsafe { Some(&*(self as *const dyn Executable as *const T)) }
53        } else {
54            None
55        }
56    }
57}