Move get_module_info into the Engine

This commit is contained in:
Felix Schütt
2023-01-19 19:43:01 +01:00
parent af67c86621
commit b26b48794c
3 changed files with 14 additions and 30 deletions

View File

@@ -224,7 +224,7 @@ impl CreateExe {
return Err(anyhow::anyhow!("input path cannot be a directory")); return Err(anyhow::anyhow!("input path cannot be a directory"));
} }
let (store, compiler_type) = self.compiler.get_store_for_target(target.clone())?; let (_, compiler_type) = self.compiler.get_store_for_target(target.clone())?;
println!("Compiler: {}", compiler_type.to_string()); println!("Compiler: {}", compiler_type.to_string());
println!("Target: {}", target.triple()); println!("Target: {}", target.triple());
@@ -273,7 +273,7 @@ impl CreateExe {
) )
}?; }?;
get_module_infos(&tempdir, &atoms, object_format, &store)?; get_module_infos(&tempdir, &atoms, object_format)?;
let mut entrypoint = get_entrypoint(&tempdir)?; let mut entrypoint = get_entrypoint(&tempdir)?;
create_header_files_in_dir(&tempdir, &mut entrypoint, &atoms, &self.precompiled_atom)?; create_header_files_in_dir(&tempdir, &mut entrypoint, &atoms, &self.precompiled_atom)?;
link_exe_from_dir( link_exe_from_dir(
@@ -964,17 +964,14 @@ fn get_module_infos(
directory: &Path, directory: &Path,
atoms: &[(String, Vec<u8>)], atoms: &[(String, Vec<u8>)],
object_format: ObjectFormat, object_format: ObjectFormat,
store: &Store,
) -> Result<BTreeMap<String, ModuleInfo>, anyhow::Error> { ) -> Result<BTreeMap<String, ModuleInfo>, anyhow::Error> {
let mut entrypoint = let mut entrypoint =
get_entrypoint(directory).with_context(|| anyhow::anyhow!("get module infos"))?; get_entrypoint(directory).with_context(|| anyhow::anyhow!("get module infos"))?;
let mut module_infos = BTreeMap::new(); let mut module_infos = BTreeMap::new();
for (atom_name, atom_bytes) in atoms { for (atom_name, atom_bytes) in atoms {
let tunables = BaseTunables::for_target(store.engine().target()); let module_info = Engine::get_module_info(atom_bytes.as_slice())
let module_info = .map_err(|e| anyhow::anyhow!("could not get module info for atom {atom_name}: {e}"))?;
Artifact::get_module_info(store.engine(), atom_bytes.as_slice(), &tunables)
.map_err(|e| anyhow::anyhow!("could not compile module {atom_name}: {e}"))?;
if let Some(s) = entrypoint if let Some(s) = entrypoint
.atoms .atoms

View File

@@ -56,19 +56,8 @@ impl Artifact {
data: &[u8], data: &[u8],
tunables: &dyn Tunables, tunables: &dyn Tunables,
) -> Result<Self, CompileError> { ) -> Result<Self, CompileError> {
let artifact = Self::get_artifact_build(engine, data, tunables)?;
let mut inner_engine = engine.inner_mut(); let mut inner_engine = engine.inner_mut();
Self::from_parts(&mut inner_engine, artifact)
}
#[cfg(feature = "compiler")]
fn get_artifact_build(
engine: &Engine,
data: &[u8],
tunables: &dyn Tunables,
) -> Result<ArtifactBuild, CompileError> {
let environ = ModuleEnvironment::new(); let environ = ModuleEnvironment::new();
let mut inner_engine = engine.inner_mut();
let translation = environ.translate(data).map_err(CompileError::Wasm)?; let translation = environ.translate(data).map_err(CompileError::Wasm)?;
let module = translation.module; let module = translation.module;
let memory_styles: PrimaryMap<MemoryIndex, MemoryStyle> = module let memory_styles: PrimaryMap<MemoryIndex, MemoryStyle> = module
@@ -90,18 +79,7 @@ impl Artifact {
table_styles, table_styles,
)?; )?;
Ok(artifact) Self::from_parts(&mut inner_engine, artifact)
}
/// Compile a data buffer into a `ArtifactBuild`, which may then be instantiated.
#[cfg(feature = "compiler")]
pub fn get_module_info(
engine: &Engine,
data: &[u8],
tunables: &dyn Tunables,
) -> Result<ModuleInfo, CompileError> {
let artifact = Self::get_artifact_build(engine, data, tunables)?;
Ok(artifact.create_module_info())
} }
/// Compile a data buffer into a `ArtifactBuild`, which may then be instantiated. /// Compile a data buffer into a `ArtifactBuild`, which may then be instantiated.

View File

@@ -74,6 +74,15 @@ impl Engine {
} }
} }
/// Returns only the `ModuleInfo` given a `wasm` byte slice
#[cfg(feature = "compiler")]
pub fn get_module_info(data: &[u8]) -> Result<ModuleInfo, CompileError> {
// this is from `artifact_builder.rs`
let environ = crate::ModuleEnvironment::new();
let translation = environ.translate(data).map_err(CompileError::Wasm)?;
Ok(translation.module)
}
/// Returns the name of this engine /// Returns the name of this engine
pub fn name(&self) -> &str { pub fn name(&self) -> &str {
self.name.as_str() self.name.as_str()