use crate::artifact::{self, IntegrityError};
use wasmtime::component::{Component, Linker};
use wasmtime::{Config, Engine, Store};
wasmtime::component::bindgen!({
path: "core/wit",
world: "aethel-core",
});
#[derive(Debug)]
pub enum LoadError {
Integrity(IntegrityError),
Runtime(wasmtime::Error),
}
impl core::fmt::Display for LoadError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
LoadError::Integrity(e) => write!(f, "{e}"),
LoadError::Runtime(e) => write!(f, "the embedded component failed to load: {e}"),
}
}
}
impl std::error::Error for LoadError {}
impl From<IntegrityError> for LoadError {
fn from(e: IntegrityError) -> Self {
LoadError::Integrity(e)
}
}
pub fn load() -> Result<(Store<()>, AethelCore), LoadError> {
load_bytes(artifact::COMPONENT)
}
pub fn load_bytes(bytes: &[u8]) -> Result<(Store<()>, AethelCore), LoadError> {
artifact::verify(bytes)?;
let mut config = Config::new();
config.wasm_component_model(true);
let engine = Engine::new(&config).map_err(LoadError::Runtime)?;
let component = Component::from_binary(&engine, bytes).map_err(LoadError::Runtime)?;
let linker = Linker::new(&engine);
let mut store = Store::new(&engine, ());
let bindings =
AethelCore::instantiate(&mut store, &component, &linker).map_err(LoadError::Runtime)?;
Ok((store, bindings))
}