Struct wit_parser::Resolve
source · pub struct Resolve {
pub worlds: Arena<World>,
pub interfaces: Arena<Interface>,
pub types: Arena<TypeDef>,
pub packages: Arena<Package>,
pub package_names: IndexMap<PackageName, PackageId>,
}Expand description
Representation of a fully resolved set of WIT packages.
This structure contains a graph of WIT packages and all of their contents
merged together into the contained arenas. All items are sorted
topologically and everything here is fully resolved, so with a Resolve no
name lookups are necessary and instead everything is index-based.
Working with a WIT package requires inserting it into a Resolve to ensure
that all of its dependencies are satisfied. This will give the full picture
of that package’s types and such.
Each item in a Resolve has a parent link to trace it back to the original
package as necessary.
Fields§
§worlds: Arena<World>All knowns worlds within this Resolve.
Each world points at a PackageId which is stored below. No ordering is
guaranteed between this list of worlds.
interfaces: Arena<Interface>All knowns interfaces within this Resolve.
Each interface points at a PackageId which is stored below. No
ordering is guaranteed between this list of interfaces.
types: Arena<TypeDef>All knowns types within this Resolve.
Types are topologically sorted such that any type referenced from one type is guaranteed to be defined previously. Otherwise though these are not sorted by interface for example.
packages: Arena<Package>All knowns packages within this Resolve.
This list of packages is not sorted. Sorted packages can be queried
through Resolve::topological_packages.
package_names: IndexMap<PackageName, PackageId>A map of package names to the ID of the package with that name.
Implementations§
source§impl Resolve
impl Resolve
sourcepub fn wasm_signature(
&self,
variant: AbiVariant,
func: &Function
) -> WasmSignature
pub fn wasm_signature( &self, variant: AbiVariant, func: &Function ) -> WasmSignature
Get the WebAssembly type signature for this interface function
The first entry returned is the list of parameters and the second entry is the list of results for the wasm function signature.
source§impl Resolve
impl Resolve
sourcepub fn push_path(
&mut self,
path: impl AsRef<Path>
) -> Result<(PackageId, Vec<PathBuf>)>
pub fn push_path( &mut self, path: impl AsRef<Path> ) -> Result<(PackageId, Vec<PathBuf>)>
Parse a WIT package from the input path.
The input path can be one of:
- A directory containing a WIT package with an optional
depsdirectory for any dependent WIT packages it references. - A single standalone WIT file depending on what’s already in
Resolve. - A wasm-encoded WIT package as a single file in the wasm binary format.
- A wasm-encoded WIT package as a single file in the wasm text format.
The PackageId of the parsed package is returned. For more information
see Resolve::push_dir and Resolve::push_file. This method will
automatically call the appropriate method based on what kind of
filesystem entry path is.
Returns the top-level PackageId as well as a list of all files read
during this parse.
sourcepub fn push_dir(&mut self, path: &Path) -> Result<(PackageId, Vec<PathBuf>)>
pub fn push_dir(&mut self, path: &Path) -> Result<(PackageId, Vec<PathBuf>)>
Parses the filesystem directory at path as a WIT package and returns
the fully resolved PackageId as a result.
The directory itself is parsed with UnresolvedPackage::parse_dir
which has more information on the layout of the directory. This method,
however, additionally supports an optional deps dir where dependencies
can be located.
All entries in the deps directory are inspected and parsed as follows:
- Any directories inside of
depsare assumed to be another WIT package and are parsed withUnresolvedPackage::parse_dir. - WIT files (
*.wit) are parsed withUnresolvedPackage::parse_file. - WebAssembly files (
*.wasmor*.wat) are assumed to be WIT packages encoded to wasm and are parsed and inserted intoself.
This function returns the PackageId of the root parsed package at
path, along with a list of all paths that were consumed during parsing
for the root package and all dependency packages.
sourcepub fn push_file(&mut self, path: impl AsRef<Path>) -> Result<PackageId>
pub fn push_file(&mut self, path: impl AsRef<Path>) -> Result<PackageId>
Parses the contents of path from the filesystem and pushes the result
into this Resolve.
The path referenced here can be one of:
- A WIT file. Note that in this case this single WIT file will be the
entire package and any dependencies it has must already be in
self. - A WIT package encoded as WebAssembly, either in text or binary form.
In this the package and all of its dependencies are automatically
inserted into
self.
In both situations the PackageId of the resulting resolved package is
returned from this method.
sourcepub fn push(&mut self, unresolved: UnresolvedPackage) -> Result<PackageId>
pub fn push(&mut self, unresolved: UnresolvedPackage) -> Result<PackageId>
Appends a new UnresolvedPackage to this Resolve, creating a
fully resolved package with no dangling references.
All the dependencies of unresolved must already have been loaded
within this Resolve via previous calls to push or other methods such
as Resolve::push_path.
Any dependency resolution error or otherwise world-elaboration error will be returned here. If successful a package identifier is returned which corresponds to the package that was just inserted.
pub fn all_bits_valid(&self, ty: &Type) -> bool
sourcepub fn merge(&mut self, resolve: Resolve) -> Result<Remap>
pub fn merge(&mut self, resolve: Resolve) -> Result<Remap>
Merges all the contents of a different Resolve into this one. The
Remap structure returned provides a mapping from all old indices to
new indices
This operation can fail if resolve disagrees with self about the
packages being inserted. Otherwise though this will additionally attempt
to “union” packages found in resolve with those found in self.
Unioning packages is keyed on the name/url of packages for those with
URLs present. If found then it’s assumed that both Resolve instances
were originally created from the same contents and are two views
of the same package.
sourcepub fn merge_worlds(&mut self, from: WorldId, into: WorldId) -> Result<()>
pub fn merge_worlds(&mut self, from: WorldId, into: WorldId) -> Result<()>
Merges the world from into the world into.
This will attempt to merge one world into another, unioning all of its
imports and exports together. This is an operation performed by
wit-component, for example where two different worlds from two
different libraries were linked into the same core wasm file and are
producing a singular world that will be the final component’s
interface.
This operation can fail if the imports/exports overlap.
sourcepub fn id_of(&self, interface: InterfaceId) -> Option<String>
pub fn id_of(&self, interface: InterfaceId) -> Option<String>
Returns the ID of the specified interface.
Returns None for unnamed interfaces.
sourcepub fn id_of_name(&self, pkg: PackageId, name: &str) -> String
pub fn id_of_name(&self, pkg: PackageId, name: &str) -> String
Returns the ID of the specified name within the pkg.
sourcepub fn select_world(
&self,
pkg: PackageId,
world: Option<&str>
) -> Result<WorldId>
pub fn select_world( &self, pkg: PackageId, world: Option<&str> ) -> Result<WorldId>
Attempts to locate a world given the “default” package pkg and the
optional string specifier world.
This method is intended to be used by bindings generation tools to
select a world from either pkg or a package in this Resolve.
If world is None then pkg must have precisely one world which will
be returned.
If world is Some then it can either be:
-
A kebab-name of a world contained within
pkgwhich is being selected, such as"the-world". -
An ID-based form of a world which is selected within this
Resolve, ignoringpkg. For example"wasi:http/proxy".
If successful the corresponding WorldId is returned, otherwise an
error is returned.
sourcepub fn name_world_key(&self, key: &WorldKey) -> String
pub fn name_world_key(&self, key: &WorldKey) -> String
Assigns a human readable name to the WorldKey specified.
sourcepub fn type_interface_dep(&self, id: TypeId) -> Option<InterfaceId>
pub fn type_interface_dep(&self, id: TypeId) -> Option<InterfaceId>
Returns the interface that id uses a type from, if it uses a type from
a different interface than id is defined within.
If id is not a use-of-a-type or it’s using a type in the same
interface then None is returned.
sourcepub fn interface_direct_deps(
&self,
id: InterfaceId
) -> impl Iterator<Item = InterfaceId> + '_
pub fn interface_direct_deps( &self, id: InterfaceId ) -> impl Iterator<Item = InterfaceId> + '_
Returns an iterator of all interfaces that the interface id depends
on.
Interfaces may depend on others for type information to resolve type imports.
Note that the returned iterator may yield the same interface as a
dependency multiple times. Additionally only direct dependencies of id
are yielded, not transitive dependencies.
sourcepub fn package_direct_deps(
&self,
id: PackageId
) -> impl Iterator<Item = PackageId> + '_
pub fn package_direct_deps( &self, id: PackageId ) -> impl Iterator<Item = PackageId> + '_
Returns an iterator of all packages that the package id depends
on.
Packages may depend on others for type information to resolve type imports or interfaces to resolve worlds.
Note that the returned iterator may yield the same package as a
dependency multiple times. Additionally only direct dependencies of id
are yielded, not transitive dependencies.
sourcepub fn topological_packages(&self) -> Vec<PackageId>
pub fn topological_packages(&self) -> Vec<PackageId>
Returns a topological ordering of packages contained in this Resolve.
This returns a list of PackageId such that when visited in order it’s
guaranteed that all dependencies will have been defined by prior items
in the list.