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};
fn default_flags() -> settings::Flags {
let flag_builder = settings::builder();
settings::Flags::new(flag_builder)
}
#[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 {
pub fn new() -> Config {
Config {
debug_info: false,
features: Default::default(),
flags: default_flags(),
strategy: CompilationStrategy::Auto,
}
}
pub fn debug_info(&mut self, enable: bool) -> &mut Self {
self.debug_info = enable;
self
}
pub fn flags(&mut self, flags: settings::Flags) -> &mut Self {
self.flags = flags;
self
}
pub fn features(&mut self, features: Features) -> &mut Self {
self.features = features;
self
}
pub fn strategy(&mut self, strategy: CompilationStrategy) -> &mut Self {
self.strategy = strategy;
self
}
}
impl Default for Config {
fn default() -> Config {
Config::new()
}
}
#[derive(Default)]
pub struct Engine {
config: Config,
}
impl Engine {
pub fn new(config: &Config) -> Engine {
Engine {
config: config.clone(),
}
}
}
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
}
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)
}
}