| 1 |
use crate::{encode_section, ConstExpr, Encode, RefType, Section, SectionId}; |
| 2 |
use std::borrow::Cow; |
| 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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#[derive(Clone, Default, Debug)] |
| 42 |
pub struct ElementSection { |
| 43 |
bytes: Vec<u8>, |
| 44 |
num_added: u32, |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
#[derive(Clone, Debug)] |
| 49 |
pub enum Elements<'a> { |
| 50 |
|
| 51 |
Functions(Cow<'a, [u32]>), |
| 52 |
|
| 53 |
Expressions(RefType, Cow<'a, [ConstExpr]>), |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
#[derive(Clone, Debug)] |
| 58 |
pub enum ElementMode<'a> { |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
Passive, |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
Declared, |
| 67 |
|
| 68 |
Active { |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
table: Option<u32>, |
| 75 |
|
| 76 |
offset: &'a ConstExpr, |
| 77 |
}, |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
#[derive(Clone, Debug)] |
| 82 |
pub struct ElementSegment<'a> { |
| 83 |
|
| 84 |
pub mode: ElementMode<'a>, |
| 85 |
|
| 86 |
pub elements: Elements<'a>, |
| 87 |
} |
| 88 |
|
| 89 |
impl ElementSection { |
| 90 |
|
| 91 |
pub fn new() -> Self { |
| 92 |
Self::default() |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
pub fn len(&self) -> u32 { |
| 97 |
self.num_added |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
pub fn is_empty(&self) -> bool { |
| 102 |
self.num_added == 0 |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
pub fn segment<'a>(&mut self, segment: ElementSegment<'a>) -> &mut Self { |
| 107 |
let expr_bit = match segment.elements { |
| 108 |
Elements::Expressions(..) => 0b100u32, |
| 109 |
Elements::Functions(_) => 0b000u32, |
| 110 |
}; |
| 111 |
let mut encode_type = false; |
| 112 |
match &segment.mode { |
| 113 |
ElementMode::Passive => { |
| 114 |
(0x01 | expr_bit).encode(&mut self.bytes); |
| 115 |
encode_type = true; |
| 116 |
} |
| 117 |
ElementMode::Active { table, offset } => { |
| 118 |
match (table, &segment.elements) { |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
(None, Elements::Functions(_) | Elements::Expressions(RefType::FUNCREF, _)) => { |
| 123 |
(expr_bit).encode(&mut self.bytes); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
(None, Elements::Expressions(..)) | (Some(_), _) => { |
| 130 |
(0x02 | expr_bit).encode(&mut self.bytes); |
| 131 |
table.unwrap_or(0).encode(&mut self.bytes); |
| 132 |
encode_type = true; |
| 133 |
} |
| 134 |
} |
| 135 |
offset.encode(&mut self.bytes); |
| 136 |
} |
| 137 |
ElementMode::Declared => { |
| 138 |
(0x03 | expr_bit).encode(&mut self.bytes); |
| 139 |
encode_type = true; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
match segment.elements { |
| 144 |
Elements::Functions(fs) => { |
| 145 |
if encode_type { |
| 146 |
|
| 147 |
self.bytes.push(0x00); |
| 148 |
} |
| 149 |
fs.encode(&mut self.bytes); |
| 150 |
} |
| 151 |
Elements::Expressions(ty, e) => { |
| 152 |
if encode_type { |
| 153 |
ty.encode(&mut self.bytes); |
| 154 |
} |
| 155 |
e.len().encode(&mut self.bytes); |
| 156 |
for expr in e.iter() { |
| 157 |
expr.encode(&mut self.bytes); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
self.num_added += 1; |
| 163 |
self |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
pub fn active( |
| 172 |
&mut self, |
| 173 |
table_index: Option<u32>, |
| 174 |
offset: &ConstExpr, |
| 175 |
elements: Elements<'_>, |
| 176 |
) -> &mut Self { |
| 177 |
self.segment(ElementSegment { |
| 178 |
mode: ElementMode::Active { |
| 179 |
table: table_index, |
| 180 |
offset, |
| 181 |
}, |
| 182 |
elements, |
| 183 |
}) |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
pub fn passive<'a>(&mut self, elements: Elements<'a>) -> &mut Self { |
| 190 |
self.segment(ElementSegment { |
| 191 |
mode: ElementMode::Passive, |
| 192 |
elements, |
| 193 |
}) |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
pub fn declared<'a>(&mut self, elements: Elements<'a>) -> &mut Self { |
| 200 |
self.segment(ElementSegment { |
| 201 |
mode: ElementMode::Declared, |
| 202 |
elements, |
| 203 |
}) |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
pub fn raw(&mut self, raw_bytes: &[u8]) -> &mut Self { |
| 208 |
self.bytes.extend_from_slice(raw_bytes); |
| 209 |
self.num_added += 1; |
| 210 |
self |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
impl Encode for ElementSection { |
| 215 |
fn encode(&self, sink: &mut Vec<u8>) { |
| 216 |
encode_section(sink, self.num_added, &self.bytes); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
impl Section for ElementSection { |
| 221 |
fn id(&self) -> u8 { |
| 222 |
SectionId::Element.into() |
| 223 |
} |
| 224 |
} |