Skip to content

Rename re-exported Module to WasmModule for clarity #160

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions chisel/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use libchisel::{
checkfloat::CheckFloat, checkstartfunc::CheckStartFunc, deployer::Deployer,
dropsection::DropSection, remapimports::RemapImports, remapstart::RemapStart, repack::Repack,
snip::Snip, trimexports::TrimExports, trimstartfunc::TrimStartFunc,
verifyexports::VerifyExports, verifyimports::VerifyImports, Module, ModulePreset,
verifyexports::VerifyExports, verifyimports::VerifyImports, WasmModule, ModulePreset,
ModuleTranslator, ModuleValidator,
};

Expand Down Expand Up @@ -161,7 +161,7 @@ impl ChiselDriver {
};

// Deserialize the Wasm binary and parse its names section.
let mut wasm = match Module::from_bytes(wasm_raw) {
let mut wasm = match WasmModule::from_bytes(wasm_raw) {
Ok(wasm) => {
chisel_debug!(1, "Successfully deserialized Wasm module");
// TODO: Make this error recoverable
Expand Down Expand Up @@ -216,7 +216,7 @@ impl ChiselDriver {
&mut self,
name: String,
module: ModuleConfig,
wasm: &mut Module,
wasm: &mut WasmModule,
) -> Result<ModuleResult, DriverError> {
let result = match name.as_str() {
"checkfloat" => {
Expand Down
12 changes: 6 additions & 6 deletions chisel/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::path::PathBuf;

use ansi_term::Colour::{Green, Red, Yellow};

use libchisel::{Module, ModuleError};
use libchisel::{WasmModule, ModuleError};

#[derive(Clone)]
/// Main result structure returned by ChiselDriver, containing a manifest of modules executed and
Expand All @@ -28,7 +28,7 @@ pub struct RulesetResult {
ruleset_name: String,
results: Vec<ModuleResult>,
output_path: PathBuf,
output_module: Option<Module>,
output_module: Option<WasmModule>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -76,7 +76,7 @@ impl RulesetResult {
self.output_path = path;
}

pub fn set_output_module(&mut self, module: Module) {
pub fn set_output_module(&mut self, module: WasmModule) {
self.output_module = Some(module);
}

Expand Down Expand Up @@ -204,7 +204,7 @@ mod tests {
fn writer_success_to_stdout() {
let mut ruleset_result = {
let mut result = RulesetResult::new("Test".to_string());
let module = Module::default();
let module = WasmModule::default();
result.set_output_module(module);
result.set_output_path(PathBuf::from("/dev/stdout"));
result
Expand All @@ -225,7 +225,7 @@ mod tests {
fn writer_deny_raw_binary_to_stdout() {
let mut ruleset_result = {
let mut result = RulesetResult::new("Test".to_string());
let module = Module::default();
let module = WasmModule::default();
result.set_output_module(module);
result.set_output_path(PathBuf::from("/dev/stdout"));
result
Expand All @@ -239,7 +239,7 @@ mod tests {
fn writer_invalid_mode() {
let mut ruleset_result = {
let mut result = RulesetResult::new("Test".to_string());
let module = Module::default();
let module = WasmModule::default();
result.set_output_module(module);
result.set_output_path(PathBuf::from("/dev/stdout"));
result
Expand Down
12 changes: 7 additions & 5 deletions libchisel/src/binaryenopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::collections::HashMap;

use parity_wasm::elements::Module;

use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator};
use super::{
ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator, WasmModule,
};

// FIXME: change level names
pub enum BinaryenOptimiser {
Expand Down Expand Up @@ -61,11 +63,11 @@ impl ModulePreset for BinaryenOptimiser {
}

impl ModuleTranslator for BinaryenOptimiser {
fn translate_inplace(&self, _module: &mut Module) -> Result<bool, ModuleError> {
fn translate_inplace(&self, _module: &mut WasmModule) -> Result<bool, ModuleError> {
Err(ModuleError::NotSupported)
}

fn translate(&self, module: &Module) -> Result<Option<Module>, ModuleError> {
fn translate(&self, module: &WasmModule) -> Result<Option<WasmModule>, ModuleError> {
let has_names_section = module.has_names_section();

// FIXME: could just move this into `BinaryenOptimiser`
Expand Down Expand Up @@ -109,7 +111,7 @@ impl ModuleTranslator for BinaryenOptimiser {

let serialized = module.clone().to_bytes()?;
let output = binaryen_optimiser(&serialized, &config)?;
let output = Module::from_bytes(&output)?;
let output = WasmModule::from_bytes(&output)?;
Ok(Some(output))
}
}
Expand Down Expand Up @@ -147,7 +149,7 @@ mod tests {
0x0a, 0x05, 0x01, 0x03, 0x00, 0x01, 0x0b,
];

let module = Module::from_bytes(&input).unwrap();
let module = WasmModule::from_bytes(&input).unwrap();
let translator = BinaryenOptimiser::with_preset("O0").unwrap();
let result = translator.translate(&module).unwrap().unwrap();
let serialized = result.to_bytes().unwrap();
Expand Down
10 changes: 5 additions & 5 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, ModuleConfig, ModuleError, ModuleKind, ModuleValidator, WasmModule};

/// Struct on which ModuleValidator is implemented.
pub struct CheckFloat {}
Expand Down Expand Up @@ -41,7 +41,7 @@ impl CheckFloat {

impl ModuleValidator for CheckFloat {
// NOTE: this will not check for SIMD instructions.
fn validate(&self, module: &Module) -> Result<bool, ModuleError> {
fn validate(&self, module: &WasmModule) -> Result<bool, ModuleError> {
let code_section = module.code_section();
if code_section.is_none() {
return Err(ModuleError::NotFound);
Expand Down Expand Up @@ -148,7 +148,7 @@ mod tests {
0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01, 0x03, 0x61, 0x64, 0x64,
0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b,
];
let module = Module::from_bytes(&wasm).unwrap();
let module = WasmModule::from_bytes(&wasm).unwrap();
let checker = CheckFloat::new();
let result = checker.validate(&module).unwrap();
assert_eq!(true, result);
Expand All @@ -168,7 +168,7 @@ mod tests {
0x7d, 0x01, 0x7d, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01, 0x03, 0x61, 0x64, 0x64,
0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x92, 0x0b,
];
let module = Module::from_bytes(&wasm).unwrap();
let module = WasmModule::from_bytes(&wasm).unwrap();
let checker = CheckFloat::new();
let result = checker.validate(&module).unwrap();
assert_eq!(false, result);
Expand All @@ -188,7 +188,7 @@ mod tests {
0x7c, 0x01, 0x7c, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01, 0x03, 0x61, 0x64, 0x64,
0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0xa0, 0x0b,
];
let module = Module::from_bytes(&wasm).unwrap();
let module = WasmModule::from_bytes(&wasm).unwrap();
let checker = CheckFloat::new();
let result = checker.validate(&module).unwrap();
assert_eq!(false, result);
Expand Down
14 changes: 6 additions & 8 deletions libchisel/src/checkstartfunc.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::collections::HashMap;

use parity_wasm::elements::Module;

use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleValidator};
use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleValidator, WasmModule};

/// Struct on which ModuleValidator is implemented.
pub struct CheckStartFunc {
Expand Down Expand Up @@ -51,7 +49,7 @@ impl ModuleConfig for CheckStartFunc {
}

impl ModuleValidator for CheckStartFunc {
fn validate(&self, module: &Module) -> Result<bool, ModuleError> {
fn validate(&self, module: &WasmModule) -> Result<bool, ModuleError> {
Ok(module.start_section().is_some() == self.start_required)
}
}
Expand All @@ -68,7 +66,7 @@ mod tests {
0x08, 0x01, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
];

let module = Module::from_bytes(&wasm).unwrap();
let module = WasmModule::from_bytes(&wasm).unwrap();
let checker = CheckStartFunc::new(true);

let result = checker.validate(&module).unwrap();
Expand All @@ -83,7 +81,7 @@ mod tests {
0x08, 0x01, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
];

let module = Module::from_bytes(&wasm).unwrap();
let module = WasmModule::from_bytes(&wasm).unwrap();
let checker = CheckStartFunc::new(false);

let result = checker.validate(&module).unwrap();
Expand All @@ -98,7 +96,7 @@ mod tests {
0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
];

let module = Module::from_bytes(&wasm).unwrap();
let module = WasmModule::from_bytes(&wasm).unwrap();
let checker = CheckStartFunc::new(false);

let result = checker.validate(&module).unwrap();
Expand All @@ -113,7 +111,7 @@ mod tests {
0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
];

let module = Module::from_bytes(&wasm).unwrap();
let module = WasmModule::from_bytes(&wasm).unwrap();
let checker = CheckStartFunc::new(true);

let result = checker.validate(&module).unwrap();
Expand Down
20 changes: 11 additions & 9 deletions libchisel/src/deployer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::collections::HashMap;

use parity_wasm::builder;
use parity_wasm::elements::{CustomSection, Module};
use parity_wasm::elements::CustomSection;

use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator};
use super::{
ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator, WasmModule,
};

/// Enum on which ModuleTranslator is implemented.
pub enum Deployer {
Expand Down Expand Up @@ -94,13 +96,13 @@ fn deployer_code() -> Vec<u8> {
}

/// Returns a module which contains the deployable bytecode as a custom section.
fn create_custom_deployer(payload: &[u8]) -> Result<Module, ModuleError> {
fn create_custom_deployer(payload: &[u8]) -> Result<WasmModule, ModuleError> {
// The standard deployer code, which expects a 32 bit little endian as the trailing content
// immediately following the payload, placed in a custom section.
let code = deployer_code();

// This is the pre-written deployer code.
let mut module = Module::from_bytes(&code)?;
let mut module = WasmModule::from_bytes(&code)?;

// Re-write memory to pre-allocate enough for code size
let memory_initial = (payload.len() as u32 / 65536) + 1;
Expand Down Expand Up @@ -129,7 +131,7 @@ fn create_custom_deployer(payload: &[u8]) -> Result<Module, ModuleError> {

/// Returns a module which contains the deployable bytecode as a data segment.
#[rustfmt::skip]
fn create_memory_deployer(payload: &[u8]) -> Module {
fn create_memory_deployer(payload: &[u8]) -> WasmModule {
// Instructions calling finish(0, payload_len)
let instructions = vec![
parity_wasm::elements::Instruction::I32Const(0),
Expand Down Expand Up @@ -183,11 +185,11 @@ fn create_memory_deployer(payload: &[u8]) -> Module {
}

impl ModuleTranslator for Deployer {
fn translate_inplace(&self, _module: &mut Module) -> Result<bool, ModuleError> {
fn translate_inplace(&self, _module: &mut WasmModule) -> Result<bool, ModuleError> {
Err(ModuleError::NotSupported)
}

fn translate(&self, module: &Module) -> Result<Option<Module>, ModuleError> {
fn translate(&self, module: &WasmModule) -> Result<Option<WasmModule>, ModuleError> {
let payload = module.clone().to_bytes()?;
let output = match self {
Deployer::Memory => create_memory_deployer(&payload),
Expand Down Expand Up @@ -302,7 +304,7 @@ mod tests {

#[test]
fn customsection_interface_test() {
let payload = Module::default();
let payload = WasmModule::default();
let module = Deployer::with_preset("customsection")
.unwrap()
.translate(&payload)
Expand All @@ -325,7 +327,7 @@ mod tests {

#[test]
fn memory_interface_test() {
let payload = Module::default();
let payload = WasmModule::default();
let module = Deployer::with_preset("memory")
.unwrap()
.translate(&payload)
Expand Down
30 changes: 15 additions & 15 deletions libchisel/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use parity_wasm::elements::Module;
pub use parity_wasm::elements::Module as WasmModule;

use std::collections::HashMap;
use std::{error, fmt};
Expand Down Expand Up @@ -50,20 +50,20 @@ pub trait ChiselModule<'a> {

pub trait ModuleCreator {
/// Returns new module.
fn create(&self) -> Result<Module, ModuleError>;
fn create(&self) -> Result<WasmModule, ModuleError>;
}

pub trait ModuleTranslator {
/// Translates module. Returns new module or none if nothing was modified. Can fail with ModuleError::NotSupported.
fn translate(&self, module: &Module) -> Result<Option<Module>, ModuleError>;
fn translate(&self, module: &WasmModule) -> Result<Option<WasmModule>, ModuleError>;

/// Translates module in-place. Returns true if the module was modified. Can fail with ModuleError::NotSupported.
fn translate_inplace(&self, module: &mut Module) -> Result<bool, ModuleError>;
fn translate_inplace(&self, module: &mut WasmModule) -> Result<bool, ModuleError>;
}

pub trait ModuleValidator {
/// Validates module. Returns true if it is valid or false if invalid.
fn validate(&self, module: &Module) -> Result<bool, ModuleError>;
fn validate(&self, module: &WasmModule) -> Result<bool, ModuleError>;
}

pub trait ModulePreset {
Expand Down Expand Up @@ -141,22 +141,22 @@ mod tests {
struct SampleModule {}

impl ModuleCreator for SampleModule {
fn create(&self) -> Result<Module, ModuleError> {
Ok(Module::default())
fn create(&self) -> Result<WasmModule, ModuleError> {
Ok(WasmModule::default())
}
}

impl ModuleTranslator for SampleModule {
fn translate(&self, _module: &Module) -> Result<Option<Module>, ModuleError> {
Ok(Some(Module::default()))
fn translate(&self, _module: &WasmModule) -> Result<Option<WasmModule>, ModuleError> {
Ok(Some(WasmModule::default()))
}
fn translate_inplace(&self, _module: &mut Module) -> Result<bool, ModuleError> {
fn translate_inplace(&self, _module: &mut WasmModule) -> Result<bool, ModuleError> {
Ok(true)
}
}

impl ModuleValidator for SampleModule {
fn validate(&self, _module: &Module) -> Result<bool, ModuleError> {
fn validate(&self, _module: &WasmModule) -> Result<bool, ModuleError> {
Ok(true)
}
}
Expand Down Expand Up @@ -189,21 +189,21 @@ mod tests {
#[test]
fn translator_succeeds() {
let translator = SampleModule {};
let result = translator.translate(&Module::default());
let result = translator.translate(&WasmModule::default());
assert!(result.is_ok());
}

#[test]
fn translator_inplace_succeeds() {
let translator = SampleModule {};
let result = translator.translate_inplace(&mut Module::default());
let result = translator.translate_inplace(&mut WasmModule::default());
assert!(result.is_ok());
}

#[test]
fn validator_succeeds() {
let validator = SampleModule {};
let result = validator.validate(&Module::default());
let result = validator.validate(&WasmModule::default());
assert!(result.is_ok());
}

Expand Down Expand Up @@ -247,7 +247,7 @@ mod tests {

let as_trait: &dyn ModuleValidator = opaque.as_abstract();

let result = as_trait.validate(&Module::default());
let result = as_trait.validate(&WasmModule::default());
assert!(result.is_ok());
}
}