wasm_encoder/core/
globals.rs1use crate::{encode_section, ConstExpr, Encode, Section, SectionId, ValType};
2use alloc::vec::Vec;
3
4#[derive(Clone, Default, Debug)]
29pub struct GlobalSection {
30 bytes: Vec<u8>,
31 num_added: u32,
32}
33
34impl GlobalSection {
35 pub fn new() -> Self {
37 Self::default()
38 }
39
40 pub fn len(&self) -> u32 {
42 self.num_added
43 }
44
45 pub fn is_empty(&self) -> bool {
47 self.num_added == 0
48 }
49
50 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 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#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
80pub struct GlobalType {
81 pub val_type: ValType,
83 pub mutable: bool,
85 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}