Added API to deserialize from file

This commit is contained in:
Syrus
2020-05-07 22:15:27 -07:00
parent 63d7df78fa
commit 6fe3d362b2
3 changed files with 37 additions and 10 deletions

View File

@@ -162,9 +162,9 @@ impl Module {
/// Deserializes a a serialized Module binary into a `Module`.
/// > Note: the module has to be serialized before with the `serialize` method.
///
/// # Unsafety
/// # Safety
///
/// This function is inherently `unsafe` as the provided bytes:
/// This function is inherently **unsafe** as the provided bytes:
/// 1. Are going to be deserialized directly into Rust objects.
/// 2. Contains the function assembly bodies and, if intercepted,
/// a malicious actor could inject code into executable
@@ -184,6 +184,28 @@ impl Module {
Ok(Self::from_compiled_module(store, compiled))
}
/// Deserializes a a serialized Module located in a `Path` into a `Module`.
/// > Note: the module has to be serialized before with the `serialize` method.
///
/// # Safety
///
/// Please check [`Module::deserialize`].
///
/// # Usage
///
/// ```ignore
/// # use wasmer::*;
/// # let store = Store::default();
/// let module = Module::deserialize_from_file(&store, path)?;
/// ```
pub unsafe fn deserialize_from_file(
store: &Store,
path: impl AsRef<Path>,
) -> Result<Self, DeserializeError> {
let compiled = store.engine().deserialize_from_file(path.as_ref())?;
Ok(Self::from_compiled_module(store, compiled))
}
fn from_compiled_module(store: &Store, compiled: Arc<CompiledModule>) -> Self {
Module {
store: store.clone(),