wasm_encoder/core/
data.rs1use crate::{encode_section, encoding_size, ConstExpr, Encode, Section, SectionId};
2use alloc::vec::Vec;
3
4#[derive(Clone, Default, Debug)]
39pub struct DataSection {
40 bytes: Vec<u8>,
41 num_added: u32,
42}
43
44#[derive(Clone, Debug)]
46pub struct DataSegment<'a, D> {
47 pub mode: DataSegmentMode<'a>,
49 pub data: D,
51}
52
53#[derive(Clone, Debug)]
55pub enum DataSegmentMode<'a> {
56 Active {
58 memory_index: u32,
60 offset: &'a ConstExpr,
62 },
63 Passive,
67}
68
69impl DataSection {
70 pub fn new() -> Self {
72 Self::default()
73 }
74
75 pub fn len(&self) -> u32 {
77 self.num_added
78 }
79
80 pub fn is_empty(&self) -> bool {
82 self.num_added == 0
83 }
84
85 pub fn segment<D>(&mut self, segment: DataSegment<D>) -> &mut Self
87 where
88 D: IntoIterator<Item = u8>,
89 D::IntoIter: ExactSizeIterator,
90 {
91 match segment.mode {
92 DataSegmentMode::Passive => {
93 self.bytes.push(0x01);
94 }
95 DataSegmentMode::Active {
96 memory_index: 0,
97 offset,
98 } => {
99 self.bytes.push(0x00);
100 offset.encode(&mut self.bytes);
101 }
102 DataSegmentMode::Active {
103 memory_index,
104 offset,
105 } => {
106 self.bytes.push(0x02);
107 memory_index.encode(&mut self.bytes);
108 offset.encode(&mut self.bytes);
109 }
110 }
111
112 let data = segment.data.into_iter();
113 data.len().encode(&mut self.bytes);
114 self.bytes.extend(data);
115
116 self.num_added += 1;
117 self
118 }
119
120 pub fn active<D>(&mut self, memory_index: u32, offset: &ConstExpr, data: D) -> &mut Self
122 where
123 D: IntoIterator<Item = u8>,
124 D::IntoIter: ExactSizeIterator,
125 {
126 self.segment(DataSegment {
127 mode: DataSegmentMode::Active {
128 memory_index,
129 offset,
130 },
131 data,
132 })
133 }
134
135 pub fn passive<D>(&mut self, data: D) -> &mut Self
139 where
140 D: IntoIterator<Item = u8>,
141 D::IntoIter: ExactSizeIterator,
142 {
143 self.segment(DataSegment {
144 mode: DataSegmentMode::Passive,
145 data,
146 })
147 }
148
149 pub fn raw(&mut self, already_encoded_data_segment: &[u8]) -> &mut Self {
151 self.bytes.extend_from_slice(already_encoded_data_segment);
152 self.num_added += 1;
153 self
154 }
155}
156
157impl Encode for DataSection {
158 fn encode(&self, sink: &mut Vec<u8>) {
159 encode_section(sink, self.num_added, &self.bytes);
160 }
161}
162
163impl Section for DataSection {
164 fn id(&self) -> u8 {
165 SectionId::Data.into()
166 }
167}
168
169#[derive(Clone, Copy, Debug)]
171pub struct DataCountSection {
172 pub count: u32,
174}
175
176impl Encode for DataCountSection {
177 fn encode(&self, sink: &mut Vec<u8>) {
178 encoding_size(self.count).encode(sink);
179 self.count.encode(sink);
180 }
181}
182
183impl Section for DataCountSection {
184 fn id(&self) -> u8 {
185 SectionId::DataCount.into()
186 }
187}