1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use crate::core::reader::span::Span;
use crate::core::reader::types::ValType;
use crate::core::reader::{WasmReadable, WasmReader};
use crate::execution::assert_validated::UnwrapValidatedExt;
use crate::{unreachable_validated, Error, Result};

#[derive(Debug, Copy, Clone)]
pub struct Global {
    pub ty: GlobalType,
    // TODO validate init_expr during validation and execute during instantiation
    pub init_expr: Span,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct GlobalType {
    pub ty: ValType,
    pub is_mut: bool,
}

impl WasmReadable for GlobalType {
    fn read(wasm: &mut WasmReader) -> Result<Self> {
        let ty = ValType::read(wasm)?;
        let is_mut = match wasm.read_u8()? {
            0x00 => false,
            0x01 => true,
            other => return Err(Error::InvalidMutType(other)),
        };
        Ok(Self { ty, is_mut })
    }

    fn read_unvalidated(wasm: &mut WasmReader) -> Self {
        let ty = ValType::read_unvalidated(wasm);
        let is_mut = match wasm.read_u8().unwrap_validated() {
            0x00 => false,
            0x01 => true,
            _ => unreachable_validated!(),
        };

        Self { ty, is_mut }
    }
}