add delete subcommand to wit_embedder

This commit is contained in:
vms 2020-06-04 20:36:07 +03:00
parent 59f9b7555f
commit 5d7fdec0ee
6 changed files with 155 additions and 71 deletions

View File

@ -0,0 +1,53 @@
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use super::errors::WITParserError;
use super::custom::WIT_SECTION_NAME;
use walrus::ModuleConfig;
use std::path::PathBuf;
pub fn delete_wit_section(
in_wasm_path: PathBuf,
out_wasm_path: PathBuf,
) -> Result<(), WITParserError> {
let mut module = ModuleConfig::new()
.parse_file(&in_wasm_path)
.map_err(WITParserError::CorruptedWasmFile)?;
let wit_section_ids = module
.customs
.iter()
.filter_map(|(id, section)| {
if section.name() == WIT_SECTION_NAME {
Some(id)
} else {
None
}
})
.collect::<Vec<_>>();
for id in wit_section_ids {
module.customs.delete(id);
}
module
.emit_wasm_file(&out_wasm_path)
.map_err(WITParserError::WasmEmitError)?;
Ok(())
}

View File

@ -25,18 +25,16 @@ use wasmer_wit::{
use std::path::PathBuf;
pub struct EmbedderConfig {
pub in_wasm_path: PathBuf,
pub out_wasm_path: PathBuf,
pub wit: String,
}
pub fn embed_text_wit(options: &EmbedderConfig) -> Result<(), WITParserError> {
pub fn embed_text_wit(
in_wasm_path: PathBuf,
out_wasm_path: PathBuf,
wit: &str,
) -> Result<(), WITParserError> {
let mut module = ModuleConfig::new()
.parse_file(&options.in_wasm_path)
.parse_file(&in_wasm_path)
.map_err(WITParserError::CorruptedWasmFile)?;
let buffer = Buffer::new(&options.wit)?;
let buffer = Buffer::new(wit)?;
let ast = parse(&buffer)?;
let mut bytes = vec![];
@ -45,7 +43,7 @@ pub fn embed_text_wit(options: &EmbedderConfig) -> Result<(), WITParserError> {
let custom = WITCustom(bytes);
module.customs.add(custom);
module
.emit_wasm_file(&options.out_wasm_path)
.emit_wasm_file(&out_wasm_path)
.map_err(WITParserError::WasmEmitError)?;
Ok(())

View File

@ -18,11 +18,11 @@ mod custom;
mod errors;
mod extractor;
mod embedder;
mod deleter;
pub use errors::WITParserError;
pub use embedder::EmbedderConfig;
pub use embedder::embed_text_wit;
pub use deleter::delete_wit_section;
pub use extractor::extract_fce_wit;
pub use extractor::extract_text_wit;

View File

@ -18,8 +18,8 @@ use super::wit_prelude::*;
use super::fce_module::FCEModule;
use fce_wit_interfaces::FCEWITInterfaces;
use wasmer_wit::interpreter::wasm;
use fce_wit_interfaces::WITAstType;
use wasmer_wit::interpreter::wasm;
use wasmer_wit::interpreter::wasm::structures::{LocalImportIndex, TypedIndex};
use wasmer_core::Instance as WasmerInstance;

View File

@ -0,0 +1,56 @@
use clap::{App, Arg, SubCommand};
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const AUTHORS: &str = env!("CARGO_PKG_AUTHORS");
pub const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
pub const IN_WASM_PATH: &str = "in-wasm-path";
pub const WIT_PATH: &str = "wit-path";
pub const OUT_WASM_PATH: &str = "out-wasm-path";
pub fn embed_wit<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("embed")
.about("embed WIT to provided Wasm file")
.args(&[
Arg::with_name(IN_WASM_PATH)
.required(true)
.takes_value(true)
.short("i")
.help("path to the wasm file"),
Arg::with_name(WIT_PATH)
.required(true)
.takes_value(true)
.short("w")
.help("path to file with WIT"),
Arg::with_name(OUT_WASM_PATH)
.takes_value(true)
.short("o")
.help("path to result file with embedded WIT"),
])
}
pub fn show_wit<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("show")
.about("show WIT in provided Wasm file")
.args(&[Arg::with_name(IN_WASM_PATH)
.required(true)
.takes_value(true)
.short("i")
.help("path to the wasm file")])
}
pub fn delete_wit<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("delete")
.about("delete all WIT sections in provided Wasm file")
.args(&[
Arg::with_name(IN_WASM_PATH)
.required(true)
.takes_value(true)
.short("i")
.help("path to the wasm file"),
Arg::with_name(OUT_WASM_PATH)
.takes_value(true)
.short("o")
.help("path to result file with deleted WIT sections"),
])
}

View File

@ -13,54 +13,28 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#![warn(rust_2018_idioms)]
#![deny(
dead_code,
nonstandard_style,
unused_imports,
unused_mut,
unused_variables,
unused_unsafe,
unreachable_patterns
)]
mod args;
use args::*;
use fce_wit_interfaces::EmbedderConfig;
use fce_wit_interfaces::embed_text_wit;
use fce_wit_interfaces::extract_text_wit;
use fce_wit_interfaces::delete_wit_section;
use clap::{App, AppSettings, Arg, SubCommand};
use failure::err_msg;
use clap::{App, AppSettings};
use std::path::PathBuf;
const VERSION: &str = env!("CARGO_PKG_VERSION");
const AUTHORS: &str = env!("CARGO_PKG_AUTHORS");
const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
const IN_WASM_PATH: &str = "in-wasm-path";
const WIT_PATH: &str = "wit-path";
const OUT_WASM_PATH: &str = "out-wasm-path";
fn embed_wit<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("embed")
.about("embed WIT to provided Wasm file")
.args(&[
Arg::with_name(IN_WASM_PATH)
.required(true)
.takes_value(true)
.short("i")
.help("path to the wasm file"),
Arg::with_name(WIT_PATH)
.required(true)
.takes_value(true)
.short("w")
.help("path to file with WIT"),
Arg::with_name(OUT_WASM_PATH)
.takes_value(true)
.short("o")
.help("path to result file with embedded WIT"),
])
}
fn show_wit<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("show")
.about("show WIT in provided Wasm file")
.args(&[Arg::with_name(IN_WASM_PATH)
.required(true)
.takes_value(true)
.short("i")
.help("path to the wasm file")])
}
pub fn main() -> Result<(), exitfailure::ExitFailure> {
let app = App::new("CLI tool for embedding WIT to provided Wasm file")
.version(VERSION)
@ -68,7 +42,8 @@ pub fn main() -> Result<(), exitfailure::ExitFailure> {
.about(DESCRIPTION)
.setting(AppSettings::ArgRequiredElseHelp)
.subcommand(embed_wit())
.subcommand(show_wit());
.subcommand(show_wit())
.subcommand(delete_wit());
match app.get_matches().subcommand() {
("embed", Some(arg)) => {
@ -81,20 +56,11 @@ pub fn main() -> Result<(), exitfailure::ExitFailure> {
let wit = String::from_utf8(std::fs::read(wit_path)?).unwrap();
let options = EmbedderConfig {
in_wasm_path: PathBuf::from(in_wasm_path),
wit,
out_wasm_path: PathBuf::from(out_wasm_path),
};
match embed_text_wit(&options) {
Ok(_) => {
println!("WIT successfully embedded");
}
Err(e) => {
println!("{}", e);
}
};
embed_text_wit(
PathBuf::from(in_wasm_path),
PathBuf::from(out_wasm_path),
&wit,
)?;
Ok(())
}
@ -107,6 +73,17 @@ pub fn main() -> Result<(), exitfailure::ExitFailure> {
Ok(())
}
c => Err(err_msg(format!("Unexpected command: {}", c.0)).into()),
("delete", Some(arg)) => {
let in_wasm_path = arg.value_of(IN_WASM_PATH).unwrap();
let out_wasm_path = match arg.value_of(OUT_WASM_PATH) {
Some(path) => path,
None => in_wasm_path,
};
delete_wit_section(PathBuf::from(in_wasm_path), PathBuf::from(out_wasm_path))?;
Ok(())
}
c => Err(failure::err_msg(format!("Unexpected command: {}", c.0)).into()),
}
}