wasmparser/readers/component/
imports.rs

1use crate::{
2    BinaryReader, ComponentExternalKind, ComponentValType, FromReader, Result, SectionLimited,
3};
4
5/// Represents the type bounds for imports and exports.
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub enum TypeBounds {
8    /// The type is bounded by equality.
9    Eq,
10}
11
12impl<'a> FromReader<'a> for TypeBounds {
13    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
14        Ok(match reader.read_u8()? {
15            0x00 => TypeBounds::Eq,
16            x => return reader.invalid_leading_byte(x, "type bound"),
17        })
18    }
19}
20
21/// Represents a reference to a component type.
22#[derive(Clone, Copy, Debug, Eq, PartialEq)]
23pub enum ComponentTypeRef {
24    /// The reference is to a core module type.
25    ///
26    /// The index is expected to be core type index to a core module type.
27    Module(u32),
28    /// The reference is to a function type.
29    ///
30    /// The index is expected to be a type index to a function type.
31    Func(u32),
32    /// The reference is to a value type.
33    Value(ComponentValType),
34    /// The reference is to a bounded type.
35    ///
36    /// The index is expected to be a type index.
37    Type(TypeBounds, u32),
38    /// The reference is to an instance type.
39    ///
40    /// The index is a type index to an instance type.
41    Instance(u32),
42    /// The reference is to a component type.
43    ///
44    /// The index is a type index to a component type.
45    Component(u32),
46}
47
48impl ComponentTypeRef {
49    /// Returns the corresponding [`ComponentExternalKind`] for this reference.
50    pub fn kind(&self) -> ComponentExternalKind {
51        match self {
52            ComponentTypeRef::Module(_) => ComponentExternalKind::Module,
53            ComponentTypeRef::Func(_) => ComponentExternalKind::Func,
54            ComponentTypeRef::Value(_) => ComponentExternalKind::Value,
55            ComponentTypeRef::Type(..) => ComponentExternalKind::Type,
56            ComponentTypeRef::Instance(_) => ComponentExternalKind::Instance,
57            ComponentTypeRef::Component(_) => ComponentExternalKind::Component,
58        }
59    }
60}
61
62impl<'a> FromReader<'a> for ComponentTypeRef {
63    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
64        Ok(match reader.read()? {
65            ComponentExternalKind::Module => ComponentTypeRef::Module(reader.read()?),
66            ComponentExternalKind::Func => ComponentTypeRef::Func(reader.read()?),
67            ComponentExternalKind::Value => ComponentTypeRef::Value(reader.read()?),
68            ComponentExternalKind::Type => ComponentTypeRef::Type(reader.read()?, reader.read()?),
69            ComponentExternalKind::Instance => ComponentTypeRef::Instance(reader.read()?),
70            ComponentExternalKind::Component => ComponentTypeRef::Component(reader.read()?),
71        })
72    }
73}
74
75/// Represents an import in a WebAssembly component
76#[derive(Debug, Copy, Clone)]
77pub struct ComponentImport<'a> {
78    /// The name of the imported item.
79    pub name: &'a str,
80    /// The optional URL of the imported item.
81    pub url: &'a str,
82    /// The type reference for the import.
83    pub ty: ComponentTypeRef,
84}
85
86impl<'a> FromReader<'a> for ComponentImport<'a> {
87    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
88        Ok(ComponentImport {
89            name: reader.read()?,
90            url: reader.read()?,
91            ty: reader.read()?,
92        })
93    }
94}
95
96/// A reader for the import section of a WebAssembly component.
97///
98/// # Examples
99///
100/// ```
101/// use wasmparser::ComponentImportSectionReader;
102/// let data: &[u8] = &[0x01, 0x01, 0x41, 0x00, 0x01, 0x66];
103/// let reader = ComponentImportSectionReader::new(data, 0).unwrap();
104/// for import in reader {
105///     let import = import.expect("import");
106///     println!("Import: {:?}", import);
107/// }
108/// ```
109pub type ComponentImportSectionReader<'a> = SectionLimited<'a, ComponentImport<'a>>;