1mod branch_hints;
2mod code;
3mod custom;
4mod data;
5mod dump;
6mod elements;
7mod exports;
8mod functions;
9mod globals;
10mod imports;
11mod instructions;
12mod linking;
13mod memories;
14mod names;
15mod producers;
16mod start;
17mod tables;
18mod tags;
19mod types;
20
21pub use branch_hints::*;
22pub use code::*;
23pub use custom::*;
24pub use data::*;
25pub use dump::*;
26pub use elements::*;
27pub use exports::*;
28pub use functions::*;
29pub use globals::*;
30pub use imports::*;
31pub use instructions::*;
32pub use linking::*;
33pub use memories::*;
34pub use names::*;
35pub use producers::*;
36pub use start::*;
37pub use tables::*;
38pub use tags::*;
39pub use types::*;
40
41use crate::Encode;
42use alloc::vec::Vec;
43
44pub(crate) const CORE_FUNCTION_SORT: u8 = 0x00;
45pub(crate) const CORE_TABLE_SORT: u8 = 0x01;
46pub(crate) const CORE_MEMORY_SORT: u8 = 0x02;
47pub(crate) const CORE_GLOBAL_SORT: u8 = 0x03;
48pub(crate) const CORE_TAG_SORT: u8 = 0x04;
49pub(crate) const CORE_FUNCTION_EXACT_SORT: u8 = 0x20;
50
51pub trait Section: Encode {
57 fn id(&self) -> u8;
59
60 fn append_to(&self, dst: &mut Vec<u8>) {
62 dst.push(self.id());
63 self.encode(dst);
64 }
65}
66
67#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
69#[repr(u8)]
70pub enum SectionId {
71 Custom = 0,
73 Type = 1,
75 Import = 2,
77 Function = 3,
79 Table = 4,
81 Memory = 5,
83 Global = 6,
85 Export = 7,
87 Start = 8,
89 Element = 9,
91 Code = 10,
93 Data = 11,
95 DataCount = 12,
97 Tag = 13,
101}
102
103impl From<SectionId> for u8 {
104 #[inline]
105 fn from(id: SectionId) -> u8 {
106 id as u8
107 }
108}
109
110impl Encode for SectionId {
111 fn encode(&self, sink: &mut Vec<u8>) {
112 sink.push(*self as u8);
113 }
114}
115
116#[derive(Clone, Debug)]
122pub struct Module {
123 pub(crate) bytes: Vec<u8>,
124}
125
126impl Module {
127 #[rustfmt::skip]
129 pub const HEADER: [u8; 8] = [
130 0x00, 0x61, 0x73, 0x6D,
132 0x01, 0x00, 0x00, 0x00,
134 ];
135
136 #[rustfmt::skip]
138 pub fn new() -> Self {
139 Module {
140 bytes: Self::HEADER.to_vec(),
141 }
142 }
143
144 pub fn section(&mut self, section: &impl Section) -> &mut Self {
153 self.bytes.push(section.id());
154 section.encode(&mut self.bytes);
155 self
156 }
157
158 pub fn as_slice(&self) -> &[u8] {
160 &self.bytes
161 }
162
163 pub fn len(&self) -> usize {
165 self.bytes.len()
166 }
167
168 pub fn finish(self) -> Vec<u8> {
171 self.bytes
172 }
173}
174
175impl Default for Module {
176 fn default() -> Self {
177 Self::new()
178 }
179}