wasmer/lib/runtime-core/src/sig_registry.rs

42 lines
1.1 KiB
Rust
Raw Normal View History

2019-01-16 18:26:10 +00:00
use crate::{
structures::Map,
types::{FuncSig, SigIndex},
};
use hashbrown::HashMap;
2019-01-08 17:09:47 +00:00
#[derive(Debug)]
2019-01-08 17:09:47 +00:00
pub struct SigRegistry {
func_table: HashMap<FuncSig, SigIndex>,
2019-01-08 17:09:47 +00:00
sig_assoc: Map<SigIndex, FuncSig>,
duplicated_sig_assoc: Map<SigIndex, SigIndex>,
2019-01-08 17:09:47 +00:00
}
impl SigRegistry {
pub fn new() -> Self {
Self {
func_table: HashMap::new(),
2019-01-08 17:09:47 +00:00
sig_assoc: Map::new(),
duplicated_sig_assoc: Map::new(),
2019-01-08 17:09:47 +00:00
}
}
2019-01-09 02:57:28 +00:00
2019-01-08 17:09:47 +00:00
pub fn register(&mut self, func_sig: FuncSig) -> SigIndex {
// self.sig_assoc.push(func_sig)
let func_table = &mut self.func_table;
let sig_assoc = &mut self.sig_assoc;
let sig_index = *func_table
.entry(func_sig.clone())
.or_insert_with(|| sig_assoc.push(func_sig));
self.duplicated_sig_assoc.push(sig_index);
sig_index
2019-01-08 17:09:47 +00:00
}
pub fn lookup_deduplicated_sigindex(&self, sig_index: SigIndex) -> SigIndex {
self.duplicated_sig_assoc[sig_index]
2019-01-08 17:09:47 +00:00
}
pub fn lookup_func_sig(&self, sig_index: SigIndex) -> &FuncSig {
&self.sig_assoc[sig_index]
2019-01-08 17:09:47 +00:00
}
}