From f51af3946cedc80195db0e00d78f1ce4b06661c1 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Sat, 18 Jan 2020 23:38:36 +0000 Subject: [PATCH] Merge ModuleConfig trait into ChiselModule --- libchisel/src/binaryenopt.rs | 4 +--- libchisel/src/checkfloat.rs | 4 +--- libchisel/src/checkstartfunc.rs | 4 +--- libchisel/src/deployer.rs | 4 +--- libchisel/src/dropsection.rs | 16 +++++++--------- libchisel/src/lib.rs | 30 +++++++++++++++++++----------- libchisel/src/remapimports.rs | 5 +---- libchisel/src/remapstart.rs | 4 +--- libchisel/src/repack.rs | 4 +--- libchisel/src/snip.rs | 22 ++++++++++------------ libchisel/src/trimexports.rs | 28 +++++++++++++--------------- libchisel/src/trimstartfunc.rs | 4 +--- libchisel/src/verifyexports.rs | 4 +--- libchisel/src/verifyimports.rs | 4 +--- 14 files changed, 59 insertions(+), 78 deletions(-) diff --git a/libchisel/src/binaryenopt.rs b/libchisel/src/binaryenopt.rs index 7236e7a..3dbb73b 100644 --- a/libchisel/src/binaryenopt.rs +++ b/libchisel/src/binaryenopt.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use parity_wasm::elements::Module; -use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator}; +use super::{ChiselModule, ModuleError, ModuleKind, ModulePreset, ModuleTranslator}; // FIXME: change level names pub enum BinaryenOptimiser { @@ -29,9 +29,7 @@ impl<'a> ChiselModule<'a> for BinaryenOptimiser { fn as_abstract(&'a self) -> Self::ObjectReference { self as Self::ObjectReference } -} -impl ModuleConfig for BinaryenOptimiser { fn with_defaults() -> Result { Ok(BinaryenOptimiser::O2) } diff --git a/libchisel/src/checkfloat.rs b/libchisel/src/checkfloat.rs index 26c57be..b949796 100644 --- a/libchisel/src/checkfloat.rs +++ b/libchisel/src/checkfloat.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use parity_wasm::elements::{Instruction, Module}; -use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleValidator}; +use super::{ChiselModule, ModuleError, ModuleKind, ModuleValidator}; /// Struct on which ModuleValidator is implemented. pub struct CheckFloat {} @@ -21,9 +21,7 @@ impl<'a> ChiselModule<'a> for CheckFloat { fn as_abstract(&'a self) -> Self::ObjectReference { self as Self::ObjectReference } -} -impl ModuleConfig for CheckFloat { fn with_defaults() -> Result { Ok(CheckFloat {}) } diff --git a/libchisel/src/checkstartfunc.rs b/libchisel/src/checkstartfunc.rs index 844a5ff..14a7347 100644 --- a/libchisel/src/checkstartfunc.rs +++ b/libchisel/src/checkstartfunc.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use parity_wasm::elements::Module; -use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleValidator}; +use super::{ChiselModule, ModuleError, ModuleKind, ModuleValidator}; /// Struct on which ModuleValidator is implemented. pub struct CheckStartFunc { @@ -31,9 +31,7 @@ impl<'a> ChiselModule<'a> for CheckStartFunc { fn as_abstract(&'a self) -> Self::ObjectReference { self as Self::ObjectReference } -} -impl ModuleConfig for CheckStartFunc { fn with_defaults() -> Result { Err(ModuleError::NotSupported) } diff --git a/libchisel/src/deployer.rs b/libchisel/src/deployer.rs index d2392a2..e6a675e 100644 --- a/libchisel/src/deployer.rs +++ b/libchisel/src/deployer.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use parity_wasm::builder; use parity_wasm::elements::{CustomSection, Module}; -use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator}; +use super::{ChiselModule, ModuleError, ModuleKind, ModulePreset, ModuleTranslator}; /// Enum on which ModuleTranslator is implemented. pub enum Deployer { @@ -25,9 +25,7 @@ impl<'a> ChiselModule<'a> for Deployer { fn as_abstract(&'a self) -> Self::ObjectReference { self as Self::ObjectReference } -} -impl ModuleConfig for Deployer { fn with_defaults() -> Result { Err(ModuleError::NotSupported) } diff --git a/libchisel/src/dropsection.rs b/libchisel/src/dropsection.rs index 673ac43..c3f406e 100644 --- a/libchisel/src/dropsection.rs +++ b/libchisel/src/dropsection.rs @@ -3,7 +3,13 @@ use std::error::Error; use parity_wasm::elements::{Module, Section}; -use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleTranslator}; +use super::{ChiselModule, ModuleError, ModuleKind, ModuleTranslator}; + +impl From for ModuleError { + fn from(error: std::num::ParseIntError) -> Self { + ModuleError::Custom(error.description().to_string()) + } +} /// Enum on which ModuleTranslator is implemented. #[derive(Debug)] @@ -31,15 +37,7 @@ impl<'a> ChiselModule<'a> for DropSection { fn as_abstract(&'a self) -> Self::ObjectReference { self as Self::ObjectReference } -} - -impl From for ModuleError { - fn from(error: std::num::ParseIntError) -> Self { - ModuleError::Custom(error.description().to_string()) - } -} -impl ModuleConfig for DropSection { fn with_defaults() -> Result { Err(ModuleError::NotSupported) } diff --git a/libchisel/src/lib.rs b/libchisel/src/lib.rs index 1d25e35..e275696 100644 --- a/libchisel/src/lib.rs +++ b/libchisel/src/lib.rs @@ -46,6 +46,16 @@ pub trait ChiselModule<'a> { /// Borrows the instance as a trait object. fn as_abstract(&'a self) -> Self::ObjectReference; + + // Create instance with default settings. + fn with_defaults() -> Result + where + Self: Sized; + + // Create instance with a specific configuration. + fn with_config(config: &HashMap) -> Result + where + Self: Sized; } pub trait ModuleCreator { @@ -66,23 +76,13 @@ pub trait ModuleValidator { fn validate(&self, module: &Module) -> Result; } +// TODO: remove this pub trait ModulePreset { fn with_preset(preset: &str) -> Result where Self: std::marker::Sized; } -// TODO: move this to be part of ChiselModule and retire ModulePreset -pub trait ModuleConfig { - fn with_defaults() -> Result - where - Self: std::marker::Sized; - - fn with_config(config: &HashMap) -> Result - where - Self: std::marker::Sized; -} - impl From for ModuleError { fn from(error: String) -> Self { ModuleError::Custom(error) @@ -177,6 +177,14 @@ mod tests { fn as_abstract(&'a self) -> Self::ObjectReference { self as Self::ObjectReference } + + fn with_defaults() -> Result { + Err(ModuleError::NotSupported) + } + + fn with_config(_config: &HashMap) -> Result { + Err(ModuleError::NotSupported) + } } #[test] diff --git a/libchisel/src/remapimports.rs b/libchisel/src/remapimports.rs index 8166a96..fd5ce04 100644 --- a/libchisel/src/remapimports.rs +++ b/libchisel/src/remapimports.rs @@ -3,8 +3,7 @@ use std::collections::HashMap; use parity_wasm::elements::{ImportEntry, ImportSection, Module}; use super::{ - imports::ImportList, ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, - ModuleTranslator, + imports::ImportList, ChiselModule, ModuleError, ModuleKind, ModulePreset, ModuleTranslator, }; pub struct RemapImports<'a> { @@ -30,9 +29,7 @@ impl<'a> ChiselModule<'a> for RemapImports<'a> { fn as_abstract(&'a self) -> Self::ObjectReference { self as Self::ObjectReference } -} -impl<'a> ModuleConfig for RemapImports<'a> { fn with_defaults() -> Result { Err(ModuleError::NotSupported) } diff --git a/libchisel/src/remapstart.rs b/libchisel/src/remapstart.rs index 3665404..210a992 100644 --- a/libchisel/src/remapstart.rs +++ b/libchisel/src/remapstart.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use parity_wasm::elements::{ExportEntry, ExportSection, Internal, Module, Section}; -use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator}; +use super::{ChiselModule, ModuleError, ModuleKind, ModulePreset, ModuleTranslator}; pub struct RemapStart; @@ -30,9 +30,7 @@ impl<'a> ChiselModule<'a> for RemapStart { fn as_abstract(&'a self) -> Self::ObjectReference { self as Self::ObjectReference } -} -impl ModuleConfig for RemapStart { fn with_defaults() -> Result { Ok(RemapStart {}) } diff --git a/libchisel/src/repack.rs b/libchisel/src/repack.rs index 6c4a08b..a491217 100644 --- a/libchisel/src/repack.rs +++ b/libchisel/src/repack.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use parity_wasm::builder; use parity_wasm::elements::Module; -use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleTranslator}; +use super::{ChiselModule, ModuleError, ModuleKind, ModuleTranslator}; pub struct Repack; @@ -27,9 +27,7 @@ impl<'a> ChiselModule<'a> for Repack { fn as_abstract(&'a self) -> Self::ObjectReference { self as Self::ObjectReference } -} -impl ModuleConfig for Repack { fn with_defaults() -> Result { Ok(Repack::new()) } diff --git a/libchisel/src/snip.rs b/libchisel/src/snip.rs index 3cf28d7..6c56513 100644 --- a/libchisel/src/snip.rs +++ b/libchisel/src/snip.rs @@ -2,7 +2,16 @@ use std::collections::HashMap; use parity_wasm::elements::Module; -use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleTranslator}; +use super::{ChiselModule, ModuleError, ModuleKind, ModuleTranslator}; + +// TODO: consider making this a generic helper? +fn check_bool_option(config: &HashMap, option: &str, default: bool) -> bool { + if let Some(value) = config.get(option) { + value == "true" + } else { + default + } +} #[derive(Clone)] pub struct Snip(wasm_snip::Options); @@ -32,18 +41,7 @@ impl<'a> ChiselModule<'a> for Snip { fn as_abstract(&'a self) -> Self::ObjectReference { self as Self::ObjectReference } -} - -// TODO: consider making this a generic helper? -fn check_bool_option(config: &HashMap, option: &str, default: bool) -> bool { - if let Some(value) = config.get(option) { - value == "true" - } else { - default - } -} -impl ModuleConfig for Snip { fn with_defaults() -> Result { Ok(Snip::new()) } diff --git a/libchisel/src/trimexports.rs b/libchisel/src/trimexports.rs index 4a46a95..51e14c8 100644 --- a/libchisel/src/trimexports.rs +++ b/libchisel/src/trimexports.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use parity_wasm::elements::{ExportEntry, ExportSection, Internal, Module}; -use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator}; +use super::{ChiselModule, ModuleError, ModuleKind, ModulePreset, ModuleTranslator}; /// Struct containing a list of valid exports. struct ExportWhitelist { @@ -29,6 +29,18 @@ impl<'a> ChiselModule<'a> for TrimExports { fn as_abstract(&'a self) -> Self::ObjectReference { self as Self::ObjectReference } + + fn with_defaults() -> Result { + Ok(TrimExports::new()) + } + + fn with_config(config: &HashMap) -> Result { + if let Some(preset) = config.get("preset") { + TrimExports::with_preset(preset) + } else { + Err(ModuleError::NotSupported) + } + } } /// Helper that compares the enum variant of two WebAssembly exports. @@ -107,20 +119,6 @@ impl TrimExports { } } -impl ModuleConfig for TrimExports { - fn with_defaults() -> Result { - Ok(TrimExports::new()) - } - - fn with_config(config: &HashMap) -> Result { - if let Some(preset) = config.get("preset") { - TrimExports::with_preset(preset) - } else { - Err(ModuleError::NotSupported) - } - } -} - impl ModulePreset for TrimExports { /// Takes a given preset string and constructs a context with the /// corresponding whitelist. diff --git a/libchisel/src/trimstartfunc.rs b/libchisel/src/trimstartfunc.rs index a9b64b9..70d05bd 100644 --- a/libchisel/src/trimstartfunc.rs +++ b/libchisel/src/trimstartfunc.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use parity_wasm::elements::Module; -use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator}; +use super::{ChiselModule, ModuleError, ModuleKind, ModulePreset, ModuleTranslator}; pub struct TrimStartFunc; @@ -31,9 +31,7 @@ impl<'a> ChiselModule<'a> for TrimStartFunc { fn as_abstract(&'a self) -> Self::ObjectReference { self as Self::ObjectReference } -} -impl ModuleConfig for TrimStartFunc { fn with_defaults() -> Result { Err(ModuleError::NotSupported) } diff --git a/libchisel/src/verifyexports.rs b/libchisel/src/verifyexports.rs index 036da6a..1f0c522 100644 --- a/libchisel/src/verifyexports.rs +++ b/libchisel/src/verifyexports.rs @@ -4,7 +4,7 @@ use parity_wasm::elements::{ ExportSection, External, FunctionSection, FunctionType, ImportSection, Internal, Module, Type, }; -use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleValidator}; +use super::{ChiselModule, ModuleError, ModuleKind, ModulePreset, ModuleValidator}; /// Enum representing a type of export and any extra data to check. pub enum ExportType<'a> { @@ -39,9 +39,7 @@ impl<'a> ChiselModule<'a> for VerifyExports<'a> { fn as_abstract(&'a self) -> Self::ObjectReference { self as Self::ObjectReference } -} -impl<'a> ModuleConfig for VerifyExports<'a> { fn with_defaults() -> Result { Err(ModuleError::NotSupported) } diff --git a/libchisel/src/verifyimports.rs b/libchisel/src/verifyimports.rs index f9d72c8..d954d8f 100644 --- a/libchisel/src/verifyimports.rs +++ b/libchisel/src/verifyimports.rs @@ -4,7 +4,7 @@ use parity_wasm::elements::{External, FunctionType, ImportSection, Module, Type} use super::{ imports::{ImportList, ImportType}, - ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleValidator, + ChiselModule, ModuleError, ModuleKind, ModulePreset, ModuleValidator, }; /// Enum representing the state of an import in a module. @@ -51,9 +51,7 @@ impl<'a> ChiselModule<'a> for VerifyImports<'a> { fn as_abstract(&'a self) -> Self::ObjectReference { self as Self::ObjectReference } -} -impl<'a> ModuleConfig for VerifyImports<'a> { fn with_defaults() -> Result { Err(ModuleError::NotSupported) }