Added serialize_to_file API

This commit is contained in:
Syrus
2020-06-01 17:02:05 -07:00
parent e5a6be5dda
commit c5e38a7468
3 changed files with 30 additions and 5 deletions

View File

@@ -171,8 +171,8 @@ impl Module {
Ok(Self::from_compiled_module(store, compiled)) Ok(Self::from_compiled_module(store, compiled))
} }
/// Serializes a module into it a propietary serializable format, /// Serializes a module into a binary representation that the `Engine`
/// so it can be used later by [`Module::deserialize`]. /// can later process via [`Module::deserialize`].
/// ///
/// # Usage /// # Usage
/// ///
@@ -189,6 +189,24 @@ impl Module {
self.artifact.serialize() self.artifact.serialize()
} }
/// Serializes a module into a file that the `Engine`
/// can later process via [`Module::deserialize_from_file`].
///
/// # Usage
///
/// ```ignore
/// # use wasmer::*;
/// # fn main() -> anyhow::Result<()> {
/// # let store = Store::default();
/// # let module = Module::from_file(&store, "path/to/foo.wasm")?;
/// module.serialize_to_file("path/to/foo.so")?;
/// # Ok(())
/// # }
/// ```
pub fn serialize_to_file(&self, path: impl AsRef<Path>) -> Result<(), SerializeError> {
self.artifact.serialize_to_file(path.as_ref())
}
/// Deserializes a a serialized Module binary into a `Module`. /// Deserializes a a serialized Module binary into a `Module`.
/// > Note: the module has to be serialized before with the `serialize` method. /// > Note: the module has to be serialized before with the `serialize` method.
/// ///

View File

@@ -2,6 +2,8 @@ use crate::{
resolve_imports, InstantiationError, Resolver, RuntimeError, SerializeError, Tunables, resolve_imports, InstantiationError, Resolver, RuntimeError, SerializeError, Tunables,
}; };
use std::any::Any; use std::any::Any;
use std::fs;
use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use wasm_common::entity::{BoxedSlice, PrimaryMap}; use wasm_common::entity::{BoxedSlice, PrimaryMap};
use wasm_common::{ use wasm_common::{
@@ -64,6 +66,13 @@ pub trait Artifact {
/// Serializes an artifact into bytes /// Serializes an artifact into bytes
fn serialize(&self) -> Result<Vec<u8>, SerializeError>; fn serialize(&self) -> Result<Vec<u8>, SerializeError>;
/// Serializes an artifact into a file path
fn serialize_to_file(&self, path: &Path) -> Result<(), SerializeError> {
let serialized = self.serialize()?;
fs::write(&path, serialized)?;
Ok(())
}
/// Crate an `Instance` from this `Artifact`. /// Crate an `Instance` from this `Artifact`.
/// ///
/// # Unsafety /// # Unsafety

View File

@@ -1,7 +1,6 @@
use crate::store::StoreOptions; use crate::store::StoreOptions;
use crate::warning; use crate::warning;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use structopt::StructOpt; use structopt::StructOpt;
use wasmer::*; use wasmer::*;
@@ -63,8 +62,7 @@ impl Compile {
println!("Compiler: {}", compiler_name); println!("Compiler: {}", compiler_name);
println!("Target: {}", target.triple()); println!("Target: {}", target.triple());
let module = Module::from_file(&store, &self.path)?; let module = Module::from_file(&store, &self.path)?;
let serialized = module.serialize()?; let _ = module.serialize_to_file(&self.output)?;
fs::write(&self.output, serialized)?;
eprintln!( eprintln!(
"✔ File compiled successfully to `{}`.", "✔ File compiled successfully to `{}`.",
self.output.display(), self.output.display(),