use std::sync::Arc;
use sim_kernel::{
AbiVersion, Cx, Dependency, Export, Lib, LibManifest, LibTarget, Linker, Result, Symbol,
TableBackend, Value, Version,
};
use crate::HashTable;
pub struct HashBackend;
impl TableBackend for HashBackend {
fn name(&self) -> &str {
"hash"
}
fn new_table(&self, cx: &mut Cx, entries: Vec<(Symbol, Value)>) -> Result<Value> {
cx.factory()
.opaque(Arc::new(HashTable::with_entries(entries)))
}
}
pub struct HashTableLib;
impl Lib for HashTableLib {
fn manifest(&self) -> LibManifest {
LibManifest {
id: Symbol::qualified("table", "hash"),
version: Version(env!("CARGO_PKG_VERSION").to_owned()),
abi: AbiVersion { major: 0, minor: 1 },
target: LibTarget::HostRegistered,
requires: Vec::<Dependency>::new(),
capabilities: Vec::new(),
exports: Vec::<Export>::new(),
}
}
fn load(&self, _cx: &mut sim_kernel::LoadCx, _linker: &mut Linker<'_>) -> Result<()> {
Ok(())
}
}
pub fn install_hash_table_lib(cx: &mut Cx) -> Result<()> {
if cx
.registry()
.lib(&Symbol::qualified("table", "hash"))
.is_some()
{
return Ok(());
}
cx.table_registry_mut().register(Arc::new(HashBackend));
cx.load_lib(&HashTableLib).map(|_| ())
}