wasmer_engine_universal_artifact/
engine.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! Universal compilation.

use loupe::MemoryUsage;
use wasmer_compiler::CompileError;
use wasmer_compiler::Compiler;
use wasmer_types::Features;

/// The Builder contents of `UniversalEngine`
#[derive(MemoryUsage)]
pub struct UniversalEngineBuilder {
    /// The compiler
    #[cfg(feature = "compiler")]
    compiler: Option<Box<dyn Compiler>>,
    /// The features to compile the Wasm module with
    features: Features,
}

impl UniversalEngineBuilder {
    /// Create a new builder with pre-made components
    #[cfg(feature = "compiler")]
    pub fn new(compiler: Option<Box<dyn Compiler>>, features: Features) -> Self {
        UniversalEngineBuilder { compiler, features }
    }

    /// Gets the compiler associated to this engine.
    #[cfg(feature = "compiler")]
    pub fn compiler(&self) -> Result<&dyn Compiler, CompileError> {
        if self.compiler.is_none() {
            return Err(CompileError::Codegen(
                "The UniversalEngine is not compiled in.".to_string(),
            ));
        }
        Ok(&**self.compiler.as_ref().unwrap())
    }

    /// Gets the compiler associated to this engine.
    #[cfg(not(feature = "compiler"))]
    pub fn compiler(&self) -> Result<&dyn Compiler, CompileError> {
        return Err(CompileError::Codegen(
            "The UniversalEngine is not compiled in.".to_string(),
        ));
    }

    /// Validate the module
    #[cfg(feature = "compiler")]
    pub fn validate<'data>(&self, data: &'data [u8]) -> Result<(), CompileError> {
        self.compiler()?.validate_module(self.features(), data)
    }

    /// Validate the module
    #[cfg(not(feature = "compiler"))]
    pub fn validate<'data>(&self, _data: &'data [u8]) -> Result<(), CompileError> {
        Err(CompileError::Validate(
            "The UniversalEngine is not compiled with compiler support, which is required for validating"
                .to_string(),
        ))
    }

    /// The Wasm features
    pub fn features(&self) -> &Features {
        &self.features
    }
}