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