Implementing deserializing from binary

This commit is contained in:
Syrus
2020-05-08 14:56:47 -07:00
parent 081d93a94c
commit 1cda09c7b6

View File

@@ -4,9 +4,10 @@ use crate::NativeModule;
use std::cell::RefCell;
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::io::{Read, Write};
use std::path::Path;
use std::sync::Arc;
use tempfile::NamedTempFile;
use wasm_common::entity::PrimaryMap;
use wasm_common::{FunctionType, LocalFunctionIndex, MemoryIndex, SignatureIndex, TableIndex};
use wasmer_compiler::{Compilation, CompileError, FunctionBody, Target};
@@ -171,20 +172,30 @@ impl Engine for NativeEngine {
Ok(serialized)
}
/// Deserializes a WebAssembly module
/// Deserializes a WebAssembly module (binary content of a Shared Object file)
fn deserialize(&self, bytes: &[u8]) -> Result<Arc<dyn BaseCompiledModule>, DeserializeError> {
unimplemented!("Deserialize from bytes not yet implemented.");
if !Self::is_deserializable(&bytes) {
return Err(DeserializeError::Incompatible(
"The provided bytes are not in any native format Wasmer can understand".to_string(),
));
}
// Dump the bytes into a file, so we can read it with our `dlopen`
let named_file = NamedTempFile::new().unwrap();
let (mut file, path) = named_file.keep().expect("Can't persist file");
file.write_all(&bytes).expect("Can't write the files");
self.deserialize_from_file(&path)
}
/// Deserializes a WebAssembly module from a path
/// It should point to a Shared Object file generated by this engine.
fn deserialize_from_file(
&self,
file_ref: &Path,
) -> Result<Arc<dyn BaseCompiledModule>, DeserializeError> {
// TODO: Return an IoDeserializeError, so we don't need to map the error
let mut file = File::open(&file_ref).expect("Can't open file");
let mut buffer = [0; 4];
// read up to 4 bytes
let mut buffer = [0; 5];
// read up to 5 bytes
file.read(&mut buffer).expect("Can't read from file");
if !Self::is_deserializable(&buffer) {
return Err(DeserializeError::Incompatible(