wit_parser/
decoding.rs

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