Skip to content
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
1 change: 0 additions & 1 deletion chisel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ edition = "2018"

[dependencies]
libchisel = { path = "../libchisel", version = "0.5.0" }
parity-wasm = "^0.40.2"
clap = "2.32.0"
serde = "1.0.80"
serde_derive = "1.0.80"
Expand Down
11 changes: 5 additions & 6 deletions chisel/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
extern crate libchisel;
extern crate parity_wasm;
#[macro_use]
extern crate clap;
extern crate serde;
Expand All @@ -10,7 +9,7 @@ extern crate serde_yaml;
mod logger;
mod config;

use std::fs::{read, read_to_string};
use std::fs::{read, read_to_string, write};
use std::process;

use libchisel::{
Expand All @@ -23,7 +22,6 @@ use libchisel::binaryenopt::*;

use clap::{App, Arg, ArgMatches, SubCommand};
use libchisel::*;
use parity_wasm::elements::{deserialize_buffer, serialize_to_file, Module};
use serde_yaml::Value;

// Error messages
Expand Down Expand Up @@ -311,7 +309,7 @@ fn execute_module(context: &ModuleContext, module: &mut Module) -> bool {

fn chisel_execute(context: &ChiselContext) -> Result<bool, &'static str> {
if let Ok(buffer) = read(context.file()) {
if let Ok(module) = deserialize_buffer::<Module>(&buffer) {
if let Ok(module) = Module::from_bytes(&buffer) {
// If we do not parse the NamesSection here, parity-wasm will drop it at serialisation
// It is useful to have this for a number of optimisation passes, including binaryenopt and snip
// TODO: better error handling
Expand All @@ -327,12 +325,13 @@ fn chisel_execute(context: &ChiselContext) -> Result<bool, &'static str> {

// If the module was mutated, serialize to file.
if original != module {
let serialized = module.to_bytes().expect("Failed to serialize Module");
if let Some(path) = context.outfile() {
chisel_debug!(1, "Writing to file: {}", path);
serialize_to_file(path, module).unwrap();
write(path, serialized).unwrap();
} else {
chisel_debug!(1, "No output file specified; writing in place");
serialize_to_file(context.file(), module).unwrap();
write(context.file(), serialized).unwrap();
}
}
Ok(chisel_results)
Expand Down
20 changes: 1 addition & 19 deletions libchisel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate binaryen;
extern crate parity_wasm;
extern crate rustc_hex;

use parity_wasm::elements::Module;
pub use parity_wasm::elements::Module;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed yet, but perhaps we could benefit from a prelude of sorts containing re-exports and the like.


use std::{error, fmt};

Expand All @@ -28,11 +28,6 @@ pub mod verifyimports;

mod depgraph;

// This helper is exported here for users of this library not needing to import parity_wasm.
pub fn module_from_bytes<T: AsRef<[u8]>>(input: T) -> Result<Module, ModuleError> {
Ok(Module::from_bytes(input)?)
}

#[derive(Eq, PartialEq, Debug)]
pub enum ModuleKind {
Creator,
Expand Down Expand Up @@ -250,17 +245,4 @@ mod tests {
let result = as_trait.validate(&Module::default());
assert!(result.is_ok());
}

#[test]
fn loading_from_bytes() {
let module_orig = Module::default();

let output = module_orig.clone().to_bytes().unwrap();
let module = module_from_bytes(&output).unwrap();
assert_eq!(module_orig, module);

let output = module_orig.clone().to_bytes().unwrap();
let module = Module::from_bytes(&output).unwrap();
assert_eq!(module_orig, module);
}
}