sophon_wasm/elements/
global_entry.rs

1use std::io;
2use super::{Deserialize, Serialize, Error, GlobalType, InitExpr};
3
4/// Global entry in the module.
5#[derive(Clone)]
6pub struct GlobalEntry {
7    global_type: GlobalType,
8    init_expr: InitExpr,
9}
10
11impl GlobalEntry {
12    /// New global entry
13    pub fn new(global_type: GlobalType, init_expr: InitExpr) -> Self {
14        GlobalEntry {
15            global_type: global_type,
16            init_expr: init_expr,
17        }
18    }
19    /// Global type.
20    pub fn global_type(&self) -> &GlobalType { &self.global_type }
21    /// Initialization expression (opcodes) for global.
22    pub fn init_expr(&self) -> &InitExpr { &self.init_expr }
23    /// Global type (mutable)
24    pub fn global_type_mut(&mut self) -> &mut GlobalType { &mut self.global_type }
25    /// Initialization expression (opcodes) for global (mutable)
26    pub fn init_expr_mut(&mut self) -> &mut InitExpr { &mut self.init_expr }
27}
28
29impl Deserialize for GlobalEntry {
30    type Error = Error;
31
32    fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
33        let global_type = GlobalType::deserialize(reader)?;
34        let init_expr = InitExpr::deserialize(reader)?;
35
36        Ok(GlobalEntry {
37            global_type: global_type,
38            init_expr: init_expr,
39        })
40    }
41}
42
43impl Serialize for GlobalEntry {
44    type Error = Error;
45
46    fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
47        self.global_type.serialize(writer)?;
48        self.init_expr.serialize(writer)
49    }
50}