use std::sync::Arc;
use sim_kernel::{
AbiVersion, Cx, Dependency, Export, Lib, LibManifest, LibTarget, Linker, Result, Symbol,
TableBackend, Value, Version,
};
use crate::LazyTable;
pub struct LazyBackend;
impl TableBackend for LazyBackend {
fn name(&self) -> &str {
"lazy"
}
fn new_table(&self, cx: &mut Cx, entries: Vec<(Symbol, Value)>) -> Result<Value> {
cx.factory()
.opaque(Arc::new(LazyTable::with_entries(entries)))
}
}
pub struct LazyTableLib;
impl Lib for LazyTableLib {
fn manifest(&self) -> LibManifest {
LibManifest {
id: Symbol::qualified("table", "lazy"),
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_lazy_table_lib(cx: &mut Cx) -> Result<()> {
let lib_id = Symbol::qualified("table", "lazy");
if cx.registry().lib(&lib_id).is_some() {
return Ok(());
}
cx.table_registry_mut().register(Arc::new(LazyBackend));
cx.load_lib(&LazyTableLib).map(|_| ())
}