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
use crate::error::LinkError;
use std::sync::Arc;
use wasmer_types::entity::{EntityRef, PrimaryMap};
use wasmer_types::{
GlobalType, LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex, MemoryIndex, MemoryType,
TableIndex, TableType,
};
use wasmer_vm::MemoryError;
use wasmer_vm::{Global, Memory, ModuleInfo, Table};
use wasmer_vm::{MemoryStyle, TableStyle};
pub trait Tunables {
fn memory_style(&self, memory: &MemoryType) -> MemoryStyle;
fn table_style(&self, table: &TableType) -> TableStyle;
fn create_memory(
&self,
ty: &MemoryType,
style: &MemoryStyle,
) -> Result<Arc<dyn Memory>, MemoryError>;
fn create_table(&self, ty: &TableType, style: &TableStyle) -> Result<Arc<dyn Table>, String>;
fn create_global(&self, ty: GlobalType) -> Result<Arc<Global>, String> {
Ok(Arc::new(Global::new(ty)))
}
fn create_memories(
&self,
module: &ModuleInfo,
memory_styles: &PrimaryMap<MemoryIndex, MemoryStyle>,
) -> Result<PrimaryMap<LocalMemoryIndex, Arc<dyn Memory>>, LinkError> {
let num_imports = module.num_imported_memories;
let mut memories: PrimaryMap<LocalMemoryIndex, _> =
PrimaryMap::with_capacity(module.memories.len() - num_imports);
for index in num_imports..module.memories.len() {
let mi = MemoryIndex::new(index);
let ty = &module.memories[mi];
let style = &memory_styles[mi];
memories.push(
self.create_memory(ty, style)
.map_err(|e| LinkError::Resource(format!("Failed to create memory: {}", e)))?,
);
}
Ok(memories)
}
fn create_tables(
&self,
module: &ModuleInfo,
table_styles: &PrimaryMap<TableIndex, TableStyle>,
) -> Result<PrimaryMap<LocalTableIndex, Arc<dyn Table>>, LinkError> {
let num_imports = module.num_imported_tables;
let mut tables: PrimaryMap<LocalTableIndex, _> =
PrimaryMap::with_capacity(module.tables.len() - num_imports);
for index in num_imports..module.tables.len() {
let ti = TableIndex::new(index);
let ty = &module.tables[ti];
let style = &table_styles[ti];
tables.push(self.create_table(ty, style).map_err(LinkError::Resource)?);
}
Ok(tables)
}
fn create_globals(
&self,
module: &ModuleInfo,
) -> Result<PrimaryMap<LocalGlobalIndex, Arc<Global>>, LinkError> {
let num_imports = module.num_imported_globals;
let mut vmctx_globals = PrimaryMap::with_capacity(module.globals.len() - num_imports);
for &global_type in module.globals.values().skip(num_imports) {
vmctx_globals.push(
self.create_global(global_type)
.map_err(LinkError::Resource)?,
);
}
Ok(vmctx_globals)
}
}