wasmparser_nostd/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
53#[derive(Debug, Clone)]
55pub struct ComponentExport<'a> {
56 pub name: &'a str,
58 pub url: &'a str,
60 pub kind: ComponentExternalKind,
62 pub index: u32,
64 pub ty: Option<ComponentTypeRef>,
66}
67
68pub 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}