do_main() thing

This commit is contained in:
NikVolf
2018-03-27 17:33:10 +03:00
parent 12f67a8836
commit 2248aa58cf
+22 -24
View File
@@ -94,12 +94,7 @@ fn has_ctor(module: &elements::Module) -> bool {
} }
} }
fn die(e: Error) -> ! { fn do_main() -> Result<(), Error> {
eprintln!("{}", e);
std::process::exit(1)
}
fn main() {
utils::init_log(); utils::init_log();
let matches = App::new("wasm-build") let matches = App::new("wasm-build")
@@ -153,7 +148,7 @@ fn main() {
} else if source_target_val == source::EMSCRIPTEN_TRIPLET { } else if source_target_val == source::EMSCRIPTEN_TRIPLET {
source_input = source_input.emscripten() source_input = source_input.emscripten()
} else { } else {
println!("--target can be: '{}' or '{}'", source::EMSCRIPTEN_TRIPLET, source::UNKNOWN_TRIPLET); eprintln!("--target can be: '{}' or '{}'", source::EMSCRIPTEN_TRIPLET, source::UNKNOWN_TRIPLET);
::std::process::exit(1); ::std::process::exit(1);
} }
@@ -161,11 +156,12 @@ fn main() {
source_input = source_input.with_final(final_name); source_input = source_input.with_final(final_name);
} }
process_output(&source_input).unwrap_or_else(|e| die(e)); process_output(&source_input)?;
let path = wasm_path(&source_input); let path = wasm_path(&source_input);
let mut module = parity_wasm::deserialize_file(&path).unwrap_or_else(|e| die(Error::Decoding(e, path.to_string()))); let mut module = parity_wasm::deserialize_file(&path)
.map_err(|e| Error::Decoding(e, path.to_string()))?;
if let source::SourceTarget::Emscripten = source_input.target() { if let source::SourceTarget::Emscripten = source_input.target() {
module = ununderscore_funcs(module); module = ununderscore_funcs(module);
@@ -202,34 +198,36 @@ fn main() {
utils::optimize( utils::optimize(
&mut module, &mut module,
vec![CALL_SYMBOL] vec![CALL_SYMBOL]
).unwrap_or_else(|e| die(Error::from(e))) )?;
} }
if let Some(save_raw_path) = matches.value_of("save_raw") { if let Some(save_raw_path) = matches.value_of("save_raw") {
parity_wasm::serialize_to_file(save_raw_path, module.clone()) parity_wasm::serialize_to_file(save_raw_path, module.clone()).map_err(Error::Encoding)?;
.unwrap_or_else(|e| die(Error::Encoding(e)));
} }
let raw_module = parity_wasm::serialize(module) let raw_module = parity_wasm::serialize(module).map_err(Error::Encoding)?;
.unwrap_or_else(|e| die(Error::Encoding(e)));
// If module has an exported function with name=CREATE_SYMBOL // If module has an exported function with name=CREATE_SYMBOL
// build will pack the module (raw_module) into this funciton and export as CALL_SYMBOL. // build will pack the module (raw_module) into this funciton and export as CALL_SYMBOL.
// Otherwise it will just save an optimised raw_module // Otherwise it will just save an optimised raw_module
if has_ctor(&ctor_module) { if has_ctor(&ctor_module) {
if !matches.is_present("skip_optimization") { if !matches.is_present("skip_optimization") {
utils::optimize(&mut ctor_module, vec![CREATE_SYMBOL]) utils::optimize(&mut ctor_module, vec![CREATE_SYMBOL])?;
.unwrap_or_else(|e| die(Error::from(e)))
} }
let ctor_module = utils::pack_instance(raw_module, ctor_module) let ctor_module = utils::pack_instance(raw_module, ctor_module)?;
.unwrap_or_else(|e| die(Error::from(e))); parity_wasm::serialize_to_file(&path, ctor_module).map_err(Error::Encoding)?;
parity_wasm::serialize_to_file(&path, ctor_module)
.unwrap_or_else(|e| die(Error::Encoding(e)))
} else { } else {
let mut file = fs::File::create(&path) let mut file = fs::File::create(&path)?;
.unwrap_or_else(|io| die(Error::from(io))); file.write_all(&raw_module)?;
file.write_all(&raw_module) }
.unwrap_or_else(|io| die(Error::from(io)));
Ok(())
}
fn main() {
if let Err(e) = do_main() {
eprintln!("{}", e);
std::process::exit(1)
} }
} }