Skip to content

Commit

Permalink
Remove new() from CheckFloat, Repack and Snip
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Jan 18, 2020
1 parent 35bbe00 commit 99391ba
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 45 deletions.
6 changes: 3 additions & 3 deletions chisel/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl ChiselDriver {
) -> Result<ModuleResult, DriverError> {
let result = match name.as_str() {
"checkfloat" => {
let checkfloat = CheckFloat::new();
let checkfloat = CheckFloat::with_defaults().expect("Should not fail");
let module_result = checkfloat.validate(wasm);
ModuleResult::Validator(name, module_result)
}
Expand Down Expand Up @@ -306,7 +306,7 @@ impl ChiselDriver {
ModuleResult::Translator(name, module_result)
}
"repack" => {
let repack = Repack::new();
let repack = Repack::with_defaults().expect("Should not fail");
let module_result = repack.translate(wasm).expect("No failure cases");

let did_mutate = if let Some(new_wasm) = module_result {
Expand All @@ -319,7 +319,7 @@ impl ChiselDriver {
ModuleResult::Translator(name, Ok(did_mutate))
}
"snip" => {
let snip = Snip::new();
let snip = Snip::with_defaults().expect("Should not fail");
let module_result = match snip.translate(wasm) {
Ok(result) => result,
Err(e) => {
Expand Down
14 changes: 4 additions & 10 deletions libchisel/src/checkfloat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ impl ModuleConfig for CheckFloat {
}
}

impl CheckFloat {
pub fn new() -> Self {
CheckFloat {}
}
}

impl ModuleValidator for CheckFloat {
// NOTE: this will not check for SIMD instructions.
fn validate(&self, module: &Module) -> Result<bool, ModuleError> {
Expand Down Expand Up @@ -149,7 +143,7 @@ mod tests {
0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b,
];
let module = Module::from_bytes(&wasm).unwrap();
let checker = CheckFloat::new();
let checker = CheckFloat::with_defaults().unwrap();
let result = checker.validate(&module).unwrap();
assert_eq!(true, result);
}
Expand All @@ -169,7 +163,7 @@ mod tests {
0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x92, 0x0b,
];
let module = Module::from_bytes(&wasm).unwrap();
let checker = CheckFloat::new();
let checker = CheckFloat::with_defaults().unwrap();
let result = checker.validate(&module).unwrap();
assert_eq!(false, result);
}
Expand All @@ -189,15 +183,15 @@ mod tests {
0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0xa0, 0x0b,
];
let module = Module::from_bytes(&wasm).unwrap();
let checker = CheckFloat::new();
let checker = CheckFloat::with_defaults().unwrap();
let result = checker.validate(&module).unwrap();
assert_eq!(false, result);
}

#[test]
fn no_code_section() {
let module = builder::module().build();
let checker = CheckFloat::new();
let checker = CheckFloat::with_defaults().unwrap();
let result = checker.validate(&module);
assert_eq!(true, result.is_err());
assert_eq!(result.err().unwrap(), ModuleError::NotFound)
Expand Down
16 changes: 5 additions & 11 deletions libchisel/src/repack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleTranslato

pub struct Repack;

impl Repack {
pub fn new() -> Self {
Repack {}
}
}

impl<'a> ChiselModule<'a> for Repack {
type ObjectReference = &'a dyn ModuleTranslator;

Expand All @@ -31,7 +25,7 @@ impl<'a> ChiselModule<'a> for Repack {

impl ModuleConfig for Repack {
fn with_defaults() -> Result<Self, ModuleError> {
Ok(Repack::new())
Ok(Repack {})
}

fn with_config(_config: &HashMap<String, String>) -> Result<Self, ModuleError> {
Expand Down Expand Up @@ -64,7 +58,7 @@ mod tests {
fn smoke_test() {
let module = Module::default();

let repack = Repack::new();
let repack = Repack::with_defaults().unwrap();
assert_eq!(module, repack.translate(&module).unwrap().unwrap());
}

Expand All @@ -89,7 +83,7 @@ mod tests {
.build()
.build();

let repack = Repack::new();
let repack = Repack::with_defaults().unwrap();
assert_eq!(module, repack.translate(&module).unwrap().unwrap());
}

Expand Down Expand Up @@ -119,7 +113,7 @@ mod tests {
.sections_mut()
.push(parity_wasm::elements::Section::Custom(custom));

let repack = Repack::new();
let repack = Repack::with_defaults().unwrap();
assert_ne!(module, repack.translate(&module).unwrap().unwrap());
}

Expand All @@ -137,7 +131,7 @@ mod tests {
.parse_names()
.expect("parsing the names section failed");
assert_eq!(module.names_section().is_some(), true);
let repack = Repack::new();
let repack = Repack::with_defaults().unwrap();
// Repack drops names section too.
let output = repack.translate(&module).unwrap().unwrap();
assert_eq!(output.has_names_section(), false);
Expand Down
20 changes: 7 additions & 13 deletions libchisel/src/snip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@ use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleTranslato
#[derive(Clone)]
pub struct Snip(wasm_snip::Options);

impl Snip {
pub fn new() -> Self {
let mut options = wasm_snip::Options::default();
// TODO: expose these as options
options.snip_rust_fmt_code = true;
options.snip_rust_panicking_code = true;
options.skip_producers_section = true;
Snip { 0: options }
}
}

impl<'a> ChiselModule<'a> for Snip {
type ObjectReference = &'a dyn ModuleTranslator;

Expand Down Expand Up @@ -45,7 +34,12 @@ fn check_bool_option(config: &HashMap<String, String>, option: &str, default: bo

impl ModuleConfig for Snip {
fn with_defaults() -> Result<Self, ModuleError> {
Ok(Snip::new())
let mut options = wasm_snip::Options::default();
// TODO: expose these as options
options.snip_rust_fmt_code = true;
options.snip_rust_panicking_code = true;
options.skip_producers_section = true;
Ok(Snip { 0: options })
}

fn with_config(config: &HashMap<String, String>) -> Result<Self, ModuleError> {
Expand Down Expand Up @@ -114,7 +108,7 @@ mod tests {
.unwrap();

let module = Module::from_bytes(&wasm).unwrap();
let module = Snip::new().translate(&module);
let module = Snip::with_defaults().unwrap().translate(&module);
let module = module
.expect("translation to be succesful")
.expect("new module to be returned");
Expand Down
11 changes: 3 additions & 8 deletions libchisel/src/trimexports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,6 @@ impl ExportWhitelist {
}

impl TrimExports {
/// Constructs an empty `trimexports` context.
pub fn new() -> Self {
TrimExports {
whitelist: ExportWhitelist::new(),
}
}

/// Iterates over the export section, if there is one, and removes
/// unnecessary entries.
fn trim_exports(&self, module: &mut Module) -> bool {
Expand Down Expand Up @@ -109,7 +102,9 @@ impl TrimExports {

impl ModuleConfig for TrimExports {
fn with_defaults() -> Result<Self, ModuleError> {
Ok(TrimExports::new())
Ok(TrimExports {
whitelist: ExportWhitelist::new(),
})
}

fn with_config(config: &HashMap<String, String>) -> Result<Self, ModuleError> {
Expand Down

0 comments on commit 99391ba

Please sign in to comment.