wasmparser/readers/core/
code.rs1use crate::{BinaryReader, FromReader, OperatorsReader, Result, SectionLimited, ValType};
17use core::ops::Range;
18
19pub type CodeSectionReader<'a> = SectionLimited<'a, FunctionBody<'a>>;
21
22#[derive(Debug, Clone)]
24pub struct FunctionBody<'a> {
25 reader: BinaryReader<'a>,
26}
27
28impl<'a> FunctionBody<'a> {
29 pub fn new(reader: BinaryReader<'a>) -> Self {
31 Self { reader }
32 }
33
34 pub fn get_binary_reader(&self) -> BinaryReader<'a> {
36 self.reader.clone()
37 }
38
39 fn skip_locals(reader: &mut BinaryReader) -> Result<()> {
40 let count = reader.read_var_u32()?;
41 for _ in 0..count {
42 reader.read_var_u32()?;
43 reader.read::<ValType>()?;
44 }
45 Ok(())
46 }
47
48 pub fn get_locals_reader(&self) -> Result<LocalsReader<'a>> {
50 let mut reader = self.reader.clone();
51 let declaration_count = reader.read_var_u32()?;
52 Ok(LocalsReader {
53 reader,
54 declaration_count,
55 total_count: 0,
56 })
57 }
58
59 pub fn get_binary_reader_for_operators(&self) -> Result<BinaryReader<'a>> {
61 let mut reader = self.reader.clone();
62 Self::skip_locals(&mut reader)?;
63 Ok(reader)
64 }
65
66 pub fn get_operators_reader(&self) -> Result<OperatorsReader<'a>> {
69 Ok(OperatorsReader::new(
70 self.get_binary_reader_for_operators()?,
71 ))
72 }
73
74 pub fn range(&self) -> Range<usize> {
76 self.reader.range()
77 }
78
79 pub fn as_bytes(&self) -> &'a [u8] {
83 self.reader.remaining_buffer()
84 }
85}
86
87impl<'a> FromReader<'a> for FunctionBody<'a> {
88 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
89 let reader = reader.read_reader()?;
90 Ok(FunctionBody::new(reader))
91 }
92}
93
94pub struct LocalsReader<'a> {
96 reader: BinaryReader<'a>,
97 declaration_count: u32,
98 total_count: u32,
99}
100
101impl<'a> LocalsReader<'a> {
102 pub fn get_count(&self) -> u32 {
104 self.declaration_count
105 }
106
107 pub fn original_position(&self) -> usize {
109 self.reader.original_position()
110 }
111
112 pub fn read(&mut self) -> Result<(u32, ValType)> {
114 let count = self.reader.read()?;
115 match self.total_count.checked_add(count) {
116 Some(total) => self.total_count = total,
117 None => bail!(self.reader.original_position(), "too many locals"),
118 }
119 let value_type = self.reader.read()?;
120 Ok((count, value_type))
121 }
122
123 pub fn get_binary_reader(self) -> BinaryReader<'a> {
125 self.reader
126 }
127}
128
129impl<'a> IntoIterator for LocalsReader<'a> {
130 type Item = Result<(u32, ValType)>;
131 type IntoIter = LocalsIterator<'a>;
132 fn into_iter(self) -> Self::IntoIter {
133 let count = self.declaration_count;
134 LocalsIterator {
135 reader: self,
136 left: count,
137 err: false,
138 }
139 }
140}
141
142pub struct LocalsIterator<'a> {
144 reader: LocalsReader<'a>,
145 left: u32,
146 err: bool,
147}
148
149impl<'a> Iterator for LocalsIterator<'a> {
150 type Item = Result<(u32, ValType)>;
151 fn next(&mut self) -> Option<Self::Item> {
152 if self.err || self.left == 0 {
153 return None;
154 }
155 let result = self.reader.read();
156 self.err = result.is_err();
157 self.left -= 1;
158 Some(result)
159 }
160 fn size_hint(&self) -> (usize, Option<usize>) {
161 let count = self.reader.get_count() as usize;
162 (count, Some(count))
163 }
164}
165
166impl<'a> LocalsIterator<'a> {
167 pub fn into_binary_reader_for_operators(self) -> BinaryReader<'a> {
169 debug_assert!(self.err || self.left == 0);
170 self.reader.get_binary_reader()
171 }
172
173 pub fn into_operators_reader(self) -> OperatorsReader<'a> {
175 OperatorsReader::new(self.reader.get_binary_reader())
176 }
177}