wasm_encoder/core/
globals.rs

1use crate::{encode_section, ConstExpr, Encode, Section, SectionId, ValType};
2use alloc::vec::Vec;
3
4/// An encoder for the global section.
5///
6/// Global sections are only supported for modules.
7///
8/// # Example
9///
10/// ```
11/// use wasm_encoder::{ConstExpr, Module, GlobalSection, GlobalType, Instruction, ValType};
12///
13/// let mut globals = GlobalSection::new();
14/// globals.global(
15///     GlobalType {
16///         val_type: ValType::I32,
17///         mutable: false,
18///         shared: false,
19///     },
20///     &ConstExpr::i32_const(42),
21/// );
22///
23/// let mut module = Module::new();
24/// module.section(&globals);
25///
26/// let wasm_bytes = module.finish();
27/// ```
28#[derive(Clone, Default, Debug)]
29pub struct GlobalSection {
30    bytes: Vec<u8>,
31    num_added: u32,
32}
33
34impl GlobalSection {
35    /// Create a new global section encoder.
36    pub fn new() -> Self {
37        Self::default()
38    }
39
40    /// The number of globals in the section.
41    pub fn len(&self) -> u32 {
42        self.num_added
43    }
44
45    /// Determines if the section is empty.
46    pub fn is_empty(&self) -> bool {
47        self.num_added == 0
48    }
49
50    /// Define a global.
51    pub fn global(&mut self, global_type: GlobalType, init_expr: &ConstExpr) -> &mut Self {
52        global_type.encode(&mut self.bytes);
53        init_expr.encode(&mut self.bytes);
54        self.num_added += 1;
55        self
56    }
57
58    /// Add a raw byte slice into this code section as a global.
59    pub fn raw(&mut self, data: &[u8]) -> &mut Self {
60        self.bytes.extend(data);
61        self.num_added += 1;
62        self
63    }
64}
65
66impl Encode for GlobalSection {
67    fn encode(&self, sink: &mut Vec<u8>) {
68        encode_section(sink, self.num_added, &self.bytes);
69    }
70}
71
72impl Section for GlobalSection {
73    fn id(&self) -> u8 {
74        SectionId::Global.into()
75    }
76}
77
78/// A global's type.
79#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
80pub struct GlobalType {
81    /// This global's value type.
82    pub val_type: ValType,
83    /// Whether this global is mutable or not.
84    pub mutable: bool,
85    /// Whether this global is shared or not.
86    pub shared: bool,
87}
88
89impl Encode for GlobalType {
90    fn encode(&self, sink: &mut Vec<u8>) {
91        self.val_type.encode(sink);
92        let mut flag = 0;
93        if self.mutable {
94            flag |= 0b01;
95        }
96        if self.shared {
97            flag |= 0b10;
98        }
99        sink.push(flag);
100    }
101}