1 /* Copyright 2018 Mozilla Foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 use super::{BinaryReader, Result, SectionIteratorLimited, SectionReader, SectionWithLimitedItems};
17 
18 pub struct FunctionSectionReader<'a> {
19     reader: BinaryReader<'a>,
20     count: u32,
21 }
22 
23 impl<'a> FunctionSectionReader<'a> {
new(data: &'a [u8], offset: usize) -> Result<FunctionSectionReader<'a>>24     pub fn new(data: &'a [u8], offset: usize) -> Result<FunctionSectionReader<'a>> {
25         let mut reader = BinaryReader::new_with_offset(data, offset);
26         let count = reader.read_var_u32()?;
27         Ok(FunctionSectionReader { reader, count })
28     }
29 
original_position(&self) -> usize30     pub fn original_position(&self) -> usize {
31         self.reader.original_position()
32     }
33 
get_count(&self) -> u3234     pub fn get_count(&self) -> u32 {
35         self.count
36     }
37 
38     /// Reads function type index from the function section.
39     ///
40     /// # Examples
41     /// ```
42     /// # let data: &[u8] = &[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
43     /// #     0x01, 0x4, 0x01, 0x60, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00,
44     /// #     0x0a, 0x05, 0x01, 0x03, 0x00, 0x01, 0x0b];
45     /// use wasmparser::ModuleReader;
46     /// let mut reader = ModuleReader::new(data).expect("module reader");
47     /// let section = reader.read().expect("type section");
48     /// let section = reader.read().expect("function section");
49     /// let mut function_reader = section.get_function_section_reader().expect("function section reader");
50     /// for _ in 0..function_reader.get_count() {
51     ///     let ty = function_reader.read().expect("function type index");
52     ///     println!("Function type index: {}", ty);
53     /// }
54     /// ```
read(&mut self) -> Result<u32>55     pub fn read(&mut self) -> Result<u32> {
56         self.reader.read_var_u32()
57     }
58 }
59 
60 impl<'a> SectionReader for FunctionSectionReader<'a> {
61     type Item = u32;
read(&mut self) -> Result<Self::Item>62     fn read(&mut self) -> Result<Self::Item> {
63         FunctionSectionReader::read(self)
64     }
eof(&self) -> bool65     fn eof(&self) -> bool {
66         self.reader.eof()
67     }
original_position(&self) -> usize68     fn original_position(&self) -> usize {
69         FunctionSectionReader::original_position(self)
70     }
71 }
72 
73 impl<'a> SectionWithLimitedItems for FunctionSectionReader<'a> {
get_count(&self) -> u3274     fn get_count(&self) -> u32 {
75         FunctionSectionReader::get_count(self)
76     }
77 }
78 
79 impl<'a> IntoIterator for FunctionSectionReader<'a> {
80     type Item = Result<u32>;
81     type IntoIter = SectionIteratorLimited<FunctionSectionReader<'a>>;
82 
into_iter(self) -> Self::IntoIter83     fn into_iter(self) -> Self::IntoIter {
84         SectionIteratorLimited::new(self)
85     }
86 }
87