| 1 |
use crate::{encode_section, encoding_size, ConstExpr, Encode, Section, SectionId}; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#[derive(Clone, Default, Debug)] |
| 38 |
pub struct DataSection { |
| 39 |
bytes: Vec<u8>, |
| 40 |
num_added: u32, |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
#[derive(Clone, Debug)] |
| 45 |
pub struct DataSegment<'a, D> { |
| 46 |
|
| 47 |
pub mode: DataSegmentMode<'a>, |
| 48 |
|
| 49 |
pub data: D, |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
#[derive(Clone, Debug)] |
| 54 |
pub enum DataSegmentMode<'a> { |
| 55 |
|
| 56 |
Active { |
| 57 |
|
| 58 |
memory_index: u32, |
| 59 |
|
| 60 |
offset: &'a ConstExpr, |
| 61 |
}, |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
Passive, |
| 66 |
} |
| 67 |
|
| 68 |
impl DataSection { |
| 69 |
|
| 70 |
pub fn new() -> Self { |
| 71 |
Self::default() |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
pub fn len(&self) -> u32 { |
| 76 |
self.num_added |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
pub fn is_empty(&self) -> bool { |
| 81 |
self.num_added == 0 |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
pub fn segment<D>(&mut self, segment: DataSegment<D>) -> &mut Self |
| 86 |
where |
| 87 |
D: IntoIterator<Item = u8>, |
| 88 |
D::IntoIter: ExactSizeIterator, |
| 89 |
{ |
| 90 |
match segment.mode { |
| 91 |
DataSegmentMode::Passive => { |
| 92 |
self.bytes.push(0x01); |
| 93 |
} |
| 94 |
DataSegmentMode::Active { |
| 95 |
memory_index: 0, |
| 96 |
offset, |
| 97 |
} => { |
| 98 |
self.bytes.push(0x00); |
| 99 |
offset.encode(&mut self.bytes); |
| 100 |
} |
| 101 |
DataSegmentMode::Active { |
| 102 |
memory_index, |
| 103 |
offset, |
| 104 |
} => { |
| 105 |
self.bytes.push(0x02); |
| 106 |
memory_index.encode(&mut self.bytes); |
| 107 |
offset.encode(&mut self.bytes); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
let data = segment.data.into_iter(); |
| 112 |
data.len().encode(&mut self.bytes); |
| 113 |
self.bytes.extend(data); |
| 114 |
|
| 115 |
self.num_added += 1; |
| 116 |
self |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
pub fn active<D>(&mut self, memory_index: u32, offset: &ConstExpr, data: D) -> &mut Self |
| 121 |
where |
| 122 |
D: IntoIterator<Item = u8>, |
| 123 |
D::IntoIter: ExactSizeIterator, |
| 124 |
{ |
| 125 |
self.segment(DataSegment { |
| 126 |
mode: DataSegmentMode::Active { |
| 127 |
memory_index, |
| 128 |
offset, |
| 129 |
}, |
| 130 |
data, |
| 131 |
}) |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
pub fn passive<D>(&mut self, data: D) -> &mut Self |
| 138 |
where |
| 139 |
D: IntoIterator<Item = u8>, |
| 140 |
D::IntoIter: ExactSizeIterator, |
| 141 |
{ |
| 142 |
self.segment(DataSegment { |
| 143 |
mode: DataSegmentMode::Passive, |
| 144 |
data, |
| 145 |
}) |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
pub fn raw(&mut self, already_encoded_data_segment: &[u8]) -> &mut Self { |
| 150 |
self.bytes.extend_from_slice(already_encoded_data_segment); |
| 151 |
self.num_added += 1; |
| 152 |
self |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
impl Encode for DataSection { |
| 157 |
fn encode(&self, sink: &mut Vec<u8>) { |
| 158 |
encode_section(sink, self.num_added, &self.bytes); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
impl Section for DataSection { |
| 163 |
fn id(&self) -> u8 { |
| 164 |
SectionId::Data.into() |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
#[derive(Clone, Copy, Debug)] |
| 170 |
pub struct DataCountSection { |
| 171 |
|
| 172 |
pub count: u32, |
| 173 |
} |
| 174 |
|
| 175 |
impl Encode for DataCountSection { |
| 176 |
fn encode(&self, sink: &mut Vec<u8>) { |
| 177 |
encoding_size(self.count).encode(sink); |
| 178 |
self.count.encode(sink); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
impl Section for DataCountSection { |
| 183 |
fn id(&self) -> u8 { |
| 184 |
SectionId::DataCount.into() |
| 185 |
} |
| 186 |
} |