use tinywasm_types::TinyWasmModule;
use crate::{Imports, ModuleInstance, Result, Store};
#[derive(Debug)]
pub struct Module {
pub(crate) data: TinyWasmModule,
}
impl From<&TinyWasmModule> for Module {
fn from(data: &TinyWasmModule) -> Self {
Self { data: data.clone() }
}
}
impl From<TinyWasmModule> for Module {
fn from(data: TinyWasmModule) -> Self {
Self { data }
}
}
impl Module {
#[cfg(feature = "parser")]
pub fn parse_bytes(wasm: &[u8]) -> Result<Self> {
let parser = tinywasm_parser::Parser::new();
let data = parser.parse_module_bytes(wasm)?;
Ok(data.into())
}
#[cfg(all(feature = "parser", feature = "std"))]
pub fn parse_file(path: impl AsRef<crate::std::path::Path> + Clone) -> Result<Self> {
let parser = tinywasm_parser::Parser::new();
let data = parser.parse_module_file(path)?;
Ok(data.into())
}
#[cfg(all(feature = "parser", feature = "std"))]
pub fn parse_stream(stream: impl crate::std::io::Read) -> Result<Self> {
let parser = tinywasm_parser::Parser::new();
let data = parser.parse_module_stream(stream)?;
Ok(data.into())
}
pub fn instantiate(self, store: &mut Store, imports: Option<Imports>) -> Result<ModuleInstance> {
let instance = ModuleInstance::instantiate(store, self, imports)?;
let _ = instance.start(store)?;
Ok(instance)
}
}