Skip to main content

wit_parser/
decoding.rs

1use crate::*;
2use alloc::string::{String, ToString};
3use alloc::vec;
4use alloc::vec::Vec;
5use anyhow::{Context, anyhow, bail};
6use core::mem;
7use std::io::Read;
8use wasmparser::Chunk;
9use wasmparser::{
10    ComponentExternalKind, Parser, Payload, PrimitiveValType, ValidPayload, Validator,
11    WasmFeatures,
12    component_types::{
13        ComponentAnyTypeId, ComponentDefinedType, ComponentEntityType, ComponentFuncType,
14        ComponentInstanceType, ComponentType, ComponentValType,
15    },
16    names::{ComponentName, ComponentNameKind},
17    types,
18    types::Types,
19};
20
21/// Represents information about a decoded WebAssembly component.
22struct ComponentInfo {
23    /// Wasmparser-defined type information learned after a component is fully
24    /// validated.
25    types: types::Types,
26    /// List of all imports and exports from this component.
27    externs: Vec<(String, Extern)>,
28    /// Decoded package metadata
29    package_metadata: Option<PackageMetadata>,
30}
31
32struct DecodingExport {
33    name: String,
34    kind: ComponentExternalKind,
35    index: u32,
36}
37
38enum Extern {
39    Import(String),
40    Export(DecodingExport),
41}
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44enum WitEncodingVersion {
45    V1,
46    V2,
47}
48
49impl ComponentInfo {
50    /// Creates a new component info by parsing the given WebAssembly component bytes.
51    fn from_reader(mut reader: impl Read) -> Result<Self> {
52        let mut validator = Validator::new_with_features(WasmFeatures::all());
53        let mut externs = Vec::new();
54        let mut depth = 1;
55        let mut types = None;
56        let mut _package_metadata = None;
57        let mut cur = Parser::new(0);
58        let mut eof = false;
59        let mut stack = Vec::new();
60        let mut buffer = Vec::new();
61
62        loop {
63            let chunk = cur.parse(&buffer, eof)?;
64            let (payload, consumed) = match chunk {
65                Chunk::NeedMoreData(hint) => {
66                    assert!(!eof); // otherwise an error would be returned
67
68                    // Use the hint to preallocate more space, then read
69                    // some more data into our buffer.
70                    //
71                    // Note that the buffer management here is not ideal,
72                    // but it's compact enough to fit in an example!
73                    let len = buffer.len();
74                    buffer.extend((0..hint).map(|_| 0u8));
75                    let n = reader.read(&mut buffer[len..])?;
76                    buffer.truncate(len + n);
77                    eof = n == 0;
78                    continue;
79                }
80
81                Chunk::Parsed { consumed, payload } => (payload, consumed),
82            };
83            match validator.payload(&payload)? {
84                ValidPayload::Ok => {}
85                ValidPayload::Parser(_) => depth += 1,
86                ValidPayload::End(t) => {
87                    depth -= 1;
88                    if depth == 0 {
89                        types = Some(t);
90                    }
91                }
92                ValidPayload::Func(..) => {}
93            }
94
95            match payload {
96                Payload::ComponentImportSection(s) if depth == 1 => {
97                    for import in s {
98                        let import = import?;
99                        externs.push((
100                            import.name.0.to_string(),
101                            Extern::Import(import.name.0.to_string()),
102                        ));
103                    }
104                }
105                Payload::ComponentExportSection(s) if depth == 1 => {
106                    for export in s {
107                        let export = export?;
108                        externs.push((
109                            export.name.0.to_string(),
110                            Extern::Export(DecodingExport {
111                                name: export.name.0.to_string(),
112                                kind: export.kind,
113                                index: export.index,
114                            }),
115                        ));
116                    }
117                }
118                #[cfg(feature = "serde")]
119                Payload::CustomSection(s) if s.name() == PackageMetadata::SECTION_NAME => {
120                    if _package_metadata.is_some() {
121                        bail!("multiple {:?} sections", PackageMetadata::SECTION_NAME);
122                    }
123                    _package_metadata = Some(PackageMetadata::decode(s.data())?);
124                }
125                Payload::ModuleSection { parser, .. }
126                | Payload::ComponentSection { parser, .. } => {
127                    stack.push(cur.clone());
128                    cur = parser.clone();
129                }
130                Payload::End(_) => {
131                    if let Some(parent_parser) = stack.pop() {
132                        cur = parent_parser.clone();
133                    } else {
134                        break;
135                    }
136                }
137                _ => {}
138            }
139
140            // once we're done processing the payload we can forget the
141            // original.
142            buffer.drain(..consumed);
143        }
144
145        Ok(Self {
146            types: types.unwrap(),
147            externs,
148            package_metadata: _package_metadata,
149        })
150    }
151
152    fn is_wit_package(&self) -> Option<WitEncodingVersion> {
153        // all wit package exports must be component types, and there must be at
154        // least one
155        if self.externs.is_empty() {
156            return None;
157        }
158
159        if !self.externs.iter().all(|(_, item)| {
160            let export = match item {
161                Extern::Export(e) => e,
162                _ => return false,
163            };
164            match export.kind {
165                ComponentExternalKind::Type => matches!(
166                    self.types.as_ref().component_any_type_at(export.index),
167                    ComponentAnyTypeId::Component(_)
168                ),
169                _ => false,
170            }
171        }) {
172            return None;
173        }
174
175        // The distinction between v1 and v2 encoding formats is the structure of the export
176        // strings for each component. The v1 format uses "<namespace>:<package>/wit" as the name
177        // for the top-level exports, while the v2 format uses the unqualified name of the encoded
178        // entity.
179        match ComponentName::new(&self.externs[0].0, 0).ok()?.kind() {
180            ComponentNameKind::Interface(name) if name.interface().as_str() == "wit" => {
181                Some(WitEncodingVersion::V1)
182            }
183            ComponentNameKind::Label(_) => Some(WitEncodingVersion::V2),
184            _ => None,
185        }
186    }
187
188    fn decode_wit_v1_package(&self) -> Result<(Resolve, PackageId)> {
189        let mut decoder = WitPackageDecoder::new(&self.types);
190
191        let mut pkg = None;
192        for (name, item) in self.externs.iter() {
193            let export = match item {
194                Extern::Export(e) => e,
195                _ => unreachable!(),
196            };
197            let id = self.types.as_ref().component_type_at(export.index);
198            let ty = &self.types[id];
199            if pkg.is_some() {
200                bail!("more than one top-level exported component type found");
201            }
202            let name = ComponentName::new(name, 0).unwrap();
203            pkg = Some(
204                decoder
205                    .decode_v1_package(&name, ty)
206                    .with_context(|| format!("failed to decode document `{name}`"))?,
207            );
208        }
209
210        let pkg = pkg.ok_or_else(|| anyhow!("no exported component type found"))?;
211        let (mut resolve, package) = decoder.finish(pkg);
212        if let Some(package_metadata) = &self.package_metadata {
213            package_metadata.inject(&mut resolve, package)?;
214        }
215        Ok((resolve, package))
216    }
217
218    fn decode_wit_v2_package(&self) -> Result<(Resolve, PackageId)> {
219        let mut decoder = WitPackageDecoder::new(&self.types);
220
221        let mut pkg_name = None;
222
223        let mut interfaces = IndexMap::default();
224        let mut worlds = IndexMap::default();
225        let mut fields = PackageFields {
226            interfaces: &mut interfaces,
227            worlds: &mut worlds,
228        };
229
230        for (_, item) in self.externs.iter() {
231            let export = match item {
232                Extern::Export(e) => e,
233                _ => unreachable!(),
234            };
235
236            let index = export.index;
237            let id = self.types.as_ref().component_type_at(index);
238            let component = &self.types[id];
239
240            // The single export of this component will determine if it's a world or an interface:
241            // worlds export a component, while interfaces export an instance.
242            if component.exports.len() != 1 {
243                bail!(
244                    "Expected a single export, but found {} instead",
245                    component.exports.len()
246                );
247            }
248
249            let name = component.exports.keys().nth(0).unwrap();
250
251            let name = match component.exports[name] {
252                ComponentEntityType::Component(ty) => {
253                    let package_name =
254                        decoder.decode_world(name.as_str(), &self.types[ty], &mut fields)?;
255                    package_name
256                }
257                ComponentEntityType::Instance(ty) => {
258                    let package_name = decoder.decode_interface(
259                        name.as_str(),
260                        &component.imports,
261                        &self.types[ty],
262                        &mut fields,
263                    )?;
264                    package_name
265                }
266                _ => unreachable!(),
267            };
268
269            if let Some(pkg_name) = pkg_name.as_ref() {
270                // TODO: when we have fully switched to the v2 format, we should switch to parsing
271                // multiple wit documents instead of bailing.
272                if pkg_name != &name {
273                    bail!("item defined with mismatched package name")
274                }
275            } else {
276                pkg_name.replace(name);
277            }
278        }
279
280        let pkg = if let Some(name) = pkg_name {
281            Package {
282                name,
283                docs: Docs::default(),
284                interfaces,
285                worlds,
286            }
287        } else {
288            bail!("no exported component type found");
289        };
290
291        let (mut resolve, package) = decoder.finish(pkg);
292        if let Some(package_metadata) = &self.package_metadata {
293            package_metadata.inject(&mut resolve, package)?;
294        }
295        Ok((resolve, package))
296    }
297
298    fn decode_component(&self) -> Result<(Resolve, WorldId)> {
299        assert!(self.is_wit_package().is_none());
300        let mut decoder = WitPackageDecoder::new(&self.types);
301        // Note that this name is arbitrarily chosen. We may one day perhaps
302        // want to encode this in the component binary format itself, but for
303        // now it shouldn't be an issue to have a defaulted name here.
304        let world_name = "root";
305        let world = decoder.resolve.worlds.alloc(World {
306            name: world_name.to_string(),
307            docs: Default::default(),
308            imports: Default::default(),
309            exports: Default::default(),
310            package: None,
311            includes: Default::default(),
312            stability: Default::default(),
313            span: Default::default(),
314        });
315        let mut package = Package {
316            // Similar to `world_name` above this is arbitrarily chosen as it's
317            // not otherwise encoded in a binary component. This theoretically
318            // shouldn't cause issues, however.
319            name: PackageName {
320                namespace: "root".to_string(),
321                version: None,
322                name: "component".to_string(),
323            },
324            docs: Default::default(),
325            worlds: [(world_name.to_string(), world)].into_iter().collect(),
326            interfaces: Default::default(),
327        };
328
329        let mut fields = PackageFields {
330            worlds: &mut package.worlds,
331            interfaces: &mut package.interfaces,
332        };
333
334        for (_name, item) in self.externs.iter() {
335            match item {
336                Extern::Import(import) => {
337                    decoder.decode_component_import(import, world, &mut fields)?
338                }
339                Extern::Export(export) => {
340                    decoder.decode_component_export(export, world, &mut fields)?
341                }
342            }
343        }
344
345        let (mut resolve, pkg) = decoder.finish(package);
346        if let Some(package_metadata) = &self.package_metadata {
347            package_metadata.inject(&mut resolve, pkg)?;
348        }
349        Ok((resolve, world))
350    }
351}
352
353/// Result of the [`decode`] function.
354pub enum DecodedWasm {
355    /// The input to [`decode`] was one or more binary-encoded WIT package(s).
356    ///
357    /// The full resolve graph is here plus the identifier of the packages that
358    /// were encoded. Note that other packages may be within the resolve if any
359    /// of the main packages refer to other, foreign packages.
360    WitPackage(Resolve, PackageId),
361
362    /// The input to [`decode`] was a component and its interface is specified
363    /// by the world here.
364    Component(Resolve, WorldId),
365}
366
367impl DecodedWasm {
368    /// Returns the [`Resolve`] for WIT types contained.
369    pub fn resolve(&self) -> &Resolve {
370        match self {
371            DecodedWasm::WitPackage(resolve, _) => resolve,
372            DecodedWasm::Component(resolve, _) => resolve,
373        }
374    }
375
376    /// Returns the main packages of what was decoded.
377    pub fn package(&self) -> PackageId {
378        match self {
379            DecodedWasm::WitPackage(_, id) => *id,
380            DecodedWasm::Component(resolve, world) => resolve.worlds[*world].package.unwrap(),
381        }
382    }
383}
384
385/// Decode for incremental reading
386pub fn decode_reader(reader: impl Read) -> Result<DecodedWasm> {
387    let info = ComponentInfo::from_reader(reader)?;
388
389    if let Some(version) = info.is_wit_package() {
390        match version {
391            WitEncodingVersion::V1 => {
392                log::debug!("decoding a v1 WIT package encoded as wasm");
393                let (resolve, pkg) = info.decode_wit_v1_package()?;
394                Ok(DecodedWasm::WitPackage(resolve, pkg))
395            }
396            WitEncodingVersion::V2 => {
397                log::debug!("decoding a v2 WIT package encoded as wasm");
398                let (resolve, pkg) = info.decode_wit_v2_package()?;
399                Ok(DecodedWasm::WitPackage(resolve, pkg))
400            }
401        }
402    } else {
403        log::debug!("inferring the WIT of a concrete component");
404        let (resolve, world) = info.decode_component()?;
405        Ok(DecodedWasm::Component(resolve, world))
406    }
407}
408
409/// Decodes an in-memory WebAssembly binary into a WIT [`Resolve`] and
410/// associated metadata.
411///
412/// The WebAssembly binary provided here can either be a
413/// WIT-package-encoded-as-binary or an actual component itself. A [`Resolve`]
414/// is always created and the return value indicates which was detected.
415pub fn decode(bytes: &[u8]) -> Result<DecodedWasm> {
416    decode_reader(bytes)
417}
418
419/// Decodes the single component type `world` specified as a WIT world.
420///
421/// The `world` should be an exported component type. The `world` must have been
422/// previously created via `encode_world` meaning that it is a component that
423/// itself imports nothing and exports a single component, and the single
424/// component export represents the world. The name of the export is also the
425/// name of the package/world/etc.
426pub fn decode_world(wasm: &[u8]) -> Result<(Resolve, WorldId)> {
427    let mut validator = Validator::new_with_features(WasmFeatures::all());
428    let mut exports = Vec::new();
429    let mut depth = 1;
430    let mut types = None;
431
432    for payload in Parser::new(0).parse_all(wasm) {
433        let payload = payload?;
434
435        match validator.payload(&payload)? {
436            ValidPayload::Ok => {}
437            ValidPayload::Parser(_) => depth += 1,
438            ValidPayload::End(t) => {
439                depth -= 1;
440                if depth == 0 {
441                    types = Some(t);
442                }
443            }
444            ValidPayload::Func(..) => {}
445        }
446
447        match payload {
448            Payload::ComponentExportSection(s) if depth == 1 => {
449                for export in s {
450                    exports.push(export?);
451                }
452            }
453            _ => {}
454        }
455    }
456
457    if exports.len() != 1 {
458        bail!("expected one export in component");
459    }
460    if exports[0].kind != ComponentExternalKind::Type {
461        bail!("expected an export of a type");
462    }
463    if exports[0].ty.is_some() {
464        bail!("expected an un-ascribed exported type");
465    }
466    let types = types.as_ref().unwrap();
467    let world = match types.as_ref().component_any_type_at(exports[0].index) {
468        ComponentAnyTypeId::Component(c) => c,
469        _ => bail!("expected an exported component type"),
470    };
471
472    let mut decoder = WitPackageDecoder::new(types);
473    let mut interfaces = IndexMap::default();
474    let mut worlds = IndexMap::default();
475    let ty = &types[world];
476    assert_eq!(ty.imports.len(), 0);
477    assert_eq!(ty.exports.len(), 1);
478    let name = ty.exports.keys().nth(0).unwrap();
479    let ty = match ty.exports[0] {
480        ComponentEntityType::Component(ty) => ty,
481        _ => unreachable!(),
482    };
483    let name = decoder.decode_world(
484        name,
485        &types[ty],
486        &mut PackageFields {
487            interfaces: &mut interfaces,
488            worlds: &mut worlds,
489        },
490    )?;
491    let (resolve, pkg) = decoder.finish(Package {
492        name,
493        interfaces,
494        worlds,
495        docs: Default::default(),
496    });
497    // The package decoded here should only have a single world so extract that
498    // here to return.
499    let world = *resolve.packages[pkg].worlds.iter().next().unwrap().1;
500    Ok((resolve, world))
501}
502
503struct PackageFields<'a> {
504    interfaces: &'a mut IndexMap<String, InterfaceId>,
505    worlds: &'a mut IndexMap<String, WorldId>,
506}
507
508struct WitPackageDecoder<'a> {
509    resolve: Resolve,
510    types: &'a Types,
511    foreign_packages: IndexMap<String, Package>,
512    iface_to_package_index: HashMap<InterfaceId, usize>,
513    named_interfaces: HashMap<String, InterfaceId>,
514
515    /// A map which tracks named resources to what their corresponding `TypeId`
516    /// is. This first layer of key in this map is the owner scope of a
517    /// resource, more-or-less the `world` or `interface` that it's defined
518    /// within. The second layer of this map is keyed by name of the resource
519    /// and points to the actual ID of the resource.
520    ///
521    /// This map is populated in `register_type_export`.
522    resources: HashMap<TypeOwner, HashMap<String, TypeId>>,
523
524    /// A map from a type id to what it's been translated to.
525    type_map: HashMap<ComponentAnyTypeId, TypeId>,
526}
527
528impl WitPackageDecoder<'_> {
529    fn new<'a>(types: &'a Types) -> WitPackageDecoder<'a> {
530        WitPackageDecoder {
531            resolve: Resolve::default(),
532            types,
533            type_map: HashMap::new(),
534            foreign_packages: Default::default(),
535            iface_to_package_index: Default::default(),
536            named_interfaces: Default::default(),
537            resources: Default::default(),
538        }
539    }
540
541    fn decode_v1_package(&mut self, name: &ComponentName, ty: &ComponentType) -> Result<Package> {
542        // Process all imports for this package first, where imports are
543        // importing from remote packages.
544        for (name, ty) in ty.imports.iter() {
545            let ty = match ty {
546                ComponentEntityType::Instance(idx) => &self.types[*idx],
547                _ => bail!("import `{name}` is not an instance"),
548            };
549            self.register_import(name, ty)
550                .with_context(|| format!("failed to process import `{name}`"))?;
551        }
552
553        let mut package = Package {
554            // The name encoded for packages must be of the form `foo:bar/wit`
555            // where "wit" is just a placeholder for now. The package name in
556            // this case would be `foo:bar`.
557            name: match name.kind() {
558                ComponentNameKind::Interface(name) if name.interface().as_str() == "wit" => {
559                    name.to_package_name()
560                }
561                _ => bail!("package name is not a valid id: {name}"),
562            },
563            docs: Default::default(),
564            interfaces: Default::default(),
565            worlds: Default::default(),
566        };
567
568        let mut fields = PackageFields {
569            interfaces: &mut package.interfaces,
570            worlds: &mut package.worlds,
571        };
572
573        for (name, ty) in ty.exports.iter() {
574            match ty {
575                ComponentEntityType::Instance(idx) => {
576                    let ty = &self.types[*idx];
577                    self.register_interface(name.as_str(), ty, &mut fields)
578                        .with_context(|| format!("failed to process export `{name}`"))?;
579                }
580                ComponentEntityType::Component(idx) => {
581                    let ty = &self.types[*idx];
582                    self.register_world(name.as_str(), ty, &mut fields)
583                        .with_context(|| format!("failed to process export `{name}`"))?;
584                }
585                _ => bail!("component export `{name}` is not an instance or component"),
586            }
587        }
588        Ok(package)
589    }
590
591    fn decode_interface<'a>(
592        &mut self,
593        name: &str,
594        imports: &wasmparser::collections::IndexMap<String, ComponentEntityType>,
595        ty: &ComponentInstanceType,
596        fields: &mut PackageFields<'a>,
597    ) -> Result<PackageName> {
598        let component_name = self
599            .parse_component_name(name)
600            .context("expected world name to have an ID form")?;
601
602        let package = match component_name.kind() {
603            ComponentNameKind::Interface(name) => name.to_package_name(),
604            _ => bail!("expected world name to be fully qualified"),
605        };
606
607        for (name, ty) in imports.iter() {
608            let ty = match ty {
609                ComponentEntityType::Instance(idx) => &self.types[*idx],
610                _ => bail!("import `{name}` is not an instance"),
611            };
612            self.register_import(name, ty)
613                .with_context(|| format!("failed to process import `{name}`"))?;
614        }
615
616        let _ = self.register_interface(name, ty, fields)?;
617
618        Ok(package)
619    }
620
621    fn decode_world<'a>(
622        &mut self,
623        name: &str,
624        ty: &ComponentType,
625        fields: &mut PackageFields<'a>,
626    ) -> Result<PackageName> {
627        let kebab_name = self
628            .parse_component_name(name)
629            .context("expected world name to have an ID form")?;
630
631        let package = match kebab_name.kind() {
632            ComponentNameKind::Interface(name) => name.to_package_name(),
633            _ => bail!("expected world name to be fully qualified"),
634        };
635
636        let _ = self.register_world(name, ty, fields)?;
637
638        Ok(package)
639    }
640
641    fn decode_component_import<'a>(
642        &mut self,
643        name: &str,
644        world: WorldId,
645        package: &mut PackageFields<'a>,
646    ) -> Result<()> {
647        log::debug!("decoding component import `{name}`");
648        let ty = self
649            .types
650            .as_ref()
651            .component_entity_type_of_import(name)
652            .unwrap();
653        let owner = TypeOwner::World(world);
654        let (name, item) = match ty {
655            ComponentEntityType::Instance(i) => {
656                let ty = &self.types[i];
657                let (name, id) = if name.contains('/') {
658                    let id = self.register_import(name, ty)?;
659                    (WorldKey::Interface(id), id)
660                } else {
661                    self.register_interface(name, ty, package)
662                        .with_context(|| format!("failed to decode WIT from import `{name}`"))?
663                };
664                (
665                    name,
666                    WorldItem::Interface {
667                        id,
668                        stability: Default::default(),
669                        span: Default::default(),
670                    },
671                )
672            }
673            ComponentEntityType::Func(i) => {
674                let ty = &self.types[i];
675                let func = self
676                    .convert_function(name, ty, owner)
677                    .with_context(|| format!("failed to decode function from import `{name}`"))?;
678                (WorldKey::Name(name.to_string()), WorldItem::Function(func))
679            }
680            ComponentEntityType::Type {
681                referenced,
682                created,
683            } => {
684                let id = self
685                    .register_type_export(name, owner, referenced, created)
686                    .with_context(|| format!("failed to decode type from export `{name}`"))?;
687                (
688                    WorldKey::Name(name.to_string()),
689                    WorldItem::Type {
690                        id,
691                        span: Default::default(),
692                    },
693                )
694            }
695            // All other imports do not form part of the component's world
696            _ => return Ok(()),
697        };
698        self.resolve.worlds[world].imports.insert(name, item);
699        Ok(())
700    }
701
702    fn decode_component_export<'a>(
703        &mut self,
704        export: &DecodingExport,
705        world: WorldId,
706        package: &mut PackageFields<'a>,
707    ) -> Result<()> {
708        let name = &export.name;
709        log::debug!("decoding component export `{name}`");
710        let types = self.types.as_ref();
711        let ty = types.component_entity_type_of_export(name).unwrap();
712        let (name, item) = match ty {
713            ComponentEntityType::Func(i) => {
714                let ty = &types[i];
715                let func = self
716                    .convert_function(name, ty, TypeOwner::World(world))
717                    .with_context(|| format!("failed to decode function from export `{name}`"))?;
718
719                (WorldKey::Name(name.to_string()), WorldItem::Function(func))
720            }
721            ComponentEntityType::Instance(i) => {
722                let ty = &types[i];
723                let (name, id) = if name.contains('/') {
724                    let id = self.register_import(name, ty)?;
725                    (WorldKey::Interface(id), id)
726                } else {
727                    self.register_interface(name, ty, package)
728                        .with_context(|| format!("failed to decode WIT from export `{name}`"))?
729                };
730                (
731                    name,
732                    WorldItem::Interface {
733                        id,
734                        stability: Default::default(),
735                        span: Default::default(),
736                    },
737                )
738            }
739            _ => {
740                bail!("component export `{name}` was not a function or instance")
741            }
742        };
743        self.resolve.worlds[world].exports.insert(name, item);
744        Ok(())
745    }
746
747    /// Registers that the `name` provided is either imported interface from a
748    /// foreign package or  referencing a previously defined interface in this
749    /// package.
750    ///
751    /// This function will internally ensure that `name` is well-structured and
752    /// will fill in any information as necessary. For example with a foreign
753    /// dependency the foreign package structure, types, etc, all need to be
754    /// created. For a local dependency it's instead ensured that all the types
755    /// line up with the previous definitions.
756    fn register_import(&mut self, name: &str, ty: &ComponentInstanceType) -> Result<InterfaceId> {
757        let (is_local, interface) = match self.named_interfaces.get(name) {
758            Some(id) => (true, *id),
759            None => (false, self.extract_dep_interface(name)?),
760        };
761        let owner = TypeOwner::Interface(interface);
762        for (name, ty) in ty.exports.iter() {
763            log::debug!("decoding import instance export `{name}`");
764            match *ty {
765                ComponentEntityType::Type {
766                    referenced,
767                    created,
768                } => {
769                    match self.resolve.interfaces[interface]
770                        .types
771                        .get(name.as_str())
772                        .copied()
773                    {
774                        // If this name is already defined as a type in the
775                        // specified interface then that's ok. For package-local
776                        // interfaces that's expected since the interface was
777                        // fully defined. For remote interfaces it means we're
778                        // using something that was already used elsewhere. In
779                        // both cases continue along.
780                        //
781                        // Notably for the remotely defined case this will also
782                        // walk over the structure of the type and register
783                        // internal wasmparser ids with wit-parser ids. This is
784                        // necessary to ensure that anonymous types like
785                        // `list<u8>` defined in original definitions are
786                        // unified with anonymous types when duplicated inside
787                        // of worlds. Overall this prevents, for example, extra
788                        // `list<u8>` types from popping up when decoding. This
789                        // is not strictly necessary but assists with
790                        // roundtripping assertions during fuzzing.
791                        Some(id) => {
792                            log::debug!("type already exist");
793                            match referenced {
794                                ComponentAnyTypeId::Defined(ty) => {
795                                    self.register_defined(id, &self.types[ty])?;
796                                }
797                                ComponentAnyTypeId::Resource(_) => {}
798                                _ => unreachable!(),
799                            }
800                            let prev = self.type_map.insert(created, id);
801                            assert!(prev.is_none());
802                        }
803
804                        // If the name is not defined, however, then there's two
805                        // possibilities:
806                        //
807                        // * For package-local interfaces this is an error
808                        //   because the package-local interface defined
809                        //   everything already and this is referencing
810                        //   something that isn't defined.
811                        //
812                        // * For remote interfaces they're never fully declared
813                        //   so it's lazily filled in here. This means that the
814                        //   view of remote interfaces ends up being the minimal
815                        //   slice needed for this resolve, which is what's
816                        //   intended.
817                        None => {
818                            if is_local {
819                                bail!("instance type export `{name}` not defined in interface");
820                            }
821                            let id = self.register_type_export(
822                                name.as_str(),
823                                owner,
824                                referenced,
825                                created,
826                            )?;
827                            let prev = self.resolve.interfaces[interface]
828                                .types
829                                .insert(name.to_string(), id);
830                            assert!(prev.is_none());
831                        }
832                    }
833                }
834
835                // This has similar logic to types above where we lazily fill in
836                // functions for remote dependencies and otherwise assert
837                // they're already defined for local dependencies.
838                ComponentEntityType::Func(ty) => {
839                    let def = &self.types[ty];
840                    if self.resolve.interfaces[interface]
841                        .functions
842                        .contains_key(name.as_str())
843                    {
844                        // TODO: should ideally verify that function signatures
845                        // match.
846                        continue;
847                    }
848                    if is_local {
849                        bail!("instance function export `{name}` not defined in interface");
850                    }
851                    let func = self.convert_function(name.as_str(), def, owner)?;
852                    let prev = self.resolve.interfaces[interface]
853                        .functions
854                        .insert(name.to_string(), func);
855                    assert!(prev.is_none());
856                }
857
858                _ => bail!("instance type export `{name}` is not a type"),
859            }
860        }
861
862        Ok(interface)
863    }
864
865    fn find_alias(&self, id: ComponentAnyTypeId) -> Option<TypeId> {
866        // Consult `type_map` for `referenced` or anything in its
867        // chain of aliases to determine what it maps to. This may
868        // bottom out in `None` in the case that this type is
869        // just now being defined, but this should otherwise follow
870        // chains of aliases to determine what exactly this was a
871        // `use` of if it exists.
872        let mut prev = None;
873        let mut cur = id;
874        while prev.is_none() {
875            prev = self.type_map.get(&cur).copied();
876            cur = match self.types.as_ref().peel_alias(cur) {
877                Some(next) => next,
878                None => break,
879            };
880        }
881        prev
882    }
883
884    /// This will parse the `name_string` as a component model ID string and
885    /// ensure that there's an `InterfaceId` corresponding to its components.
886    fn extract_dep_interface(&mut self, name_string: &str) -> Result<InterfaceId> {
887        let name = ComponentName::new(name_string, 0).unwrap();
888        let name = match name.kind() {
889            ComponentNameKind::Interface(name) => name,
890            _ => bail!("package name is not a valid id: {name_string}"),
891        };
892        let package_name = name.to_package_name();
893        // Lazily create a `Package` as necessary, along with the interface.
894        let package = self
895            .foreign_packages
896            .entry(package_name.to_string())
897            .or_insert_with(|| Package {
898                name: package_name.clone(),
899                docs: Default::default(),
900                interfaces: Default::default(),
901                worlds: Default::default(),
902            });
903        let interface = *package
904            .interfaces
905            .entry(name.interface().to_string())
906            .or_insert_with(|| {
907                self.resolve.interfaces.alloc(Interface {
908                    name: Some(name.interface().to_string()),
909                    docs: Default::default(),
910                    types: IndexMap::default(),
911                    functions: IndexMap::default(),
912                    package: None,
913                    stability: Default::default(),
914                    span: Default::default(),
915                    clone_of: None,
916                })
917            });
918
919        // Record a mapping of which foreign package this interface belongs to
920        self.iface_to_package_index.insert(
921            interface,
922            self.foreign_packages
923                .get_full(&package_name.to_string())
924                .unwrap()
925                .0,
926        );
927        Ok(interface)
928    }
929
930    /// A general-purpose helper function to translate a component instance
931    /// into a WIT interface.
932    ///
933    /// This is one of the main workhorses of this module. This handles
934    /// interfaces both at the type level, for concrete components, and
935    /// internally within worlds as well.
936    ///
937    /// The `name` provided is the contextual ID or name of the interface. This
938    /// could be a kebab-name in the case of a world import or export or it can
939    /// also be an ID. This is used to guide insertion into various maps.
940    ///
941    /// The `ty` provided is the actual component type being decoded.
942    ///
943    /// The `package` is where to insert the final interface if `name` is an ID
944    /// meaning it's registered as a named standalone item within the package.
945    fn register_interface<'a>(
946        &mut self,
947        name: &str,
948        ty: &ComponentInstanceType,
949        package: &mut PackageFields<'a>,
950    ) -> Result<(WorldKey, InterfaceId)> {
951        // If this interface's name is already known then that means this is an
952        // interface that's both imported and exported.  Use `register_import`
953        // to draw connections between types and this interface's types.
954        if self.named_interfaces.contains_key(name) {
955            let id = self.register_import(name, ty)?;
956            return Ok((WorldKey::Interface(id), id));
957        }
958
959        // If this is a bare kebab-name for an interface then the interface's
960        // listed name is `None` and the name goes out through the key.
961        // Otherwise this name is extracted from `name` interpreted as an ID.
962        let interface_name = self.extract_interface_name_from_component_name(name)?;
963
964        let mut interface = Interface {
965            name: interface_name.clone(),
966            docs: Default::default(),
967            types: IndexMap::default(),
968            functions: IndexMap::default(),
969            package: None,
970            stability: Default::default(),
971            span: Default::default(),
972            clone_of: None,
973        };
974
975        let owner = TypeOwner::Interface(self.resolve.interfaces.next_id());
976        for (name, ty) in ty.exports.iter() {
977            match *ty {
978                ComponentEntityType::Type {
979                    referenced,
980                    created,
981                } => {
982                    let ty = self
983                        .register_type_export(name.as_str(), owner, referenced, created)
984                        .with_context(|| format!("failed to register type export '{name}'"))?;
985                    let prev = interface.types.insert(name.to_string(), ty);
986                    assert!(prev.is_none());
987                }
988
989                ComponentEntityType::Func(ty) => {
990                    let ty = &self.types[ty];
991                    let func = self
992                        .convert_function(name.as_str(), ty, owner)
993                        .with_context(|| format!("failed to convert function '{name}'"))?;
994                    let prev = interface.functions.insert(name.to_string(), func);
995                    assert!(prev.is_none());
996                }
997                _ => bail!("instance type export `{name}` is not a type or function"),
998            };
999        }
1000        let id = self.resolve.interfaces.alloc(interface);
1001        let key = match interface_name {
1002            // If this interface is named then it's part of the package, so
1003            // insert it. Additionally register it in `named_interfaces` so
1004            // further use comes back to this original definition.
1005            Some(interface_name) => {
1006                let prev = package.interfaces.insert(interface_name, id);
1007                assert!(prev.is_none(), "duplicate interface added for {name:?}");
1008                let prev = self.named_interfaces.insert(name.to_string(), id);
1009                assert!(prev.is_none());
1010                WorldKey::Interface(id)
1011            }
1012
1013            // If this interface isn't named then its key is always a
1014            // kebab-name.
1015            None => WorldKey::Name(name.to_string()),
1016        };
1017        Ok((key, id))
1018    }
1019
1020    fn parse_component_name(&self, name: &str) -> Result<ComponentName> {
1021        ComponentName::new(name, 0)
1022            .with_context(|| format!("cannot extract item name from: {name}"))
1023    }
1024
1025    fn extract_interface_name_from_component_name(&self, name: &str) -> Result<Option<String>> {
1026        let component_name = self.parse_component_name(name)?;
1027        match component_name.kind() {
1028            ComponentNameKind::Interface(name) => Ok(Some(name.interface().to_string())),
1029            ComponentNameKind::Label(_name) => Ok(None),
1030            _ => bail!("cannot extract item name from: {name}"),
1031        }
1032    }
1033
1034    fn register_type_export(
1035        &mut self,
1036        name: &str,
1037        owner: TypeOwner,
1038        referenced: ComponentAnyTypeId,
1039        created: ComponentAnyTypeId,
1040    ) -> Result<TypeId> {
1041        let kind = match self.find_alias(referenced) {
1042            // If this `TypeId` points to a type which has
1043            // previously been defined, meaning we're aliasing a
1044            // prior definition.
1045            Some(prev) => {
1046                log::debug!("type export for `{name}` is an alias");
1047                TypeDefKind::Type(Type::Id(prev))
1048            }
1049
1050            // ... or this `TypeId`'s source definition has never
1051            // been seen before, so declare the full type.
1052            None => {
1053                log::debug!("type export for `{name}` is a new type");
1054                match referenced {
1055                    ComponentAnyTypeId::Defined(ty) => self
1056                        .convert_defined(&self.types[ty])
1057                        .context("failed to convert unaliased type")?,
1058                    ComponentAnyTypeId::Resource(_) => TypeDefKind::Resource,
1059                    _ => unreachable!(),
1060                }
1061            }
1062        };
1063        let ty = self.resolve.types.alloc(TypeDef {
1064            name: Some(name.to_string()),
1065            kind,
1066            docs: Default::default(),
1067            stability: Default::default(),
1068            owner,
1069            span: Default::default(),
1070        });
1071
1072        // If this is a resource then doubly-register it in `self.resources` so
1073        // the ID allocated here can be looked up via name later on during
1074        // `convert_function`.
1075        if let TypeDefKind::Resource = self.resolve.types[ty].kind {
1076            let prev = self
1077                .resources
1078                .entry(owner)
1079                .or_insert(HashMap::new())
1080                .insert(name.to_string(), ty);
1081            assert!(prev.is_none());
1082        }
1083
1084        let prev = self.type_map.insert(created, ty);
1085        assert!(prev.is_none());
1086        Ok(ty)
1087    }
1088
1089    fn register_world<'a>(
1090        &mut self,
1091        name: &str,
1092        ty: &ComponentType,
1093        package: &mut PackageFields<'a>,
1094    ) -> Result<WorldId> {
1095        let name = self
1096            .extract_interface_name_from_component_name(name)?
1097            .context("expected world name to have an ID form")?;
1098        let mut world = World {
1099            name: name.clone(),
1100            docs: Default::default(),
1101            imports: Default::default(),
1102            exports: Default::default(),
1103            includes: Default::default(),
1104            package: None,
1105            stability: Default::default(),
1106            span: Default::default(),
1107        };
1108
1109        let owner = TypeOwner::World(self.resolve.worlds.next_id());
1110        for (name, ty) in ty.imports.iter() {
1111            let (name, item) = match ty {
1112                ComponentEntityType::Instance(idx) => {
1113                    let ty = &self.types[*idx];
1114                    let (name, id) = if name.contains('/') {
1115                        // If a name is an interface import then it is either to
1116                        // a package-local or foreign interface, and both
1117                        // situations are handled in `register_import`.
1118                        let id = self.register_import(name, ty)?;
1119                        (WorldKey::Interface(id), id)
1120                    } else {
1121                        // A plain kebab-name indicates an inline interface that
1122                        // wasn't declared explicitly elsewhere with a name, and
1123                        // `register_interface` will create a new `Interface`
1124                        // with no name.
1125                        self.register_interface(name, ty, package)?
1126                    };
1127                    (
1128                        name,
1129                        WorldItem::Interface {
1130                            id,
1131                            stability: Default::default(),
1132                            span: Default::default(),
1133                        },
1134                    )
1135                }
1136                ComponentEntityType::Type {
1137                    created,
1138                    referenced,
1139                } => {
1140                    let ty =
1141                        self.register_type_export(name.as_str(), owner, *referenced, *created)?;
1142                    (
1143                        WorldKey::Name(name.to_string()),
1144                        WorldItem::Type {
1145                            id: ty,
1146                            span: Default::default(),
1147                        },
1148                    )
1149                }
1150                ComponentEntityType::Func(idx) => {
1151                    let ty = &self.types[*idx];
1152                    let func = self.convert_function(name.as_str(), ty, owner)?;
1153                    (WorldKey::Name(name.to_string()), WorldItem::Function(func))
1154                }
1155                _ => bail!("component import `{name}` is not an instance, func, or type"),
1156            };
1157            world.imports.insert(name, item);
1158        }
1159
1160        for (name, ty) in ty.exports.iter() {
1161            let (name, item) = match ty {
1162                ComponentEntityType::Instance(idx) => {
1163                    let ty = &self.types[*idx];
1164                    let (name, id) = if name.contains('/') {
1165                        // Note that despite this being an export this is
1166                        // calling `register_import`. With a URL this interface
1167                        // must have been previously defined so this will
1168                        // trigger the logic of either filling in a remotely
1169                        // defined interface or connecting items to local
1170                        // definitions of our own interface.
1171                        let id = self.register_import(name, ty)?;
1172                        (WorldKey::Interface(id), id)
1173                    } else {
1174                        self.register_interface(name, ty, package)?
1175                    };
1176                    (
1177                        name,
1178                        WorldItem::Interface {
1179                            id,
1180                            stability: Default::default(),
1181                            span: Default::default(),
1182                        },
1183                    )
1184                }
1185
1186                ComponentEntityType::Func(idx) => {
1187                    let ty = &self.types[*idx];
1188                    let func = self.convert_function(name.as_str(), ty, owner)?;
1189                    (WorldKey::Name(name.to_string()), WorldItem::Function(func))
1190                }
1191
1192                _ => bail!("component export `{name}` is not an instance or function"),
1193            };
1194            world.exports.insert(name, item);
1195        }
1196        let id = self.resolve.worlds.alloc(world);
1197        let prev = package.worlds.insert(name, id);
1198        assert!(prev.is_none());
1199        Ok(id)
1200    }
1201
1202    fn convert_function(
1203        &mut self,
1204        name: &str,
1205        ty: &ComponentFuncType,
1206        owner: TypeOwner,
1207    ) -> Result<Function> {
1208        let name = ComponentName::new(name, 0).unwrap();
1209        let params = ty
1210            .params
1211            .iter()
1212            .map(|(name, ty)| {
1213                Ok(Param {
1214                    name: name.to_string(),
1215                    ty: self.convert_valtype(ty)?,
1216                    span: Default::default(),
1217                })
1218            })
1219            .collect::<Result<Vec<_>>>()
1220            .context("failed to convert params")?;
1221        let result = match &ty.result {
1222            Some(ty) => Some(
1223                self.convert_valtype(ty)
1224                    .context("failed to convert anonymous result type")?,
1225            ),
1226            None => None,
1227        };
1228        Ok(Function {
1229            docs: Default::default(),
1230            stability: Default::default(),
1231            kind: match name.kind() {
1232                ComponentNameKind::Label(_) => {
1233                    if ty.async_ {
1234                        FunctionKind::AsyncFreestanding
1235                    } else {
1236                        FunctionKind::Freestanding
1237                    }
1238                }
1239                ComponentNameKind::Constructor(resource) => {
1240                    FunctionKind::Constructor(self.resources[&owner][resource.as_str()])
1241                }
1242                ComponentNameKind::Method(name) => {
1243                    if ty.async_ {
1244                        FunctionKind::AsyncMethod(self.resources[&owner][name.resource().as_str()])
1245                    } else {
1246                        FunctionKind::Method(self.resources[&owner][name.resource().as_str()])
1247                    }
1248                }
1249                ComponentNameKind::Static(name) => {
1250                    if ty.async_ {
1251                        FunctionKind::AsyncStatic(self.resources[&owner][name.resource().as_str()])
1252                    } else {
1253                        FunctionKind::Static(self.resources[&owner][name.resource().as_str()])
1254                    }
1255                }
1256
1257                // Functions shouldn't have ID-based names at this time.
1258                ComponentNameKind::Interface(_)
1259                | ComponentNameKind::Url(_)
1260                | ComponentNameKind::Hash(_)
1261                | ComponentNameKind::Dependency(_) => unreachable!(),
1262            },
1263
1264            // Note that this name includes "name mangling" such as
1265            // `[method]foo.bar` which is intentional. The `FunctionKind`
1266            // discriminant calculated above indicates how to interpret this
1267            // name.
1268            name: name.to_string(),
1269            params,
1270            result,
1271            span: Default::default(),
1272        })
1273    }
1274
1275    fn convert_valtype(&mut self, ty: &ComponentValType) -> Result<Type> {
1276        let id = match ty {
1277            ComponentValType::Primitive(ty) => return Ok(self.convert_primitive(*ty)),
1278            ComponentValType::Type(id) => *id,
1279        };
1280
1281        // Don't create duplicate types for anything previously created.
1282        let key: ComponentAnyTypeId = id.into();
1283        if let Some(ret) = self.type_map.get(&key) {
1284            return Ok(Type::Id(*ret));
1285        }
1286
1287        // Otherwise create a new `TypeDef` without a name since this is an
1288        // anonymous valtype. Note that this is invalid for some types so return
1289        // errors on those types, but eventually the `bail!` here  is
1290        // more-or-less unreachable due to expected validation to be added to
1291        // the component model binary format itself.
1292        let def = &self.types[id];
1293        let kind = self.convert_defined(def)?;
1294        match &kind {
1295            TypeDefKind::Type(_)
1296            | TypeDefKind::List(_)
1297            | TypeDefKind::Map(_, _)
1298            | TypeDefKind::FixedLengthList(..)
1299            | TypeDefKind::Tuple(_)
1300            | TypeDefKind::Option(_)
1301            | TypeDefKind::Result(_)
1302            | TypeDefKind::Handle(_)
1303            | TypeDefKind::Future(_)
1304            | TypeDefKind::Stream(_) => {}
1305
1306            TypeDefKind::Resource
1307            | TypeDefKind::Record(_)
1308            | TypeDefKind::Enum(_)
1309            | TypeDefKind::Variant(_)
1310            | TypeDefKind::Flags(_) => {
1311                bail!("unexpected unnamed type of kind '{}'", kind.as_str());
1312            }
1313            TypeDefKind::Unknown => unreachable!(),
1314        }
1315        let ty = self.resolve.types.alloc(TypeDef {
1316            name: None,
1317            docs: Default::default(),
1318            stability: Default::default(),
1319            owner: TypeOwner::None,
1320            kind,
1321            span: Default::default(),
1322        });
1323        let prev = self.type_map.insert(id.into(), ty);
1324        assert!(prev.is_none());
1325        Ok(Type::Id(ty))
1326    }
1327
1328    /// Converts a wasmparser `ComponentDefinedType`, the definition of a type
1329    /// in the component model, to a WIT `TypeDefKind` to get inserted into the
1330    /// types arena by the caller.
1331    fn convert_defined(&mut self, ty: &ComponentDefinedType) -> Result<TypeDefKind> {
1332        match ty {
1333            ComponentDefinedType::Primitive(t) => Ok(TypeDefKind::Type(self.convert_primitive(*t))),
1334
1335            ComponentDefinedType::List(t) => {
1336                let t = self.convert_valtype(t)?;
1337                Ok(TypeDefKind::List(t))
1338            }
1339
1340            ComponentDefinedType::Map(k, v) => {
1341                let k = self.convert_valtype(k)?;
1342                let v = self.convert_valtype(v)?;
1343                Ok(TypeDefKind::Map(k, v))
1344            }
1345
1346            ComponentDefinedType::FixedLengthList(t, size) => {
1347                let t = self.convert_valtype(t)?;
1348                Ok(TypeDefKind::FixedLengthList(t, *size))
1349            }
1350
1351            ComponentDefinedType::Tuple(t) => {
1352                let types = t
1353                    .types
1354                    .iter()
1355                    .map(|t| self.convert_valtype(t))
1356                    .collect::<Result<_>>()?;
1357                Ok(TypeDefKind::Tuple(Tuple { types }))
1358            }
1359
1360            ComponentDefinedType::Option(t) => {
1361                let t = self.convert_valtype(t)?;
1362                Ok(TypeDefKind::Option(t))
1363            }
1364
1365            ComponentDefinedType::Result { ok, err } => {
1366                let ok = match ok {
1367                    Some(t) => Some(self.convert_valtype(t)?),
1368                    None => None,
1369                };
1370                let err = match err {
1371                    Some(t) => Some(self.convert_valtype(t)?),
1372                    None => None,
1373                };
1374                Ok(TypeDefKind::Result(Result_ { ok, err }))
1375            }
1376
1377            ComponentDefinedType::Record(r) => {
1378                let fields = r
1379                    .fields
1380                    .iter()
1381                    .map(|(name, ty)| {
1382                        Ok(Field {
1383                            name: name.to_string(),
1384                            ty: self.convert_valtype(ty).with_context(|| {
1385                                format!("failed to convert record field '{name}'")
1386                            })?,
1387                            docs: Default::default(),
1388                            span: Default::default(),
1389                        })
1390                    })
1391                    .collect::<Result<_>>()?;
1392                Ok(TypeDefKind::Record(Record { fields }))
1393            }
1394
1395            ComponentDefinedType::Variant(v) => {
1396                let cases = v
1397                    .cases
1398                    .iter()
1399                    .map(|(name, case)| {
1400                        Ok(Case {
1401                            name: name.to_string(),
1402                            ty: match &case.ty {
1403                                Some(ty) => Some(self.convert_valtype(ty)?),
1404                                None => None,
1405                            },
1406                            docs: Default::default(),
1407                            span: Default::default(),
1408                        })
1409                    })
1410                    .collect::<Result<_>>()?;
1411                Ok(TypeDefKind::Variant(Variant { cases }))
1412            }
1413
1414            ComponentDefinedType::Flags(f) => {
1415                let flags = f
1416                    .iter()
1417                    .map(|name| Flag {
1418                        name: name.to_string(),
1419                        docs: Default::default(),
1420                        span: Default::default(),
1421                    })
1422                    .collect();
1423                Ok(TypeDefKind::Flags(Flags { flags }))
1424            }
1425
1426            ComponentDefinedType::Enum(e) => {
1427                let cases = e
1428                    .iter()
1429                    .cloned()
1430                    .map(|name| EnumCase {
1431                        name: name.into(),
1432                        docs: Default::default(),
1433                        span: Default::default(),
1434                    })
1435                    .collect();
1436                Ok(TypeDefKind::Enum(Enum { cases }))
1437            }
1438
1439            ComponentDefinedType::Own(id) => {
1440                let key: ComponentAnyTypeId = (*id).into();
1441                let id = self.type_map[&key];
1442                Ok(TypeDefKind::Handle(Handle::Own(id)))
1443            }
1444
1445            ComponentDefinedType::Borrow(id) => {
1446                let key: ComponentAnyTypeId = (*id).into();
1447                let id = self.type_map[&key];
1448                Ok(TypeDefKind::Handle(Handle::Borrow(id)))
1449            }
1450
1451            ComponentDefinedType::Future(ty) => Ok(TypeDefKind::Future(
1452                ty.as_ref().map(|ty| self.convert_valtype(ty)).transpose()?,
1453            )),
1454
1455            ComponentDefinedType::Stream(ty) => Ok(TypeDefKind::Stream(
1456                ty.as_ref().map(|ty| self.convert_valtype(ty)).transpose()?,
1457            )),
1458        }
1459    }
1460
1461    fn convert_primitive(&self, ty: PrimitiveValType) -> Type {
1462        match ty {
1463            PrimitiveValType::U8 => Type::U8,
1464            PrimitiveValType::S8 => Type::S8,
1465            PrimitiveValType::U16 => Type::U16,
1466            PrimitiveValType::S16 => Type::S16,
1467            PrimitiveValType::U32 => Type::U32,
1468            PrimitiveValType::S32 => Type::S32,
1469            PrimitiveValType::U64 => Type::U64,
1470            PrimitiveValType::S64 => Type::S64,
1471            PrimitiveValType::Bool => Type::Bool,
1472            PrimitiveValType::Char => Type::Char,
1473            PrimitiveValType::String => Type::String,
1474            PrimitiveValType::F32 => Type::F32,
1475            PrimitiveValType::F64 => Type::F64,
1476            PrimitiveValType::ErrorContext => Type::ErrorContext,
1477        }
1478    }
1479
1480    fn register_defined(&mut self, id: TypeId, def: &ComponentDefinedType) -> Result<()> {
1481        Registrar {
1482            types: &self.types,
1483            type_map: &mut self.type_map,
1484            resolve: &self.resolve,
1485        }
1486        .defined(id, def)
1487    }
1488
1489    /// Completes the decoding of this resolve by finalizing all packages into
1490    /// their topological ordering within the returned `Resolve`.
1491    ///
1492    /// Takes the root package as an argument to insert.
1493    fn finish(mut self, package: Package) -> (Resolve, PackageId) {
1494        // Build a topological ordering is then calculated by visiting all the
1495        // transitive dependencies of packages.
1496        let mut order = IndexSet::default();
1497        for i in 0..self.foreign_packages.len() {
1498            self.visit_package(i, &mut order);
1499        }
1500
1501        // Using the topological ordering create a temporary map from
1502        // index-in-`foreign_packages` to index-in-`order`
1503        let mut idx_to_pos = vec![0; self.foreign_packages.len()];
1504        for (pos, idx) in order.iter().enumerate() {
1505            idx_to_pos[*idx] = pos;
1506        }
1507        // .. and then using `idx_to_pos` sort the `foreign_packages` array based
1508        // on the position it's at in the topological ordering
1509        let mut deps = mem::take(&mut self.foreign_packages)
1510            .into_iter()
1511            .enumerate()
1512            .collect::<Vec<_>>();
1513        deps.sort_by_key(|(idx, _)| idx_to_pos[*idx]);
1514
1515        // .. and finally insert the packages, in their final topological
1516        // ordering, into the returned array.
1517        for (_idx, (_url, pkg)) in deps {
1518            self.insert_package(pkg);
1519        }
1520
1521        let id = self.insert_package(package);
1522        assert!(self.resolve.worlds.iter().all(|(_, w)| w.package.is_some()));
1523        assert!(
1524            self.resolve
1525                .interfaces
1526                .iter()
1527                .all(|(_, i)| i.package.is_some())
1528        );
1529        (self.resolve, id)
1530    }
1531
1532    fn insert_package(&mut self, package: Package) -> PackageId {
1533        let Package {
1534            name,
1535            interfaces,
1536            worlds,
1537            docs,
1538        } = package;
1539
1540        // Most of the time the `package` being inserted is not already present
1541        // in `self.resolve`, but in the case of the top-level `decode_world`
1542        // function this isn't the case. This shouldn't in general be a problem
1543        // so union-up the packages here while asserting that nothing gets
1544        // replaced by accident which would indicate a bug.
1545        let pkg = self
1546            .resolve
1547            .package_names
1548            .get(&name)
1549            .copied()
1550            .unwrap_or_else(|| {
1551                let id = self.resolve.packages.alloc(Package {
1552                    name: name.clone(),
1553                    interfaces: Default::default(),
1554                    worlds: Default::default(),
1555                    docs,
1556                });
1557                let prev = self.resolve.package_names.insert(name, id);
1558                assert!(prev.is_none());
1559                id
1560            });
1561
1562        for (name, id) in interfaces {
1563            let prev = self.resolve.packages[pkg].interfaces.insert(name, id);
1564            assert!(prev.is_none());
1565            self.resolve.interfaces[id].package = Some(pkg);
1566        }
1567
1568        for (name, id) in worlds {
1569            let prev = self.resolve.packages[pkg].worlds.insert(name, id);
1570            assert!(prev.is_none());
1571            let world = &mut self.resolve.worlds[id];
1572            world.package = Some(pkg);
1573            for (name, item) in world.imports.iter().chain(world.exports.iter()) {
1574                if let WorldKey::Name(_) = name {
1575                    if let WorldItem::Interface { id, .. } = item {
1576                        self.resolve.interfaces[*id].package = Some(pkg);
1577                    }
1578                }
1579            }
1580        }
1581
1582        pkg
1583    }
1584
1585    fn visit_package(&self, idx: usize, order: &mut IndexSet<usize>) {
1586        if order.contains(&idx) {
1587            return;
1588        }
1589
1590        let (_name, pkg) = self.foreign_packages.get_index(idx).unwrap();
1591        let interfaces = pkg.interfaces.values().copied().chain(
1592            pkg.worlds
1593                .values()
1594                .flat_map(|w| {
1595                    let world = &self.resolve.worlds[*w];
1596                    world.imports.values().chain(world.exports.values())
1597                })
1598                .filter_map(|item| match item {
1599                    WorldItem::Interface { id, .. } => Some(*id),
1600                    WorldItem::Function(_) | WorldItem::Type { .. } => None,
1601                }),
1602        );
1603        for iface in interfaces {
1604            for dep in self.resolve.interface_direct_deps(iface) {
1605                let dep_idx = self.iface_to_package_index[&dep];
1606                if dep_idx != idx {
1607                    self.visit_package(dep_idx, order);
1608                }
1609            }
1610        }
1611
1612        assert!(order.insert(idx));
1613    }
1614}
1615
1616/// Helper type to register the structure of a wasm-defined type against a
1617/// wit-defined type.
1618struct Registrar<'a> {
1619    types: &'a Types,
1620    type_map: &'a mut HashMap<ComponentAnyTypeId, TypeId>,
1621    resolve: &'a Resolve,
1622}
1623
1624impl Registrar<'_> {
1625    /// Verifies that the wasm structure of `def` matches the wit structure of
1626    /// `id` and recursively registers types.
1627    fn defined(&mut self, id: TypeId, def: &ComponentDefinedType) -> Result<()> {
1628        match def {
1629            ComponentDefinedType::Primitive(_) => Ok(()),
1630
1631            ComponentDefinedType::List(t) => {
1632                let ty = match &self.resolve.types[id].kind {
1633                    TypeDefKind::List(r) => r,
1634                    // Note that all cases below have this match and the general
1635                    // idea is that once a type is named or otherwise identified
1636                    // here there's no need to recurse. The purpose of this
1637                    // registrar is to build connections for anonymous types
1638                    // that don't otherwise have a name to ensure that they're
1639                    // decoded to reuse the same constructs consistently. For
1640                    // that reason once something is named we can bail out.
1641                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1642                    _ => bail!("expected a list"),
1643                };
1644                self.valtype(t, ty)
1645            }
1646
1647            ComponentDefinedType::Map(k, v) => {
1648                let (key_ty, value_ty) = match &self.resolve.types[id].kind {
1649                    TypeDefKind::Map(k, v) => (k, v),
1650                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1651                    _ => bail!("expected a map"),
1652                };
1653                self.valtype(k, key_ty)?;
1654                self.valtype(v, value_ty)
1655            }
1656
1657            ComponentDefinedType::FixedLengthList(t, elements) => {
1658                let ty = match &self.resolve.types[id].kind {
1659                    TypeDefKind::FixedLengthList(r, elements2) if elements2 == elements => r,
1660                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1661                    _ => bail!("expected a fixed-length {elements} list"),
1662                };
1663                self.valtype(t, ty)
1664            }
1665
1666            ComponentDefinedType::Tuple(t) => {
1667                let ty = match &self.resolve.types[id].kind {
1668                    TypeDefKind::Tuple(r) => r,
1669                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1670                    _ => bail!("expected a tuple"),
1671                };
1672                if ty.types.len() != t.types.len() {
1673                    bail!("mismatched number of tuple fields");
1674                }
1675                for (a, b) in t.types.iter().zip(ty.types.iter()) {
1676                    self.valtype(a, b)?;
1677                }
1678                Ok(())
1679            }
1680
1681            ComponentDefinedType::Option(t) => {
1682                let ty = match &self.resolve.types[id].kind {
1683                    TypeDefKind::Option(r) => r,
1684                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1685                    _ => bail!("expected an option"),
1686                };
1687                self.valtype(t, ty)
1688            }
1689
1690            ComponentDefinedType::Result { ok, err } => {
1691                let ty = match &self.resolve.types[id].kind {
1692                    TypeDefKind::Result(r) => r,
1693                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1694                    _ => bail!("expected a result"),
1695                };
1696                match (ok, &ty.ok) {
1697                    (Some(a), Some(b)) => self.valtype(a, b)?,
1698                    (None, None) => {}
1699                    _ => bail!("disagreement on result structure"),
1700                }
1701                match (err, &ty.err) {
1702                    (Some(a), Some(b)) => self.valtype(a, b)?,
1703                    (None, None) => {}
1704                    _ => bail!("disagreement on result structure"),
1705                }
1706                Ok(())
1707            }
1708
1709            ComponentDefinedType::Record(def) => {
1710                let ty = match &self.resolve.types[id].kind {
1711                    TypeDefKind::Record(r) => r,
1712                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1713                    _ => bail!("expected a record"),
1714                };
1715                if def.fields.len() != ty.fields.len() {
1716                    bail!("mismatched number of record fields");
1717                }
1718                for ((name, ty), field) in def.fields.iter().zip(&ty.fields) {
1719                    if name.as_str() != field.name {
1720                        bail!("mismatched field order");
1721                    }
1722                    self.valtype(ty, &field.ty)?;
1723                }
1724                Ok(())
1725            }
1726
1727            ComponentDefinedType::Variant(def) => {
1728                let ty = match &self.resolve.types[id].kind {
1729                    TypeDefKind::Variant(r) => r,
1730                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1731                    _ => bail!("expected a variant"),
1732                };
1733                if def.cases.len() != ty.cases.len() {
1734                    bail!("mismatched number of variant cases");
1735                }
1736                for ((name, ty), case) in def.cases.iter().zip(&ty.cases) {
1737                    if name.as_str() != case.name {
1738                        bail!("mismatched case order");
1739                    }
1740                    match (&ty.ty, &case.ty) {
1741                        (Some(a), Some(b)) => self.valtype(a, b)?,
1742                        (None, None) => {}
1743                        _ => bail!("disagreement on case type"),
1744                    }
1745                }
1746                Ok(())
1747            }
1748
1749            ComponentDefinedType::Future(payload) => {
1750                let ty = match &self.resolve.types[id].kind {
1751                    TypeDefKind::Future(p) => p,
1752                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1753                    _ => bail!("expected a future"),
1754                };
1755                match (payload, ty) {
1756                    (Some(a), Some(b)) => self.valtype(a, b),
1757                    (None, None) => Ok(()),
1758                    _ => bail!("disagreement on future payload"),
1759                }
1760            }
1761
1762            ComponentDefinedType::Stream(payload) => {
1763                let ty = match &self.resolve.types[id].kind {
1764                    TypeDefKind::Stream(p) => p,
1765                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1766                    _ => bail!("expected a stream"),
1767                };
1768                match (payload, ty) {
1769                    (Some(a), Some(b)) => self.valtype(a, b),
1770                    (None, None) => Ok(()),
1771                    _ => bail!("disagreement on stream payload"),
1772                }
1773            }
1774
1775            // These have no recursive structure so they can bail out.
1776            ComponentDefinedType::Flags(_)
1777            | ComponentDefinedType::Enum(_)
1778            | ComponentDefinedType::Own(_)
1779            | ComponentDefinedType::Borrow(_) => Ok(()),
1780        }
1781    }
1782
1783    fn valtype(&mut self, wasm: &ComponentValType, wit: &Type) -> Result<()> {
1784        let wasm = match wasm {
1785            ComponentValType::Type(wasm) => *wasm,
1786            ComponentValType::Primitive(_wasm) => {
1787                assert!(!matches!(wit, Type::Id(_)));
1788                return Ok(());
1789            }
1790        };
1791        let wit = match wit {
1792            Type::Id(id) => *id,
1793            _ => bail!("expected id-based type"),
1794        };
1795        let prev = match self.type_map.insert(wasm.into(), wit) {
1796            Some(prev) => prev,
1797            None => {
1798                let wasm = &self.types[wasm];
1799                return self.defined(wit, wasm);
1800            }
1801        };
1802        // If `wit` matches `prev` then we've just rediscovered what we already
1803        // knew which is that the `wasm` id maps to the `wit` id.
1804        //
1805        // If, however, `wit` is not equal to `prev` then that's more
1806        // interesting. Consider a component such as:
1807        //
1808        // ```wasm
1809        // (component
1810        //   (import (interface "a:b/name") (instance
1811        //      (type $l (list string))
1812        //      (type $foo (variant (case "l" $l)))
1813        //      (export "foo" (type (eq $foo)))
1814        //   ))
1815        //   (component $c
1816        //     (type $l (list string))
1817        //     (type $bar (variant (case "n" u16) (case "l" $l)))
1818        //     (export "bar" (type $bar))
1819        //     (type $foo (variant (case "l" $l)))
1820        //     (export "foo" (type $foo))
1821        //   )
1822        //   (instance $i (instantiate $c))
1823        //   (export (interface "a:b/name") (instance $i))
1824        // )
1825        // ```
1826        //
1827        // This roughly corresponds to:
1828        //
1829        // ```wit
1830        // package a:b
1831        //
1832        // interface name {
1833        //   variant bar {
1834        //     n(u16),
1835        //     l(list<string>),
1836        //   }
1837        //
1838        //   variant foo {
1839        //     l(list<string>),
1840        //   }
1841        // }
1842        //
1843        // world module {
1844        //   import name
1845        //   export name
1846        // }
1847        // ```
1848        //
1849        // In this situation first we'll see the `import` which records type
1850        // information for the `foo` type in `interface name`. Later on the full
1851        // picture of `interface name` becomes apparent with the export of a
1852        // component which has full type information. When walking over this
1853        // first `bar` is seen and its recursive structure.
1854        //
1855        // The problem arises when walking over the `foo` type. In this
1856        // situation the code path we're currently on will be hit because
1857        // there's a preexisting definition of `foo` from the import and it's
1858        // now going to be unified with what we've seen in the export. When
1859        // visiting the `list<string>` case of the `foo` variant this ends up
1860        // being different than the `list<string>` used by the `bar` variant. The
1861        // reason for this is that when visiting `bar` the wasm-defined `(list
1862        // string)` hasn't been seen before so a new type is allocated. Later
1863        // though this same wasm type is unified with the first `(list string)`
1864        // type in the `import`.
1865        //
1866        // All-in-all this ends up meaning that it's possible for `prev` to not
1867        // match `wit`. In this situation it means the decoded WIT interface
1868        // will have duplicate definitions of `list<string>`. This is,
1869        // theoretically, not that big of a problem because the same underlying
1870        // definition is still there and the meaning of the type is the same.
1871        // This can, however, perhaps be a problem for consumers where it's
1872        // assumed that all `list<string>` are equal and there's only one. For
1873        // example a bindings generator for C may assume that `list<string>`
1874        // will only appear once and generate a single name for it, but with two
1875        // different types in play here it may generate two types of the same
1876        // name (or something like that).
1877        //
1878        // For now though this is left for a future refactoring. Fixing this
1879        // issue would require tracking anonymous types during type translation
1880        // so the decoding process for the `bar` export would reuse the
1881        // `list<string>` type created from decoding the `foo` import. That's
1882        // somewhat nontrivial at this time, so it's left for a future
1883        // refactoring.
1884        let _ = prev;
1885        Ok(())
1886    }
1887}
1888
1889pub(crate) trait InterfaceNameExt {
1890    fn to_package_name(&self) -> PackageName;
1891}
1892
1893impl InterfaceNameExt for wasmparser::names::InterfaceName<'_> {
1894    fn to_package_name(&self) -> PackageName {
1895        PackageName {
1896            namespace: self.namespace().to_string(),
1897            name: self.package().to_string(),
1898            version: self.version(),
1899        }
1900    }
1901}