wasmparser/readers/component/
exports.rs1use crate::{BinaryReader, ComponentTypeRef, FromReader, Result, SectionLimited};
2
3#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5pub enum ComponentExternalKind {
6 Module,
8 Func,
10 Value,
12 Type,
14 Instance,
16 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 pub fn desc(&self) -> &'static str {
54 use ComponentExternalKind::*;
55 match self {
56 Module => "module",
57 Func => "func",
58 Value => "value",
59 Type => "type",
60 Instance => "instance",
61 Component => "component",
62 }
63 }
64}
65
66#[derive(Debug, Clone, Eq, PartialEq)]
68pub struct ComponentExport<'a> {
69 pub name: ComponentExportName<'a>,
71 pub kind: ComponentExternalKind,
73 pub index: u32,
75 pub ty: Option<ComponentTypeRef>,
77}
78
79pub type ComponentExportSectionReader<'a> = SectionLimited<'a, ComponentExport<'a>>;
81
82impl<'a> FromReader<'a> for ComponentExport<'a> {
83 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
84 Ok(ComponentExport {
85 name: reader.read()?,
86 kind: reader.read()?,
87 index: reader.read()?,
88 ty: match reader.read_u8()? {
89 0x00 => None,
90 0x01 => Some(reader.read()?),
91 other => {
92 return Err(BinaryReader::invalid_leading_byte_error(
93 other,
94 "optional component export type",
95 reader.original_position() - 1,
96 ))
97 }
98 },
99 })
100 }
101}
102
103impl<'a> FromReader<'a> for ComponentExternalKind {
104 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
105 let offset = reader.original_position();
106 let byte1 = reader.read_u8()?;
107 let byte2 = if byte1 == 0x00 {
108 Some(reader.read_u8()?)
109 } else {
110 None
111 };
112
113 ComponentExternalKind::from_bytes(byte1, byte2, offset)
114 }
115}
116
117#[derive(Debug, Copy, Clone, Eq, PartialEq)]
119#[allow(missing_docs)]
120pub struct ComponentExportName<'a>(pub &'a str);
121
122impl<'a> FromReader<'a> for ComponentExportName<'a> {
123 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
124 match reader.read_u8()? {
125 0x00 => {}
126 0x01 => {}
131 x => return reader.invalid_leading_byte(x, "export name"),
132 }
133 Ok(ComponentExportName(reader.read_string()?))
134 }
135}