tinywasm_wasmparser/readers/core/init.rs
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
16use crate::{BinaryReader, FromReader, OperatorsReader, Result};
17
18/// Represents an initialization expression.
19#[derive(Debug, Copy, Clone)]
20pub struct ConstExpr<'a> {
21 offset: usize,
22 data: &'a [u8],
23}
24
25impl<'a> ConstExpr<'a> {
26 /// Constructs a new `ConstExpr` from the given data and offset.
27 pub fn new(data: &[u8], offset: usize) -> ConstExpr {
28 ConstExpr { offset, data }
29 }
30
31 /// Gets a binary reader for the initialization expression.
32 pub fn get_binary_reader(&self) -> BinaryReader<'a> {
33 BinaryReader::new_with_offset(self.data, self.offset)
34 }
35
36 /// Gets an operators reader for the initialization expression.
37 pub fn get_operators_reader(&self) -> OperatorsReader<'a> {
38 OperatorsReader::new(self.get_binary_reader())
39 }
40}
41
42impl<'a> FromReader<'a> for ConstExpr<'a> {
43 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
44 // FIXME(#188) ideally shouldn't need to skip here
45 let reader = reader.skip(|r| r.skip_const_expr())?;
46 Ok(ConstExpr::new(
47 reader.remaining_buffer(),
48 reader.original_position(),
49 ))
50 }
51}