use crate::{BinaryReader, ConstExpr, FromReader, GlobalType, Result, SectionLimited};
#[derive(Debug, Clone)]
pub struct Global<'a> {
pub ty: GlobalType,
pub init_expr: ConstExpr<'a>,
}
pub type GlobalSectionReader<'a> = SectionLimited<'a, Global<'a>>;
impl<'a> FromReader<'a> for Global<'a> {
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
let ty = reader.read()?;
let init_expr = reader.read()?;
Ok(Global { ty, init_expr })
}
}
impl<'a> FromReader<'a> for GlobalType {
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
let content_type = reader.read()?;
let flags = reader.read_u8()?;
if flags > 0b11 {
bail!(reader.original_position() - 1, "malformed global flags")
}
Ok(GlobalType {
content_type,
mutable: (flags & 0b01) > 0,
shared: (flags & 0b10) > 0,
})
}
}