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

Implement component encoding. #11

Merged
merged 1 commit into from
Nov 25, 2023
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
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ keywords = ["webassembly", "wasm", "components", "component-model"]
repository = "https://github.com/bytecodealliance/wac"

[dependencies]
wac-parser = { workspace = true }
wac-parser = { workspace = true, default-features = false}
anyhow = { workspace = true }
clap = { workspace = true }
pretty_env_logger = { workspace = true }
Expand All @@ -28,6 +28,9 @@ tokio = { workspace = true }
owo-colors = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
wat = { workspace = true }
wasmparser = { workspace = true }
wasmprinter = { workspace = true }

[features]
default = []
Expand All @@ -38,6 +41,9 @@ wac-parser = { path = "crates/wac-parser", default-features = false }
wit-parser = "0.13.0"
wasmparser = "0.116.1"
wit-component = "0.18.0"
wasm-encoder = "0.36.2"
wasmprinter = "0.2.72"
wasm-metadata = "0.10.11"
anyhow = "1.0.75"
clap = { version = "4.4.7", features = ["derive"] }
semver = { version = "1.0.20", features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ item-type-decl ::= resource-decl | type-decl
resource-decl ::= 'resource' id '{' resource-item* '}'
resource-item ::= constructor | method
constructor ::= 'constructor' param-list
method ::= id ':' 'static'? func-type-ref
method ::= id ':' 'static'? func-type
variant-decl ::= 'variant' id '{' variant-cases '}'
variant-cases ::= variant-case (',' variant-case)* ','?
variant-case ::= id ('(' type ')')?
Expand Down
7 changes: 7 additions & 0 deletions crates/wac-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ serde = { workspace = true }
wasmparser = { workspace = true }
wit-parser = { workspace = true }
wit-component = { workspace = true }
wasm-encoder = { workspace = true }
wasm-metadata = { workspace = true }
wat = { workspace = true, optional = true }
owo-colors = { workspace = true }

Expand All @@ -32,6 +34,7 @@ pretty_assertions = "1.4.0"
pretty_env_logger = { workspace = true }
rayon = "1.8.0"
serde_json = { workspace = true }
wasmprinter = { workspace = true }

[[test]]
name = "parser"
Expand All @@ -40,3 +43,7 @@ harness = false
[[test]]
name = "resolution"
harness = false

[[test]]
name = "encoding"
harness = false
6 changes: 4 additions & 2 deletions crates/wac-parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ mod export;
mod expr;
mod import;
mod r#let;
mod printer;
mod r#type;

pub use export::*;
pub use expr::*;
pub use import::*;
pub use printer::*;
pub use r#let::*;
pub use r#type::*;

Expand Down Expand Up @@ -43,7 +45,7 @@ impl fmt::Display for Expected<'_> {
write!(f, ", ")?;
}

if i == self.expected.len() - 1 && self.count <= self.expected.len() {
if i == self.count - 1 {
write!(f, "or ")?;
}

Expand Down Expand Up @@ -306,7 +308,7 @@ macro_rules! display {
($name:ident, $method:ident) => {
impl std::fmt::Display for $name<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut printer = crate::printer::DocumentPrinter::new(f, None);
let mut printer = crate::ast::DocumentPrinter::new(f, None);
printer.$method(self)
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/wac-parser/src/ast/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl<'a> PackagePath<'a> {
/// Iterates over the segments of the package path.
pub fn segment_spans<'b>(&'b self) -> impl Iterator<Item = (&'a str, Span<'a>)> + 'b {
self.segments.split('/').map(|s| {
let start = s.as_ptr() as usize - self.name.as_ptr() as usize;
let start = self.span.start + s.as_ptr() as usize - self.name.as_ptr() as usize;
let end = start + s.len();
(s, Span::from_span(self.span.source(), &(start..end)))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,20 @@ impl<W: Write> DocumentPrinter<W> {
/// Prints the given type.
pub fn ty(&mut self, ty: &Type) -> std::fmt::Result {
match ty {
Type::U8 => write!(self.writer, "u8"),
Type::S8 => write!(self.writer, "s8"),
Type::U16 => write!(self.writer, "u16"),
Type::S16 => write!(self.writer, "s16"),
Type::U32 => write!(self.writer, "u32"),
Type::S32 => write!(self.writer, "s32"),
Type::U64 => write!(self.writer, "u64"),
Type::S64 => write!(self.writer, "s64"),
Type::Float32 => write!(self.writer, "float32"),
Type::Float64 => write!(self.writer, "float64"),
Type::Char => write!(self.writer, "char"),
Type::Bool => write!(self.writer, "bool"),
Type::String => write!(self.writer, "string"),
Type::Tuple(types) => {
Type::U8(_) => write!(self.writer, "u8"),
Type::S8(_) => write!(self.writer, "s8"),
Type::U16(_) => write!(self.writer, "u16"),
Type::S16(_) => write!(self.writer, "s16"),
Type::U32(_) => write!(self.writer, "u32"),
Type::S32(_) => write!(self.writer, "s32"),
Type::U64(_) => write!(self.writer, "u64"),
Type::S64(_) => write!(self.writer, "s64"),
Type::Float32(_) => write!(self.writer, "float32"),
Type::Float64(_) => write!(self.writer, "float64"),
Type::Char(_) => write!(self.writer, "char"),
Type::Bool(_) => write!(self.writer, "bool"),
Type::String(_) => write!(self.writer, "string"),
Type::Tuple(types, _) => {
write!(self.writer, "tuple<")?;
for (i, ty) in types.iter().enumerate() {
if i > 0 {
Expand All @@ -165,17 +165,17 @@ impl<W: Write> DocumentPrinter<W> {

write!(self.writer, ">")
}
Type::List(ty) => {
Type::List(ty, _) => {
write!(self.writer, "list<")?;
self.ty(ty)?;
write!(self.writer, ">")
}
Type::Option(ty) => {
Type::Option(ty, _) => {
write!(self.writer, "option<")?;
self.ty(ty)?;
write!(self.writer, ">")
}
Type::Result { ok, err } => match (ok, err) {
Type::Result { ok, err, .. } => match (ok, err) {
(None, None) => write!(self.writer, "result"),
(None, Some(err)) => {
write!(self.writer, "result<_, ")?;
Expand All @@ -195,7 +195,7 @@ impl<W: Write> DocumentPrinter<W> {
write!(self.writer, ">")
}
},
Type::Borrow(id) => {
Type::Borrow(id, _) => {
write!(self.writer, "borrow<{id}>", id = id.span.as_str())
}
Type::Ident(id) => write!(self.writer, "{id}", id = id.span.as_str()),
Expand Down Expand Up @@ -323,7 +323,7 @@ impl<W: Write> DocumentPrinter<W> {
write!(self.writer, "static ")?;
}

self.func_type_ref(&method.ty)?;
self.func_type(&method.ty)?;
write!(self.writer, ";")
}

Expand Down
Loading