Skip to main content

wasmparser/readers/component/
exports.rs

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