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
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use crate::context::Context;
use crate::r#ref::HostRef;
use crate::HashMap;
use alloc::{rc::Rc, string::String};
use core::cell::RefCell;
use cranelift_codegen::{ir, settings};
use wasmtime_jit::{CompilationStrategy, Features};

// Runtime Environment

// Configuration

fn default_flags() -> settings::Flags {
    let flag_builder = settings::builder();
    settings::Flags::new(flag_builder)
}

/// Global configuration options used to create an [`Engine`] and customize its
/// behavior.
///
/// This structure exposed a builder-like interface and is primarily consumed by
/// [`Engine::new()`]
#[derive(Clone)]
pub struct Config {
    pub(crate) flags: settings::Flags,
    pub(crate) features: Features,
    pub(crate) debug_info: bool,
    pub(crate) strategy: CompilationStrategy,
}

impl Config {
    /// Creates a new configuration object with the default configuration
    /// specified.
    pub fn new() -> Config {
        Config {
            debug_info: false,
            features: Default::default(),
            flags: default_flags(),
            strategy: CompilationStrategy::Auto,
        }
    }

    /// Configures whether DWARF debug information will be emitted during
    /// compilation.
    ///
    /// By default this option is `false`.
    pub fn debug_info(&mut self, enable: bool) -> &mut Self {
        self.debug_info = enable;
        self
    }

    /// Configures various flags for compilation such as optimization level and
    /// such.
    ///
    /// For more information on defaults and configuration options, see the
    /// documentation for [`Flags`](settings::Flags)
    pub fn flags(&mut self, flags: settings::Flags) -> &mut Self {
        self.flags = flags;
        self
    }

    /// Indicates which WebAssembly features are enabled for this compilation
    /// session.
    ///
    /// By default only stable features are enabled by default (and none are
    /// fully stabilized yet at this time). If you're loading wasm modules
    /// which may use non-MVP features you'll want to be sure to call this
    /// method and enable the appropriate feature in the [`Features`]
    /// structure.
    pub fn features(&mut self, features: Features) -> &mut Self {
        self.features = features;
        self
    }

    /// Configures the compilation `strategy` provided, indicating which
    /// backend will be used for compiling WebAssembly to native code.
    ///
    /// Currently the primary strategies are with cranelift (an optimizing
    /// compiler) or lightbeam (a fast single-pass JIT which produces code
    /// quickly).
    pub fn strategy(&mut self, strategy: CompilationStrategy) -> &mut Self {
        self.strategy = strategy;
        self
    }
}

impl Default for Config {
    fn default() -> Config {
        Config::new()
    }
}

// Engine

#[derive(Default)]
pub struct Engine {
    config: Config,
}

impl Engine {
    pub fn new(config: &Config) -> Engine {
        Engine {
            config: config.clone(),
        }
    }
}

// Store

pub struct Store {
    engine: HostRef<Engine>,
    context: Context,
    global_exports: Rc<RefCell<HashMap<String, Option<wasmtime_runtime::Export>>>>,
    signature_cache: HashMap<wasmtime_runtime::VMSharedSignatureIndex, ir::Signature>,
}

impl Store {
    pub fn new(engine: &HostRef<Engine>) -> Store {
        Store {
            engine: engine.clone(),
            context: Context::new(&engine.borrow().config),
            global_exports: Rc::new(RefCell::new(HashMap::new())),
            signature_cache: HashMap::new(),
        }
    }

    pub fn engine(&self) -> &HostRef<Engine> {
        &self.engine
    }

    pub(crate) fn context(&mut self) -> &mut Context {
        &mut self.context
    }

    // Specific to wasmtime: hack to pass memory around to wasi
    pub fn global_exports(
        &self,
    ) -> &Rc<RefCell<HashMap<String, Option<wasmtime_runtime::Export>>>> {
        &self.global_exports
    }

    pub(crate) fn register_cranelift_signature(
        &mut self,
        signature: &ir::Signature,
    ) -> wasmtime_runtime::VMSharedSignatureIndex {
        use crate::hash_map::Entry;
        let index = self.context().compiler().signatures().register(signature);
        match self.signature_cache.entry(index) {
            Entry::Vacant(v) => {
                v.insert(signature.clone());
            }
            Entry::Occupied(_) => (),
        }
        index
    }

    pub(crate) fn lookup_cranelift_signature(
        &self,
        type_index: wasmtime_runtime::VMSharedSignatureIndex,
    ) -> Option<&ir::Signature> {
        self.signature_cache.get(&type_index)
    }
}