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
use crate::relocation::{ExternalRelocation, TrapSink};
use std::collections::HashMap;
use std::sync::Arc;
use wasmer_runtime_core::{
backend::{sys::Memory, CacheGen},
cache::{Artifact, Error},
module::ModuleInfo,
structures::Map,
types::{LocalFuncIndex, SigIndex},
};
use serde_bench::{deserialize, serialize};
pub struct CacheGenerator {
backend_cache: BackendCache,
memory: Arc<Memory>,
}
impl CacheGenerator {
pub fn new(backend_cache: BackendCache, memory: Arc<Memory>) -> Self {
Self {
backend_cache,
memory,
}
}
}
impl CacheGen for CacheGenerator {
fn generate_cache(&self) -> Result<(Box<[u8]>, Memory), Error> {
let compiled_code = (*self.memory).clone();
Ok((
self.backend_cache.into_backend_data()?.into_boxed_slice(),
compiled_code,
))
}
}
#[derive(Serialize, Deserialize)]
pub struct TrampolineCache {
#[serde(with = "serde_bytes")]
pub code: Vec<u8>,
pub offsets: HashMap<SigIndex, usize>,
}
#[derive(Serialize, Deserialize)]
pub struct BackendCache {
pub external_relocs: Map<LocalFuncIndex, Box<[ExternalRelocation]>>,
pub offsets: Map<LocalFuncIndex, usize>,
pub trap_sink: Arc<TrapSink>,
pub trampolines: TrampolineCache,
}
impl BackendCache {
pub fn from_cache(cache: Artifact) -> Result<(ModuleInfo, Memory, Self), Error> {
let (info, backend_data, compiled_code) = cache.consume();
let backend_cache =
deserialize(&backend_data).map_err(|e| Error::DeserializeError(e.to_string()))?;
Ok((info, compiled_code, backend_cache))
}
pub fn into_backend_data(&self) -> Result<Vec<u8>, Error> {
let mut buffer = Vec::new();
serialize(&mut buffer, self).map_err(|e| Error::SerializeError(e.to_string()))?;
Ok(buffer)
}
}