Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge ModuleConfig trait into ChiselModule #198

Merged
merged 1 commit into from
Jan 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions libchisel/src/binaryenopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Self, ModuleError> {
Ok(BinaryenOptimiser::O2)
}
Expand Down
4 changes: 1 addition & 3 deletions libchisel/src/checkfloat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand All @@ -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<Self, ModuleError> {
Ok(CheckFloat {})
}
Expand Down
4 changes: 1 addition & 3 deletions libchisel/src/checkstartfunc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Self, ModuleError> {
Err(ModuleError::NotSupported)
}
Expand Down
4 changes: 1 addition & 3 deletions libchisel/src/deployer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Self, ModuleError> {
Err(ModuleError::NotSupported)
}
Expand Down
16 changes: 7 additions & 9 deletions libchisel/src/dropsection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::num::ParseIntError> for ModuleError {
fn from(error: std::num::ParseIntError) -> Self {
ModuleError::Custom(error.description().to_string())
}
}

/// Enum on which ModuleTranslator is implemented.
#[derive(Debug)]
Expand Down Expand Up @@ -31,15 +37,7 @@ impl<'a> ChiselModule<'a> for DropSection {
fn as_abstract(&'a self) -> Self::ObjectReference {
self as Self::ObjectReference
}
}

impl From<std::num::ParseIntError> for ModuleError {
fn from(error: std::num::ParseIntError) -> Self {
ModuleError::Custom(error.description().to_string())
}
}

impl ModuleConfig for DropSection {
fn with_defaults() -> Result<Self, ModuleError> {
Err(ModuleError::NotSupported)
}
Expand Down
30 changes: 19 additions & 11 deletions libchisel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, ModuleError>
where
Self: Sized;

// Create instance with a specific configuration.
fn with_config(config: &HashMap<String, String>) -> Result<Self, ModuleError>
where
Self: Sized;
}

pub trait ModuleCreator {
Expand All @@ -66,23 +76,13 @@ pub trait ModuleValidator {
fn validate(&self, module: &Module) -> Result<bool, ModuleError>;
}

// TODO: remove this
pub trait ModulePreset {
fn with_preset(preset: &str) -> Result<Self, ModuleError>
where
Self: std::marker::Sized;
}

// TODO: move this to be part of ChiselModule and retire ModulePreset
pub trait ModuleConfig {
fn with_defaults() -> Result<Self, ModuleError>
where
Self: std::marker::Sized;

fn with_config(config: &HashMap<String, String>) -> Result<Self, ModuleError>
where
Self: std::marker::Sized;
}

impl From<String> for ModuleError {
fn from(error: String) -> Self {
ModuleError::Custom(error)
Expand Down Expand Up @@ -177,6 +177,14 @@ mod tests {
fn as_abstract(&'a self) -> Self::ObjectReference {
self as Self::ObjectReference
}

fn with_defaults() -> Result<Self, ModuleError> {
Err(ModuleError::NotSupported)
}

fn with_config(_config: &HashMap<String, String>) -> Result<Self, ModuleError> {
Err(ModuleError::NotSupported)
}
}

#[test]
Expand Down
5 changes: 1 addition & 4 deletions libchisel/src/remapimports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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<Self, ModuleError> {
Err(ModuleError::NotSupported)
}
Expand Down
4 changes: 1 addition & 3 deletions libchisel/src/remapstart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Self, ModuleError> {
Ok(RemapStart {})
}
Expand Down
4 changes: 1 addition & 3 deletions libchisel/src/repack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Self, ModuleError> {
Ok(Repack::new())
}
Expand Down
22 changes: 10 additions & 12 deletions libchisel/src/snip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>, option: &str, default: bool) -> bool {
if let Some(value) = config.get(option) {
value == "true"
} else {
default
}
}

#[derive(Clone)]
pub struct Snip(wasm_snip::Options);
Expand Down Expand Up @@ -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<String, String>, option: &str, default: bool) -> bool {
if let Some(value) = config.get(option) {
value == "true"
} else {
default
}
}

impl ModuleConfig for Snip {
fn with_defaults() -> Result<Self, ModuleError> {
Ok(Snip::new())
}
Expand Down
28 changes: 13 additions & 15 deletions libchisel/src/trimexports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Self, ModuleError> {
Ok(TrimExports::new())
}

fn with_config(config: &HashMap<String, String>) -> Result<Self, ModuleError> {
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.
Expand Down Expand Up @@ -107,20 +119,6 @@ impl TrimExports {
}
}

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

fn with_config(config: &HashMap<String, String>) -> Result<Self, ModuleError> {
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.
Expand Down
4 changes: 1 addition & 3 deletions libchisel/src/trimstartfunc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Self, ModuleError> {
Err(ModuleError::NotSupported)
}
Expand Down
4 changes: 1 addition & 3 deletions libchisel/src/verifyexports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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<Self, ModuleError> {
Err(ModuleError::NotSupported)
}
Expand Down
4 changes: 1 addition & 3 deletions libchisel/src/verifyimports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Self, ModuleError> {
Err(ModuleError::NotSupported)
}
Expand Down