wasmparser_nostd/readers/component/
exports.rs

1use crate::{BinaryReader, ComponentTypeRef, FromReader, Result, SectionLimited};
2
3/// Represents the kind of an external items of a WebAssembly component.
4#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5pub enum ComponentExternalKind {
6    /// The external kind is a core module.
7    Module,
8    /// The external kind is a function.
9    Func,
10    /// The external kind is a value.
11    Value,
12    /// The external kind is a type.
13    Type,
14    /// The external kind is an instance.
15    Instance,
16    /// The external kind is a component.
17    Component,
18}
19
20impl ComponentExternalKind {
21    pub(crate) fn from_bytes(
22        byte1: u8,
23        byte2: Option<u8>,
24        offset: usize,
25    ) -> Result<ComponentExternalKind> {
26        Ok(match byte1 {
27            0x00 => match byte2.unwrap() {
28                0x11 => ComponentExternalKind::Module,
29                x => {
30                    return Err(BinaryReader::invalid_leading_byte_error(
31                        x,
32                        "component external kind",
33                        offset + 1,
34                    ))
35                }
36            },
37            0x01 => ComponentExternalKind::Func,
38            0x02 => ComponentExternalKind::Value,
39            0x03 => ComponentExternalKind::Type,
40            0x04 => ComponentExternalKind::Component,
41            0x05 => ComponentExternalKind::Instance,
42            x => {
43                return Err(BinaryReader::invalid_leading_byte_error(
44                    x,
45                    "component external kind",
46                    offset,
47                ))
48            }
49        })
50    }
51}
52
53/// Represents an export in a WebAssembly component.
54#[derive(Debug, Clone)]
55pub struct ComponentExport<'a> {
56    /// The name of the exported item.
57    pub name: &'a str,
58    /// The optional URL of the exported item.
59    pub url: &'a str,
60    /// The kind of the export.
61    pub kind: ComponentExternalKind,
62    /// The index of the exported item.
63    pub index: u32,
64    /// An optionally specified type ascribed to this export.
65    pub ty: Option<ComponentTypeRef>,
66}
67
68/// A reader for the export section of a WebAssembly component.
69pub type ComponentExportSectionReader<'a> = SectionLimited<'a, ComponentExport<'a>>;
70
71impl<'a> FromReader<'a> for ComponentExport<'a> {
72    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
73        Ok(ComponentExport {
74            name: reader.read()?,
75            url: reader.read()?,
76            kind: reader.read()?,
77            index: reader.read()?,
78            ty: match reader.read_u8()? {
79                0x00 => None,
80                0x01 => Some(reader.read()?),
81                other => {
82                    return Err(BinaryReader::invalid_leading_byte_error(
83                        other,
84                        "optional component export type",
85                        reader.original_position() - 1,
86                    ))
87                }
88            },
89        })
90    }
91}
92
93impl<'a> FromReader<'a> for ComponentExternalKind {
94    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
95        let offset = reader.original_position();
96        let byte1 = reader.read_u8()?;
97        let byte2 = if byte1 == 0x00 {
98            Some(reader.read_u8()?)
99        } else {
100            None
101        };
102
103        ComponentExternalKind::from_bytes(byte1, byte2, offset)
104    }
105}