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

[WIP] Add support for running wasm-snip #163

Closed
wants to merge 8 commits into from
Closed
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
227 changes: 139 additions & 88 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ tar = "0.4.16"
toml = "0.4"
which = "2.0.0"
zip = "0.4.2"
wasm-snip = { version = "0.1.3", default-features = false }
parity-wasm = "0.27.0"

[dev-dependencies]
tempfile = "3"
Expand Down
16 changes: 16 additions & 0 deletions src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,20 @@ impl Build {
info!(&log, "wasm bindings were built at {:#?}.", &self.out_dir);
Ok(())
}

fn set_run_wasm_snip(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
let msg = format!("{}Running WASM-opt...", emoji::RUNNER);
PBAR.step(step, &msg);

snip::set_run_wasm_snip(
// FIXME(csmoe) add wasm_snip_config() in build
self.wasm_snip_config(),
)?;
info!(
&log,
"wasm bindings were snipped at {:#?}.",
&self.crate_path.join("pkg")
);
Ok(())
}
}
20 changes: 20 additions & 0 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@
pub mod build;
mod login;
mod pack;
<<<<<<< HEAD
/// Data structures and functions for publishing a package.
pub mod publish;
pub mod test;
mod snip;
||||||| merged common ancestors
mod publish;
mod snip;
=======
mod publish;
>>>>>>> build(snip): hidde snip into build
pub mod utils;

use self::build::{Build, BuildOptions};
use self::login::login;
use self::pack::pack;
use self::publish::{access::Access, publish};
use self::test::{Test, TestOptions};
use self::snip::{snip, SnipOpitons};
use error::Error;
use slog::Logger;
use std::path::PathBuf;
Expand Down Expand Up @@ -81,6 +90,13 @@ pub enum Command {
#[structopt(name = "test")]
/// πŸ‘©β€πŸ”¬ test your wasm!
Test(TestOptions),

#[structopt(name = "snip")]
/// Replace a wasm function with an `unreachable`.

#[structopt(name = "snip")]
/// Replace a wasm function with an `unreachable`.
Snip(SnipOptions),
}

/// Run a command with the given logger!
Expand Down Expand Up @@ -123,6 +139,10 @@ pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), failu
info!(&log, "Running test command...");
Test::try_from_opts(test_opts).and_then(|t| t.run(&log))
}
Command::Snip(opts) => {
info!(&log, "Running snip command...");
snip(opts)
}
};

match status {
Expand Down
23 changes: 23 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Code related to error handling for wasm-pack
use curl;
use serde_json;
use failure;
use parity_wasm;
use serde_json;
use std::borrow::Cow;
use std::io;
use std::process::ExitStatus;
Expand Down Expand Up @@ -212,3 +215,23 @@ impl From<toml::de::Error> for Error {
Error::SerdeToml(e)
}
}

/// wasm-snip occurs error when trying to print out the result.
impl From<parity_wasm::elements::Error> for Error {
fn from(error: parity_wasm::elements::Error) -> Self {
Error::Cli {
message: "There was an wasm-snip output Error".to_owned(),
stderr: format!("{}", error),
}
}
}

/// wasm-snip occurs error when working with inputs.
impl From<failure::Error> for Error {
csmoe marked this conversation as resolved.
Show resolved Hide resolved
fn from(error: failure::Error) -> Self {
Error::Cli {
message: "There was an wasm-snip Error".to_owned(),
stderr: format!("{}", error),
}
}
}
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ extern crate serde_json;
extern crate structopt;
#[macro_use]
extern crate slog;
extern crate parity_wasm;
extern crate slog_async;
extern crate slog_term;
extern crate tar;
extern crate toml;
extern crate which;
extern crate zip;
extern crate wasm_snip;

pub mod binaries;
pub mod bindgen;
Expand All @@ -39,8 +41,13 @@ pub mod manifest;
pub mod npm;
pub mod progressbar;
pub mod readme;
<<<<<<< HEAD
pub mod target;
pub mod test;
||||||| merged common ancestors
=======
pub mod snip;
>>>>>>> build(snip): hidde snip into build

use progressbar::ProgressOutput;

Expand Down
1 change: 1 addition & 0 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fn log_file_path(cmd: &Command) -> PathBuf {
Command::Publish { path, access: _ } => path,
Command::Test(test_opts) => &test_opts.path,
Command::Login { .. } => &None,
Command::Snip(opts) => &opts.output,
};

// If the path exists attempt to use it, if not default to the current directory
Expand Down
42 changes: 42 additions & 0 deletions src/snip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use error::Error;
use parity_wasm::elements::{self, Serialize};
use wasm_snip;

#[derive(Clone, Debug, StructOpt)]
pub struct WasmSnipConfig {
input: String,
#[structopt(long = "output", short = "o")]
pub(crate) output: Option<String>,
functions: Vec<String>,
#[structopt(long = "pattern", short = "p")]
patterns: Vec<String>,
#[structopt(long = "snip_rust_fmt_code")]
snip_rust_fmt_code: bool,
#[structopt(long = "snip_rust_panicking_code")]
snip_rust_panicking_code: bool,
}

impl Into<wasm_snip::Options> for WasmSnipConfig {
fn into(self) -> wasm_snip::Options {
wasm_snip::Options {
input: ::std::path::PathBuf::from(self.input),
functions: self.functions,
patterns: self.patterns,
snip_rust_fmt_code: self.snip_rust_fmt_code,
snip_rust_panicking_code: self.snip_rust_panicking_code,
}
}
}

pub(crate) fn run_wasm_snip(opts: WasmSnipConfig) -> Result<(), Error> {
let module = wasm_snip::snip(opts.clone().into())?;

if let Some(output) = opts.output {
elements::serialize_to_file(output, module)?;
} else {
let stdout = ::std::io::stdout();
let mut stdout = stdout.lock();
module.serialize(&mut stdout)?;
}
Ok(())
}