wasmer_compiler/
traits.rs

1//! Generic Artifact abstraction for Wasmer Engines.
2
3use crate::types::target::CpuFeature;
4use crate::Features;
5use enumset::EnumSet;
6use std::any::Any;
7use std::sync::Arc;
8use wasmer_types::entity::PrimaryMap;
9use wasmer_types::SerializeError;
10use wasmer_types::{
11    DataInitializerLike, MemoryIndex, MemoryStyle, ModuleInfo, TableIndex, TableStyle,
12};
13
14/// An `Artifact` is the product that the `Engine`
15/// implementation produce and use.
16///
17/// The `Artifact` contains the compiled data for a given
18/// module as well as extra information needed to run the
19/// module at runtime, such as [`ModuleInfo`] and [`Features`].
20pub trait ArtifactCreate<'a>: Send + Sync + Upcastable {
21    /// Type of `OwnedDataInitializer` returned by the `data_initializers` method
22    type OwnedDataInitializer: DataInitializerLike<'a> + 'a;
23    /// Type of iterator returned by the `data_initializers` method
24    type OwnedDataInitializerIterator: Iterator<Item = Self::OwnedDataInitializer>;
25
26    /// Create a `ModuleInfo` for instantiation
27    fn create_module_info(&'a self) -> Arc<ModuleInfo>;
28
29    /// Sets the `ModuleInfo` name
30    fn set_module_info_name(&'a mut self, name: String) -> bool;
31
32    /// Returns the `ModuleInfo` for instantiation
33    fn module_info(&'a self) -> &ModuleInfo;
34
35    /// Returns the features for this Artifact
36    fn features(&'a self) -> &Features;
37
38    /// Returns the CPU features for this Artifact
39    fn cpu_features(&'a self) -> EnumSet<CpuFeature>;
40
41    /// Returns the memory styles associated with this `Artifact`.
42    fn memory_styles(&'a self) -> &PrimaryMap<MemoryIndex, MemoryStyle>;
43
44    /// Returns the table plans associated with this `Artifact`.
45    fn table_styles(&'a self) -> &PrimaryMap<TableIndex, TableStyle>;
46
47    /// Returns data initializers to pass to `VMInstance::initialize`
48    fn data_initializers(&'a self) -> Self::OwnedDataInitializerIterator;
49
50    /// Serializes an artifact into bytes
51    fn serialize(&'a self) -> Result<Vec<u8>, SerializeError>;
52}
53
54// Implementation of `Upcastable` taken from https://users.rust-lang.org/t/why-does-downcasting-not-work-for-subtraits/33286/7 .
55/// Trait needed to get downcasting of `Engine`s to work.
56pub trait Upcastable {
57    /// upcast ref
58    fn upcast_any_ref(&'_ self) -> &'_ dyn Any;
59    /// upcast mut ref
60    fn upcast_any_mut(&'_ mut self) -> &'_ mut dyn Any;
61    /// upcast boxed dyn
62    fn upcast_any_box(self: Box<Self>) -> Box<dyn Any>;
63}
64
65impl<T: Any + Send + Sync + 'static> Upcastable for T {
66    #[inline]
67    fn upcast_any_ref(&'_ self) -> &'_ dyn Any {
68        self
69    }
70    #[inline]
71    fn upcast_any_mut(&'_ mut self) -> &'_ mut dyn Any {
72        self
73    }
74    #[inline]
75    fn upcast_any_box(self: Box<Self>) -> Box<dyn Any> {
76        self
77    }
78}
79
80impl<'a, O, I>
81    dyn ArtifactCreate<'a, OwnedDataInitializer = O, OwnedDataInitializerIterator = I> + 'static
82{
83    /// Try to downcast the artifact into a given type.
84    #[inline]
85    pub fn downcast_ref<T: 'static>(&'a self) -> Option<&'a T> {
86        self.upcast_any_ref().downcast_ref::<T>()
87    }
88
89    /// Try to downcast the artifact into a given type mutably.
90    #[inline]
91    pub fn downcast_mut<T: 'static>(&'a mut self) -> Option<&'a mut T> {
92        self.upcast_any_mut().downcast_mut::<T>()
93    }
94}