use std::path::Path;
use std::sync::Arc;
use bytes::Bytes;
use wasmer_compiler::{Artifact, ArtifactCreate};
use wasmer_types::{
CompileError, DeserializeError, ExportsIterator, ImportsIterator, ModuleInfo, SerializeError,
};
use wasmer_types::{ExportType, ImportType};
use crate::{
engine::AsEngineRef, sys::engine::NativeEngineExt, vm::VMInstance, AsStoreMut, AsStoreRef,
InstantiationError, IntoBytes,
};
#[derive(Clone, PartialEq, Eq)]
pub struct Module {
artifact: Arc<Artifact>,
}
impl Module {
pub(crate) fn from_binary(
engine: &impl AsEngineRef,
binary: &[u8],
) -> Result<Self, CompileError> {
Self::validate(engine, binary)?;
unsafe { Self::from_binary_unchecked(engine, binary) }
}
pub(crate) unsafe fn from_binary_unchecked(
engine: &impl AsEngineRef,
binary: &[u8],
) -> Result<Self, CompileError> {
let module = Self::compile(engine, binary)?;
Ok(module)
}
#[tracing::instrument(level = "debug", skip_all)]
pub(crate) fn validate(engine: &impl AsEngineRef, binary: &[u8]) -> Result<(), CompileError> {
engine.as_engine_ref().engine().0.validate(binary)
}
#[cfg(feature = "compiler")]
fn compile(engine: &impl AsEngineRef, binary: &[u8]) -> Result<Self, CompileError> {
let artifact = engine.as_engine_ref().engine().0.compile(binary)?;
Ok(Self::from_artifact(artifact))
}
#[cfg(not(feature = "compiler"))]
fn compile(_engine: &impl AsEngineRef, _binary: &[u8]) -> Result<Self, CompileError> {
Err(CompileError::UnsupportedTarget(
"The compiler feature is not enabled, but is required to compile a Module".to_string(),
))
}
pub(crate) fn serialize(&self) -> Result<Bytes, SerializeError> {
self.artifact.serialize().map(|bytes| bytes.into())
}
#[tracing::instrument(level = "debug", skip_all)]
pub unsafe fn deserialize_unchecked(
engine: &impl AsEngineRef,
bytes: impl IntoBytes,
) -> Result<Self, DeserializeError> {
let bytes = bytes.into_bytes();
let artifact = engine
.as_engine_ref()
.engine()
.0
.deserialize_unchecked(bytes.into())?;
Ok(Self::from_artifact(artifact))
}
#[tracing::instrument(level = "debug", skip_all)]
pub unsafe fn deserialize(
engine: &impl AsEngineRef,
bytes: impl IntoBytes,
) -> Result<Self, DeserializeError> {
let bytes = bytes.into_bytes();
let artifact = engine
.as_engine_ref()
.engine()
.0
.deserialize(bytes.into())?;
Ok(Self::from_artifact(artifact))
}
pub unsafe fn deserialize_from_file_unchecked(
engine: &impl AsEngineRef,
path: impl AsRef<Path>,
) -> Result<Self, DeserializeError> {
let artifact = engine
.as_engine_ref()
.engine()
.0
.deserialize_from_file_unchecked(path.as_ref())?;
Ok(Self::from_artifact(artifact))
}
pub unsafe fn deserialize_from_file(
engine: &impl AsEngineRef,
path: impl AsRef<Path>,
) -> Result<Self, DeserializeError> {
let artifact = engine
.as_engine_ref()
.engine()
.0
.deserialize_from_file(path.as_ref())?;
Ok(Self::from_artifact(artifact))
}
pub(super) fn from_artifact(artifact: Arc<Artifact>) -> Self {
Self { artifact }
}
#[allow(clippy::result_large_err)]
pub(crate) fn instantiate(
&self,
store: &mut impl AsStoreMut,
imports: &[crate::Extern],
) -> Result<VMInstance, InstantiationError> {
if !self.artifact.allocated() {
return Err(InstantiationError::DifferentArchOS);
}
for import in imports {
if !import.is_from_store(store) {
return Err(InstantiationError::DifferentStores);
}
}
let signal_handler = store.as_store_ref().signal_handler();
let mut store_mut = store.as_store_mut();
let (engine, objects) = store_mut.engine_and_objects_mut();
let config = engine.tunables().vmconfig();
unsafe {
let mut instance_handle = self.artifact.instantiate(
engine.tunables(),
&imports
.iter()
.map(crate::Extern::to_vm_extern)
.collect::<Vec<_>>(),
objects,
)?;
self.artifact
.finish_instantiation(config, signal_handler, &mut instance_handle)?;
Ok(instance_handle)
}
}
pub(crate) fn name(&self) -> Option<&str> {
self.info().name.as_deref()
}
pub(crate) fn set_name(&mut self, name: &str) -> bool {
Arc::get_mut(&mut self.artifact).map_or(false, |artifact| {
artifact.set_module_info_name(name.to_string())
})
}
pub(crate) fn imports(&self) -> ImportsIterator<impl Iterator<Item = ImportType> + '_> {
self.info().imports()
}
pub(crate) fn exports(&self) -> ExportsIterator<impl Iterator<Item = ExportType> + '_> {
self.info().exports()
}
pub(crate) fn custom_sections<'a>(
&'a self,
name: &'a str,
) -> impl Iterator<Item = Box<[u8]>> + 'a {
self.info().custom_sections(name)
}
pub(crate) fn info(&self) -> &ModuleInfo {
self.artifact.module_info()
}
}