mod aliases;
mod builder;
mod canonicals;
mod components;
mod exports;
mod imports;
mod instances;
mod modules;
mod names;
mod start;
mod types;
pub use self::aliases::*;
pub use self::builder::*;
pub use self::canonicals::*;
pub use self::components::*;
pub use self::exports::*;
pub use self::imports::*;
pub use self::instances::*;
pub use self::modules::*;
pub use self::names::*;
pub use self::start::*;
pub use self::types::*;
use crate::{CustomSection, Encode, ProducersSection, RawCustomSection};
use alloc::vec::Vec;
const CORE_TYPE_SORT: u8 = 0x10;
const CORE_MODULE_SORT: u8 = 0x11;
const CORE_INSTANCE_SORT: u8 = 0x12;
const CORE_SORT: u8 = 0x00;
const FUNCTION_SORT: u8 = 0x01;
const VALUE_SORT: u8 = 0x02;
const TYPE_SORT: u8 = 0x03;
const COMPONENT_SORT: u8 = 0x04;
const INSTANCE_SORT: u8 = 0x05;
pub trait ComponentSection: Encode {
fn id(&self) -> u8;
fn append_to_component(&self, dst: &mut Vec<u8>) {
dst.push(self.id());
self.encode(dst);
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[repr(u8)]
pub enum ComponentSectionId {
CoreCustom = 0,
CoreModule = 1,
CoreInstance = 2,
CoreType = 3,
Component = 4,
Instance = 5,
Alias = 6,
Type = 7,
CanonicalFunction = 8,
Start = 9,
Import = 10,
Export = 11,
}
impl From<ComponentSectionId> for u8 {
#[inline]
fn from(id: ComponentSectionId) -> u8 {
id as u8
}
}
impl Encode for ComponentSectionId {
fn encode(&self, sink: &mut Vec<u8>) {
sink.push(*self as u8);
}
}
#[derive(Clone, Debug)]
pub struct Component {
bytes: Vec<u8>,
}
impl Component {
#[rustfmt::skip]
pub const HEADER: [u8; 8] = [
0x00, 0x61, 0x73, 0x6D,
0x0d, 0x00, 0x01, 0x00,
];
pub fn new() -> Self {
Self {
bytes: Self::HEADER.to_vec(),
}
}
pub fn finish(self) -> Vec<u8> {
self.bytes
}
pub fn section(&mut self, section: &impl ComponentSection) -> &mut Self {
self.bytes.push(section.id());
section.encode(&mut self.bytes);
self
}
pub fn as_slice(&self) -> &[u8] {
&self.bytes
}
}
impl Default for Component {
fn default() -> Self {
Self::new()
}
}
impl ComponentSection for CustomSection<'_> {
fn id(&self) -> u8 {
ComponentSectionId::CoreCustom.into()
}
}
impl ComponentSection for RawCustomSection<'_> {
fn id(&self) -> u8 {
ComponentSectionId::CoreCustom.into()
}
}
impl ComponentSection for ProducersSection {
fn id(&self) -> u8 {
ComponentSectionId::CoreCustom.into()
}
}