1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use crate::{
BinaryReader, BinaryReaderError, FromReader, Result, SectionLimited, Subsection, Subsections,
};
use std::ops::Range;
pub type NameMap<'a> = SectionLimited<'a, Naming<'a>>;
#[derive(Debug, Copy, Clone)]
pub struct Naming<'a> {
pub index: u32,
pub name: &'a str,
}
impl<'a> FromReader<'a> for Naming<'a> {
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
let index = reader.read_var_u32()?;
let name = reader.read_string()?;
Ok(Naming { index, name })
}
}
pub type IndirectNameMap<'a> = SectionLimited<'a, IndirectNaming<'a>>;
#[derive(Debug, Clone)]
pub struct IndirectNaming<'a> {
pub index: u32,
pub names: NameMap<'a>,
}
impl<'a> FromReader<'a> for IndirectNaming<'a> {
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
let index = reader.read_var_u32()?;
let names = reader.skip(|reader| {
let count = reader.read_var_u32()?;
for _ in 0..count {
reader.read_var_u32()?;
reader.skip_string()?;
}
Ok(())
})?;
Ok(IndirectNaming {
index,
names: NameMap::new(names.remaining_buffer(), names.original_position())?,
})
}
}
#[derive(Clone)]
pub enum Name<'a> {
Module {
name: &'a str,
name_range: Range<usize>,
},
Function(NameMap<'a>),
Local(IndirectNameMap<'a>),
Label(IndirectNameMap<'a>),
Type(NameMap<'a>),
Table(NameMap<'a>),
Memory(NameMap<'a>),
Global(NameMap<'a>),
Element(NameMap<'a>),
Data(NameMap<'a>),
Unknown {
ty: u8,
data: &'a [u8],
range: Range<usize>,
},
}
pub type NameSectionReader<'a> = Subsections<'a, Name<'a>>;
impl<'a> Subsection<'a> for Name<'a> {
fn from_reader(id: u8, mut reader: BinaryReader<'a>) -> Result<Self> {
let data = reader.remaining_buffer();
let offset = reader.original_position();
Ok(match id {
0 => {
let name = reader.read_string()?;
if !reader.eof() {
return Err(BinaryReaderError::new(
"trailing data at the end of a name",
reader.original_position(),
));
}
Name::Module {
name,
name_range: offset..offset + reader.position,
}
}
1 => Name::Function(NameMap::new(data, offset)?),
2 => Name::Local(IndirectNameMap::new(data, offset)?),
3 => Name::Label(IndirectNameMap::new(data, offset)?),
4 => Name::Type(NameMap::new(data, offset)?),
5 => Name::Table(NameMap::new(data, offset)?),
6 => Name::Memory(NameMap::new(data, offset)?),
7 => Name::Global(NameMap::new(data, offset)?),
8 => Name::Element(NameMap::new(data, offset)?),
9 => Name::Data(NameMap::new(data, offset)?),
ty => Name::Unknown {
ty,
data,
range: offset..offset + data.len(),
},
})
}
}