tinywasm_wasmparser/readers/core/globals.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, ConstExpr, FromReader, GlobalType, Result, SectionLimited};
17
18/// Represents a core WebAssembly global.
19#[derive(Debug, Copy, Clone)]
20pub struct Global<'a> {
21 /// The global's type.
22 pub ty: GlobalType,
23 /// The global's initialization expression.
24 pub init_expr: ConstExpr<'a>,
25}
26
27/// A reader for the global section of a WebAssembly module.
28pub type GlobalSectionReader<'a> = SectionLimited<'a, Global<'a>>;
29
30impl<'a> FromReader<'a> for Global<'a> {
31 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
32 let ty = reader.read()?;
33 let init_expr = reader.read()?;
34 Ok(Global { ty, init_expr })
35 }
36}
37
38impl<'a> FromReader<'a> for GlobalType {
39 fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
40 Ok(GlobalType {
41 content_type: reader.read()?,
42 mutable: match reader.read_u8()? {
43 0x00 => false,
44 0x01 => true,
45 _ => bail!(reader.original_position() - 1, "malformed mutability",),
46 },
47 })
48 }
49}