Sniff the wasm magic bytes, rather than relying on the filename extension.
This commit is contained in:
64
src/main.rs
64
src/main.rs
@@ -91,7 +91,7 @@ struct Args {
|
|||||||
flag_print: bool,
|
flag_print: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_wasm_file(path: PathBuf) -> Result<Vec<u8>, io::Error> {
|
fn read_to_end(path: PathBuf) -> Result<Vec<u8>, io::Error> {
|
||||||
let mut buf: Vec<u8> = Vec::new();
|
let mut buf: Vec<u8> = Vec::new();
|
||||||
let mut file = File::open(path)?;
|
let mut file = File::open(path)?;
|
||||||
file.read_to_end(&mut buf)?;
|
file.read_to_end(&mut buf)?;
|
||||||
@@ -136,47 +136,27 @@ fn handle_module(args: &Args, path: PathBuf, name: &str, isa: &TargetIsa) -> Res
|
|||||||
terminal.fg(term::color::MAGENTA).unwrap();
|
terminal.fg(term::color::MAGENTA).unwrap();
|
||||||
vprint!(args.flag_verbose, "Translating...");
|
vprint!(args.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();
|
||||||
match read_wasm_file(path.clone()) {
|
Command::new("wat2wasm")
|
||||||
Ok(data) => data,
|
.arg(path.clone())
|
||||||
Err(err) => {
|
.arg("-o")
|
||||||
return Err(String::from(err.description()));
|
.arg(file_path.to_str().unwrap())
|
||||||
}
|
.output()
|
||||||
}
|
.or_else(|e| if let io::ErrorKind::NotFound = e.kind() {
|
||||||
}
|
return Err(String::from("wat2wasm not found"));
|
||||||
Some("wat") => {
|
} else {
|
||||||
let tmp_dir = TempDir::new("wasmstandalone").unwrap();
|
return Err(String::from(e.description()));
|
||||||
let file_path = tmp_dir.path().join("module.wasm");
|
})?;
|
||||||
File::create(file_path.clone()).unwrap();
|
data = read_to_end(file_path).map_err(
|
||||||
Command::new("wat2wasm")
|
|err| String::from(err.description()),
|
||||||
.arg(path.clone())
|
)?;
|
||||||
.arg("-o")
|
}
|
||||||
.arg(file_path.to_str().unwrap())
|
|
||||||
.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()));
|
|
||||||
})?;
|
|
||||||
match read_wasm_file(file_path) {
|
|
||||||
Ok(data) => data,
|
|
||||||
Err(err) => {
|
|
||||||
return Err(String::from(err.description()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None | Some(&_) => {
|
|
||||||
return Err(String::from("the file extension is not wasm or wat"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let mut runtime = StandaloneRuntime::with_flags(isa.flags().clone());
|
let mut runtime = StandaloneRuntime::with_flags(isa.flags().clone());
|
||||||
let translation = {
|
let translation = {
|
||||||
match translate_module(&data, &mut runtime) {
|
match translate_module(&data, &mut runtime) {
|
||||||
|
|||||||
Reference in New Issue
Block a user