/
serialize.rs
93 lines (87 loc) 路 3.34 KB
/
serialize.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
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 loupe::MemoryUsage;
use serde::{Deserialize, Serialize};
use wasmer_compiler::{CompileModuleInfo, SectionIndex, Symbol, SymbolRegistry};
use wasmer_types::entity::{EntityRef, PrimaryMap};
use wasmer_types::{FunctionIndex, LocalFunctionIndex, OwnedDataInitializer, SignatureIndex};
/// Serializable struct that represents the compiled metadata.
#[derive(Serialize, Deserialize, Debug, MemoryUsage)]
pub struct ModuleMetadata {
pub compile_info: CompileModuleInfo,
pub prefix: String,
pub data_initializers: Box<[OwnedDataInitializer]>,
// The function body lengths (used to find function by address)
pub function_body_lengths: PrimaryMap<LocalFunctionIndex, u64>,
pub cpu_features: u64,
}
#[derive(MemoryUsage)]
pub struct ModuleMetadataSymbolRegistry {
pub prefix: String,
}
impl ModuleMetadata {
pub fn split(&mut self) -> (&mut CompileModuleInfo, ModuleMetadataSymbolRegistry) {
let compile_info = &mut self.compile_info;
let symbol_registry = ModuleMetadataSymbolRegistry {
prefix: self.prefix.clone(),
};
(compile_info, symbol_registry)
}
pub fn get_symbol_registry(&self) -> ModuleMetadataSymbolRegistry {
ModuleMetadataSymbolRegistry {
prefix: self.prefix.clone(),
}
}
}
impl SymbolRegistry for ModuleMetadataSymbolRegistry {
fn symbol_to_name(&self, symbol: Symbol) -> String {
match symbol {
Symbol::LocalFunction(index) => {
format!("wasmer_function_{}_{}", self.prefix, index.index())
}
Symbol::Section(index) => format!("wasmer_section_{}_{}", self.prefix, index.index()),
Symbol::FunctionCallTrampoline(index) => {
format!(
"wasmer_trampoline_function_call_{}_{}",
self.prefix,
index.index()
)
}
Symbol::DynamicFunctionTrampoline(index) => {
format!(
"wasmer_trampoline_dynamic_function_{}_{}",
self.prefix,
index.index()
)
}
}
}
fn name_to_symbol(&self, name: &str) -> Option<Symbol> {
if let Some(index) = name.strip_prefix(&format!("wasmer_function_{}_", self.prefix)) {
index
.parse::<u32>()
.ok()
.map(|index| Symbol::LocalFunction(LocalFunctionIndex::from_u32(index)))
} else if let Some(index) = name.strip_prefix(&format!("wasmer_section_{}_", self.prefix)) {
index
.parse::<u32>()
.ok()
.map(|index| Symbol::Section(SectionIndex::from_u32(index)))
} else if let Some(index) =
name.strip_prefix(&format!("wasmer_trampoline_function_call_{}_", self.prefix))
{
index
.parse::<u32>()
.ok()
.map(|index| Symbol::FunctionCallTrampoline(SignatureIndex::from_u32(index)))
} else if let Some(index) = name.strip_prefix(&format!(
"wasmer_trampoline_dynamic_function_{}_",
self.prefix
)) {
index
.parse::<u32>()
.ok()
.map(|index| Symbol::DynamicFunctionTrampoline(FunctionIndex::from_u32(index)))
} else {
None
}
}
}