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
218 changes: 150 additions & 68 deletions Cargo.lock

Large diffs are not rendered by default.

21 changes: 13 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ wit-component = { workspace = true }
default = ["wit", "registry"]
wat = ["wac-resolver/wat"]
wit = ["wac-resolver/wit"]
registry = ["wac-resolver/registry", "dep:indicatif", "dep:warg-client", "dep:warg-protocol"]
registry = [
"wac-resolver/registry",
"dep:indicatif",
"dep:warg-client",
"dep:warg-protocol",
]
native-tls-vendored = ["warg-client?/native-tls-vendored"]

[workspace]
Expand All @@ -58,12 +63,13 @@ wac-parser = { path = "crates/wac-parser", version = "0.7.0-dev", default-featur
wac-resolver = { path = "crates/wac-resolver", version = "0.7.0-dev", default-features = false }
wac-graph = { path = "crates/wac-graph", version = "0.7.0-dev" }
wac-types = { path = "crates/wac-types", version = "0.7.0-dev" }
wit-parser = "0.202.0"
wasmparser = "0.202.0"
wit-component = "0.202.0"
wasm-encoder = "0.202.0"
wasmprinter = "0.202.0"
wasm-metadata = "0.202.0"
wit-parser = "0.227.0"
wasmparser = "0.227.0"
wit-component = "0.227.0"
wasm-encoder = "0.227.0"
wasmprinter = "0.227.0"
wasm-metadata = "0.227.0"
wat = "1.227.0"
anyhow = "1.0.81"
clap = { version = "4.5.4", features = ["derive"] }
semver = { version = "1.0.22", features = ["serde"] }
Expand All @@ -78,7 +84,6 @@ indexmap = { version = "2.2.6", features = ["serde"] }
id-arena = "2.2.1"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.115"
wat = "1.202.0"
logos = "0.14.0"
miette = "7.2.0"
thiserror = "1.0.58"
Expand Down
64 changes: 38 additions & 26 deletions crates/wac-graph/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use indexmap::IndexMap;
use petgraph::graph::NodeIndex;
use std::collections::HashMap;
use wac_types::{
CoreExtern, DefinedType, DefinedTypeId, Enum, Flags, FuncResult, FuncTypeId, InterfaceId,
ItemKind, ModuleTypeId, PrimitiveType, Record, ResourceId, Type, Types, UsedType, ValueType,
Variant, WorldId,
CoreExtern, DefinedType, DefinedTypeId, Enum, Flags, FuncTypeId, InterfaceId, ItemKind,
ModuleTypeId, PrimitiveType, Record, ResourceId, Type, Types, UsedType, ValueType, Variant,
WorldId,
};
use wasm_encoder::{
Alias, ComponentBuilder, ComponentExportKind, ComponentOuterAliasKind, ComponentType,
ComponentTypeEncoder, ComponentTypeRef, ComponentValType, CoreTypeEncoder, EntityType,
GlobalType, InstanceType, MemoryType, ModuleType, TableType, TagKind, TagType, TypeBounds,
Alias, ComponentBuilder, ComponentCoreTypeEncoder, ComponentExportKind,
ComponentOuterAliasKind, ComponentType, ComponentTypeEncoder, ComponentTypeRef,
ComponentValType, EntityType, GlobalType, InstanceType, MemoryType, ModuleType, TableType,
TagKind, TagType, TypeBounds,
};

/// A type used to abstract the API differences between a component builder,
Expand Down Expand Up @@ -55,7 +56,7 @@ impl Encodable {
}
}

fn core_type(&mut self) -> CoreTypeEncoder {
fn core_type(&mut self) -> ComponentCoreTypeEncoder {
match self {
Encodable::Builder(t) => t.core_type().1,
Encodable::Instance(t) => t.core_type(),
Expand Down Expand Up @@ -226,27 +227,11 @@ impl<'a> TypeEncoder<'a> {
.map(|(n, ty)| (n.as_str(), self.value_type(state, *ty)))
.collect::<Vec<_>>();

let results = match &ty.results {
Some(FuncResult::Scalar(ty)) => vec![("", self.value_type(state, *ty))],
Some(FuncResult::List(results)) => results
.iter()
.map(|(n, ty)| (n.as_str(), self.value_type(state, *ty)))
.collect(),
None => Vec::new(),
};

let result = ty.result.map(|ty| self.value_type(state, ty));
let index = state.current.encodable.type_count();
let mut encoder = state.current.encodable.ty().function();
encoder.params(params);

match &ty.results {
Some(FuncResult::Scalar(_)) => {
encoder.result(results[0].1);
}
_ => {
encoder.results(results);
}
}
encoder.result(result);

log::debug!("function type encoded to type index {index}");
index
Expand All @@ -268,6 +253,8 @@ impl<'a> TypeEncoder<'a> {
DefinedType::Alias(ValueType::Borrow(id)) => self.borrow(state, *id),
DefinedType::Alias(ValueType::Own(id)) => self.own(state, *id),
DefinedType::Alias(ValueType::Defined(id)) => self.defined(state, *id),
DefinedType::Stream(ty) => self.stream(state, *ty),
DefinedType::Future(ty) => self.future(state, *ty),
};

log::debug!("defined type encoded to type index {index}");
Expand Down Expand Up @@ -476,25 +463,36 @@ impl<'a> TypeEncoder<'a> {
element_type,
initial,
maximum,
table64,
shared,
} => EntityType::Table(TableType {
element_type: (*element_type).into(),
minimum: *initial,
maximum: *maximum,
table64: *table64,
shared: *shared,
}),
CoreExtern::Memory {
memory64,
shared,
initial,
maximum,
page_size_log2,
} => EntityType::Memory(MemoryType {
minimum: *initial,
maximum: *maximum,
memory64: *memory64,
shared: *shared,
page_size_log2: *page_size_log2,
}),
CoreExtern::Global { val_type, mutable } => EntityType::Global(GlobalType {
CoreExtern::Global {
val_type,
mutable,
shared,
} => EntityType::Global(GlobalType {
val_type: (*val_type).into(),
mutable: *mutable,
shared: *shared,
}),
CoreExtern::Tag(func) => {
let index = encodable.type_count();
Expand Down Expand Up @@ -554,6 +552,20 @@ impl<'a> TypeEncoder<'a> {
index
}

fn stream(&self, state: &mut State, ty: Option<ValueType>) -> u32 {
let ty = ty.map(|ty| self.value_type(state, ty));
let index = state.current.encodable.type_count();
state.current.encodable.ty().defined_type().stream(ty);
index
}

fn future(&self, state: &mut State, ty: Option<ValueType>) -> u32 {
let ty = ty.map(|ty| self.value_type(state, ty));
let index = state.current.encodable.type_count();
state.current.encodable.ty().defined_type().future(ty);
index
}

fn option(&self, state: &mut State, ty: ValueType) -> u32 {
let ty = self.value_type(state, ty);
let index = state.current.encodable.type_count();
Expand Down
8 changes: 6 additions & 2 deletions crates/wac-graph/tests/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ impl GraphFile {
)
})?;

let mut module = wit_component::dummy_module(&resolve, world);
let mut module = wit_component::dummy_module(
&resolve,
world,
wit_parser::ManglingAndAbi::Legacy(wit_parser::LiftLowerAbi::Sync),
);
wit_component::embed_component_metadata(
&mut module,
&resolve,
Expand All @@ -147,7 +151,7 @@ impl GraphFile {
)
})?;

let encoder = ComponentEncoder::default().validate(true).module(&module)?;
let mut encoder = ComponentEncoder::default().validate(true).module(&module)?;
encoder
.encode()
.with_context(|| format!("failed to encode a component from module derived from package `{path}` for test case `{test_case}`", path = path.display()))
Expand Down
4 changes: 2 additions & 2 deletions crates/wac-parser/src/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl Peek for InstantiationArgumentName<'_> {
}
}

impl<'a> InstantiationArgumentName<'a> {
impl InstantiationArgumentName<'_> {
/// Gets the span of the instantiation argument name.
pub fn span(&self) -> SourceSpan {
match self {
Expand Down Expand Up @@ -288,7 +288,7 @@ pub enum PostfixExpr<'a> {
NamedAccess(NamedAccessExpr<'a>),
}

impl<'a> PostfixExpr<'a> {
impl PostfixExpr<'_> {
/// Gets the span of the postfix expression.
pub fn span(&self) -> SourceSpan {
match self {
Expand Down
5 changes: 0 additions & 5 deletions crates/wac-parser/src/ast/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,6 @@ impl<'a, W: Write> DocumentPrinter<'a, W> {
write!(self.writer, " -> ")?;
self.ty(ty)
}
ResultList::Named(results) => {
write!(self.writer, " -> (")?;
self.named_types(results)?;
write!(self.writer, ")")
}
}
}

Expand Down
11 changes: 2 additions & 9 deletions crates/wac-parser/src/ast/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,19 +529,12 @@ pub enum ResultList<'a> {
Empty,
/// The function returns a scalar value.
Scalar(Type<'a>),
/// The function has named results.
Named(Vec<NamedType<'a>>),
}

impl<'a> Parse<'a> for ResultList<'a> {
fn parse(lexer: &mut Lexer<'a>) -> ParseResult<Self> {
let mut lookahead = Lookahead::new(lexer);
if lookahead.peek(Token::OpenParen) {
parse_token(lexer, Token::OpenParen)?;
let results = parse_delimited(lexer, Token::CloseParen, true)?;
parse_token(lexer, Token::CloseParen)?;
Ok(Self::Named(results))
} else if Type::peek(&mut lookahead) {
if Type::peek(&mut lookahead) {
Ok(Self::Scalar(Parse::parse(lexer)?))
} else {
Ok(Self::Empty)
Expand Down Expand Up @@ -672,7 +665,7 @@ pub enum Type<'a> {
Ident(Ident<'a>),
}

impl<'a> Type<'a> {
impl Type<'_> {
/// Gets the span of the type.
pub fn span(&self) -> SourceSpan {
match self {
Expand Down
2 changes: 1 addition & 1 deletion crates/wac-parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ impl<'a> Lexer<'a> {
}
}

impl<'a> Iterator for Lexer<'a> {
impl Iterator for Lexer<'_> {
type Item = (LexerResult<Token>, SourceSpan);

fn next(&mut self) -> Option<Self::Item> {
Expand Down
41 changes: 9 additions & 32 deletions crates/wac-parser/src/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use std::{
};
use wac_graph::{
types::{
BorrowedPackageKey, DefinedType, Enum, ExternKind, Flags, FuncKind, FuncResult, FuncType,
FuncTypeId, Interface, InterfaceId, ItemKind, Package, PackageKey, PrimitiveType, Record,
Resource, ResourceAlias, ResourceId, SubtypeChecker, Type, UsedType, ValueType, Variant,
World, WorldId,
BorrowedPackageKey, DefinedType, Enum, ExternKind, Flags, FuncKind, FuncType, FuncTypeId,
Interface, InterfaceId, ItemKind, Package, PackageKey, PrimitiveType, Record, Resource,
ResourceAlias, ResourceId, SubtypeChecker, Type, UsedType, ValueType, Variant, World,
WorldId,
},
CompositionGraph, DefineTypeError, EncodeError, EncodeOptions, ExportError, ImportError,
InstantiationArgumentError, NodeId, NodeKind, PackageId, Processor,
Expand Down Expand Up @@ -664,7 +664,7 @@ pub struct Resolution<'a> {
instantiation_spans: HashMap<NodeId, SourceSpan>,
}

impl<'a> Resolution<'a> {
impl Resolution<'_> {
/// Gets the document that was resolved.
pub fn document(&self) -> &Document {
self.document
Expand Down Expand Up @@ -2113,50 +2113,27 @@ impl<'a> AstResolver<'a> {
}
}

let results = match func_results {
let result = match func_results {
ast::ResultList::Empty => {
if kind == FuncKind::Constructor {
Some(FuncResult::Scalar(ValueType::Own(resource.unwrap())))
Some(ValueType::Own(resource.unwrap()))
} else {
None
}
}
ast::ResultList::Named(results) => {
let mut list = IndexMap::new();
for result in results {
let value_type = Self::ty(state, &result.ty)?;
if value_type.contains_borrow(state.graph.types()) {
return Err(Error::BorrowInResult {
span: result.ty.span(),
});
}

if list
.insert(result.id.string.to_owned(), value_type)
.is_some()
{
return Err(Error::DuplicateResult {
name: result.id.string.to_string(),
kind,
span: result.id.span,
});
}
}
Some(FuncResult::List(list))
}
ast::ResultList::Scalar(ty) => {
let value_type = Self::ty(state, ty)?;
if value_type.contains_borrow(state.graph.types()) {
return Err(Error::BorrowInResult { span: ty.span() });
}
Some(FuncResult::Scalar(value_type))
Some(value_type)
}
};

Ok(state
.graph
.types_mut()
.add_func_type(FuncType { params, results }))
.add_func_type(FuncType { params, result }))
}

fn expr(
Expand Down
3 changes: 1 addition & 2 deletions crates/wac-parser/tests/parser/types.wac
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ record r {
x: u32,
y: string,
z: v,
}
}

/// Defining flags
flags f {
Expand All @@ -92,4 +92,3 @@ enum e {
type t = e;
type t2 = string;
type t3 = func(a: u32, b: r) -> u32;
type t4 = func() -> (a: u32, b: string);
Loading