use crate::{encode_section, Encode, Instruction, Section, SectionId, ValType};
#[derive(Clone, Default, Debug)]
pub struct ElementSection {
bytes: Vec<u8>,
num_added: u32,
}
#[derive(Clone, Copy, Debug)]
pub enum Elements<'a> {
Functions(&'a [u32]),
Expressions(&'a [Element]),
}
#[derive(Clone, Copy, Debug)]
pub enum Element {
Null,
Func(u32),
}
#[derive(Clone, Debug)]
pub enum ElementMode<'a> {
Passive,
Declared,
Active {
table: Option<u32>,
offset: &'a Instruction<'a>,
},
}
#[derive(Clone, Debug)]
pub struct ElementSegment<'a> {
pub mode: ElementMode<'a>,
pub element_type: ValType,
pub elements: Elements<'a>,
}
impl ElementSection {
pub fn new() -> Self {
Self::default()
}
pub fn len(&self) -> u32 {
self.num_added
}
pub fn is_empty(&self) -> bool {
self.num_added == 0
}
pub fn segment<'a>(&mut self, segment: ElementSegment<'a>) -> &mut Self {
let expr_bit = match segment.elements {
Elements::Expressions(_) => 0b100u32,
Elements::Functions(_) => 0b000u32,
};
match &segment.mode {
ElementMode::Active {
table: None,
offset,
} => {
(expr_bit).encode(&mut self.bytes);
offset.encode(&mut self.bytes);
Instruction::End.encode(&mut self.bytes);
}
ElementMode::Passive => {
(0x01 | expr_bit).encode(&mut self.bytes);
if expr_bit == 0 {
self.bytes.push(0x00); } else {
segment.element_type.encode(&mut self.bytes);
}
}
ElementMode::Active {
table: Some(i),
offset,
} => {
(0x02 | expr_bit).encode(&mut self.bytes);
i.encode(&mut self.bytes);
offset.encode(&mut self.bytes);
Instruction::End.encode(&mut self.bytes);
if expr_bit == 0 {
self.bytes.push(0x00); } else {
segment.element_type.encode(&mut self.bytes);
}
}
ElementMode::Declared => {
(0x03 | expr_bit).encode(&mut self.bytes);
if expr_bit == 0 {
self.bytes.push(0x00); } else {
segment.element_type.encode(&mut self.bytes);
}
}
}
match segment.elements {
Elements::Functions(fs) => {
fs.encode(&mut self.bytes);
}
Elements::Expressions(e) => {
e.len().encode(&mut self.bytes);
for expr in e {
match expr {
Element::Func(i) => Instruction::RefFunc(*i).encode(&mut self.bytes),
Element::Null => {
Instruction::RefNull(segment.element_type).encode(&mut self.bytes)
}
}
Instruction::End.encode(&mut self.bytes);
}
}
}
self.num_added += 1;
self
}
pub fn active(
&mut self,
table_index: Option<u32>,
offset: &Instruction<'_>,
element_type: ValType,
elements: Elements<'_>,
) -> &mut Self {
self.segment(ElementSegment {
mode: ElementMode::Active {
table: table_index,
offset,
},
element_type,
elements,
})
}
pub fn passive<'a>(&mut self, element_type: ValType, elements: Elements<'a>) -> &mut Self {
self.segment(ElementSegment {
mode: ElementMode::Passive,
element_type,
elements,
})
}
pub fn declared<'a>(&mut self, element_type: ValType, elements: Elements<'a>) -> &mut Self {
self.segment(ElementSegment {
mode: ElementMode::Declared,
element_type,
elements,
})
}
pub fn raw(&mut self, raw_bytes: &[u8]) -> &mut Self {
self.bytes.extend_from_slice(raw_bytes);
self.num_added += 1;
self
}
}
impl Encode for ElementSection {
fn encode(&self, sink: &mut Vec<u8>) {
encode_section(sink, SectionId::Element, self.num_added, &self.bytes);
}
}
impl Section for ElementSection {}