Improved run command

This commit is contained in:
Syrus
2020-04-30 04:38:02 -07:00
parent 67adb9215a
commit ad2c9ee540
2 changed files with 16 additions and 13 deletions

View File

@@ -58,7 +58,6 @@ pub struct FileSystemCache {
impl FileSystemCache { impl FileSystemCache {
/// Construct a new `FileSystemCache` around the specified directory. /// Construct a new `FileSystemCache` around the specified directory.
/// The contents of the cache are stored in sub-versioned directories.
pub fn new<P: Into<PathBuf>>(path: P) -> io::Result<Self> { pub fn new<P: Into<PathBuf>>(path: P) -> io::Result<Self> {
let path: PathBuf = path.into(); let path: PathBuf = path.into();
if path.exists() { if path.exists() {

View File

@@ -78,10 +78,11 @@ impl Run {
let (compiler_config, compiler_name) = self.compiler.get_config()?; let (compiler_config, compiler_name) = self.compiler.get_config()?;
let engine = Engine::new(&*compiler_config); let engine = Engine::new(&*compiler_config);
let store = Store::new(&engine); let store = Store::new(&engine);
let contents = std::fs::read(self.path.clone())?;
// We try to get it from cache, in case is enabled // We try to get it from cache, in case is enabled
let module = if cfg!(feature = "cache") && !self.disable_cache { let mut module = if cfg!(feature = "cache") && !self.disable_cache {
let mut cache = self.get_cache(compiler_name)?; let mut cache = self.get_cache(compiler_name)?;
let contents = std::fs::read(self.path.clone())?;
// Try to get the hash from the provided `--cache-key`, otherwise // Try to get the hash from the provided `--cache-key`, otherwise
// generate one from the provided file `.wasm` contents. // generate one from the provided file `.wasm` contents.
let hash = self let hash = self
@@ -92,7 +93,7 @@ impl Run {
let module = match unsafe { cache.load(&store, hash) } { let module = match unsafe { cache.load(&store, hash) } {
Ok(module) => module, Ok(module) => module,
Err(_e) => { Err(_e) => {
let module = Module::from_binary(&store, &contents)?; let module = Module::new(&store, &contents)?;
// Store the compiled Module in cache // Store the compiled Module in cache
cache.store(hash, module.clone()); cache.store(hash, module.clone());
module module
@@ -100,8 +101,10 @@ impl Run {
}; };
module module
} else { } else {
Module::from_file(&store, &self.path.clone())? Module::new(&store, &contents)?
}; };
// We set the name outside the cache, to make sure we dont cache the name
module.set_name(self.path.canonicalize()?.as_path().to_str().unwrap());
// Do we want to invoke a function? // Do we want to invoke a function?
if let Some(ref invoke) = self.invoke { if let Some(ref invoke) = self.invoke {
@@ -134,17 +137,18 @@ impl Run {
.map(|f| f.to_string_lossy().to_string()) .map(|f| f.to_string_lossy().to_string())
}) })
.unwrap_or("".to_string()); .unwrap_or("".to_string());
self.wasi return self
.execute(module, version, program_name, self.args.clone())?; .wasi
.execute(module, version, program_name, self.args.clone());
} }
} else {
// Try to instantiate the wasm file, with no provided imports
let imports = imports! {};
let instance = Instance::new(&module, &imports)?;
let start: &Func = instance.exports.get("_start")?;
start.call(&[])?;
} }
// Try to instantiate the wasm file, with no provided imports
let imports = imports! {};
let instance = Instance::new(&module, &imports)?;
let start: &Func = instance.exports.get("_start")?;
start.call(&[])?;
Ok(()) Ok(())
} }