Sniff the wasm magic bytes, rather than relying on the filename extension.

This commit is contained in:
Dan Gohman
2017-10-03 09:19:55 -07:00
parent ba14499fe9
commit d857aacec3

View File

@@ -11,7 +11,6 @@ use cretonne::settings::FlagsOrIsa;
use std::fs::File; use std::fs::File;
use std::error::Error; use std::error::Error;
use std::io; use std::io;
use std::io::prelude::*;
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
use tempdir::TempDir; use tempdir::TempDir;
@@ -78,41 +77,27 @@ fn handle_module(
terminal.fg(term::color::MAGENTA).unwrap(); terminal.fg(term::color::MAGENTA).unwrap();
vprint!(flag_verbose, "Translating... "); vprint!(flag_verbose, "Translating... ");
terminal.reset().unwrap(); terminal.reset().unwrap();
let data = match path.extension() { let mut data = read_to_end(path.clone()).map_err(|err| {
None => { String::from(err.description())
return Err(String::from("the file extension is not wasm or wat")); })?;
} if !data.starts_with(&[b'\0', b'a', b's', b'm']) {
Some(ext) => { let tmp_dir = TempDir::new("cretonne-wasm").unwrap();
match ext.to_str() { let file_path = tmp_dir.path().join("module.wasm");
Some("wasm") => { File::create(file_path.clone()).unwrap();
read_to_end(path.clone()).map_err(|err| { Command::new("wat2wasm")
String::from(err.description()) .arg(path.clone())
})? .arg("-o")
} .arg(file_path.to_str().unwrap())
Some("wat") => { .output()
let tmp_dir = TempDir::new("cretonne-wasm").unwrap(); .or_else(|e| if let io::ErrorKind::NotFound = e.kind() {
let file_path = tmp_dir.path().join("module.wasm"); return Err(String::from("wat2wasm not found"));
File::create(file_path.clone()).unwrap(); } else {
Command::new("wat2wasm") return Err(String::from(e.description()));
.arg(path.clone()) })?;
.arg("-o") data = read_to_end(file_path).map_err(
.arg(file_path.to_str().unwrap()) |err| String::from(err.description()),
.output() )?;
.or_else(|e| if let io::ErrorKind::NotFound = e.kind() { }
return Err(String::from("wat2wasm not found"));
} else {
return Err(String::from(e.description()));
})?;
read_to_end(file_path).map_err(
|err| String::from(err.description()),
)?
}
None | Some(&_) => {
return Err(String::from("the file extension is not wasm or wat"));
}
}
}
};
let mut dummy_runtime = DummyRuntime::with_flags(fisa.flags.clone()); let mut dummy_runtime = DummyRuntime::with_flags(fisa.flags.clone());
let translation = { let translation = {
let runtime: &mut WasmRuntime = &mut dummy_runtime; let runtime: &mut WasmRuntime = &mut dummy_runtime;