Skip to content

Commit 31e4575

Browse files
authored
Merge pull request #197 from wasmx/remove-new
Remove new() from CheckFloat, Repack and Snip
2 parents e82fd3b + 35ae3cc commit 31e4575

File tree

5 files changed

+23
-46
lines changed

5 files changed

+23
-46
lines changed

chisel/src/driver.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use libchisel::{
1717
checkfloat::CheckFloat, checkstartfunc::CheckStartFunc, deployer::Deployer,
1818
dropsection::DropSection, remapimports::RemapImports, remapstart::RemapStart, repack::Repack,
1919
snip::Snip, trimexports::TrimExports, trimstartfunc::TrimStartFunc,
20-
verifyexports::VerifyExports, verifyimports::VerifyImports, Module, ModulePreset,
20+
verifyexports::VerifyExports, verifyimports::VerifyImports, ChiselModule, Module, ModulePreset,
2121
ModuleTranslator, ModuleValidator,
2222
};
2323

@@ -220,7 +220,7 @@ impl ChiselDriver {
220220
) -> Result<ModuleResult, DriverError> {
221221
let result = match name.as_str() {
222222
"checkfloat" => {
223-
let checkfloat = CheckFloat::new();
223+
let checkfloat = CheckFloat::with_defaults().expect("Should not fail");
224224
let module_result = checkfloat.validate(wasm);
225225
ModuleResult::Validator(name, module_result)
226226
}
@@ -306,7 +306,7 @@ impl ChiselDriver {
306306
ModuleResult::Translator(name, module_result)
307307
}
308308
"repack" => {
309-
let repack = Repack::new();
309+
let repack = Repack::with_defaults().expect("Should not fail");
310310
let module_result = repack.translate(wasm).expect("No failure cases");
311311

312312
let did_mutate = if let Some(new_wasm) = module_result {
@@ -319,7 +319,7 @@ impl ChiselDriver {
319319
ModuleResult::Translator(name, Ok(did_mutate))
320320
}
321321
"snip" => {
322-
let snip = Snip::new();
322+
let snip = Snip::with_defaults().expect("Should not fail");
323323
let module_result = match snip.translate(wasm) {
324324
Ok(result) => result,
325325
Err(e) => {

libchisel/src/checkfloat.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ impl<'a> ChiselModule<'a> for CheckFloat {
3131
}
3232
}
3333

34-
impl CheckFloat {
35-
pub fn new() -> Self {
36-
CheckFloat {}
37-
}
38-
}
39-
4034
impl ModuleValidator for CheckFloat {
4135
// NOTE: this will not check for SIMD instructions.
4236
fn validate(&self, module: &Module) -> Result<bool, ModuleError> {
@@ -147,7 +141,7 @@ mod tests {
147141
0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b,
148142
];
149143
let module = Module::from_bytes(&wasm).unwrap();
150-
let checker = CheckFloat::new();
144+
let checker = CheckFloat::with_defaults().unwrap();
151145
let result = checker.validate(&module).unwrap();
152146
assert_eq!(true, result);
153147
}
@@ -167,7 +161,7 @@ mod tests {
167161
0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x92, 0x0b,
168162
];
169163
let module = Module::from_bytes(&wasm).unwrap();
170-
let checker = CheckFloat::new();
164+
let checker = CheckFloat::with_defaults().unwrap();
171165
let result = checker.validate(&module).unwrap();
172166
assert_eq!(false, result);
173167
}
@@ -187,15 +181,15 @@ mod tests {
187181
0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0xa0, 0x0b,
188182
];
189183
let module = Module::from_bytes(&wasm).unwrap();
190-
let checker = CheckFloat::new();
184+
let checker = CheckFloat::with_defaults().unwrap();
191185
let result = checker.validate(&module).unwrap();
192186
assert_eq!(false, result);
193187
}
194188

195189
#[test]
196190
fn no_code_section() {
197191
let module = builder::module().build();
198-
let checker = CheckFloat::new();
192+
let checker = CheckFloat::with_defaults().unwrap();
199193
let result = checker.validate(&module);
200194
assert_eq!(true, result.is_err());
201195
assert_eq!(result.err().unwrap(), ModuleError::NotFound)

libchisel/src/repack.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ use super::{ChiselModule, ModuleError, ModuleKind, ModuleTranslator};
77

88
pub struct Repack;
99

10-
impl Repack {
11-
pub fn new() -> Self {
12-
Repack {}
13-
}
14-
}
15-
1610
impl<'a> ChiselModule<'a> for Repack {
1711
type ObjectReference = &'a dyn ModuleTranslator;
1812

@@ -29,7 +23,7 @@ impl<'a> ChiselModule<'a> for Repack {
2923
}
3024

3125
fn with_defaults() -> Result<Self, ModuleError> {
32-
Ok(Repack::new())
26+
Ok(Repack {})
3327
}
3428

3529
fn with_config(_config: &HashMap<String, String>) -> Result<Self, ModuleError> {
@@ -62,7 +56,7 @@ mod tests {
6256
fn smoke_test() {
6357
let module = Module::default();
6458

65-
let repack = Repack::new();
59+
let repack = Repack::with_defaults().unwrap();
6660
assert_eq!(module, repack.translate(&module).unwrap().unwrap());
6761
}
6862

@@ -87,7 +81,7 @@ mod tests {
8781
.build()
8882
.build();
8983

90-
let repack = Repack::new();
84+
let repack = Repack::with_defaults().unwrap();
9185
assert_eq!(module, repack.translate(&module).unwrap().unwrap());
9286
}
9387

@@ -117,7 +111,7 @@ mod tests {
117111
.sections_mut()
118112
.push(parity_wasm::elements::Section::Custom(custom));
119113

120-
let repack = Repack::new();
114+
let repack = Repack::with_defaults().unwrap();
121115
assert_ne!(module, repack.translate(&module).unwrap().unwrap());
122116
}
123117

@@ -135,7 +129,7 @@ mod tests {
135129
.parse_names()
136130
.expect("parsing the names section failed");
137131
assert_eq!(module.names_section().is_some(), true);
138-
let repack = Repack::new();
132+
let repack = Repack::with_defaults().unwrap();
139133
// Repack drops names section too.
140134
let output = repack.translate(&module).unwrap().unwrap();
141135
assert_eq!(output.has_names_section(), false);

libchisel/src/snip.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,6 @@ fn check_bool_option(config: &HashMap<String, String>, option: &str, default: bo
1616
#[derive(Clone)]
1717
pub struct Snip(wasm_snip::Options);
1818

19-
impl Snip {
20-
pub fn new() -> Self {
21-
let mut options = wasm_snip::Options::default();
22-
// TODO: expose these as options
23-
options.snip_rust_fmt_code = true;
24-
options.snip_rust_panicking_code = true;
25-
options.skip_producers_section = true;
26-
Snip { 0: options }
27-
}
28-
}
29-
3019
impl<'a> ChiselModule<'a> for Snip {
3120
type ObjectReference = &'a dyn ModuleTranslator;
3221

@@ -43,7 +32,12 @@ impl<'a> ChiselModule<'a> for Snip {
4332
}
4433

4534
fn with_defaults() -> Result<Self, ModuleError> {
46-
Ok(Snip::new())
35+
let mut options = wasm_snip::Options::default();
36+
// TODO: expose these as options
37+
options.snip_rust_fmt_code = true;
38+
options.snip_rust_panicking_code = true;
39+
options.skip_producers_section = true;
40+
Ok(Snip { 0: options })
4741
}
4842

4943
fn with_config(config: &HashMap<String, String>) -> Result<Self, ModuleError> {
@@ -112,7 +106,7 @@ mod tests {
112106
.unwrap();
113107

114108
let module = Module::from_bytes(&wasm).unwrap();
115-
let module = Snip::new().translate(&module);
109+
let module = Snip::with_defaults().unwrap().translate(&module);
116110
let module = module
117111
.expect("translation to be succesful")
118112
.expect("new module to be returned");

libchisel/src/trimexports.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ impl<'a> ChiselModule<'a> for TrimExports {
3131
}
3232

3333
fn with_defaults() -> Result<Self, ModuleError> {
34-
Ok(TrimExports::new())
34+
Ok(TrimExports {
35+
whitelist: ExportWhitelist::new(),
36+
})
3537
}
3638

3739
fn with_config(config: &HashMap<String, String>) -> Result<Self, ModuleError> {
@@ -87,13 +89,6 @@ impl ExportWhitelist {
8789
}
8890

8991
impl TrimExports {
90-
/// Constructs an empty `trimexports` context.
91-
pub fn new() -> Self {
92-
TrimExports {
93-
whitelist: ExportWhitelist::new(),
94-
}
95-
}
96-
9792
/// Iterates over the export section, if there is one, and removes
9893
/// unnecessary entries.
9994
fn trim_exports(&self, module: &mut Module) -> bool {

0 commit comments

Comments
 (0)