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::{ 17 BinaryReader, FuncType, Result, SectionIteratorLimited, SectionReader, SectionWithLimitedItems, 18 }; 19 20 pub struct TypeSectionReader<'a> { 21 reader: BinaryReader<'a>, 22 count: u32, 23 } 24 25 impl<'a> TypeSectionReader<'a> { new(data: &'a [u8], offset: usize) -> Result<TypeSectionReader<'a>>26 pub fn new(data: &'a [u8], offset: usize) -> Result<TypeSectionReader<'a>> { 27 let mut reader = BinaryReader::new_with_offset(data, offset); 28 let count = reader.read_var_u32()?; 29 Ok(TypeSectionReader { reader, count }) 30 } 31 original_position(&self) -> usize32 pub fn original_position(&self) -> usize { 33 self.reader.original_position() 34 } 35 get_count(&self) -> u3236 pub fn get_count(&self) -> u32 { 37 self.count 38 } 39 40 /// Reads content of the type section. 41 /// 42 /// # Examples 43 /// ``` 44 /// # let data: &[u8] = &[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 45 /// # 0x01, 0x4, 0x01, 0x60, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 46 /// # 0x0a, 0x05, 0x01, 0x03, 0x00, 0x01, 0x0b]; 47 /// use wasmparser::ModuleReader; 48 /// let mut reader = ModuleReader::new(data).expect("module reader"); 49 /// let section = reader.read().expect("section"); 50 /// let mut type_reader = section.get_type_section_reader().expect("type section reader"); 51 /// for _ in 0..type_reader.get_count() { 52 /// let ty = type_reader.read().expect("type"); 53 /// println!("Type {:?}", ty); 54 /// } 55 /// ``` read(&mut self) -> Result<FuncType>56 pub fn read(&mut self) -> Result<FuncType> { 57 self.reader.read_func_type() 58 } 59 } 60 61 impl<'a> SectionReader for TypeSectionReader<'a> { 62 type Item = FuncType; read(&mut self) -> Result<Self::Item>63 fn read(&mut self) -> Result<Self::Item> { 64 TypeSectionReader::read(self) 65 } eof(&self) -> bool66 fn eof(&self) -> bool { 67 self.reader.eof() 68 } original_position(&self) -> usize69 fn original_position(&self) -> usize { 70 TypeSectionReader::original_position(self) 71 } 72 } 73 74 impl<'a> SectionWithLimitedItems for TypeSectionReader<'a> { get_count(&self) -> u3275 fn get_count(&self) -> u32 { 76 TypeSectionReader::get_count(self) 77 } 78 } 79 80 impl<'a> IntoIterator for TypeSectionReader<'a> { 81 type Item = Result<FuncType>; 82 type IntoIter = SectionIteratorLimited<TypeSectionReader<'a>>; 83 84 /// Implements iterator over the type section. 85 /// 86 /// # Examples 87 /// ``` 88 /// # let data: &[u8] = &[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 89 /// # 0x01, 0x4, 0x01, 0x60, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 90 /// # 0x0a, 0x05, 0x01, 0x03, 0x00, 0x01, 0x0b]; 91 /// use wasmparser::ModuleReader; 92 /// use wasmparser::{Result, FuncType}; 93 /// let mut reader = ModuleReader::new(data).expect("module reader"); 94 /// let section = reader.read().expect("section"); 95 /// let mut type_reader = section.get_type_section_reader().expect("type section reader"); 96 /// for ty in type_reader { 97 /// println!("Type {:?}", ty); 98 /// } 99 /// ``` into_iter(self) -> Self::IntoIter100 fn into_iter(self) -> Self::IntoIter { 101 SectionIteratorLimited::new(self) 102 } 103 } 104