Coverage Report

Created: 2021-03-22 08:29

/src/wasm-tools/crates/wasmparser/src/readers/table_section.rs
Line
Count
Source
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, Range, Result, SectionIteratorLimited, SectionReader, SectionWithLimitedItems,
18
    TableType,
19
};
20
21
132k
#[derive(Clone)]
22
pub struct TableSectionReader<'a> {
23
    reader: BinaryReader<'a>,
24
    count: u32,
25
}
26
27
impl<'a> TableSectionReader<'a> {
28
163k
    pub fn new(data: &'a [u8], offset: usize) -> Result<TableSectionReader<'a>> {
29
163k
        let mut reader = BinaryReader::new_with_offset(data, offset);
30
163k
        let count = reader.read_var_u32()?;
31
163k
        Ok(TableSectionReader { reader, count })
32
163k
    }
33
34
608k
    pub fn original_position(&self) -> usize {
35
608k
        self.reader.original_position()
36
608k
    }
37
38
276k
    pub fn get_count(&self) -> u32 {
39
276k
        self.count
40
276k
    }
41
42
    /// Reads content of the table section.
43
    ///
44
    /// # Examples
45
    /// ```
46
    /// use wasmparser::TableSectionReader;
47
    ///
48
    /// # let data: &[u8] = &[0x01, 0x70, 0x01, 0x01, 0x01];
49
    /// let mut table_reader = TableSectionReader::new(data, 0).unwrap();
50
    /// for _ in 0..table_reader.get_count() {
51
    ///     let table = table_reader.read().expect("table");
52
    ///     println!("Table: {:?}", table);
53
    /// }
54
    /// ```
55
619k
    pub fn read(&mut self) -> Result<TableType> {
56
619k
        self.reader.read_table_type()
57
619k
    }
58
}
59
60
impl<'a> SectionReader for TableSectionReader<'a> {
61
    type Item = TableType;
62
619k
    fn read(&mut self) -> Result<Self::Item> {
63
619k
        TableSectionReader::read(self)
64
619k
    }
65
143k
    fn eof(&self) -> bool {
66
143k
        self.reader.eof()
67
143k
    }
68
608k
    fn original_position(&self) -> usize {
69
608k
        TableSectionReader::original_position(self)
70
608k
    }
71
265k
    fn range(&self) -> Range {
72
265k
        self.reader.range()
73
265k
    }
74
}
75
76
impl<'a> SectionWithLimitedItems for TableSectionReader<'a> {
77
144k
    fn get_count(&self) -> u32 {
78
144k
        TableSectionReader::get_count(self)
79
144k
    }
80
}
81
82
impl<'a> IntoIterator for TableSectionReader<'a> {
83
    type Item = Result<TableType>;
84
    type IntoIter = SectionIteratorLimited<TableSectionReader<'a>>;
85
86
11.9k
    fn into_iter(self) -> Self::IntoIter {
87
11.9k
        SectionIteratorLimited::new(self)
88
11.9k
    }
89
}