1
0
mirror of https://github.com/fluencelabs/wasmer synced 2025-03-23 11:30:51 +00:00

73 lines
1.8 KiB
Rust
Raw Normal View History

use crate::relocation::{ExternalRelocation, TrapSink};
use hashbrown::HashMap;
use wasmer_runtime_core::{
2019-02-19 09:58:01 -08:00
backend::{
sys::Memory,
CacheGen,
},
cache::{Cache, Error},
2019-02-19 09:58:01 -08:00
module::{ModuleInfo, ModuleInner},
structures::Map,
types::{LocalFuncIndex, SigIndex},
};
2019-02-19 09:58:01 -08:00
use std::{
sync::Arc,
cell::UnsafeCell,
};
use serde_bench::{deserialize, serialize};
2019-02-19 09:58:01 -08:00
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, module: &ModuleInner) -> Result<(Box<ModuleInfo>, Box<[u8]>, Arc<Memory>), Error> {
let info = Box::new(module.info.clone());
Err(Error::Unknown("".to_string()))
}
}
#[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>,
2019-02-19 09:58:01 -08:00
pub trap_sink: Arc<TrapSink>,
pub trampolines: TrampolineCache,
}
impl BackendCache {
pub fn from_cache(cache: Cache) -> Result<(ModuleInfo, Memory, Self), Error> {
let (info, backend_data, compiled_code) = cache.consume();
let backend_cache = deserialize(backend_data.as_slice())
.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)
}
}