diff --git a/crates/wasm-encoder/src/component/builder.rs b/crates/wasm-encoder/src/component/builder.rs index 27c39ac8a0..58cfa28c6a 100644 --- a/crates/wasm-encoder/src/component/builder.rs +++ b/crates/wasm-encoder/src/component/builder.rs @@ -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 diff --git a/crates/wasm-encoder/src/component/names.rs b/crates/wasm-encoder/src/component/names.rs index 1cbb1062c6..0939984440 100644 --- a/crates/wasm-encoder/src/component/names.rs +++ b/crates/wasm-encoder/src/component/names.rs @@ -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() diff --git a/crates/wit-component/src/encoding/wit/mod.rs b/crates/wit-component/src/encoding/wit/mod.rs index a5b61a5370..f8693cf913 100644 --- a/crates/wit-component/src/encoding/wit/mod.rs +++ b/crates/wit-component/src/encoding/wit/mod.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{bail, Result}; use wasm_encoder::{ComponentBuilder, ComponentType}; use wit_parser::{PackageId, Resolve, WorldId}; @@ -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, resolve: &Resolve, package: PackageId) -> Result> { - let mut component = encode_component(use_v2, resolve, package)?; +pub fn encode(use_v2: Option, resolve: &Resolve, packages: &[PackageId]) -> Result> { + let mut component = encode_component(use_v2, resolve, packages)?; component.raw_custom_section(&crate::base_producers().raw_custom_section()); Ok(component.finish()) } @@ -43,12 +43,15 @@ pub fn encode(use_v2: Option, resolve: &Resolve, package: PackageId) -> Re pub fn encode_component( use_v2: Option, resolve: &Resolve, - package: PackageId, + packages: &[PackageId], ) -> Result { 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) + if packages.len() > 1 { + bail!("Only encoding a single package, when multiple are present") + } + v1::encode_component(resolve, packages[0]) } } diff --git a/crates/wit-component/src/encoding/wit/v2.rs b/crates/wit-component/src/encoding/wit/v2.rs index 7024c6ccf6..44a98d9176 100644 --- a/crates/wit-component/src/encoding/wit/v2.rs +++ b/crates/wit-component/src/encoding/wit/v2.rs @@ -6,6 +6,11 @@ use std::mem; use wasm_encoder::*; use wit_parser::*; +/// If set to 1, wit packages will be encoded +/// using V3 style, where all packages are wrapped in +/// a subcomponent +const USE_V3: u8 = 0; + /// Encodes the given `package` within `resolve` to a binary WebAssembly /// representation. /// @@ -24,30 +29,32 @@ 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 { +pub fn encode_component(resolve: &Resolve, packages: &[PackageId]) -> Result { 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<'_> { + fn encode_metadata(&mut self, pkg: &PackageId) -> Result<()> { + let package_metadata = PackageMetadata::extract(self.resolve, *pkg); + self.component.custom_section(&CustomSection { + name: PackageMetadata::SECTION_NAME.into(), + data: package_metadata.encode()?.into(), + }); + Ok(()) + } fn run(&mut self) -> Result<()> { // Build a set of interfaces reachable from this document, including the // interfaces in the document itself. This is used to import instances @@ -57,33 +64,59 @@ 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 mut names = NameMap::new(); + for pkg in self.packages { + if self.packages.len() > 1 || std::env::var_os("USE_V3").is_some() || USE_V3 == 1 { + let mut sub_encoder = Encoder { + component: ComponentBuilder::default(), + resolve: self.resolve, + packages: self.packages, + }; + sub_encoder.encode_package(pkg)?; + let name = &self.resolve.packages[*pkg]; + let sub = self.component.component(sub_encoder.component); + names.append(sub, &name.name.to_string()); + } else { + self.encode_package(pkg)?; + } + } + let mut final_names = ComponentNameSection::new(); + final_names.components(&names); + self.component.names(&final_names); + + Ok(()) + } + + fn encode_package(&mut self, pkg: &PackageId) -> Result<()> { + let package = &self.resolve.packages[*pkg]; + + for (name, &id) in package.interfaces.iter() { + 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 self.resolve.packages[self.package].worlds.iter() { + for (name, &world) in 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)?; - 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)); - + 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); } - + self.encode_metadata(pkg)?; Ok(()) } - fn encode_interface(&mut self, id: InterfaceId) -> Result { + fn encode_interface(&mut self, id: InterfaceId, pkg: &PackageId) -> Result { // 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 @@ -101,7 +134,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); } diff --git a/crates/wit-component/tests/interfaces.rs b/crates/wit-component/tests/interfaces.rs index 55a4cadd88..ac301cf647 100644 --- a/crates/wit-component/tests/interfaces.rs +++ b/crates/wit-component/tests/interfaces.rs @@ -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(()) diff --git a/crates/wit-component/tests/interfaces/doc-comments.wat b/crates/wit-component/tests/interfaces/doc-comments.wat index 6d61c7f477..b043389556 100644 --- a/crates/wit-component/tests/interfaces/doc-comments.wat +++ b/crates/wit-component/tests/interfaces/doc-comments.wat @@ -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") ) diff --git a/crates/wit-component/tests/interfaces/multiple-packages.wat b/crates/wit-component/tests/interfaces/multiple-packages.wat new file mode 100644 index 0000000000..db2adf54a0 --- /dev/null +++ b/crates/wit-component/tests/interfaces/multiple-packages.wat @@ -0,0 +1,77 @@ +(component + (component $another:thing (;0;) + (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)) + (@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}}}}}}}") + ) + (component $third:pkg (;1;) + (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)) + (@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}}}}}") + ) + (component $foo:bar (;2;) + (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)) + (@custom "package-docs" "\00{}") + ) + (component $fourth:thing (;3;) + (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)) + (@custom "package-docs" "\00{}") + ) + (@producers + (processed-by "wit-component" "$CARGO_PKG_VERSION") + ) +) diff --git a/crates/wit-component/tests/interfaces/multiple-packages.wit b/crates/wit-component/tests/interfaces/multiple-packages.wit new file mode 100644 index 0000000000..d592f93e55 --- /dev/null +++ b/crates/wit-component/tests/interfaces/multiple-packages.wit @@ -0,0 +1,34 @@ +package foo:bar { + world this-world { + import another:thing/something; + import third:pkg/things; + } +} + +package another:thing { + // documenting an interface + interface something { + @since(version = 1.2.3) + record my-record { + foo: string + } + } +} + +package third:pkg { + @since(version = 1.2.3) + interface things { + // documenting an type + record other-record { + foo: string + } + } +} + +package fourth:thing { + interface boo { + record foo { + some: string + } + } +} \ No newline at end of file diff --git a/crates/wit-component/tests/interfaces/multiple-packages.wit.print b/crates/wit-component/tests/interfaces/multiple-packages.wit.print new file mode 100644 index 0000000000..f825e71fbf --- /dev/null +++ b/crates/wit-component/tests/interfaces/multiple-packages.wit.print @@ -0,0 +1,40 @@ +package another:thing { + /// documenting an interface + interface something { + @since(version = 1.2.3) + record my-record { + foo: string, + } + } + +} + + +package third:pkg { + @since(version = 1.2.3) + interface things { + /// documenting an type + record other-record { + foo: string, + } + } + +} + + +package foo:bar { + world this-world { + import another:thing/something; + import third:pkg/things; + } +} + + +package fourth:thing { + interface boo { + record foo { + some: string, + } + } + +} diff --git a/crates/wit-component/tests/interfaces/wasi-http.wat b/crates/wit-component/tests/interfaces/wasi-http.wat index e75781c785..43e610f369 100644 --- a/crates/wit-component/tests/interfaces/wasi-http.wat +++ b/crates/wit-component/tests/interfaces/wasi-http.wat @@ -874,7 +874,7 @@ ) ) (export (;7;) "proxy" (type 6)) - (@custom "package-docs" "\00{\22worlds\22:{\22proxy\22:{\22docs\22:\22The `wasi:http/proxy` world captures a widely-implementable intersection of\5cnhosts that includes HTTP forward and reverse proxies. Components targeting\5cnthis world may concurrently stream in and out any number of incoming and\5cnoutgoing HTTP requests.\22}},\22interfaces\22:{\22types\22:{\22docs\22:\22This interface defines all of the types and methods for implementing\5cnHTTP Requests and Responses, both incoming and outgoing, as well as\5cntheir headers, trailers, and bodies.\22,\22funcs\22:{\22http-error-code\22:\22Attempts to extract a http-related `error` from the wasi:io `error`\5cnprovided.\5cn\5cnStream operations which return\5cn`wasi:io/stream/stream-error::last-operation-failed` have a payload of\5cntype `wasi:io/error/error` with more information about the operation\5cnthat failed. This payload can be passed through to this function to see\5cnif there's http-related information about the error to return.\5cn\5cnNote that this function is fallible because not all io-errors are\5cnhttp-related errors.\22,\22[constructor]fields\22:\22Construct an empty HTTP Fields.\5cn\5cnThe resulting `fields` is mutable.\22,\22[static]fields.from-list\22:\22Construct an HTTP Fields.\5cn\5cnThe resulting `fields` is mutable.\5cn\5cnThe list represents each key-value pair in the Fields. Keys\5cnwhich have multiple values are represented by multiple entries in this\5cnlist with the same key.\5cn\5cnThe tuple is a pair of the field key, represented as a string, and\5cnValue, represented as a list of bytes. In a valid Fields, all keys\5cnand values are valid UTF-8 strings. However, values are not always\5cnwell-formed, so they are represented as a raw list of bytes.\5cn\5cnAn error result will be returned if any header or value was\5cnsyntactically invalid, or if a header was forbidden.\22,\22[method]fields.get\22:\22Get all of the values corresponding to a key. If the key is not present\5cnin this `fields`, an empty list is returned. However, if the key is\5cnpresent but empty, this is represented by a list with one or more\5cnempty field-values present.\22,\22[method]fields.has\22:\22Returns `true` when the key is present in this `fields`. If the key is\5cnsyntactically invalid, `false` is returned.\22,\22[method]fields.set\22:\22Set all of the values for a key. Clears any existing values for that\5cnkey, if they have been set.\5cn\5cnFails with `header-error.immutable` if the `fields` are immutable.\22,\22[method]fields.delete\22:\22Delete all values for a key. Does nothing if no values for the key\5cnexist.\5cn\5cnFails with `header-error.immutable` if the `fields` are immutable.\22,\22[method]fields.append\22:\22Append a value for a key. Does not change or delete any existing\5cnvalues for that key.\5cn\5cnFails with `header-error.immutable` if the `fields` are immutable.\22,\22[method]fields.entries\22:\22Retrieve the full set of keys and values in the Fields. Like the\5cnconstructor, the list represents each key-value pair.\5cn\5cnThe outer list represents each key-value pair in the Fields. Keys\5cnwhich have multiple values are represented by multiple entries in this\5cnlist with the same key.\22,\22[method]fields.clone\22:\22Make a deep copy of the Fields. Equivelant in behavior to calling the\5cn`fields` constructor on the return value of `entries`. The resulting\5cn`fields` is mutable.\22,\22[method]incoming-request.method\22:\22Returns the method of the incoming request.\22,\22[method]incoming-request.path-with-query\22:\22Returns the path with query parameters from the request, as a string.\22,\22[method]incoming-request.scheme\22:\22Returns the protocol scheme from the request.\22,\22[method]incoming-request.authority\22:\22Returns the authority from the request, if it was present.\22,\22[method]incoming-request.headers\22:\22Get the `headers` associated with the request.\5cn\5cnThe returned `headers` resource is immutable: `set`, `append`, and\5cn`delete` operations will fail with `header-error.immutable`.\5cn\5cnThe `headers` returned are a child resource: it must be dropped before\5cnthe parent `incoming-request` is dropped. Dropping this\5cn`incoming-request` before all children are dropped will trap.\22,\22[method]incoming-request.consume\22:\22Gives the `incoming-body` associated with this request. Will only\5cnreturn success at most once, and subsequent calls will return error.\22,\22[constructor]outgoing-request\22:\22Construct a new `outgoing-request` with a default `method` of `GET`, and\5cn`none` values for `path-with-query`, `scheme`, and `authority`.\5cn\5cn* `headers` is the HTTP Headers for the Request.\5cn\5cnIt is possible to construct, or manipulate with the accessor functions\5cnbelow, an `outgoing-request` with an invalid combination of `scheme`\5cnand `authority`, or `headers` which are not permitted to be sent.\5cnIt is the obligation of the `outgoing-handler.handle` implementation\5cnto reject invalid constructions of `outgoing-request`.\22,\22[method]outgoing-request.body\22:\22Returns the resource corresponding to the outgoing Body for this\5cnRequest.\5cn\5cnReturns success on the first call: the `outgoing-body` resource for\5cnthis `outgoing-request` can be retrieved at most once. Subsequent\5cncalls will return error.\22,\22[method]outgoing-request.method\22:\22Get the Method for the Request.\22,\22[method]outgoing-request.set-method\22:\22Set the Method for the Request. Fails if the string present in a\5cn`method.other` argument is not a syntactically valid method.\22,\22[method]outgoing-request.path-with-query\22:\22Get the combination of the HTTP Path and Query for the Request.\5cnWhen `none`, this represents an empty Path and empty Query.\22,\22[method]outgoing-request.set-path-with-query\22:\22Set the combination of the HTTP Path and Query for the Request.\5cnWhen `none`, this represents an empty Path and empty Query. Fails is the\5cnstring given is not a syntactically valid path and query uri component.\22,\22[method]outgoing-request.scheme\22:\22Get the HTTP Related Scheme for the Request. When `none`, the\5cnimplementation may choose an appropriate default scheme.\22,\22[method]outgoing-request.set-scheme\22:\22Set the HTTP Related Scheme for the Request. When `none`, the\5cnimplementation may choose an appropriate default scheme. Fails if the\5cnstring given is not a syntactically valid uri scheme.\22,\22[method]outgoing-request.authority\22:\22Get the HTTP Authority for the Request. A value of `none` may be used\5cnwith Related Schemes which do not require an Authority. The HTTP and\5cnHTTPS schemes always require an authority.\22,\22[method]outgoing-request.set-authority\22:\22Set the HTTP Authority for the Request. A value of `none` may be used\5cnwith Related Schemes which do not require an Authority. The HTTP and\5cnHTTPS schemes always require an authority. Fails if the string given is\5cnnot a syntactically valid uri authority.\22,\22[method]outgoing-request.headers\22:\22Get the headers associated with the Request.\5cn\5cnThe returned `headers` resource is immutable: `set`, `append`, and\5cn`delete` operations will fail with `header-error.immutable`.\5cn\5cnThis headers resource is a child: it must be dropped before the parent\5cn`outgoing-request` is dropped, or its ownership is transfered to\5cnanother component by e.g. `outgoing-handler.handle`.\22,\22[constructor]request-options\22:\22Construct a default `request-options` value.\22,\22[method]request-options.connect-timeout\22:\22The timeout for the initial connect to the HTTP Server.\22,\22[method]request-options.set-connect-timeout\22:\22Set the timeout for the initial connect to the HTTP Server. An error\5cnreturn value indicates that this timeout is not supported.\22,\22[method]request-options.first-byte-timeout\22:\22The timeout for receiving the first byte of the Response body.\22,\22[method]request-options.set-first-byte-timeout\22:\22Set the timeout for receiving the first byte of the Response body. An\5cnerror return value indicates that this timeout is not supported.\22,\22[method]request-options.between-bytes-timeout\22:\22The timeout for receiving subsequent chunks of bytes in the Response\5cnbody stream.\22,\22[method]request-options.set-between-bytes-timeout\22:\22Set the timeout for receiving subsequent chunks of bytes in the Response\5cnbody stream. An error return value indicates that this timeout is not\5cnsupported.\22,\22[static]response-outparam.set\22:\22Set the value of the `response-outparam` to either send a response,\5cnor indicate an error.\5cn\5cnThis method consumes the `response-outparam` to ensure that it is\5cncalled at most once. If it is never called, the implementation\5cnwill respond with an error.\5cn\5cnThe user may provide an `error` to `response` to allow the\5cnimplementation determine how to respond with an HTTP error response.\22,\22[method]incoming-response.status\22:\22Returns the status code from the incoming response.\22,\22[method]incoming-response.headers\22:\22Returns the headers from the incoming response.\5cn\5cnThe returned `headers` resource is immutable: `set`, `append`, and\5cn`delete` operations will fail with `header-error.immutable`.\5cn\5cnThis headers resource is a child: it must be dropped before the parent\5cn`incoming-response` is dropped.\22,\22[method]incoming-response.consume\22:\22Returns the incoming body. May be called at most once. Returns error\5cnif called additional times.\22,\22[method]incoming-body.stream\22:\22Returns the contents of the body, as a stream of bytes.\5cn\5cnReturns success on first call: the stream representing the contents\5cncan be retrieved at most once. Subsequent calls will return error.\5cn\5cnThe returned `input-stream` resource is a child: it must be dropped\5cnbefore the parent `incoming-body` is dropped, or consumed by\5cn`incoming-body.finish`.\5cn\5cnThis invariant ensures that the implementation can determine whether\5cnthe user is consuming the contents of the body, waiting on the\5cn`future-trailers` to be ready, or neither. This allows for network\5cnbackpressure is to be applied when the user is consuming the body,\5cnand for that backpressure to not inhibit delivery of the trailers if\5cnthe user does not read the entire body.\22,\22[static]incoming-body.finish\22:\22Takes ownership of `incoming-body`, and returns a `future-trailers`.\5cnThis function will trap if the `input-stream` child is still alive.\22,\22[method]future-trailers.subscribe\22:\22Returns a pollable which becomes ready when either the trailers have\5cnbeen received, or an error has occured. When this pollable is ready,\5cnthe `get` method will return `some`.\22,\22[method]future-trailers.get\22:\22Returns the contents of the trailers, or an error which occured,\5cnonce the future is ready.\5cn\5cnThe outer `option` represents future readiness. Users can wait on this\5cn`option` to become `some` using the `subscribe` method.\5cn\5cnThe outer `result` is used to retrieve the trailers or error at most\5cnonce. It will be success on the first call in which the outer option\5cnis `some`, and error on subsequent calls.\5cn\5cnThe inner `result` represents that either the HTTP Request or Response\5cnbody, as well as any trailers, were received successfully, or that an\5cnerror occured receiving them. The optional `trailers` indicates whether\5cnor not trailers were present in the body.\5cn\5cnWhen some `trailers` are returned by this method, the `trailers`\5cnresource is immutable, and a child. Use of the `set`, `append`, or\5cn`delete` methods will return an error, and the resource must be\5cndropped before the parent `future-trailers` is dropped.\22,\22[constructor]outgoing-response\22:\22Construct an `outgoing-response`, with a default `status-code` of `200`.\5cnIf a different `status-code` is needed, it must be set via the\5cn`set-status-code` method.\5cn\5cn* `headers` is the HTTP Headers for the Response.\22,\22[method]outgoing-response.status-code\22:\22Get the HTTP Status Code for the Response.\22,\22[method]outgoing-response.set-status-code\22:\22Set the HTTP Status Code for the Response. Fails if the status-code\5cngiven is not a valid http status code.\22,\22[method]outgoing-response.headers\22:\22Get the headers associated with the Request.\5cn\5cnThe returned `headers` resource is immutable: `set`, `append`, and\5cn`delete` operations will fail with `header-error.immutable`.\5cn\5cnThis headers resource is a child: it must be dropped before the parent\5cn`outgoing-request` is dropped, or its ownership is transfered to\5cnanother component by e.g. `outgoing-handler.handle`.\22,\22[method]outgoing-response.body\22:\22Returns the resource corresponding to the outgoing Body for this Response.\5cn\5cnReturns success on the first call: the `outgoing-body` resource for\5cnthis `outgoing-response` can be retrieved at most once. Subsequent\5cncalls will return error.\22,\22[method]outgoing-body.write\22:\22Returns a stream for writing the body contents.\5cn\5cnThe returned `output-stream` is a child resource: it must be dropped\5cnbefore the parent `outgoing-body` resource is dropped (or finished),\5cnotherwise the `outgoing-body` drop or `finish` will trap.\5cn\5cnReturns success on the first call: the `output-stream` resource for\5cnthis `outgoing-body` may be retrieved at most once. Subsequent calls\5cnwill return error.\22,\22[static]outgoing-body.finish\22:\22Finalize an outgoing body, optionally providing trailers. This must be\5cncalled to signal that the response is complete. If the `outgoing-body`\5cnis dropped without calling `outgoing-body.finalize`, the implementation\5cnshould treat the body as corrupted.\5cn\5cnFails if the body's `outgoing-request` or `outgoing-response` was\5cnconstructed with a Content-Length header, and the contents written\5cnto the body (via `write`) does not match the value given in the\5cnContent-Length.\22,\22[method]future-incoming-response.subscribe\22:\22Returns a pollable which becomes ready when either the Response has\5cnbeen received, or an error has occured. When this pollable is ready,\5cnthe `get` method will return `some`.\22,\22[method]future-incoming-response.get\22:\22Returns the incoming HTTP Response, or an error, once one is ready.\5cn\5cnThe outer `option` represents future readiness. Users can wait on this\5cn`option` to become `some` using the `subscribe` method.\5cn\5cnThe outer `result` is used to retrieve the response or error at most\5cnonce. It will be success on the first call in which the outer option\5cnis `some`, and error on subsequent calls.\5cn\5cnThe inner `result` represents that either the incoming HTTP Response\5cnstatus and headers have recieved successfully, or that an error\5cnoccured. Errors may also occur while consuming the response body,\5cnbut those will be reported by the `incoming-body` and its\5cn`output-stream` child.\22},\22types\22:{\22method\22:{\22docs\22:\22This type corresponds to HTTP standard Methods.\22},\22scheme\22:{\22docs\22:\22This type corresponds to HTTP standard Related Schemes.\22},\22DNS-error-payload\22:{\22docs\22:\22Defines the case payload type for `DNS-error` above:\22},\22TLS-alert-received-payload\22:{\22docs\22:\22Defines the case payload type for `TLS-alert-received` above:\22},\22field-size-payload\22:{\22docs\22:\22Defines the case payload type for `HTTP-response-{header,trailer}-size` above:\22},\22error-code\22:{\22docs\22:\22These cases are inspired by the IANA HTTP Proxy Error Types:\5cnhttps://www.iana.org/assignments/http-proxy-status/http-proxy-status.xhtml#table-http-proxy-error-types\22,\22items\22:{\22internal-error\22:\22This is a catch-all error for anything that doesn't fit cleanly into a\5cnmore specific case. It also includes an optional string for an\5cnunstructured description of the error. Users should not depend on the\5cnstring for diagnosing errors, as it's not required to be consistent\5cnbetween implementations.\22}},\22header-error\22:{\22docs\22:\22This type enumerates the different kinds of errors that may occur when\5cnsetting or appending to a `fields` resource.\22,\22items\22:{\22invalid-syntax\22:\22This error indicates that a `field-key` or `field-value` was\5cnsyntactically invalid when used with an operation that sets headers in a\5cn`fields`.\22,\22forbidden\22:\22This error indicates that a forbidden `field-key` was used when trying\5cnto set a header in a `fields`.\22,\22immutable\22:\22This error indicates that the operation on the `fields` was not\5cnpermitted because the fields are immutable.\22}},\22field-key\22:{\22docs\22:\22Field keys are always strings.\22},\22field-value\22:{\22docs\22:\22Field values should always be ASCII strings. However, in\5cnreality, HTTP implementations often have to interpret malformed values,\5cnso they are provided as a list of bytes.\22},\22fields\22:{\22docs\22:\22This following block defines the `fields` resource which corresponds to\5cnHTTP standard Fields. Fields are a common representation used for both\5cnHeaders and Trailers.\5cn\5cnA `fields` may be mutable or immutable. A `fields` created using the\5cnconstructor, `from-list`, or `clone` will be mutable, but a `fields`\5cnresource given by other means (including, but not limited to,\5cn`incoming-request.headers`, `outgoing-request.headers`) might be be\5cnimmutable. In an immutable fields, the `set`, `append`, and `delete`\5cnoperations will fail with `header-error.immutable`.\22},\22headers\22:{\22docs\22:\22Headers is an alias for Fields.\22},\22trailers\22:{\22docs\22:\22Trailers is an alias for Fields.\22},\22incoming-request\22:{\22docs\22:\22Represents an incoming HTTP Request.\22},\22outgoing-request\22:{\22docs\22:\22Represents an outgoing HTTP Request.\22},\22request-options\22:{\22docs\22:\22Parameters for making an HTTP Request. Each of these parameters is\5cncurrently an optional timeout applicable to the transport layer of the\5cnHTTP protocol.\5cn\5cnThese timeouts are separate from any the user may use to bound a\5cnblocking call to `wasi:io/poll.poll`.\22},\22response-outparam\22:{\22docs\22:\22Represents the ability to send an HTTP Response.\5cn\5cnThis resource is used by the `wasi:http/incoming-handler` interface to\5cnallow a Response to be sent corresponding to the Request provided as the\5cnother argument to `incoming-handler.handle`.\22},\22status-code\22:{\22docs\22:\22This type corresponds to the HTTP standard Status Code.\22},\22incoming-response\22:{\22docs\22:\22Represents an incoming HTTP Response.\22},\22incoming-body\22:{\22docs\22:\22Represents an incoming HTTP Request or Response's Body.\5cn\5cnA body has both its contents - a stream of bytes - and a (possibly\5cnempty) set of trailers, indicating that the full contents of the\5cnbody have been received. This resource represents the contents as\5cnan `input-stream` and the delivery of trailers as a `future-trailers`,\5cnand ensures that the user of this interface may only be consuming either\5cnthe body contents or waiting on trailers at any given time.\22},\22future-trailers\22:{\22docs\22:\22Represents a future which may eventaully return trailers, or an error.\5cn\5cnIn the case that the incoming HTTP Request or Response did not have any\5cntrailers, this future will resolve to the empty set of trailers once the\5cncomplete Request or Response body has been received.\22},\22outgoing-response\22:{\22docs\22:\22Represents an outgoing HTTP Response.\22},\22outgoing-body\22:{\22docs\22:\22Represents an outgoing HTTP Request or Response's Body.\5cn\5cnA body has both its contents - a stream of bytes - and a (possibly\5cnempty) set of trailers, inducating the full contents of the body\5cnhave been sent. This resource represents the contents as an\5cn`output-stream` child resource, and the completion of the body (with\5cnoptional trailers) with a static function that consumes the\5cn`outgoing-body` resource, and ensures that the user of this interface\5cnmay not write to the body contents after the body has been finished.\5cn\5cnIf the user code drops this resource, as opposed to calling the static\5cnmethod `finish`, the implementation should treat the body as incomplete,\5cnand that an error has occured. The implementation should propogate this\5cnerror to the HTTP protocol by whatever means it has available,\5cnincluding: corrupting the body on the wire, aborting the associated\5cnRequest, or sending a late status code for the Response.\22},\22future-incoming-response\22:{\22docs\22:\22Represents a future which may eventaully return an incoming HTTP\5cnResponse, or an error.\5cn\5cnThis resource is returned by the `wasi:http/outgoing-handler` interface to\5cnprovide the HTTP Response corresponding to the sent Request.\22}}},\22incoming-handler\22:{\22docs\22:\22This interface defines a handler of incoming HTTP Requests. It should\5cnbe exported by components which can respond to HTTP Requests.\22,\22funcs\22:{\22handle\22:\22This function is invoked with an incoming HTTP Request, and a resource\5cn`response-outparam` which provides the capability to reply with an HTTP\5cnResponse. The response is sent by calling the `response-outparam.set`\5cnmethod, which allows execution to continue after the response has been\5cnsent. This enables both streaming to the response body, and performing other\5cnwork.\5cn\5cnThe implementor of this function must write a response to the\5cn`response-outparam` before returning, or else the caller will respond\5cnwith an error on its behalf.\22}},\22outgoing-handler\22:{\22docs\22:\22This interface defines a handler of outgoing HTTP Requests. It should be\5cnimported by components which wish to make HTTP Requests.\22,\22funcs\22:{\22handle\22:\22This function is invoked with an outgoing HTTP Request, and it returns\5cna resource `future-incoming-response` which represents an HTTP Response\5cnwhich may arrive in the future.\5cn\5cnThe `options` argument accepts optional parameters for the HTTP\5cnprotocol's transport layer.\5cn\5cnThis function may return an error if the `outgoing-request` is invalid\5cnor not allowed to be made. Otherwise, protocol errors are reported\5cnthrough the `future-incoming-response`.\22}}}}") + (@custom "package-docs" "\00{\22worlds\22:{\22proxy\22:{\22docs\22:\22The `wasi:http/proxy` world captures a widely-implementable intersection of\5cnhosts that includes HTTP forward and reverse proxies. Components targeting\5cnthis world may concurrently stream in and out any number of incoming and\5cnoutgoing HTTP requests.\22}},\22interfaces\22:{\22types\22:{\22docs\22:\22This interface defines all of the types and methods for implementing\5cnHTTP Requests and Responses, both incoming and outgoing, as well as\5cntheir headers, trailers, and bodies.\22,\22funcs\22:{\22[constructor]fields\22:\22Construct an empty HTTP Fields.\5cn\5cnThe resulting `fields` is mutable.\22,\22[constructor]outgoing-request\22:\22Construct a new `outgoing-request` with a default `method` of `GET`, and\5cn`none` values for `path-with-query`, `scheme`, and `authority`.\5cn\5cn* `headers` is the HTTP Headers for the Request.\5cn\5cnIt is possible to construct, or manipulate with the accessor functions\5cnbelow, an `outgoing-request` with an invalid combination of `scheme`\5cnand `authority`, or `headers` which are not permitted to be sent.\5cnIt is the obligation of the `outgoing-handler.handle` implementation\5cnto reject invalid constructions of `outgoing-request`.\22,\22[constructor]outgoing-response\22:\22Construct an `outgoing-response`, with a default `status-code` of `200`.\5cnIf a different `status-code` is needed, it must be set via the\5cn`set-status-code` method.\5cn\5cn* `headers` is the HTTP Headers for the Response.\22,\22[constructor]request-options\22:\22Construct a default `request-options` value.\22,\22[method]fields.append\22:\22Append a value for a key. Does not change or delete any existing\5cnvalues for that key.\5cn\5cnFails with `header-error.immutable` if the `fields` are immutable.\22,\22[method]fields.clone\22:\22Make a deep copy of the Fields. Equivelant in behavior to calling the\5cn`fields` constructor on the return value of `entries`. The resulting\5cn`fields` is mutable.\22,\22[method]fields.delete\22:\22Delete all values for a key. Does nothing if no values for the key\5cnexist.\5cn\5cnFails with `header-error.immutable` if the `fields` are immutable.\22,\22[method]fields.entries\22:\22Retrieve the full set of keys and values in the Fields. Like the\5cnconstructor, the list represents each key-value pair.\5cn\5cnThe outer list represents each key-value pair in the Fields. Keys\5cnwhich have multiple values are represented by multiple entries in this\5cnlist with the same key.\22,\22[method]fields.get\22:\22Get all of the values corresponding to a key. If the key is not present\5cnin this `fields`, an empty list is returned. However, if the key is\5cnpresent but empty, this is represented by a list with one or more\5cnempty field-values present.\22,\22[method]fields.has\22:\22Returns `true` when the key is present in this `fields`. If the key is\5cnsyntactically invalid, `false` is returned.\22,\22[method]fields.set\22:\22Set all of the values for a key. Clears any existing values for that\5cnkey, if they have been set.\5cn\5cnFails with `header-error.immutable` if the `fields` are immutable.\22,\22[method]future-incoming-response.get\22:\22Returns the incoming HTTP Response, or an error, once one is ready.\5cn\5cnThe outer `option` represents future readiness. Users can wait on this\5cn`option` to become `some` using the `subscribe` method.\5cn\5cnThe outer `result` is used to retrieve the response or error at most\5cnonce. It will be success on the first call in which the outer option\5cnis `some`, and error on subsequent calls.\5cn\5cnThe inner `result` represents that either the incoming HTTP Response\5cnstatus and headers have recieved successfully, or that an error\5cnoccured. Errors may also occur while consuming the response body,\5cnbut those will be reported by the `incoming-body` and its\5cn`output-stream` child.\22,\22[method]future-incoming-response.subscribe\22:\22Returns a pollable which becomes ready when either the Response has\5cnbeen received, or an error has occured. When this pollable is ready,\5cnthe `get` method will return `some`.\22,\22[method]future-trailers.get\22:\22Returns the contents of the trailers, or an error which occured,\5cnonce the future is ready.\5cn\5cnThe outer `option` represents future readiness. Users can wait on this\5cn`option` to become `some` using the `subscribe` method.\5cn\5cnThe outer `result` is used to retrieve the trailers or error at most\5cnonce. It will be success on the first call in which the outer option\5cnis `some`, and error on subsequent calls.\5cn\5cnThe inner `result` represents that either the HTTP Request or Response\5cnbody, as well as any trailers, were received successfully, or that an\5cnerror occured receiving them. The optional `trailers` indicates whether\5cnor not trailers were present in the body.\5cn\5cnWhen some `trailers` are returned by this method, the `trailers`\5cnresource is immutable, and a child. Use of the `set`, `append`, or\5cn`delete` methods will return an error, and the resource must be\5cndropped before the parent `future-trailers` is dropped.\22,\22[method]future-trailers.subscribe\22:\22Returns a pollable which becomes ready when either the trailers have\5cnbeen received, or an error has occured. When this pollable is ready,\5cnthe `get` method will return `some`.\22,\22[method]incoming-body.stream\22:\22Returns the contents of the body, as a stream of bytes.\5cn\5cnReturns success on first call: the stream representing the contents\5cncan be retrieved at most once. Subsequent calls will return error.\5cn\5cnThe returned `input-stream` resource is a child: it must be dropped\5cnbefore the parent `incoming-body` is dropped, or consumed by\5cn`incoming-body.finish`.\5cn\5cnThis invariant ensures that the implementation can determine whether\5cnthe user is consuming the contents of the body, waiting on the\5cn`future-trailers` to be ready, or neither. This allows for network\5cnbackpressure is to be applied when the user is consuming the body,\5cnand for that backpressure to not inhibit delivery of the trailers if\5cnthe user does not read the entire body.\22,\22[method]incoming-request.authority\22:\22Returns the authority from the request, if it was present.\22,\22[method]incoming-request.consume\22:\22Gives the `incoming-body` associated with this request. Will only\5cnreturn success at most once, and subsequent calls will return error.\22,\22[method]incoming-request.headers\22:\22Get the `headers` associated with the request.\5cn\5cnThe returned `headers` resource is immutable: `set`, `append`, and\5cn`delete` operations will fail with `header-error.immutable`.\5cn\5cnThe `headers` returned are a child resource: it must be dropped before\5cnthe parent `incoming-request` is dropped. Dropping this\5cn`incoming-request` before all children are dropped will trap.\22,\22[method]incoming-request.method\22:\22Returns the method of the incoming request.\22,\22[method]incoming-request.path-with-query\22:\22Returns the path with query parameters from the request, as a string.\22,\22[method]incoming-request.scheme\22:\22Returns the protocol scheme from the request.\22,\22[method]incoming-response.consume\22:\22Returns the incoming body. May be called at most once. Returns error\5cnif called additional times.\22,\22[method]incoming-response.headers\22:\22Returns the headers from the incoming response.\5cn\5cnThe returned `headers` resource is immutable: `set`, `append`, and\5cn`delete` operations will fail with `header-error.immutable`.\5cn\5cnThis headers resource is a child: it must be dropped before the parent\5cn`incoming-response` is dropped.\22,\22[method]incoming-response.status\22:\22Returns the status code from the incoming response.\22,\22[method]outgoing-body.write\22:\22Returns a stream for writing the body contents.\5cn\5cnThe returned `output-stream` is a child resource: it must be dropped\5cnbefore the parent `outgoing-body` resource is dropped (or finished),\5cnotherwise the `outgoing-body` drop or `finish` will trap.\5cn\5cnReturns success on the first call: the `output-stream` resource for\5cnthis `outgoing-body` may be retrieved at most once. Subsequent calls\5cnwill return error.\22,\22[method]outgoing-request.authority\22:\22Get the HTTP Authority for the Request. A value of `none` may be used\5cnwith Related Schemes which do not require an Authority. The HTTP and\5cnHTTPS schemes always require an authority.\22,\22[method]outgoing-request.body\22:\22Returns the resource corresponding to the outgoing Body for this\5cnRequest.\5cn\5cnReturns success on the first call: the `outgoing-body` resource for\5cnthis `outgoing-request` can be retrieved at most once. Subsequent\5cncalls will return error.\22,\22[method]outgoing-request.headers\22:\22Get the headers associated with the Request.\5cn\5cnThe returned `headers` resource is immutable: `set`, `append`, and\5cn`delete` operations will fail with `header-error.immutable`.\5cn\5cnThis headers resource is a child: it must be dropped before the parent\5cn`outgoing-request` is dropped, or its ownership is transfered to\5cnanother component by e.g. `outgoing-handler.handle`.\22,\22[method]outgoing-request.method\22:\22Get the Method for the Request.\22,\22[method]outgoing-request.path-with-query\22:\22Get the combination of the HTTP Path and Query for the Request.\5cnWhen `none`, this represents an empty Path and empty Query.\22,\22[method]outgoing-request.scheme\22:\22Get the HTTP Related Scheme for the Request. When `none`, the\5cnimplementation may choose an appropriate default scheme.\22,\22[method]outgoing-request.set-authority\22:\22Set the HTTP Authority for the Request. A value of `none` may be used\5cnwith Related Schemes which do not require an Authority. The HTTP and\5cnHTTPS schemes always require an authority. Fails if the string given is\5cnnot a syntactically valid uri authority.\22,\22[method]outgoing-request.set-method\22:\22Set the Method for the Request. Fails if the string present in a\5cn`method.other` argument is not a syntactically valid method.\22,\22[method]outgoing-request.set-path-with-query\22:\22Set the combination of the HTTP Path and Query for the Request.\5cnWhen `none`, this represents an empty Path and empty Query. Fails is the\5cnstring given is not a syntactically valid path and query uri component.\22,\22[method]outgoing-request.set-scheme\22:\22Set the HTTP Related Scheme for the Request. When `none`, the\5cnimplementation may choose an appropriate default scheme. Fails if the\5cnstring given is not a syntactically valid uri scheme.\22,\22[method]outgoing-response.body\22:\22Returns the resource corresponding to the outgoing Body for this Response.\5cn\5cnReturns success on the first call: the `outgoing-body` resource for\5cnthis `outgoing-response` can be retrieved at most once. Subsequent\5cncalls will return error.\22,\22[method]outgoing-response.headers\22:\22Get the headers associated with the Request.\5cn\5cnThe returned `headers` resource is immutable: `set`, `append`, and\5cn`delete` operations will fail with `header-error.immutable`.\5cn\5cnThis headers resource is a child: it must be dropped before the parent\5cn`outgoing-request` is dropped, or its ownership is transfered to\5cnanother component by e.g. `outgoing-handler.handle`.\22,\22[method]outgoing-response.set-status-code\22:\22Set the HTTP Status Code for the Response. Fails if the status-code\5cngiven is not a valid http status code.\22,\22[method]outgoing-response.status-code\22:\22Get the HTTP Status Code for the Response.\22,\22[method]request-options.between-bytes-timeout\22:\22The timeout for receiving subsequent chunks of bytes in the Response\5cnbody stream.\22,\22[method]request-options.connect-timeout\22:\22The timeout for the initial connect to the HTTP Server.\22,\22[method]request-options.first-byte-timeout\22:\22The timeout for receiving the first byte of the Response body.\22,\22[method]request-options.set-between-bytes-timeout\22:\22Set the timeout for receiving subsequent chunks of bytes in the Response\5cnbody stream. An error return value indicates that this timeout is not\5cnsupported.\22,\22[method]request-options.set-connect-timeout\22:\22Set the timeout for the initial connect to the HTTP Server. An error\5cnreturn value indicates that this timeout is not supported.\22,\22[method]request-options.set-first-byte-timeout\22:\22Set the timeout for receiving the first byte of the Response body. An\5cnerror return value indicates that this timeout is not supported.\22,\22[static]fields.from-list\22:\22Construct an HTTP Fields.\5cn\5cnThe resulting `fields` is mutable.\5cn\5cnThe list represents each key-value pair in the Fields. Keys\5cnwhich have multiple values are represented by multiple entries in this\5cnlist with the same key.\5cn\5cnThe tuple is a pair of the field key, represented as a string, and\5cnValue, represented as a list of bytes. In a valid Fields, all keys\5cnand values are valid UTF-8 strings. However, values are not always\5cnwell-formed, so they are represented as a raw list of bytes.\5cn\5cnAn error result will be returned if any header or value was\5cnsyntactically invalid, or if a header was forbidden.\22,\22[static]incoming-body.finish\22:\22Takes ownership of `incoming-body`, and returns a `future-trailers`.\5cnThis function will trap if the `input-stream` child is still alive.\22,\22[static]outgoing-body.finish\22:\22Finalize an outgoing body, optionally providing trailers. This must be\5cncalled to signal that the response is complete. If the `outgoing-body`\5cnis dropped without calling `outgoing-body.finalize`, the implementation\5cnshould treat the body as corrupted.\5cn\5cnFails if the body's `outgoing-request` or `outgoing-response` was\5cnconstructed with a Content-Length header, and the contents written\5cnto the body (via `write`) does not match the value given in the\5cnContent-Length.\22,\22[static]response-outparam.set\22:\22Set the value of the `response-outparam` to either send a response,\5cnor indicate an error.\5cn\5cnThis method consumes the `response-outparam` to ensure that it is\5cncalled at most once. If it is never called, the implementation\5cnwill respond with an error.\5cn\5cnThe user may provide an `error` to `response` to allow the\5cnimplementation determine how to respond with an HTTP error response.\22,\22http-error-code\22:\22Attempts to extract a http-related `error` from the wasi:io `error`\5cnprovided.\5cn\5cnStream operations which return\5cn`wasi:io/stream/stream-error::last-operation-failed` have a payload of\5cntype `wasi:io/error/error` with more information about the operation\5cnthat failed. This payload can be passed through to this function to see\5cnif there's http-related information about the error to return.\5cn\5cnNote that this function is fallible because not all io-errors are\5cnhttp-related errors.\22},\22types\22:{\22method\22:{\22docs\22:\22This type corresponds to HTTP standard Methods.\22},\22scheme\22:{\22docs\22:\22This type corresponds to HTTP standard Related Schemes.\22},\22DNS-error-payload\22:{\22docs\22:\22Defines the case payload type for `DNS-error` above:\22},\22TLS-alert-received-payload\22:{\22docs\22:\22Defines the case payload type for `TLS-alert-received` above:\22},\22field-size-payload\22:{\22docs\22:\22Defines the case payload type for `HTTP-response-{header,trailer}-size` above:\22},\22error-code\22:{\22docs\22:\22These cases are inspired by the IANA HTTP Proxy Error Types:\5cnhttps://www.iana.org/assignments/http-proxy-status/http-proxy-status.xhtml#table-http-proxy-error-types\22,\22items\22:{\22internal-error\22:\22This is a catch-all error for anything that doesn't fit cleanly into a\5cnmore specific case. It also includes an optional string for an\5cnunstructured description of the error. Users should not depend on the\5cnstring for diagnosing errors, as it's not required to be consistent\5cnbetween implementations.\22}},\22header-error\22:{\22docs\22:\22This type enumerates the different kinds of errors that may occur when\5cnsetting or appending to a `fields` resource.\22,\22items\22:{\22invalid-syntax\22:\22This error indicates that a `field-key` or `field-value` was\5cnsyntactically invalid when used with an operation that sets headers in a\5cn`fields`.\22,\22forbidden\22:\22This error indicates that a forbidden `field-key` was used when trying\5cnto set a header in a `fields`.\22,\22immutable\22:\22This error indicates that the operation on the `fields` was not\5cnpermitted because the fields are immutable.\22}},\22field-key\22:{\22docs\22:\22Field keys are always strings.\22},\22field-value\22:{\22docs\22:\22Field values should always be ASCII strings. However, in\5cnreality, HTTP implementations often have to interpret malformed values,\5cnso they are provided as a list of bytes.\22},\22fields\22:{\22docs\22:\22This following block defines the `fields` resource which corresponds to\5cnHTTP standard Fields. Fields are a common representation used for both\5cnHeaders and Trailers.\5cn\5cnA `fields` may be mutable or immutable. A `fields` created using the\5cnconstructor, `from-list`, or `clone` will be mutable, but a `fields`\5cnresource given by other means (including, but not limited to,\5cn`incoming-request.headers`, `outgoing-request.headers`) might be be\5cnimmutable. In an immutable fields, the `set`, `append`, and `delete`\5cnoperations will fail with `header-error.immutable`.\22},\22headers\22:{\22docs\22:\22Headers is an alias for Fields.\22},\22trailers\22:{\22docs\22:\22Trailers is an alias for Fields.\22},\22incoming-request\22:{\22docs\22:\22Represents an incoming HTTP Request.\22},\22outgoing-request\22:{\22docs\22:\22Represents an outgoing HTTP Request.\22},\22request-options\22:{\22docs\22:\22Parameters for making an HTTP Request. Each of these parameters is\5cncurrently an optional timeout applicable to the transport layer of the\5cnHTTP protocol.\5cn\5cnThese timeouts are separate from any the user may use to bound a\5cnblocking call to `wasi:io/poll.poll`.\22},\22response-outparam\22:{\22docs\22:\22Represents the ability to send an HTTP Response.\5cn\5cnThis resource is used by the `wasi:http/incoming-handler` interface to\5cnallow a Response to be sent corresponding to the Request provided as the\5cnother argument to `incoming-handler.handle`.\22},\22status-code\22:{\22docs\22:\22This type corresponds to the HTTP standard Status Code.\22},\22incoming-response\22:{\22docs\22:\22Represents an incoming HTTP Response.\22},\22incoming-body\22:{\22docs\22:\22Represents an incoming HTTP Request or Response's Body.\5cn\5cnA body has both its contents - a stream of bytes - and a (possibly\5cnempty) set of trailers, indicating that the full contents of the\5cnbody have been received. This resource represents the contents as\5cnan `input-stream` and the delivery of trailers as a `future-trailers`,\5cnand ensures that the user of this interface may only be consuming either\5cnthe body contents or waiting on trailers at any given time.\22},\22future-trailers\22:{\22docs\22:\22Represents a future which may eventaully return trailers, or an error.\5cn\5cnIn the case that the incoming HTTP Request or Response did not have any\5cntrailers, this future will resolve to the empty set of trailers once the\5cncomplete Request or Response body has been received.\22},\22outgoing-response\22:{\22docs\22:\22Represents an outgoing HTTP Response.\22},\22outgoing-body\22:{\22docs\22:\22Represents an outgoing HTTP Request or Response's Body.\5cn\5cnA body has both its contents - a stream of bytes - and a (possibly\5cnempty) set of trailers, inducating the full contents of the body\5cnhave been sent. This resource represents the contents as an\5cn`output-stream` child resource, and the completion of the body (with\5cnoptional trailers) with a static function that consumes the\5cn`outgoing-body` resource, and ensures that the user of this interface\5cnmay not write to the body contents after the body has been finished.\5cn\5cnIf the user code drops this resource, as opposed to calling the static\5cnmethod `finish`, the implementation should treat the body as incomplete,\5cnand that an error has occured. The implementation should propogate this\5cnerror to the HTTP protocol by whatever means it has available,\5cnincluding: corrupting the body on the wire, aborting the associated\5cnRequest, or sending a late status code for the Response.\22},\22future-incoming-response\22:{\22docs\22:\22Represents a future which may eventaully return an incoming HTTP\5cnResponse, or an error.\5cn\5cnThis resource is returned by the `wasi:http/outgoing-handler` interface to\5cnprovide the HTTP Response corresponding to the sent Request.\22}}},\22incoming-handler\22:{\22docs\22:\22This interface defines a handler of incoming HTTP Requests. It should\5cnbe exported by components which can respond to HTTP Requests.\22,\22funcs\22:{\22handle\22:\22This function is invoked with an incoming HTTP Request, and a resource\5cn`response-outparam` which provides the capability to reply with an HTTP\5cnResponse. The response is sent by calling the `response-outparam.set`\5cnmethod, which allows execution to continue after the response has been\5cnsent. This enables both streaming to the response body, and performing other\5cnwork.\5cn\5cnThe implementor of this function must write a response to the\5cn`response-outparam` before returning, or else the caller will respond\5cnwith an error on its behalf.\22}},\22outgoing-handler\22:{\22docs\22:\22This interface defines a handler of outgoing HTTP Requests. It should be\5cnimported by components which wish to make HTTP Requests.\22,\22funcs\22:{\22handle\22:\22This function is invoked with an outgoing HTTP Request, and it returns\5cna resource `future-incoming-response` which represents an HTTP Response\5cnwhich may arrive in the future.\5cn\5cnThe `options` argument accepts optional parameters for the HTTP\5cnprotocol's transport layer.\5cn\5cnThis function may return an error if the `outgoing-request` is invalid\5cnor not allowed to be made. Otherwise, protocol errors are reported\5cnthrough the `future-incoming-response`.\22}}}}") (@producers (processed-by "wit-component" "$CARGO_PKG_VERSION") ) diff --git a/crates/wit-parser/src/decoding.rs b/crates/wit-parser/src/decoding.rs index 1c61ecfd48..5344fb196f 100644 --- a/crates/wit-parser/src/decoding.rs +++ b/crates/wit-parser/src/decoding.rs @@ -5,6 +5,12 @@ use std::mem; use std::slice; use std::{collections::HashMap, io::Read}; use wasmparser::Chunk; +#[cfg(feature = "serde")] +use wasmparser::ComponentNameSectionReader; +#[cfg(feature = "serde")] +use wasmparser::KnownCustom; +#[cfg(feature = "serde")] +use wasmparser::NameMap; use wasmparser::{ names::{ComponentName, ComponentNameKind}, types, @@ -14,12 +20,17 @@ use wasmparser::{ }; /// Represents information about a decoded WebAssembly component. +#[derive(Default)] struct ComponentInfo { + /// Package name + name: Option, /// Wasmparser-defined type information learned after a component is fully /// validated. - types: types::Types, + types: Option, /// List of all imports and exports from this component. externs: Vec<(String, Extern)>, + /// Packages + explicit: Vec, /// Decoded package metadata package_metadata: Option, } @@ -41,10 +52,45 @@ enum WitEncodingVersion { V2, } +#[cfg(feature = "serde")] +fn register_names( + names: ComponentNameSectionReader, + explicit: &mut ComponentInfo, +) -> Result> { + let mut packages = Vec::new(); + for section in names { + match section? { + wasmparser::ComponentName::Components(s) => { + let map: NameMap = s.into(); + for item in map { + let name = item?; + let mut parts = name.name.split(":"); + let namespace = parts.next().expect("expected identifier"); + let id = parts.next(); + if let Some(id) = id { + let pkg_name = PackageName { + namespace: namespace.to_string(), + name: id.to_string(), + version: None, + }; + explicit.name = Some(pkg_name.clone()); + packages.push(pkg_name); + } + } + } + _ => {} + } + } + Ok(packages) +} + impl ComponentInfo { /// Creates a new component info by parsing the given WebAssembly component bytes. fn from_reader(mut reader: impl Read) -> Result { + let mut explicit = Vec::new(); + let mut cur_package = ComponentInfo::default(); + let mut is_implicit = true; let mut validator = Validator::new_with_features(WasmFeatures::all()); let mut externs = Vec::new(); let mut depth = 1; @@ -81,45 +127,82 @@ impl ComponentInfo { ValidPayload::Parser(_) => depth += 1, ValidPayload::End(t) => { depth -= 1; - if depth == 0 { + if depth == 1 { + is_implicit = true; + cur_package.types = Some(t); + explicit.push(mem::take(&mut cur_package)); + } else if depth == 0 { types = Some(t); + } else { + explicit.push(mem::take(&mut cur_package)); } } ValidPayload::Func(..) => {} } match payload { - Payload::ComponentImportSection(s) if depth == 1 => { + Payload::ComponentImportSection(s) => { for import in s { let import = import?; - externs.push(( - import.name.0.to_string(), - Extern::Import(import.name.0.to_string()), - )); + if is_implicit { + externs.push(( + import.name.0.to_string(), + Extern::Import(import.name.0.to_string()), + )); + } else { + cur_package.externs.push(( + import.name.0.to_string(), + Extern::Import(import.name.0.to_string()), + )); + } } } - Payload::ComponentExportSection(s) if depth == 1 => { + Payload::ComponentExportSection(s) => { for export in s { let export = export?; - externs.push(( - export.name.0.to_string(), - Extern::Export(DecodingExport { - name: export.name.0.to_string(), - kind: export.kind, - index: export.index, - }), - )); + if is_implicit { + externs.push(( + export.name.0.to_string(), + Extern::Export(DecodingExport { + name: export.name.0.to_string(), + kind: export.kind, + index: export.index, + }), + )); + } else { + cur_package.externs.push(( + export.name.0.to_string(), + Extern::Export(DecodingExport { + name: export.name.0.to_string(), + kind: export.kind, + index: export.index, + }), + )); + } } } #[cfg(feature = "serde")] - Payload::CustomSection(s) if s.name() == PackageMetadata::SECTION_NAME => { - if _package_metadata.is_some() { - bail!("multiple {:?} sections", PackageMetadata::SECTION_NAME); + Payload::CustomSection(s) => { + if s.name() == PackageMetadata::SECTION_NAME { + _package_metadata = Some(PackageMetadata::decode(s.data())?); + cur_package.package_metadata = _package_metadata.clone(); + } else if s.name() == "component-name" { + if let KnownCustom::ComponentName(reader) = s.as_known() { + let _packages = register_names(reader, &mut cur_package)?; + if _packages.len() == explicit.len() { + for (i, exp) in explicit.iter_mut().enumerate() { + exp.name = Some(_packages[i].clone()); + } + } + } else { + bail!("Expected component name section") + } } - _package_metadata = Some(PackageMetadata::decode(s.data())?); } Payload::ModuleSection { parser, .. } | Payload::ComponentSection { parser, .. } => { + is_implicit = false; + cur_package = ComponentInfo::default(); stack.push(cur.clone()); cur = parser.clone(); } @@ -139,13 +222,57 @@ impl ComponentInfo { } Ok(Self { - types: types.unwrap(), + name: None, + types, + explicit, externs, package_metadata: _package_metadata, }) } fn is_wit_package(&self) -> Option { + // Check if each explicitly defined package is wit + if !self.explicit.is_empty() { + // all wit package exports must be component types, and there must be at + // least one + for expl in self.explicit.iter() { + if expl.externs.is_empty() { + return None; + } + if !expl.externs.iter().all(|(_, item)| { + let export = match item { + Extern::Export(e) => e, + _ => return false, + }; + match export.kind { + ComponentExternalKind::Type => matches!( + expl.types + .as_ref() + .unwrap() + .component_any_type_at(export.index), + types::ComponentAnyTypeId::Component(_) + ), + _ => false, + } + }) { + return None; + } + } + // If all packages are explicit, root package will have no extern exports + if self.externs.len() == 0 { + return Some(WitEncodingVersion::V2); + } + // // The distinction between v1 and v2 encoding formats is the structure of the export + // // strings for each component. The v1 format uses ":/wit" as the name + // // for the top-level exports, while the v2 format uses the unqualified name of the encoded + // // entity. + return match ComponentName::new(&self.externs[0].0, 0).ok()?.kind() { + ComponentNameKind::Interface(name) if name.interface().as_str() == "wit" => { + Some(WitEncodingVersion::V1) + } + _ => Some(WitEncodingVersion::V2), + }; + } // all wit package exports must be component types, and there must be at // least one if self.externs.is_empty() { @@ -159,7 +286,10 @@ impl ComponentInfo { }; match export.kind { ComponentExternalKind::Type => matches!( - self.types.component_any_type_at(export.index), + self.types + .as_ref() + .unwrap() + .component_any_type_at(export.index), types::ComponentAnyTypeId::Component(_) ), _ => false, @@ -176,13 +306,12 @@ impl ComponentInfo { ComponentNameKind::Interface(name) if name.interface().as_str() == "wit" => { Some(WitEncodingVersion::V1) } - ComponentNameKind::Label(_) => Some(WitEncodingVersion::V2), - _ => None, + _ => Some(WitEncodingVersion::V2), } } fn decode_wit_v1_package(&self) -> Result<(Resolve, PackageId)> { - let mut decoder = WitPackageDecoder::new(&self.types); + let mut decoder = WitPackageDecoder::new(&self.types.as_ref().unwrap()); let mut pkg = None; for (name, item) in self.externs.iter() { @@ -190,8 +319,8 @@ impl ComponentInfo { Extern::Export(e) => e, _ => unreachable!(), }; - let id = self.types.component_type_at(export.index); - let ty = &self.types[id]; + let id = self.types.as_ref().unwrap().component_type_at(export.index); + let ty = &self.types.as_ref().unwrap()[id]; if pkg.is_some() { bail!("more than one top-level exported component type found"); } @@ -211,27 +340,21 @@ impl ComponentInfo { Ok((resolve, package)) } - fn decode_wit_v2_package(&self) -> Result<(Resolve, PackageId)> { - let mut decoder = WitPackageDecoder::new(&self.types); - - let mut pkg_name = None; - - let mut interfaces = IndexMap::new(); - let mut worlds = IndexMap::new(); - let mut fields = PackageFields { - interfaces: &mut interfaces, - worlds: &mut worlds, - }; - - for (_, item) in self.externs.iter() { + fn decode_externs( + info: &ComponentInfo, + decoder: &mut WitPackageDecoder, + pkg_name: &mut Option, + mut fields: &mut PackageFields, + ) -> Result<()> { + for (_, item) in info.externs.iter() { let export = match item { Extern::Export(e) => e, _ => unreachable!(), }; let index = export.index; - let id = self.types.component_type_at(index); - let component = &self.types[id]; + let id = info.types.as_ref().unwrap().component_type_at(index); + let component = &info.types.as_ref().unwrap()[id]; // The single export of this component will determine if it's a world or an interface: // worlds export a component, while interfaces export an instance. @@ -242,22 +365,25 @@ impl ComponentInfo { ); } - let name = component.exports.keys().nth(0).unwrap(); + let key_name = component.exports.keys().nth(0).unwrap(); - let name = match component.exports[name] { + let name = match component.exports[key_name] { types::ComponentEntityType::Component(ty) => { - let package_name = - decoder.decode_world(name.as_str(), &self.types[ty], &mut fields)?; - package_name + let package_name = decoder.decode_world( + key_name.as_str(), + &info.types.as_ref().unwrap()[ty], + &mut fields, + )?; + Some(package_name) } types::ComponentEntityType::Instance(ty) => { let package_name = decoder.decode_interface( - name.as_str(), + key_name.as_str(), &component.imports, - &self.types[ty], + &info.types.as_ref().unwrap()[ty], &mut fields, )?; - package_name + Some(package_name) } _ => unreachable!(), }; @@ -265,35 +391,106 @@ impl ComponentInfo { if let Some(pkg_name) = pkg_name.as_ref() { // TODO: when we have fully switched to the v2 format, we should switch to parsing // multiple wit documents instead of bailing. - if pkg_name != &name { + if *pkg_name != name.unwrap() { bail!("item defined with mismatched package name") } } else { - pkg_name.replace(name); + pkg_name.replace(name.unwrap()); } } + Ok(()) + } + + fn decode_wit_v2_packages(&self) -> Result<(Resolve, Vec)> { + let mut decoder = WitPackageDecoder::new(&self.types.as_ref().unwrap()); + + let mut pkg_name = None; - let pkg = if let Some(name) = pkg_name { - Package { - name, + let mut interfaces = IndexMap::new(); + let mut worlds = IndexMap::new(); + let mut fields = PackageFields { + interfaces: &mut interfaces, + worlds: &mut worlds, + }; + let mut implicit = None; + ComponentInfo::decode_externs(self, &mut decoder, &mut pkg_name, &mut fields)?; + + let mut resolve = if let Some(name) = &pkg_name { + let pkg = Package { + name: name.clone(), docs: Docs::default(), - interfaces, + interfaces: interfaces.clone(), worlds, + }; + let (mut resolve, package) = decoder.finish(pkg); + implicit = Some(package); + // For now this is a sufficient condition to know that we're working with + // an implicit package declaration. This will need to be reworked when + // mixed package declarations are supported + // if let Some(package) = implicit { + if let Some(package_metadata) = &self.package_metadata { + package_metadata.inject(&mut resolve, package)?; } + resolve } else { - bail!("no exported component type found"); + Resolve::new() }; - let (mut resolve, package) = decoder.finish(pkg); - if let Some(package_metadata) = &self.package_metadata { - package_metadata.inject(&mut resolve, package)?; + for explicit in &self.explicit { + let mut cur_decoder = WitPackageDecoder::new(explicit.types.as_ref().unwrap()); + let mut interfaces = IndexMap::new(); + let mut worlds = IndexMap::new(); + let mut fields = PackageFields { + interfaces: &mut interfaces, + worlds: &mut worlds, + }; + pkg_name = None; + ComponentInfo::decode_externs(explicit, &mut cur_decoder, &mut pkg_name, &mut fields)?; + let pkg = if let Some(name) = pkg_name { + Package { + name: name.clone(), + docs: Docs::default(), + interfaces, + worlds, + } + } else { + Package { + name: explicit.name.as_ref().unwrap().clone(), + docs: Docs::default(), + interfaces, + worlds, + } + }; + let (cur_resolve, _) = cur_decoder.finish(pkg); + resolve.merge(cur_resolve)?; } - Ok((resolve, package)) + + let package_names = resolve.package_names.clone(); + for package in &self.explicit { + if let Some(package_metadata) = &package.package_metadata { + let name = package.name.as_ref().unwrap(); + let pkg = package_names.get(&name.clone()); + if let Some(pkg) = pkg { + package_metadata.inject(&mut resolve, *pkg)?; + } + } + } + + let packages = if let Some(package) = implicit { + vec![package] + } else { + resolve + .package_names + .iter() + .map(|(_, v)| v.to_owned()) + .collect() + }; + Ok((resolve, packages)) } fn decode_component(&self) -> Result<(Resolve, WorldId)> { assert!(self.is_wit_package().is_none()); - let mut decoder = WitPackageDecoder::new(&self.types); + let mut decoder = WitPackageDecoder::new(&self.types.as_ref().unwrap()); // Note that this name is arbitrarily chosen. We may one day perhaps // want to encode this in the component binary format itself, but for // now it shouldn't be an issue to have a defaulted name here. @@ -390,8 +587,8 @@ pub fn decode_reader(reader: impl Read) -> Result { } WitEncodingVersion::V2 => { log::debug!("decoding a v2 WIT package encoded as wasm"); - let (resolve, pkg) = info.decode_wit_v2_package()?; - Ok(DecodedWasm::WitPackages(resolve, vec![pkg])) + let (resolve, pkgs) = info.decode_wit_v2_packages()?; + Ok(DecodedWasm::WitPackages(resolve, pkgs)) } } } else { diff --git a/crates/wit-parser/src/metadata.rs b/crates/wit-parser/src/metadata.rs index 103e3d03af..be49258e03 100644 --- a/crates/wit-parser/src/metadata.rs +++ b/crates/wit-parser/src/metadata.rs @@ -48,6 +48,7 @@ const TRY_TO_EMIT_V0_BY_DEFAULT: bool = true; /// Represents serializable doc comments parsed from a WIT package. #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(deny_unknown_fields))] +#[derive(Clone)] pub struct PackageMetadata { #[cfg_attr( feature = "serde", @@ -163,6 +164,7 @@ impl PackageMetadata { #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(deny_unknown_fields))] +#[derive(Clone)] struct WorldMetadata { #[cfg_attr( feature = "serde", @@ -500,6 +502,7 @@ impl WorldMetadata { #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(deny_unknown_fields))] +#[derive(Clone)] struct InterfaceMetadata { #[cfg_attr( feature = "serde", @@ -527,12 +530,13 @@ impl InterfaceMetadata { fn extract(resolve: &Resolve, id: InterfaceId) -> Self { let interface = &resolve.interfaces[id]; - let funcs = interface + let mut funcs: IndexMap = interface .functions .iter() .map(|(name, func)| (name.to_string(), FunctionMetadata::extract(func))) .filter(|(_, item)| !item.is_empty()) .collect(); + funcs.sort_keys(); let types = interface .types .iter() @@ -586,6 +590,7 @@ impl InterfaceMetadata { #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(untagged, deny_unknown_fields))] +#[derive(Clone)] enum FunctionMetadata { /// In the v0 format function metadata was only a string so this variant /// is preserved for the v0 format. In the future this can be removed @@ -656,6 +661,7 @@ impl FunctionMetadata { #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(deny_unknown_fields))] +#[derive(Clone)] struct TypeMetadata { #[cfg_attr( feature = "serde", diff --git a/crates/wit-parser/src/resolve.rs b/crates/wit-parser/src/resolve.rs index be539e9e54..a789c09d29 100644 --- a/crates/wit-parser/src/resolve.rs +++ b/crates/wit-parser/src/resolve.rs @@ -85,7 +85,7 @@ pub struct Resolve { /// A package is a collection of interfaces and worlds. Packages additionally /// have a unique identifier that affects generated components and uniquely /// identifiers this particular package. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(Serialize))] pub struct Package { /// A unique name corresponding to this package. @@ -377,7 +377,7 @@ impl Resolve { pub fn push_file(&mut self, path: impl AsRef) -> Result> { match self._push_file(path.as_ref())? { #[cfg(feature = "decoding")] - ParsedFile::Package(id) => Ok(vec![id]), + ParsedFile::Package(ids) => Ok(vec![ids]), ParsedFile::Unresolved(pkgs) => self.push_group(pkgs), } } @@ -1037,6 +1037,7 @@ impl Resolve { }, }; let pkg = &self.packages[pkg]; + pkg.worlds .get(&world_name) .copied() diff --git a/crates/wit-smith/src/lib.rs b/crates/wit-smith/src/lib.rs index 3a70a00f83..caaa54dc61 100644 --- a/crates/wit-smith/src/lib.rs +++ b/crates/wit-smith/src/lib.rs @@ -41,5 +41,5 @@ pub fn smith(config: &Config, u: &mut Unstructured<'_>) -> Result> { } let pkg = last.unwrap(); - Ok(wit_component::encode(Some(true), &resolve, pkg).expect("failed to encode WIT document")) + Ok(wit_component::encode(Some(true), &resolve, &[pkg]).expect("failed to encode WIT document")) } diff --git a/fuzz/src/roundtrip_wit.rs b/fuzz/src/roundtrip_wit.rs index 5cc6678265..3c0090e35b 100644 --- a/fuzz/src/roundtrip_wit.rs +++ b/fuzz/src/roundtrip_wit.rs @@ -28,8 +28,8 @@ pub fn run(u: &mut Unstructured<'_>) -> Result<()> { } let pkg2 = pkgs2[0]; - let wasm2 = - wit_component::encode(Some(true), &resolve2, pkg2).expect("failed to encode WIT document"); + let wasm2 = wit_component::encode(Some(true), &resolve2, &[pkg2]) + .expect("failed to encode WIT document"); write_file("doc2.wasm", &wasm2); roundtrip_through_printing("doc2", &resolve2, &wasm2); @@ -96,7 +96,7 @@ fn roundtrip_through_printing(file: &str, resolve: &Resolve, wasm: &[u8]) { // Finally encode the `new_resolve` which should be the exact same as // before. - let wasm2 = wit_component::encode(Some(true), &new_resolve, last.unwrap()).unwrap(); + let wasm2 = wit_component::encode(Some(true), &new_resolve, &[last.unwrap()]).unwrap(); write_file(&format!("{file}-reencoded.wasm"), &wasm2); if wasm != wasm2 { panic!("failed to roundtrip through text printing"); diff --git a/src/bin/wasm-tools/component.rs b/src/bin/wasm-tools/component.rs index adc24f30d2..598c4df897 100644 --- a/src/bin/wasm-tools/component.rs +++ b/src/bin/wasm-tools/component.rs @@ -596,12 +596,9 @@ impl WitOpts { fn emit_wasm(&self, decoded: &DecodedWasm) -> Result<()> { assert!(self.wasm || self.wat); assert!(self.out_dir.is_none()); - if decoded.packages().len() != 1 { - bail!("emitting WASM for multi-package WIT files is not yet supported") - } - let decoded_package = decoded.packages()[0]; - let bytes = wit_component::encode(None, decoded.resolve(), decoded_package)?; + let decoded_packages = decoded.packages(); + let bytes = wit_component::encode(None, decoded.resolve(), decoded_packages)?; if !self.skip_validation { wasmparser::Validator::new_with_features( WasmFeatures::default() | WasmFeatures::COMPONENT_MODEL,