/src/wasm-tools/crates/wasmparser/src/readers/component/names.rs
Line | Count | Source |
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 | 0 | fn from_reader(id: u8, mut reader: BinaryReader<'a>) -> Result<Self> { |
44 | 0 | let data = reader.remaining_buffer(); |
45 | 0 | let offset = reader.original_position(); |
46 | 0 | Ok(match id { |
47 | | 0 => { |
48 | 0 | let name = reader.read_unlimited_string()?; |
49 | 0 | if !reader.eof() { |
50 | 0 | return Err(BinaryReaderError::new( |
51 | 0 | "trailing data at the end of a name", |
52 | 0 | reader.original_position(), |
53 | 0 | )); |
54 | 0 | } |
55 | 0 | ComponentName::Component { |
56 | 0 | name, |
57 | 0 | name_range: offset..reader.original_position(), |
58 | 0 | } |
59 | | } |
60 | | 1 => { |
61 | 0 | let ctor: fn(NameMap<'a>) -> ComponentName<'a> = match reader.read_u8()? { |
62 | 0 | 0x00 => match reader.read_u8()? { |
63 | 0 | 0x00 => ComponentName::CoreFuncs, |
64 | 0 | 0x01 => ComponentName::CoreTables, |
65 | 0 | 0x02 => ComponentName::CoreMemories, |
66 | 0 | 0x03 => ComponentName::CoreGlobals, |
67 | 0 | 0x04 => ComponentName::CoreTags, |
68 | 0 | 0x10 => ComponentName::CoreTypes, |
69 | 0 | 0x11 => ComponentName::CoreModules, |
70 | 0 | 0x12 => ComponentName::CoreInstances, |
71 | | _ => { |
72 | 0 | return Ok(ComponentName::Unknown { |
73 | 0 | ty: 1, |
74 | 0 | data, |
75 | 0 | range: offset..offset + data.len(), |
76 | 0 | }); |
77 | | } |
78 | | }, |
79 | 0 | 0x01 => ComponentName::Funcs, |
80 | 0 | 0x02 => ComponentName::Values, |
81 | 0 | 0x03 => ComponentName::Types, |
82 | 0 | 0x04 => ComponentName::Components, |
83 | 0 | 0x05 => ComponentName::Instances, |
84 | | _ => { |
85 | 0 | return Ok(ComponentName::Unknown { |
86 | 0 | ty: 1, |
87 | 0 | data, |
88 | 0 | range: offset..offset + data.len(), |
89 | 0 | }); |
90 | | } |
91 | | }; |
92 | 0 | ctor(NameMap::new(reader.shrink())?) |
93 | | } |
94 | 0 | ty => ComponentName::Unknown { |
95 | 0 | ty, |
96 | 0 | data, |
97 | 0 | range: offset..offset + data.len(), |
98 | 0 | }, |
99 | | }) |
100 | 0 | } |
101 | | } |