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

Enable binary encoding and printing of multi-package wit files #1609

Closed
Closed
Show file tree
Hide file tree
Changes from 15 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
2 changes: 1 addition & 1 deletion ci/docker/x86_64-linux/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ FROM almalinux:8

RUN dnf install -y git gcc

ENV PATH=$PATH:/rust/bin
ENV PATH=$PATH:/rust/bin
4 changes: 4 additions & 0 deletions crates/wasm-encoder/src/component/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ pub struct ComponentBuilder {
}

impl ComponentBuilder {
/// Adds sub component names
pub fn names(&mut self, names: &ComponentNameSection) {
self.component.section(names);
}
/// Returns the current number of core modules.
pub fn core_module_count(&self) -> u32 {
self.core_modules
Expand Down
3 changes: 3 additions & 0 deletions crates/wasm-encoder/src/component/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ enum Subsection {
}

impl ComponentNameSection {
/// Human readable component names
pub const SECTION_NAME: &'static str = "component-name";

/// Creates a new blank `name` custom section.
pub fn new() -> Self {
Self::default()
Expand Down
10 changes: 5 additions & 5 deletions crates/wit-component/src/encoding/wit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ fn use_v2_encoding() -> bool {
///
/// The binary returned can be [`decode`d](crate::decode) to recover the WIT
/// package provided.
pub fn encode(use_v2: Option<bool>, resolve: &Resolve, package: PackageId) -> Result<Vec<u8>> {
let mut component = encode_component(use_v2, resolve, package)?;
pub fn encode(use_v2: Option<bool>, resolve: &Resolve, packages: &[PackageId]) -> Result<Vec<u8>> {
let mut component = encode_component(use_v2, resolve, packages)?;
component.raw_custom_section(&crate::base_producers().raw_custom_section());
Ok(component.finish())
}
Expand All @@ -43,12 +43,12 @@ pub fn encode(use_v2: Option<bool>, resolve: &Resolve, package: PackageId) -> Re
pub fn encode_component(
use_v2: Option<bool>,
resolve: &Resolve,
package: PackageId,
packages: &[PackageId],
) -> Result<ComponentBuilder> {
if use_v2.unwrap_or_else(use_v2_encoding) {
v2::encode_component(resolve, package)
v2::encode_component(resolve, packages)
} else {
v1::encode_component(resolve, package)
v1::encode_component(resolve, packages[0])
macovedj marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
118 changes: 90 additions & 28 deletions crates/wit-component/src/encoding/wit/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,21 @@ use wit_parser::*;
///
/// The binary returned can be [`decode`d](crate::decode) to recover the WIT
/// package provided.
pub fn encode_component(resolve: &Resolve, package: PackageId) -> Result<ComponentBuilder> {
pub fn encode_component(resolve: &Resolve, packages: &[PackageId]) -> Result<ComponentBuilder> {
let mut encoder = Encoder {
component: ComponentBuilder::default(),
resolve,
package,
packages,
};
encoder.run()?;

let package_metadata = PackageMetadata::extract(resolve, package);
encoder.component.custom_section(&CustomSection {
name: PackageMetadata::SECTION_NAME.into(),
data: package_metadata.encode()?.into(),
});

Ok(encoder.component)
}

struct Encoder<'a> {
component: ComponentBuilder,
resolve: &'a Resolve,
package: PackageId,
packages: &'a [PackageId],
}

impl Encoder<'_> {
Expand All @@ -57,33 +51,101 @@ impl Encoder<'_> {
// decoding process where everyone's view of a foreign document agrees
// notably on the order that types are defined in to assist with
// roundtripping.
for (name, &id) in self.resolve.packages[self.package].interfaces.iter() {
let component_ty = self.encode_interface(id)?;
let ty = self.component.type_component(&component_ty);
self.component
.export(name.as_ref(), ComponentExportKind::Type, ty, None);
let mut names = NameMap::new();
for pkg in self.packages {
if self.packages.len() > 1 {
let sub_encoder = Encoder {
component: ComponentBuilder::default(),
resolve: self.resolve,
packages: self.packages,
};
self.encode_package(&mut names, pkg, Some(sub_encoder))?;
} else {
self.encode_package(&mut names, pkg, None)?;
}
}
if self.packages.len() == 1 {
// let pkg = self.packages[0];
let package_metadata = PackageMetadata::extract(self.resolve, self.packages[0]);
self.component.custom_section(&CustomSection {
name: PackageMetadata::SECTION_NAME.into(),
data: package_metadata.encode()?.into(),
});
}
let mut final_names = ComponentNameSection::new();
final_names.components(&names);
self.component.names(&final_names);

for (name, &world) in self.resolve.packages[self.package].worlds.iter() {
// Encode the `world` directly as a component, then create a wrapper
// component that exports that component.
let component_ty = super::encode_world(self.resolve, world)?;
Ok(())
}

let world = &self.resolve.worlds[world];
let mut wrapper = ComponentType::new();
wrapper.ty().component(&component_ty);
let pkg = &self.resolve.packages[world.package.unwrap()];
wrapper.export(&pkg.name.interface_id(name), ComponentTypeRef::Component(0));
fn encode_package(
&mut self,
names: &mut NameMap,
pkg: &PackageId,
mut sub_encoder: Option<Encoder>,
macovedj marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<()> {
let package = &self.resolve.packages[*pkg];
if let Some(ref mut sub) = sub_encoder {
let package_metadata = PackageMetadata::extract(self.resolve, *pkg);
sub.component.custom_section(&CustomSection {
name: PackageMetadata::SECTION_NAME.into(),
data: package_metadata.encode()?.into(),
});
macovedj marked this conversation as resolved.
Show resolved Hide resolved
}

let ty = self.component.type_component(&wrapper);
self.component
.export(name.as_ref(), ComponentExportKind::Type, ty, None);
for (name, &id) in package.interfaces.iter() {
if let Some(ref mut sub) = sub_encoder {
let component_ty = sub.encode_interface(id, pkg)?;
let ty = sub.component.type_component(&component_ty);
sub.component
.export(name.as_ref(), ComponentExportKind::Type, ty, None);
} else {
let component_ty = self.encode_interface(id, pkg)?;
let ty = self.component.type_component(&component_ty);
self.component
.export(name.as_ref(), ComponentExportKind::Type, ty, None);
}
}
for (name, &world) in package.worlds.iter() {
if let Some(ref mut sub) = sub_encoder {
// Encode the `world` directly as a component, then create a wrapper
// component that exports that component.
let component_ty = super::encode_world(self.resolve, world)?;

let world = &sub.resolve.worlds[world];
let mut wrapper = ComponentType::new();
wrapper.ty().component(&component_ty);
let pkg = &sub.resolve.packages[world.package.unwrap()];
wrapper.export(&pkg.name.interface_id(name), ComponentTypeRef::Component(0));
let ty = sub.component.type_component(&wrapper);
sub.component
.export(name.as_ref(), ComponentExportKind::Type, ty, None);
} else {
// Encode the `world` directly as a component, then create a wrapper
// component that exports that component.
let component_ty = super::encode_world(self.resolve, world)?;
let world = &self.resolve.worlds[world];
let mut wrapper = ComponentType::new();
wrapper.ty().component(&component_ty);
let package = &self.resolve.packages[world.package.unwrap()];
wrapper.export(
&package.name.interface_id(name),
ComponentTypeRef::Component(0),
);
let ty = self.component.type_component(&wrapper);
self.component
.export(name.as_ref(), ComponentExportKind::Type, ty, None);
}
}
if let Some(sub_encoder) = sub_encoder {
let sub = self.component.component(sub_encoder.component);
names.append(sub, &package.name.to_string());
}
Ok(())
}

fn encode_interface(&mut self, id: InterfaceId) -> Result<ComponentType> {
fn encode_interface(&mut self, id: InterfaceId, pkg: &PackageId) -> Result<ComponentType> {
// Build a set of interfaces reachable from this document, including the
// interfaces in the document itself. This is used to import instances
// into the component type we're encoding. Note that entire interfaces
Expand All @@ -101,7 +163,7 @@ impl Encoder<'_> {
let mut used_names = IndexSet::new();
for id in interfaces.iter() {
let iface = &self.resolve.interfaces[*id];
if iface.package == Some(self.package) {
if iface.package == Some(*pkg) {
let first = used_names.insert(iface.name.as_ref().unwrap().clone());
assert!(first);
}
Expand Down
53 changes: 23 additions & 30 deletions crates/wit-component/tests/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,42 +54,35 @@ fn run_test(path: &Path, is_dir: bool) -> Result<()> {
resolve.push_file(path)?
};

for package in packages {
assert_print(&resolve, &[package], path, is_dir)?;
assert_print(&resolve, &packages, path, is_dir)?;

let features = WasmFeatures::default() | WasmFeatures::COMPONENT_MODEL;
let features = WasmFeatures::default() | WasmFeatures::COMPONENT_MODEL;

// First convert the WIT package to a binary WebAssembly output, then
// convert that binary wasm to textual wasm, then assert it matches the
// expectation.
let wasm = wit_component::encode(Some(true), &resolve, package)?;
let wat = wasmprinter::print_bytes(&wasm)?;
assert_output(&path.with_extension("wat"), &wat)?;
wasmparser::Validator::new_with_features(features)
.validate_all(&wasm)
.context("failed to validate wasm output")?;
// First convert the WIT package to a binary WebAssembly output, then
// convert that binary wasm to textual wasm, then assert it matches the
// expectation.
let wasm = wit_component::encode(Some(true), &resolve, &packages)?;
let wat = wasmprinter::print_bytes(&wasm)?;
assert_output(&path.with_extension("wat"), &wat)?;
wasmparser::Validator::new_with_features(features)
.validate_all(&wasm)
.context("failed to validate wasm output")?;

// Next decode a fresh WIT package from the WebAssembly generated. Print
// this package's documents and assert they all match the expectations.
let decoded = wit_component::decode(&wasm)?;
assert_eq!(
1,
decoded.packages().len(),
"Each input WIT package should produce WASM that contains only one package"
);
// Next decode a fresh WIT package from the WebAssembly generated. Print
// this package's documents and assert they all match the expectations.
let decoded = wit_component::decode(&wasm)?;

let decoded_package = decoded.packages()[0];
let resolve = decoded.resolve();
let decoded_package = decoded.packages();
let resolve = decoded.resolve();

assert_print(resolve, decoded.packages(), path, is_dir)?;
assert_print(resolve, decoded.packages(), path, is_dir)?;

// Finally convert the decoded package to wasm again and make sure it
// matches the prior wasm.
let wasm2 = wit_component::encode(Some(true), resolve, decoded_package)?;
if wasm != wasm2 {
let wat2 = wasmprinter::print_bytes(&wasm)?;
assert_eq!(wat, wat2, "document did not roundtrip correctly");
}
// Finally convert the decoded package to wasm again and make sure it
// matches the prior wasm.
let wasm2 = wit_component::encode(Some(true), resolve, decoded_package)?;
if wasm != wasm2 {
let wat2 = wasmprinter::print_bytes(&wasm2)?;
assert_eq!(wat, wat2, "document did not roundtrip correctly");
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/wit-component/tests/interfaces/doc-comments.wat
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
)
)
(export (;5;) "coverage-world" (type 4))
(@custom "package-docs" "\00{\22docs\22:\22package docs;\22,\22worlds\22:{\22coverage-world\22:{\22docs\22:\22world docs\22,\22interfaces\22:{\22i\22:{\22docs\22:\22world inline interface docs\22,\22funcs\22:{\22f\22:\22inline interface func docs\22},\22types\22:{\22t\22:{\22docs\22:\22inline interface typedef docs\22}}}},\22types\22:{\22t\22:{\22docs\22:\22world typedef docs\22}},\22funcs\22:{\22imp\22:\22world func import docs\22,\22exp\22:\22world func export docs\22}}},\22interfaces\22:{\22coverage-iface\22:{\22docs\22:\22interface docs\22,\22funcs\22:{\22[constructor]res\22:\22constructor docs\22,\22[method]res.m\22:\22method docs\22,\22[static]res.s\22:\22static func docs\22,\22f\22:\22interface func docs\22},\22types\22:{\22t\22:{\22docs\22:\22basic typedef docs\22},\22r\22:{\22docs\22:\22record typedef docs\22,\22items\22:{\22f1\22:\22record field docs\22}},\22fl\22:{\22items\22:{\22f1\22:\22flag docs\22}},\22v\22:{\22items\22:{\22c1\22:\22variant case docs\22}},\22e\22:{\22items\22:{\22c1\22:\22enum case docs\22}}}},\22other-comment-forms\22:{\22docs\22:\22other comment forms\5cn multi-line block\22,\22funcs\22:{\22multiple-lines-split\22:\22one doc line\5cnnon-doc in the middle\5cnanother doc line\22,\22mixed-forms\22:\22mixed forms; line doc\5cnplus block doc\5cn multi-line\22}}}}")
(@custom "package-docs" "\00{\22docs\22:\22package docs;\22,\22worlds\22:{\22coverage-world\22:{\22docs\22:\22world docs\22,\22interfaces\22:{\22i\22:{\22docs\22:\22world inline interface docs\22,\22funcs\22:{\22f\22:\22inline interface func docs\22},\22types\22:{\22t\22:{\22docs\22:\22inline interface typedef docs\22}}}},\22types\22:{\22t\22:{\22docs\22:\22world typedef docs\22}},\22funcs\22:{\22imp\22:\22world func import docs\22,\22exp\22:\22world func export docs\22}}},\22interfaces\22:{\22coverage-iface\22:{\22docs\22:\22interface docs\22,\22funcs\22:{\22[constructor]res\22:\22constructor docs\22,\22[method]res.m\22:\22method docs\22,\22[static]res.s\22:\22static func docs\22,\22f\22:\22interface func docs\22},\22types\22:{\22t\22:{\22docs\22:\22basic typedef docs\22},\22r\22:{\22docs\22:\22record typedef docs\22,\22items\22:{\22f1\22:\22record field docs\22}},\22fl\22:{\22items\22:{\22f1\22:\22flag docs\22}},\22v\22:{\22items\22:{\22c1\22:\22variant case docs\22}},\22e\22:{\22items\22:{\22c1\22:\22enum case docs\22}}}},\22other-comment-forms\22:{\22docs\22:\22other comment forms\5cn multi-line block\22,\22funcs\22:{\22mixed-forms\22:\22mixed forms; line doc\5cnplus block doc\5cn multi-line\22,\22multiple-lines-split\22:\22one doc line\5cnnon-doc in the middle\5cnanother doc line\22}}}}")
(@producers
(processed-by "wit-component" "$CARGO_PKG_VERSION")
)
Expand Down
77 changes: 77 additions & 0 deletions crates/wit-component/tests/interfaces/multiple-packages.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
(component
(component $another:thing (;0;)
(@custom "package-docs" "\01{\22interfaces\22:{\22something\22:{\22docs\22:\22documenting an interface\22,\22types\22:{\22my-record\22:{\22stability\22:{\22stable\22:{\22since\22:\221.2.3\22}}}}}}}")
(type (;0;)
(component
(type (;0;)
(instance
(type (;0;) (record (field "foo" string)))
(export (;1;) "my-record" (type (eq 0)))
)
)
(export (;0;) "another:thing/something" (instance (type 0)))
)
)
(export (;1;) "something" (type 0))
)
(component $third:pkg (;1;)
(@custom "package-docs" "\01{\22interfaces\22:{\22things\22:{\22stability\22:{\22stable\22:{\22since\22:\221.2.3\22}},\22types\22:{\22other-record\22:{\22docs\22:\22documenting an type\22}}}}}")
(type (;0;)
(component
(type (;0;)
(instance
(type (;0;) (record (field "foo" string)))
(export (;1;) "other-record" (type (eq 0)))
)
)
(export (;0;) "third:pkg/things" (instance (type 0)))
)
)
(export (;1;) "things" (type 0))
)
(component $foo:bar (;2;)
(@custom "package-docs" "\00{}")
(type (;0;)
(component
(type (;0;)
(component
(type (;0;)
(instance
(type (;0;) (record (field "foo" string)))
(export (;1;) "my-record" (type (eq 0)))
)
)
(import "another:thing/something" (instance (;0;) (type 0)))
(type (;1;)
(instance
(type (;0;) (record (field "foo" string)))
(export (;1;) "other-record" (type (eq 0)))
)
)
(import "third:pkg/things" (instance (;1;) (type 1)))
)
)
(export (;0;) "foo:bar/this-world" (component (type 0)))
)
)
(export (;1;) "this-world" (type 0))
)
(component $fourth:thing (;3;)
(@custom "package-docs" "\00{}")
(type (;0;)
(component
(type (;0;)
(instance
(type (;0;) (record (field "some" string)))
(export (;1;) "foo" (type (eq 0)))
)
)
(export (;0;) "fourth:thing/boo" (instance (type 0)))
)
)
(export (;1;) "boo" (type 0))
)
(@producers
(processed-by "wit-component" "$CARGO_PKG_VERSION")
)
)
Loading