use crate::{ConstExpr, Encode, RefType, Section, SectionId, encode_section};
use alloc::borrow::Cow;
use alloc::vec::Vec;
#[derive(Clone, Default, Debug)]
pub struct ElementSection {
bytes: Vec<u8>,
num_added: u32,
}
#[derive(Clone, Debug)]
pub enum Elements<'a> {
Functions(Cow<'a, [u32]>),
Expressions(RefType, Cow<'a, [ConstExpr]>),
}
#[derive(Clone, Debug)]
pub enum ElementMode<'a> {
Passive,
Declared,
Active {
table: Option<u32>,
offset: &'a ConstExpr,
},
}
#[derive(Clone, Debug)]
pub struct ElementSegment<'a> {
pub mode: ElementMode<'a>,
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,
};
let mut encode_type = false;
match &segment.mode {
ElementMode::Passive => {
(0x01 | expr_bit).encode(&mut self.bytes);
encode_type = true;
}
ElementMode::Active { table, offset } => {
match (table, &segment.elements) {
(None, Elements::Functions(_) | Elements::Expressions(RefType::FUNCREF, _)) => {
(expr_bit).encode(&mut self.bytes);
}
(None, Elements::Expressions(..)) | (Some(_), _) => {
(0x02 | expr_bit).encode(&mut self.bytes);
table.unwrap_or(0).encode(&mut self.bytes);
encode_type = true;
}
}
offset.encode(&mut self.bytes);
}
ElementMode::Declared => {
(0x03 | expr_bit).encode(&mut self.bytes);
encode_type = true;
}
}
match segment.elements {
Elements::Functions(fs) => {
if encode_type {
self.bytes.push(0x00);
}
fs.encode(&mut self.bytes);
}
Elements::Expressions(ty, e) => {
if encode_type {
ty.encode(&mut self.bytes);
}
e.len().encode(&mut self.bytes);
for expr in e.iter() {
expr.encode(&mut self.bytes);
}
}
}
self.num_added += 1;
self
}
pub fn active(
&mut self,
table_index: Option<u32>,
offset: &ConstExpr,
elements: Elements<'_>,
) -> &mut Self {
self.segment(ElementSegment {
mode: ElementMode::Active {
table: table_index,
offset,
},
elements,
})
}
pub fn passive<'a>(&mut self, elements: Elements<'a>) -> &mut Self {
self.segment(ElementSegment {
mode: ElementMode::Passive,
elements,
})
}
pub fn declared<'a>(&mut self, elements: Elements<'a>) -> &mut Self {
self.segment(ElementSegment {
mode: ElementMode::Declared,
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, self.num_added, &self.bytes);
}
}
impl Section for ElementSection {
fn id(&self) -> u8 {
SectionId::Element.into()
}
}