Coverage Report

Created: 2024-10-16 07:58

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