#![deny(missing_docs, missing_debug_implementations)]
mod aliases;
mod code;
mod custom;
mod data;
mod elements;
mod exports;
mod functions;
mod globals;
mod imports;
mod instances;
mod linking;
mod memories;
mod modules;
mod start;
mod tables;
mod types;
pub use aliases::*;
pub use code::*;
pub use custom::*;
pub use data::*;
pub use elements::*;
pub use exports::*;
pub use functions::*;
pub use globals::*;
pub use imports::*;
pub use instances::*;
pub use linking::*;
pub use memories::*;
pub use modules::*;
pub use start::*;
pub use tables::*;
pub use types::*;
pub mod encoders;
use std::convert::TryFrom;
#[derive(Clone, Debug)]
pub struct Module {
bytes: Vec<u8>,
}
pub trait Section {
fn id(&self) -> u8;
fn encode<S>(&self, sink: &mut S)
where
S: Extend<u8>;
}
#[derive(Clone, Copy, Debug)]
pub struct RawSection<'a> {
pub id: u8,
pub data: &'a [u8],
}
impl Section for RawSection<'_> {
fn id(&self) -> u8 {
self.id
}
fn encode<S>(&self, sink: &mut S)
where
S: Extend<u8>,
{
sink.extend(
encoders::u32(u32::try_from(self.data.len()).unwrap()).chain(self.data.iter().copied()),
);
}
}
impl Module {
#[rustfmt::skip]
pub fn new() -> Self {
Module {
bytes: vec![
0x00, 0x61, 0x73, 0x6D,
0x01, 0x00, 0x00, 0x00,
],
}
}
pub fn section(&mut self, section: &impl Section) -> &mut Self {
self.bytes.push(section.id());
section.encode(&mut self.bytes);
self
}
pub fn as_slice(&self) -> &[u8] {
&self.bytes
}
pub fn finish(self) -> Vec<u8> {
self.bytes
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[repr(u8)]
#[allow(missing_docs)]
pub enum SectionId {
Custom = 0,
Type = 1,
Import = 2,
Function = 3,
Table = 4,
Memory = 5,
Global = 6,
Export = 7,
Start = 8,
Element = 9,
Code = 10,
Data = 11,
DataCount = 12,
Module = 14,
Instance = 15,
Alias = 16,
}
impl From<SectionId> for u8 {
#[inline]
fn from(id: SectionId) -> u8 {
id as u8
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum ValType {
I32 = 0x7F,
I64 = 0x7E,
F32 = 0x7D,
F64 = 0x7C,
V128 = 0x7B,
FuncRef = 0x70,
ExternRef = 0x6F,
}
impl From<ValType> for u8 {
#[inline]
fn from(t: ValType) -> u8 {
t as u8
}
}