Skip to main content

wit_parser/
decoding.rs

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