From 3255d4be753846209409f9b0d083542cae2de303 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Tue, 9 May 2017 12:54:16 +0300 Subject: [PATCH] result return for optimizer, test correctness --- opt/src/main.rs | 2 +- src/lib.rs | 2 +- src/optimizer.rs | 21 +++++++++++++++++---- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/opt/src/main.rs b/opt/src/main.rs index 893fb03..4b96420 100644 --- a/opt/src/main.rs +++ b/opt/src/main.rs @@ -18,7 +18,7 @@ fn main() { // Invoke optimizer // Contract is supposed to have only these functions as public api // All other symbols not usable by this list is optimized away - wasm_utils::optimize(&mut module, vec!["_call"]); + wasm_utils::optimize(&mut module, vec!["_call"]).expect("Optimizer to finish without errors"); parity_wasm::serialize_to_file(&args[2], module).unwrap(); } diff --git a/src/lib.rs b/src/lib.rs index dbc3c5c..a316b7e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,7 @@ mod symbols; mod logger; mod ext; -pub use optimizer::optimize; +pub use optimizer::{optimize, Error as OptimizerError}; pub use gas::inject_gas_counter; pub use logger::init_log; pub use ext::externalize; \ No newline at end of file diff --git a/src/optimizer.rs b/src/optimizer.rs index 59d6ca4..131b6bc 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -3,17 +3,24 @@ use parity_wasm::elements; use symbols::{Symbol, expand_symbols, push_code_symbols, resolve_function}; +#[derive(Debug)] +pub enum Error { + /// Since optimizer starts with export entries, export + /// section is supposed to exist. + NoExportSection, +} + pub fn optimize( module: &mut elements::Module, // Module to optimize used_exports: Vec<&str>, // List of only exports that will be usable after optimization -) { +) -> Result<(), Error> { // WebAssembly exports optimizer // Motivation: emscripten compiler backend compiles in many unused exports // which in turn compile in unused imports and leaves unused functions // Algo starts from the top, listing all items that should stay let mut stay = HashSet::new(); - for (index, entry) in module.export_section().expect("Export section to exist").entries().iter().enumerate() { + for (index, entry) in module.export_section().ok_or(Error::NoExportSection)?.entries().iter().enumerate() { if used_exports.iter().find(|e| **e == entry.field()).is_some() { stay.insert(Symbol::Export(index)); } @@ -239,6 +246,7 @@ pub fn optimize( } } + Ok(()) } @@ -355,11 +363,16 @@ mod tests { use parity_wasm::builder; use super::*; + /// @spec + /// Optimizer presumes that export section exists and contains + /// all symbols passed as a second parameter. Since empty module + /// obviously contains no export section, optimizer should return + /// error on it. #[test] fn empty() { let mut module = builder::module().build(); - optimize(&mut module, vec!["_call"]); + let result = optimize(&mut module, vec!["_call"]); - assert!(module.type_section().is_none()); + assert!(result.is_err()); } } \ No newline at end of file