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
164
165
166
167
168
use super::*;
use crate::module::FCEModule;
use crate::module::RecordTypes;
use serde::Serialize;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::rc::Rc;
#[derive(PartialEq, Eq, Debug, Clone, Serialize)]
pub struct FCEModuleInterface<'a> {
pub record_types: &'a RecordTypes,
pub function_signatures: Vec<FCEFunctionSignature>,
}
pub struct FCE {
modules: HashMap<String, FCEModule>,
}
impl FCE {
pub fn new() -> Self {
Self {
modules: HashMap::new(),
}
}
pub fn call<MN: AsRef<str>, FN: AsRef<str>>(
&mut self,
module_name: MN,
func_name: FN,
arguments: &[IValue],
) -> Result<Vec<IValue>> {
self.modules.get_mut(module_name.as_ref()).map_or_else(
|| {
Err(FCEError::NoSuchModule(format!(
"trying to call module with name {} that is not loaded",
module_name.as_ref()
)))
},
|module| module.call(func_name.as_ref(), arguments),
)
}
pub fn load_module<S: Into<String>>(
&mut self,
name: S,
wasm_bytes: &[u8],
config: FCEModuleConfig,
) -> Result<()> {
self.load_module_(name.into(), wasm_bytes, config)
}
fn load_module_(
&mut self,
name: String,
wasm_bytes: &[u8],
config: FCEModuleConfig,
) -> Result<()> {
let _prepared_wasm_bytes = crate::misc::prepare_module(wasm_bytes, config.mem_pages_count)?;
let module = FCEModule::new(&wasm_bytes, config, &self.modules)?;
match self.modules.entry(name) {
Entry::Vacant(entry) => {
entry.insert(module);
Ok(())
}
Entry::Occupied(entry) => Err(FCEError::NonUniqueModuleName(entry.key().clone())),
}
}
pub fn unload_module<S: AsRef<str>>(&mut self, name: S) -> Result<()> {
self.modules
.remove(name.as_ref())
.map(|_| ())
.ok_or_else(|| {
FCEError::NoSuchModule(format!(
"trying to unload module with name {} that is not loaded",
name.as_ref()
))
})
}
pub fn module_wasi_state<S: AsRef<str>>(
&mut self,
module_name: S,
) -> Option<&wasmer_wasi::state::WasiState> {
self.modules
.get_mut(module_name.as_ref())
.map(|module| module.get_wasi_state())
}
pub fn interface(&self) -> impl Iterator<Item = (&str, FCEModuleInterface<'_>)> {
self.modules
.iter()
.map(|(module_name, module)| (module_name.as_str(), Self::get_module_interface(module)))
}
pub fn module_interface<S: AsRef<str>>(
&self,
module_name: S,
) -> Option<FCEModuleInterface<'_>> {
self.modules
.get(module_name.as_ref())
.map(|module| Self::get_module_interface(module))
}
pub fn module_record_types<S: AsRef<str>>(&self, module_name: S) -> Option<&RecordTypes> {
self.modules
.get(module_name.as_ref())
.map(|module| module.export_record_types())
}
pub fn module_record_type_by_id<S: AsRef<str>>(
&self,
module_name: S,
record_id: u64,
) -> Option<&Rc<IRecordType>> {
self.modules
.get(module_name.as_ref())
.and_then(|module| module.export_record_type_by_id(record_id))
}
fn get_module_interface(module: &FCEModule) -> FCEModuleInterface<'_> {
let record_types = module.export_record_types();
let function_signatures = module.get_exports_signatures().collect::<Vec<_>>();
FCEModuleInterface {
record_types,
function_signatures,
}
}
}
impl Default for FCE {
fn default() -> Self {
Self::new()
}
}