wasmparser/readers/component/
exports.rs1use crate::{
2 BinaryReader, ComponentExternName, ComponentTypeRef, FromReader, Result, SectionLimited,
3};
4
5#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub enum ComponentExternalKind {
8 Module,
10 Func,
12 Value,
14 Type,
16 Instance,
18 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 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#[derive(Debug, Clone, Eq, PartialEq)]
70pub struct ComponentExport<'a> {
71 pub name: ComponentExternName<'a>,
73 pub kind: ComponentExternalKind,
75 pub index: u32,
77 pub ty: Option<ComponentTypeRef>,
79}
80
81pub 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}