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

wit-component: implement a tool to create WebAssembly components. #183

Merged
merged 13 commits into from Apr 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
261 changes: 147 additions & 114 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/parser/src/ast/lex.rs
Expand Up @@ -87,6 +87,7 @@ pub enum Token {
}

#[derive(Eq, PartialEq, Debug)]
#[allow(dead_code)]
pub enum Error {
InvalidCharInString(usize, char),
InvalidCharInId(usize, char),
Expand Down
11 changes: 11 additions & 0 deletions crates/wit-component/Cargo.toml
Expand Up @@ -4,6 +4,11 @@ version = "0.1.0"
authors = ["Peter Huene <peter@huene.dev>"]
edition = "2021"

[[bin]]
name = "wit-component"
path = "src/bin/wit-component.rs"
required-features = ["cli"]
peterhuene marked this conversation as resolved.
Show resolved Hide resolved

[[bin]]
name = "wit2wasm"
path = "src/bin/wit2wasm.rs"
Expand All @@ -20,10 +25,16 @@ wasm-encoder = { git = "https://github.com/bytecodealliance/wasm-tools" }
wat = { git = "https://github.com/bytecodealliance/wasm-tools" }
wit-parser = { path = "../parser" }
anyhow = "1.0.55"
indexmap = "1.8.0"
clap = { version = "3.1.0", features = ["derive"], optional = true }
env_logger = { version = "0.9.0", optional = true }
log = { version = "0.4.14", optional = true }

[dev-dependencies]
wasmprinter = { git = "https://github.com/bytecodealliance/wasm-tools" }
glob = "0.3.0"
pretty_assertions = "1.2.0"

[features]
default = ["cli"]
cli = ["clap", "env_logger", "log"]
3 changes: 3 additions & 0 deletions crates/wit-component/README.md
Expand Up @@ -19,6 +19,9 @@

## Tools

* `wit-component` - creates a WebAssembly component from a core WebAssembly module and a set of
`.wit` files representing the component's imported and exported interfaces.

* `wit2wasm` - encodes an interface definition (in `wit`) as an "interface-only" WebAssembly component.
A `.wasm` component file will be generated that stores a full description of the original interface.

Expand Down
13 changes: 13 additions & 0 deletions crates/wit-component/src/bin/wit-component.rs
@@ -0,0 +1,13 @@
use clap::Parser;
use wit_component::cli::WitComponentApp;

fn main() {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
.format_target(false)
.init();

if let Err(e) = WitComponentApp::parse().execute() {
log::error!("{:?}", e);
std::process::exit(1);
}
}
133 changes: 122 additions & 11 deletions crates/wit-component/src/cli.rs
Expand Up @@ -2,21 +2,122 @@

#![deny(missing_docs)]

use crate::{decode_interface_component, encode_interface_component, InterfacePrinter};
use crate::{
decode_interface_component, encoding::ComponentEncoder, InterfacePrinter, StringEncoding,
};
use anyhow::{bail, Context, Result};
use clap::Parser;
use std::path::{Path, PathBuf};
use wit_parser::Interface;

fn read_interface(path: impl AsRef<Path>) -> Result<Interface> {
let path = path.as_ref();
fn parse_named_interface(s: &str) -> Result<Interface> {
let (name, path) = s
.split_once('=')
.ok_or_else(|| anyhow::anyhow!("expected a value with format `NAME=INTERFACE`"))?;

parse_interface(Some(name.to_string()), Path::new(path))
}

fn parse_unnamed_interface(s: &str) -> Result<Interface> {
parse_interface(None, Path::new(s))
}

fn parse_interface(name: Option<String>, path: &Path) -> Result<Interface> {
if !path.is_file() {
bail!("interface file `{}` does not exist", path.display(),);
}

Interface::parse_file(&path)
.with_context(|| format!("failed to parse interface file `{}`", path.display()))
let mut interface = Interface::parse_file(&path)
.with_context(|| format!("failed to parse interface file `{}`", path.display()))?;

interface.name = name.unwrap_or_else(|| "".to_string());

Ok(interface)
}

/// WebAssembly component encoder.
///
/// Encodes a WebAssembly component from a core WebAssembly module.
#[derive(Debug, Parser)]
#[clap(name = "component-encoder", version = env!("CARGO_PKG_VERSION"))]
pub struct WitComponentApp {
/// The path to an interface definition file the component imports.
#[clap(long = "import", value_name = "NAME=INTERFACE", parse(try_from_str = parse_named_interface))]
pub imports: Vec<Interface>,

/// The path to an interface definition file the component exports.
#[clap(long = "export", value_name = "NAME=INTERFACE", parse(try_from_str = parse_named_interface))]
pub exports: Vec<Interface>,

/// The path of the output WebAssembly component.
#[clap(long, short = 'o', value_name = "OUTPUT")]
pub output: Option<PathBuf>,

/// The default interface of the component.
#[clap(long, short = 'i', value_name = "INTERFACE", parse(try_from_str = parse_unnamed_interface))]
pub interface: Option<Interface>,

/// Skip validation of the output component.
#[clap(long)]
pub skip_validation: bool,

/// The expected string encoding format for the component.
/// Supported values are: `utf8` (default), `utf16`, and `compact-utf16`.
#[clap(long, value_name = "ENCODING")]
pub encoding: Option<StringEncoding>,

/// Path to the WebAssembly module to encode.
#[clap(index = 1, value_name = "MODULE")]
pub module: PathBuf,
}

impl WitComponentApp {
/// Executes the application.
pub fn execute(self) -> Result<()> {
if !self.module.is_file() {
bail!(
"module `{}` does not exist as a file",
self.module.display()
);
}

let output = self.output.unwrap_or_else(|| {
let mut stem: PathBuf = self.module.file_stem().unwrap().into();
stem.set_extension("wasm");
stem
});

let module = wat::parse_file(&self.module)
.with_context(|| format!("failed to parse module `{}`", self.module.display()))?;

let mut encoder = ComponentEncoder::default()
.module(&module)
.imports(&self.imports)
.exports(&self.exports)
.validate(!self.skip_validation);

if let Some(interface) = &self.interface {
encoder = encoder.interface(interface);
}

if let Some(encoding) = &self.encoding {
encoder = encoder.encoding(*encoding);
}

let bytes = encoder.encode().with_context(|| {
format!(
"failed to encode a component from module `{}`",
self.module.display()
)
})?;

std::fs::write(&output, bytes)
.with_context(|| format!("failed to write output file `{}`", output.display()))?;

println!("encoded component `{}`", output.display());

Ok(())
}
}

/// WebAssembly interface encoder.
Expand All @@ -26,11 +127,11 @@ fn read_interface(path: impl AsRef<Path>) -> Result<Interface> {
#[clap(name = "wit2wasm", version = env!("CARGO_PKG_VERSION"))]
pub struct WitToWasmApp {
/// The path of the output WebAssembly component.
#[clap(long, short = 'o', value_name = "OUTPUT", parse(from_os_str))]
#[clap(long, short = 'o', value_name = "OUTPUT")]
pub output: Option<PathBuf>,

/// The path to the WebAssembly interface file to encode.
#[clap(index = 1, value_name = "INTERFACE", parse(from_os_str))]
#[clap(index = 1, value_name = "INTERFACE")]
pub interface: PathBuf,
}

Expand All @@ -43,8 +144,18 @@ impl WitToWasmApp {
stem
});

let interface = read_interface(self.interface)?;
let bytes = encode_interface_component(&interface)?;
let interface = parse_interface(None, &self.interface)?;

let encoder = ComponentEncoder::default()
.interface(&interface)
.types_only(true);

let bytes = encoder.encode().with_context(|| {
format!(
"failed to encode a component from interface `{}`",
self.interface.display()
)
})?;

std::fs::write(&output, bytes)
.with_context(|| format!("failed to write output file `{}`", output.display()))?;
Expand All @@ -62,11 +173,11 @@ impl WitToWasmApp {
#[clap(name = "wit2wasm", version = env!("CARGO_PKG_VERSION"))]
pub struct WasmToWitApp {
/// The path of the output WebAssembly interface file.
#[clap(long, short = 'o', value_name = "OUTPUT", parse(from_os_str))]
#[clap(long, short = 'o', value_name = "OUTPUT")]
pub output: Option<PathBuf>,

/// The path to the WebAssembly component to decode.
#[clap(index = 1, value_name = "COMPONENT", parse(from_os_str))]
#[clap(index = 1, value_name = "COMPONENT")]
pub component: PathBuf,
}

Expand Down
8 changes: 3 additions & 5 deletions crates/wit-component/src/decoding.rs
Expand Up @@ -24,14 +24,12 @@ impl<'a> ComponentInfo<'a> {
pub fn new(mut bytes: &'a [u8]) -> Result<Self> {
let mut parser = Parser::new(0);
let mut parsers = Vec::new();
let mut validator = Validator::new();
let mut exported_types = Vec::new();
let mut exported_functions = Vec::new();

validator.wasm_features(WasmFeatures {
let mut validator = Validator::new_with_features(WasmFeatures {
component_model: true,
..Default::default()
});
let mut exported_types = Vec::new();
let mut exported_functions = Vec::new();

loop {
match parser.parse(bytes, true)? {
Expand Down