#![warn(missing_docs)]
use crate::builtins::Blob;
use crate::io::{encode_decode_as, Decode, DecodeError, DecodeErrorKind, Encode, Wasmbin};
use crate::sections::{Section, StdPayload};
use crate::visit::Visit;
use std::cmp::Ordering;
const MAGIC_AND_VERSION: [u8; 8] = [b'\0', b'a', b's', b'm', 0x01, 0x00, 0x00, 0x00];
#[derive(Debug, Default, PartialEq, Eq, Hash, Clone, Visit)]
struct MagicAndVersion;
encode_decode_as!(MagicAndVersion, {
MagicAndVersion <=> MAGIC_AND_VERSION,
}, |actual| {
Err(DecodeErrorKind::InvalidMagic { actual }.into())
});
#[derive(Wasmbin)]
#[repr(transparent)]
struct ModuleRepr {
magic_and_version: MagicAndVersion,
sections: Vec<Section>,
}
#[derive(Debug, Default, PartialEq, Eq, Hash, Clone, Visit)]
pub struct Module {
pub sections: Vec<Section>,
}
impl Encode for Module {
fn encode(&self, w: &mut impl std::io::Write) -> std::io::Result<()> {
unsafe { &*(self as *const Module).cast::<ModuleRepr>() }.encode(w)
}
}
impl Decode for Module {
fn decode(r: &mut impl std::io::Read) -> Result<Self, DecodeError> {
ModuleRepr::decode(r).map(|repr| unsafe { std::mem::transmute::<ModuleRepr, Module>(repr) })
}
}
impl Module {
pub fn decode_from(mut r: impl std::io::Read) -> Result<Module, DecodeError> {
Self::decode(&mut r)
}
pub fn encode_into<W: std::io::Write>(&self, mut w: W) -> std::io::Result<W> {
self.encode(&mut w)?;
Ok(w)
}
pub fn find_std_section<T: StdPayload>(&self) -> Option<&Blob<T>> {
self.sections.iter().find_map(Section::try_as)
}
pub fn find_std_section_mut<T: StdPayload>(&mut self) -> Option<&mut Blob<T>> {
self.sections.iter_mut().find_map(Section::try_as_mut)
}
#[allow(clippy::missing_panics_doc)]
pub fn find_or_insert_std_section<T: StdPayload>(
&mut self,
insert_callback: impl FnOnce() -> T,
) -> &mut Blob<T> {
let mut index = self.sections.len();
let mut insert = true;
for (i, section) in self.sections.iter_mut().enumerate() {
match section.kind().cmp(&T::KIND) {
Ordering::Less => continue,
Ordering::Equal => {
insert = false;
}
Ordering::Greater => {}
}
index = i;
break;
}
if insert {
self.sections.insert(index, insert_callback().into());
}
self.sections[index]
.try_as_mut()
.expect("internal error: couldn't convert back just inserted section")
}
}