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        let prev = self.type_map.insert(created, ty);
1094        assert!(prev.is_none());
1095        Ok(ty)
1096    }
1097
1098    fn register_world<'a>(
1099        &mut self,
1100        name: &str,
1101        ty: &ComponentType,
1102        package: &mut PackageFields<'a>,
1103    ) -> Result<WorldId> {
1104        let name = self
1105            .extract_interface_name_from_component_name(name)?
1106            .context("expected world name to have an ID form")?;
1107        let mut world = World {
1108            name: name.clone(),
1109            docs: Default::default(),
1110            imports: Default::default(),
1111            exports: Default::default(),
1112            includes: Default::default(),
1113            package: None,
1114            stability: Default::default(),
1115            span: Default::default(),
1116        };
1117
1118        let owner = TypeOwner::World(self.resolve.worlds.next_id());
1119        for (name, ty) in ty.imports.iter() {
1120            let (name, item) = match ty.ty {
1121                ComponentEntityType::Instance(idx) => {
1122                    let t = &self.types[idx];
1123                    self.decode_world_instance(name, ty.implements.as_deref(), t, package)?
1124                }
1125                ComponentEntityType::Type {
1126                    created,
1127                    referenced,
1128                } => {
1129                    let ty =
1130                        self.register_type_export(name.as_str(), owner, referenced, created)?;
1131                    (
1132                        WorldKey::Name(name.to_string()),
1133                        WorldItem::Type {
1134                            id: ty,
1135                            span: Default::default(),
1136                        },
1137                    )
1138                }
1139                ComponentEntityType::Func(idx) => {
1140                    let ty = &self.types[idx];
1141                    let func = self.convert_function(name.as_str(), ty, owner)?;
1142                    (WorldKey::Name(name.to_string()), WorldItem::Function(func))
1143                }
1144                _ => bail!("component import `{name}` is not an instance, func, or type"),
1145            };
1146            world.imports.insert(name, item);
1147        }
1148
1149        for (name, item) in ty.exports.iter() {
1150            let (name, item) = match item.ty {
1151                ComponentEntityType::Instance(idx) => {
1152                    let ty = &self.types[idx];
1153                    self.decode_world_instance(name, item.implements.as_deref(), ty, package)?
1154                }
1155
1156                ComponentEntityType::Func(idx) => {
1157                    let ty = &self.types[idx];
1158                    let func = self.convert_function(name.as_str(), ty, owner)?;
1159                    (WorldKey::Name(name.to_string()), WorldItem::Function(func))
1160                }
1161
1162                _ => bail!("component export `{name}` is not an instance or function"),
1163            };
1164            world.exports.insert(name, item);
1165        }
1166        let id = self.resolve.worlds.alloc(world);
1167        let prev = package.worlds.insert(name, id);
1168        assert!(prev.is_none());
1169        Ok(id)
1170    }
1171
1172    fn convert_function(
1173        &mut self,
1174        name: &str,
1175        ty: &ComponentFuncType,
1176        owner: TypeOwner,
1177    ) -> Result<Function> {
1178        let name = ComponentName::new(name, 0).unwrap();
1179        let params = ty
1180            .params
1181            .iter()
1182            .map(|(name, ty)| {
1183                Ok(Param {
1184                    name: name.to_string(),
1185                    ty: self.convert_valtype(ty)?,
1186                    span: Default::default(),
1187                })
1188            })
1189            .collect::<Result<Vec<_>>>()
1190            .context("failed to convert params")?;
1191        let result = match &ty.result {
1192            Some(ty) => Some(
1193                self.convert_valtype(ty)
1194                    .context("failed to convert anonymous result type")?,
1195            ),
1196            None => None,
1197        };
1198        Ok(Function {
1199            docs: Default::default(),
1200            stability: Default::default(),
1201            kind: match name.kind() {
1202                ComponentNameKind::Label(_) => {
1203                    if ty.async_ {
1204                        FunctionKind::AsyncFreestanding
1205                    } else {
1206                        FunctionKind::Freestanding
1207                    }
1208                }
1209                ComponentNameKind::Constructor(resource) => {
1210                    FunctionKind::Constructor(self.resources[&owner][resource.as_str()])
1211                }
1212                ComponentNameKind::Method(name) => {
1213                    if ty.async_ {
1214                        FunctionKind::AsyncMethod(self.resources[&owner][name.resource().as_str()])
1215                    } else {
1216                        FunctionKind::Method(self.resources[&owner][name.resource().as_str()])
1217                    }
1218                }
1219                ComponentNameKind::Static(name) => {
1220                    if ty.async_ {
1221                        FunctionKind::AsyncStatic(self.resources[&owner][name.resource().as_str()])
1222                    } else {
1223                        FunctionKind::Static(self.resources[&owner][name.resource().as_str()])
1224                    }
1225                }
1226
1227                // Functions shouldn't have ID-based names at this time.
1228                ComponentNameKind::Interface(_)
1229                | ComponentNameKind::Url(_)
1230                | ComponentNameKind::Hash(_)
1231                | ComponentNameKind::Dependency(_) => unreachable!(),
1232            },
1233
1234            // Note that this name includes "name mangling" such as
1235            // `[method]foo.bar` which is intentional. The `FunctionKind`
1236            // discriminant calculated above indicates how to interpret this
1237            // name.
1238            name: name.to_string(),
1239            params,
1240            result,
1241            span: Default::default(),
1242        })
1243    }
1244
1245    fn convert_valtype(&mut self, ty: &ComponentValType) -> Result<Type> {
1246        let id = match ty {
1247            ComponentValType::Primitive(ty) => return Ok(self.convert_primitive(*ty)),
1248            ComponentValType::Type(id) => *id,
1249        };
1250
1251        // Don't create duplicate types for anything previously created.
1252        let key: ComponentAnyTypeId = id.into();
1253        if let Some(ret) = self.type_map.get(&key) {
1254            return Ok(Type::Id(*ret));
1255        }
1256
1257        // Otherwise create a new `TypeDef` without a name since this is an
1258        // anonymous valtype. Note that this is invalid for some types so return
1259        // errors on those types, but eventually the `bail!` here  is
1260        // more-or-less unreachable due to expected validation to be added to
1261        // the component model binary format itself.
1262        let def = &self.types[id];
1263        let kind = self.convert_defined(def)?;
1264        match &kind {
1265            TypeDefKind::Type(_)
1266            | TypeDefKind::List(_)
1267            | TypeDefKind::Map(_, _)
1268            | TypeDefKind::FixedLengthList(..)
1269            | TypeDefKind::Tuple(_)
1270            | TypeDefKind::Option(_)
1271            | TypeDefKind::Result(_)
1272            | TypeDefKind::Handle(_)
1273            | TypeDefKind::Future(_)
1274            | TypeDefKind::Stream(_) => {}
1275
1276            TypeDefKind::Resource
1277            | TypeDefKind::Record(_)
1278            | TypeDefKind::Enum(_)
1279            | TypeDefKind::Variant(_)
1280            | TypeDefKind::Flags(_) => {
1281                bail!("unexpected unnamed type of kind '{}'", kind.as_str());
1282            }
1283            TypeDefKind::Unknown => unreachable!(),
1284        }
1285        let ty = self.resolve.types.alloc(TypeDef {
1286            name: None,
1287            docs: Default::default(),
1288            stability: Default::default(),
1289            owner: TypeOwner::None,
1290            kind,
1291            span: Default::default(),
1292        });
1293        let prev = self.type_map.insert(id.into(), ty);
1294        assert!(prev.is_none());
1295        Ok(Type::Id(ty))
1296    }
1297
1298    /// Converts a wasmparser `ComponentDefinedType`, the definition of a type
1299    /// in the component model, to a WIT `TypeDefKind` to get inserted into the
1300    /// types arena by the caller.
1301    fn convert_defined(&mut self, ty: &ComponentDefinedType) -> Result<TypeDefKind> {
1302        match ty {
1303            ComponentDefinedType::Primitive(t) => Ok(TypeDefKind::Type(self.convert_primitive(*t))),
1304
1305            ComponentDefinedType::List(t) => {
1306                let t = self.convert_valtype(t)?;
1307                Ok(TypeDefKind::List(t))
1308            }
1309
1310            ComponentDefinedType::Map(k, v) => {
1311                let k = self.convert_valtype(k)?;
1312                let v = self.convert_valtype(v)?;
1313                Ok(TypeDefKind::Map(k, v))
1314            }
1315
1316            ComponentDefinedType::FixedLengthList(t, size) => {
1317                let t = self.convert_valtype(t)?;
1318                Ok(TypeDefKind::FixedLengthList(t, *size))
1319            }
1320
1321            ComponentDefinedType::Tuple(t) => {
1322                let types = t
1323                    .types
1324                    .iter()
1325                    .map(|t| self.convert_valtype(t))
1326                    .collect::<Result<_>>()?;
1327                Ok(TypeDefKind::Tuple(Tuple { types }))
1328            }
1329
1330            ComponentDefinedType::Option(t) => {
1331                let t = self.convert_valtype(t)?;
1332                Ok(TypeDefKind::Option(t))
1333            }
1334
1335            ComponentDefinedType::Result { ok, err } => {
1336                let ok = match ok {
1337                    Some(t) => Some(self.convert_valtype(t)?),
1338                    None => None,
1339                };
1340                let err = match err {
1341                    Some(t) => Some(self.convert_valtype(t)?),
1342                    None => None,
1343                };
1344                Ok(TypeDefKind::Result(Result_ { ok, err }))
1345            }
1346
1347            ComponentDefinedType::Record(r) => {
1348                let fields = r
1349                    .fields
1350                    .iter()
1351                    .map(|(name, ty)| {
1352                        Ok(Field {
1353                            name: name.to_string(),
1354                            ty: self.convert_valtype(ty).with_context(|| {
1355                                format!("failed to convert record field '{name}'")
1356                            })?,
1357                            docs: Default::default(),
1358                            span: Default::default(),
1359                        })
1360                    })
1361                    .collect::<Result<_>>()?;
1362                Ok(TypeDefKind::Record(Record { fields }))
1363            }
1364
1365            ComponentDefinedType::Variant(v) => {
1366                let cases = v
1367                    .cases
1368                    .iter()
1369                    .map(|(name, case)| {
1370                        Ok(Case {
1371                            name: name.to_string(),
1372                            ty: match &case.ty {
1373                                Some(ty) => Some(self.convert_valtype(ty)?),
1374                                None => None,
1375                            },
1376                            docs: Default::default(),
1377                            span: Default::default(),
1378                        })
1379                    })
1380                    .collect::<Result<_>>()?;
1381                Ok(TypeDefKind::Variant(Variant { cases }))
1382            }
1383
1384            ComponentDefinedType::Flags(f) => {
1385                let flags = f
1386                    .iter()
1387                    .map(|name| Flag {
1388                        name: name.to_string(),
1389                        docs: Default::default(),
1390                        span: Default::default(),
1391                    })
1392                    .collect();
1393                Ok(TypeDefKind::Flags(Flags { flags }))
1394            }
1395
1396            ComponentDefinedType::Enum(e) => {
1397                let cases = e
1398                    .iter()
1399                    .cloned()
1400                    .map(|name| EnumCase {
1401                        name: name.into(),
1402                        docs: Default::default(),
1403                        span: Default::default(),
1404                    })
1405                    .collect();
1406                Ok(TypeDefKind::Enum(Enum { cases }))
1407            }
1408
1409            ComponentDefinedType::Own(id) => {
1410                let key: ComponentAnyTypeId = (*id).into();
1411                let id = self.type_map[&key];
1412                Ok(TypeDefKind::Handle(Handle::Own(id)))
1413            }
1414
1415            ComponentDefinedType::Borrow(id) => {
1416                let key: ComponentAnyTypeId = (*id).into();
1417                let id = self.type_map[&key];
1418                Ok(TypeDefKind::Handle(Handle::Borrow(id)))
1419            }
1420
1421            ComponentDefinedType::Future(ty) => Ok(TypeDefKind::Future(
1422                ty.as_ref().map(|ty| self.convert_valtype(ty)).transpose()?,
1423            )),
1424
1425            ComponentDefinedType::Stream(ty) => Ok(TypeDefKind::Stream(
1426                ty.as_ref().map(|ty| self.convert_valtype(ty)).transpose()?,
1427            )),
1428        }
1429    }
1430
1431    fn convert_primitive(&self, ty: PrimitiveValType) -> Type {
1432        match ty {
1433            PrimitiveValType::U8 => Type::U8,
1434            PrimitiveValType::S8 => Type::S8,
1435            PrimitiveValType::U16 => Type::U16,
1436            PrimitiveValType::S16 => Type::S16,
1437            PrimitiveValType::U32 => Type::U32,
1438            PrimitiveValType::S32 => Type::S32,
1439            PrimitiveValType::U64 => Type::U64,
1440            PrimitiveValType::S64 => Type::S64,
1441            PrimitiveValType::Bool => Type::Bool,
1442            PrimitiveValType::Char => Type::Char,
1443            PrimitiveValType::String => Type::String,
1444            PrimitiveValType::F32 => Type::F32,
1445            PrimitiveValType::F64 => Type::F64,
1446            PrimitiveValType::ErrorContext => Type::ErrorContext,
1447        }
1448    }
1449
1450    fn register_defined(&mut self, id: TypeId, def: &ComponentDefinedType) -> Result<()> {
1451        Registrar {
1452            types: &self.types,
1453            type_map: &mut self.type_map,
1454            resolve: &self.resolve,
1455        }
1456        .defined(id, def)
1457    }
1458
1459    /// Completes the decoding of this resolve by finalizing all packages into
1460    /// their topological ordering within the returned `Resolve`.
1461    ///
1462    /// Takes the root package as an argument to insert.
1463    fn finish(mut self, package: Package) -> (Resolve, PackageId) {
1464        // Build a topological ordering is then calculated by visiting all the
1465        // transitive dependencies of packages.
1466        let mut order = IndexSet::default();
1467        for i in 0..self.foreign_packages.len() {
1468            self.visit_package(i, &mut order);
1469        }
1470
1471        // Using the topological ordering create a temporary map from
1472        // index-in-`foreign_packages` to index-in-`order`
1473        let mut idx_to_pos = vec![0; self.foreign_packages.len()];
1474        for (pos, idx) in order.iter().enumerate() {
1475            idx_to_pos[*idx] = pos;
1476        }
1477        // .. and then using `idx_to_pos` sort the `foreign_packages` array based
1478        // on the position it's at in the topological ordering
1479        let mut deps = mem::take(&mut self.foreign_packages)
1480            .into_iter()
1481            .enumerate()
1482            .collect::<Vec<_>>();
1483        deps.sort_by_key(|(idx, _)| idx_to_pos[*idx]);
1484
1485        // .. and finally insert the packages, in their final topological
1486        // ordering, into the returned array.
1487        for (_idx, (_url, pkg)) in deps {
1488            self.insert_package(pkg);
1489        }
1490
1491        let id = self.insert_package(package);
1492        assert!(self.resolve.worlds.iter().all(|(_, w)| w.package.is_some()));
1493        assert!(
1494            self.resolve
1495                .interfaces
1496                .iter()
1497                .all(|(_, i)| i.package.is_some())
1498        );
1499        (self.resolve, id)
1500    }
1501
1502    fn insert_package(&mut self, package: Package) -> PackageId {
1503        let Package {
1504            name,
1505            interfaces,
1506            worlds,
1507            docs,
1508        } = package;
1509
1510        // Most of the time the `package` being inserted is not already present
1511        // in `self.resolve`, but in the case of the top-level `decode_world`
1512        // function this isn't the case. This shouldn't in general be a problem
1513        // so union-up the packages here while asserting that nothing gets
1514        // replaced by accident which would indicate a bug.
1515        let pkg = self
1516            .resolve
1517            .package_names
1518            .get(&name)
1519            .copied()
1520            .unwrap_or_else(|| {
1521                let id = self.resolve.packages.alloc(Package {
1522                    name: name.clone(),
1523                    interfaces: Default::default(),
1524                    worlds: Default::default(),
1525                    docs,
1526                });
1527                let prev = self.resolve.package_names.insert(name, id);
1528                assert!(prev.is_none());
1529                id
1530            });
1531
1532        for (name, id) in interfaces {
1533            let prev = self.resolve.packages[pkg].interfaces.insert(name, id);
1534            assert!(prev.is_none());
1535            self.resolve.interfaces[id].package = Some(pkg);
1536        }
1537
1538        for (name, id) in worlds {
1539            let prev = self.resolve.packages[pkg].worlds.insert(name, id);
1540            assert!(prev.is_none());
1541            let world = &mut self.resolve.worlds[id];
1542            world.package = Some(pkg);
1543            for (name, item) in world.imports.iter().chain(world.exports.iter()) {
1544                if let WorldKey::Name(_) = name {
1545                    if let WorldItem::Interface { id, .. } = item {
1546                        let iface = &mut self.resolve.interfaces[*id];
1547                        if iface.name.is_none() {
1548                            iface.package = Some(pkg);
1549                        }
1550                    }
1551                }
1552            }
1553        }
1554
1555        pkg
1556    }
1557
1558    fn visit_package(&self, idx: usize, order: &mut IndexSet<usize>) {
1559        if order.contains(&idx) {
1560            return;
1561        }
1562
1563        let (_name, pkg) = self.foreign_packages.get_index(idx).unwrap();
1564        let interfaces = pkg.interfaces.values().copied().chain(
1565            pkg.worlds
1566                .values()
1567                .flat_map(|w| {
1568                    let world = &self.resolve.worlds[*w];
1569                    world.imports.values().chain(world.exports.values())
1570                })
1571                .filter_map(|item| match item {
1572                    WorldItem::Interface { id, .. } => Some(*id),
1573                    WorldItem::Function(_) | WorldItem::Type { .. } => None,
1574                }),
1575        );
1576        for iface in interfaces {
1577            for dep in self.resolve.interface_direct_deps(iface) {
1578                let dep_idx = self.iface_to_package_index[&dep];
1579                if dep_idx != idx {
1580                    self.visit_package(dep_idx, order);
1581                }
1582            }
1583        }
1584
1585        assert!(order.insert(idx));
1586    }
1587}
1588
1589/// Helper type to register the structure of a wasm-defined type against a
1590/// wit-defined type.
1591struct Registrar<'a> {
1592    types: &'a Types,
1593    type_map: &'a mut HashMap<ComponentAnyTypeId, TypeId>,
1594    resolve: &'a Resolve,
1595}
1596
1597impl Registrar<'_> {
1598    /// Verifies that the wasm structure of `def` matches the wit structure of
1599    /// `id` and recursively registers types.
1600    fn defined(&mut self, id: TypeId, def: &ComponentDefinedType) -> Result<()> {
1601        match def {
1602            ComponentDefinedType::Primitive(_) => Ok(()),
1603
1604            ComponentDefinedType::List(t) => {
1605                let ty = match &self.resolve.types[id].kind {
1606                    TypeDefKind::List(r) => r,
1607                    // Note that all cases below have this match and the general
1608                    // idea is that once a type is named or otherwise identified
1609                    // here there's no need to recurse. The purpose of this
1610                    // registrar is to build connections for anonymous types
1611                    // that don't otherwise have a name to ensure that they're
1612                    // decoded to reuse the same constructs consistently. For
1613                    // that reason once something is named we can bail out.
1614                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1615                    _ => bail!("expected a list"),
1616                };
1617                self.valtype(t, ty)
1618            }
1619
1620            ComponentDefinedType::Map(k, v) => {
1621                let (key_ty, value_ty) = match &self.resolve.types[id].kind {
1622                    TypeDefKind::Map(k, v) => (k, v),
1623                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1624                    _ => bail!("expected a map"),
1625                };
1626                self.valtype(k, key_ty)?;
1627                self.valtype(v, value_ty)
1628            }
1629
1630            ComponentDefinedType::FixedLengthList(t, elements) => {
1631                let ty = match &self.resolve.types[id].kind {
1632                    TypeDefKind::FixedLengthList(r, elements2) if elements2 == elements => r,
1633                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1634                    _ => bail!("expected a fixed-length {elements} list"),
1635                };
1636                self.valtype(t, ty)
1637            }
1638
1639            ComponentDefinedType::Tuple(t) => {
1640                let ty = match &self.resolve.types[id].kind {
1641                    TypeDefKind::Tuple(r) => r,
1642                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1643                    _ => bail!("expected a tuple"),
1644                };
1645                if ty.types.len() != t.types.len() {
1646                    bail!("mismatched number of tuple fields");
1647                }
1648                for (a, b) in t.types.iter().zip(ty.types.iter()) {
1649                    self.valtype(a, b)?;
1650                }
1651                Ok(())
1652            }
1653
1654            ComponentDefinedType::Option(t) => {
1655                let ty = match &self.resolve.types[id].kind {
1656                    TypeDefKind::Option(r) => r,
1657                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1658                    _ => bail!("expected an option"),
1659                };
1660                self.valtype(t, ty)
1661            }
1662
1663            ComponentDefinedType::Result { ok, err } => {
1664                let ty = match &self.resolve.types[id].kind {
1665                    TypeDefKind::Result(r) => r,
1666                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1667                    _ => bail!("expected a result"),
1668                };
1669                match (ok, &ty.ok) {
1670                    (Some(a), Some(b)) => self.valtype(a, b)?,
1671                    (None, None) => {}
1672                    _ => bail!("disagreement on result structure"),
1673                }
1674                match (err, &ty.err) {
1675                    (Some(a), Some(b)) => self.valtype(a, b)?,
1676                    (None, None) => {}
1677                    _ => bail!("disagreement on result structure"),
1678                }
1679                Ok(())
1680            }
1681
1682            ComponentDefinedType::Record(def) => {
1683                let ty = match &self.resolve.types[id].kind {
1684                    TypeDefKind::Record(r) => r,
1685                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1686                    _ => bail!("expected a record"),
1687                };
1688                if def.fields.len() != ty.fields.len() {
1689                    bail!("mismatched number of record fields");
1690                }
1691                for ((name, ty), field) in def.fields.iter().zip(&ty.fields) {
1692                    if name.as_str() != field.name {
1693                        bail!("mismatched field order");
1694                    }
1695                    self.valtype(ty, &field.ty)?;
1696                }
1697                Ok(())
1698            }
1699
1700            ComponentDefinedType::Variant(def) => {
1701                let ty = match &self.resolve.types[id].kind {
1702                    TypeDefKind::Variant(r) => r,
1703                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1704                    _ => bail!("expected a variant"),
1705                };
1706                if def.cases.len() != ty.cases.len() {
1707                    bail!("mismatched number of variant cases");
1708                }
1709                for ((name, ty), case) in def.cases.iter().zip(&ty.cases) {
1710                    if name.as_str() != case.name {
1711                        bail!("mismatched case order");
1712                    }
1713                    match (&ty.ty, &case.ty) {
1714                        (Some(a), Some(b)) => self.valtype(a, b)?,
1715                        (None, None) => {}
1716                        _ => bail!("disagreement on case type"),
1717                    }
1718                }
1719                Ok(())
1720            }
1721
1722            ComponentDefinedType::Future(payload) => {
1723                let ty = match &self.resolve.types[id].kind {
1724                    TypeDefKind::Future(p) => p,
1725                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1726                    _ => bail!("expected a future"),
1727                };
1728                match (payload, ty) {
1729                    (Some(a), Some(b)) => self.valtype(a, b),
1730                    (None, None) => Ok(()),
1731                    _ => bail!("disagreement on future payload"),
1732                }
1733            }
1734
1735            ComponentDefinedType::Stream(payload) => {
1736                let ty = match &self.resolve.types[id].kind {
1737                    TypeDefKind::Stream(p) => p,
1738                    TypeDefKind::Type(Type::Id(_)) => return Ok(()),
1739                    _ => bail!("expected a stream"),
1740                };
1741                match (payload, ty) {
1742                    (Some(a), Some(b)) => self.valtype(a, b),
1743                    (None, None) => Ok(()),
1744                    _ => bail!("disagreement on stream payload"),
1745                }
1746            }
1747
1748            // These have no recursive structure so they can bail out.
1749            ComponentDefinedType::Flags(_)
1750            | ComponentDefinedType::Enum(_)
1751            | ComponentDefinedType::Own(_)
1752            | ComponentDefinedType::Borrow(_) => Ok(()),
1753        }
1754    }
1755
1756    fn valtype(&mut self, wasm: &ComponentValType, wit: &Type) -> Result<()> {
1757        let wasm = match wasm {
1758            ComponentValType::Type(wasm) => *wasm,
1759            ComponentValType::Primitive(_wasm) => {
1760                assert!(!matches!(wit, Type::Id(_)));
1761                return Ok(());
1762            }
1763        };
1764        let wit = match wit {
1765            Type::Id(id) => *id,
1766            _ => bail!("expected id-based type"),
1767        };
1768        let prev = match self.type_map.insert(wasm.into(), wit) {
1769            Some(prev) => prev,
1770            None => {
1771                let wasm = &self.types[wasm];
1772                return self.defined(wit, wasm);
1773            }
1774        };
1775        // If `wit` matches `prev` then we've just rediscovered what we already
1776        // knew which is that the `wasm` id maps to the `wit` id.
1777        //
1778        // If, however, `wit` is not equal to `prev` then that's more
1779        // interesting. Consider a component such as:
1780        //
1781        // ```wasm
1782        // (component
1783        //   (import (interface "a:b/name") (instance
1784        //      (type $l (list string))
1785        //      (type $foo (variant (case "l" $l)))
1786        //      (export "foo" (type (eq $foo)))
1787        //   ))
1788        //   (component $c
1789        //     (type $l (list string))
1790        //     (type $bar (variant (case "n" u16) (case "l" $l)))
1791        //     (export "bar" (type $bar))
1792        //     (type $foo (variant (case "l" $l)))
1793        //     (export "foo" (type $foo))
1794        //   )
1795        //   (instance $i (instantiate $c))
1796        //   (export (interface "a:b/name") (instance $i))
1797        // )
1798        // ```
1799        //
1800        // This roughly corresponds to:
1801        //
1802        // ```wit
1803        // package a:b
1804        //
1805        // interface name {
1806        //   variant bar {
1807        //     n(u16),
1808        //     l(list<string>),
1809        //   }
1810        //
1811        //   variant foo {
1812        //     l(list<string>),
1813        //   }
1814        // }
1815        //
1816        // world module {
1817        //   import name
1818        //   export name
1819        // }
1820        // ```
1821        //
1822        // In this situation first we'll see the `import` which records type
1823        // information for the `foo` type in `interface name`. Later on the full
1824        // picture of `interface name` becomes apparent with the export of a
1825        // component which has full type information. When walking over this
1826        // first `bar` is seen and its recursive structure.
1827        //
1828        // The problem arises when walking over the `foo` type. In this
1829        // situation the code path we're currently on will be hit because
1830        // there's a preexisting definition of `foo` from the import and it's
1831        // now going to be unified with what we've seen in the export. When
1832        // visiting the `list<string>` case of the `foo` variant this ends up
1833        // being different than the `list<string>` used by the `bar` variant. The
1834        // reason for this is that when visiting `bar` the wasm-defined `(list
1835        // string)` hasn't been seen before so a new type is allocated. Later
1836        // though this same wasm type is unified with the first `(list string)`
1837        // type in the `import`.
1838        //
1839        // All-in-all this ends up meaning that it's possible for `prev` to not
1840        // match `wit`. In this situation it means the decoded WIT interface
1841        // will have duplicate definitions of `list<string>`. This is,
1842        // theoretically, not that big of a problem because the same underlying
1843        // definition is still there and the meaning of the type is the same.
1844        // This can, however, perhaps be a problem for consumers where it's
1845        // assumed that all `list<string>` are equal and there's only one. For
1846        // example a bindings generator for C may assume that `list<string>`
1847        // will only appear once and generate a single name for it, but with two
1848        // different types in play here it may generate two types of the same
1849        // name (or something like that).
1850        //
1851        // For now though this is left for a future refactoring. Fixing this
1852        // issue would require tracking anonymous types during type translation
1853        // so the decoding process for the `bar` export would reuse the
1854        // `list<string>` type created from decoding the `foo` import. That's
1855        // somewhat nontrivial at this time, so it's left for a future
1856        // refactoring.
1857        let _ = prev;
1858        Ok(())
1859    }
1860}
1861
1862pub(crate) trait InterfaceNameExt {
1863    fn to_package_name(&self) -> PackageName;
1864}
1865
1866impl InterfaceNameExt for wasmparser::names::InterfaceName<'_> {
1867    fn to_package_name(&self) -> PackageName {
1868        PackageName {
1869            namespace: self.namespace().to_string(),
1870            name: self.package().to_string(),
1871            version: self.version(),
1872        }
1873    }
1874}