use crate::function_info::FunctionDefinition;
use mun_abi as abi;
use mun_memory::type_table::TypeTable;
use rustc_hash::FxHashMap;
use std::sync::Arc;
#[derive(Clone, Default)]
pub struct DispatchTable {
functions: FxHashMap<String, Arc<FunctionDefinition>>,
}
impl DispatchTable {
pub fn get_fn(&self, fn_path: &str) -> Option<Arc<FunctionDefinition>> {
self.functions.get(fn_path).map(Clone::clone)
}
pub fn insert_fn<S: ToString>(
&mut self,
fn_path: S,
fn_info: Arc<FunctionDefinition>,
) -> Option<Arc<FunctionDefinition>> {
self.functions.insert(fn_path.to_string(), fn_info)
}
pub fn remove_module(&mut self, assembly: &abi::ModuleInfo) {
for function in assembly.functions() {
if let Some(value) = self.functions.get(function.prototype.name()) {
if value.fn_ptr == function.fn_ptr {
self.functions.remove(function.prototype.name());
}
}
}
}
pub fn insert_module(&mut self, assembly: &abi::ModuleInfo, type_table: &TypeTable) {
for fn_def in assembly.functions() {
let fn_def = FunctionDefinition::try_from_abi(fn_def, type_table)
.expect("All types from a loaded assembly must exist in the type table.");
self.insert_fn(fn_def.prototype.name.clone(), Arc::new(fn_def));
}
}
}