wasmparser_nostd/readers/core/
memories.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, MemoryType, Result, SectionLimited};
17
18/// A reader for the memory section of a WebAssembly module.
19pub type MemorySectionReader<'a> = SectionLimited<'a, MemoryType>;
20
21impl<'a> FromReader<'a> for MemoryType {
22    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
23        let pos = reader.original_position();
24        let flags = reader.read_u8()?;
25        if (flags & !0b111) != 0 {
26            bail!(pos, "invalid memory limits flags");
27        }
28
29        let memory64 = flags & 0b100 != 0;
30        let shared = flags & 0b010 != 0;
31        let has_max = flags & 0b001 != 0;
32        Ok(MemoryType {
33            memory64,
34            shared,
35            // FIXME(WebAssembly/memory64#21) as currently specified if the
36            // `shared` flag is set we should be reading a 32-bit limits field
37            // here. That seems a bit odd to me at the time of this writing so
38            // I've taken the liberty of reading a 64-bit limits field in those
39            // situations. I suspect that this is a typo in the spec, but if not
40            // we'll need to update this to read a 32-bit limits field when the
41            // shared flag is set.
42            initial: if memory64 {
43                reader.read_var_u64()?
44            } else {
45                reader.read_var_u32()?.into()
46            },
47            maximum: if !has_max {
48                None
49            } else if memory64 {
50                Some(reader.read_var_u64()?)
51            } else {
52                Some(reader.read_var_u32()?.into())
53            },
54        })
55    }
56}