Debian Codegraph @

crates/wasmparser/src/readers/component/names.rs

raw
1 use crate::{BinaryReader, BinaryReaderError, NameMap, Result, Subsection, Subsections};
2 use core::ops::Range;
3
4 /// Type used to iterate and parse the contents of the `component-name` custom
5 /// section in components, similar to the `name` section of core modules.
6 pub type ComponentNameSectionReader<'a> = Subsections<'a, ComponentName<'a>>;
7
8 /// Represents a name read from the names custom section.
9 #[derive(Clone)]
10 #[allow(missing_docs)]
11 pub enum ComponentName<'a> {
12 Component {
13 name: &'a str,
14 name_range: Range<usize>,
15 },
16 CoreFuncs(NameMap<'a>),
17 CoreGlobals(NameMap<'a>),
18 CoreMemories(NameMap<'a>),
19 CoreTables(NameMap<'a>),
20 CoreTags(NameMap<'a>),
21 CoreModules(NameMap<'a>),
22 CoreInstances(NameMap<'a>),
23 CoreTypes(NameMap<'a>),
24 Types(NameMap<'a>),
25 Instances(NameMap<'a>),
26 Components(NameMap<'a>),
27 Funcs(NameMap<'a>),
28 Values(NameMap<'a>),
29
30 /// An unknown [name subsection](https://webassembly.github.io/spec/core/appendix/custom.html#subsections).
31 Unknown {
32 /// The identifier for this subsection.
33 ty: u8,
34 /// The contents of this subsection.
35 data: &'a [u8],
36 /// The range of bytes, relative to the start of the original data
37 /// stream, that the contents of this subsection reside in.
38 range: Range<usize>,
39 },
40 }
41
42 impl<'a> Subsection<'a> for ComponentName<'a> {
43 fn from_reader(id: u8, mut reader: BinaryReader<'a>) -> Result<Self> {
44 let data = reader.remaining_buffer();
45 let offset = reader.original_position();
46 Ok(match id {
47 0 => {
48 let name = reader.read_string()?;
49 if !reader.eof() {
50 return Err(BinaryReaderError::new(
51 "trailing data at the end of a name",
52 reader.original_position(),
53 ));
54 }
55 ComponentName::Component {
56 name,
57 name_range: offset..reader.original_position(),
58 }
59 }
60 1 => {
61 let ctor: fn(NameMap<'a>) -> ComponentName<'a> = match reader.read_u8()? {
62 0x00 => match reader.read_u8()? {
63 0x00 => ComponentName::CoreFuncs,
64 0x01 => ComponentName::CoreTables,
65 0x02 => ComponentName::CoreMemories,
66 0x03 => ComponentName::CoreGlobals,
67 0x04 => ComponentName::CoreTags,
68 0x10 => ComponentName::CoreTypes,
69 0x11 => ComponentName::CoreModules,
70 0x12 => ComponentName::CoreInstances,
71 _ => {
72 return Ok(ComponentName::Unknown {
73 ty: 1,
74 data,
75 range: offset..offset + data.len(),
76 });
77 }
78 },
79 0x01 => ComponentName::Funcs,
80 0x02 => ComponentName::Values,
81 0x03 => ComponentName::Types,
82 0x04 => ComponentName::Components,
83 0x05 => ComponentName::Instances,
84 _ => {
85 return Ok(ComponentName::Unknown {
86 ty: 1,
87 data,
88 range: offset..offset + data.len(),
89 });
90 }
91 };
92 ctor(NameMap::new(reader.shrink())?)
93 }
94 ty => ComponentName::Unknown {
95 ty,
96 data,
97 range: offset..offset + data.len(),
98 },
99 })
100 }
101 }