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;
49
50pub trait Section: Encode {
56 fn id(&self) -> u8;
58
59 fn append_to(&self, dst: &mut Vec<u8>) {
61 dst.push(self.id());
62 self.encode(dst);
63 }
64}
65
66#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
68#[repr(u8)]
69pub enum SectionId {
70 Custom = 0,
72 Type = 1,
74 Import = 2,
76 Function = 3,
78 Table = 4,
80 Memory = 5,
82 Global = 6,
84 Export = 7,
86 Start = 8,
88 Element = 9,
90 Code = 10,
92 Data = 11,
94 DataCount = 12,
96 Tag = 13,
100}
101
102impl From<SectionId> for u8 {
103 #[inline]
104 fn from(id: SectionId) -> u8 {
105 id as u8
106 }
107}
108
109impl Encode for SectionId {
110 fn encode(&self, sink: &mut Vec<u8>) {
111 sink.push(*self as u8);
112 }
113}
114
115#[derive(Clone, Debug)]
121pub struct Module {
122 pub(crate) bytes: Vec<u8>,
123}
124
125impl Module {
126 #[rustfmt::skip]
128 pub const HEADER: [u8; 8] = [
129 0x00, 0x61, 0x73, 0x6D,
131 0x01, 0x00, 0x00, 0x00,
133 ];
134
135 #[rustfmt::skip]
137 pub fn new() -> Self {
138 Module {
139 bytes: Self::HEADER.to_vec(),
140 }
141 }
142
143 pub fn section(&mut self, section: &impl Section) -> &mut Self {
152 self.bytes.push(section.id());
153 section.encode(&mut self.bytes);
154 self
155 }
156
157 pub fn as_slice(&self) -> &[u8] {
159 &self.bytes
160 }
161
162 pub fn len(&self) -> usize {
164 self.bytes.len()
165 }
166
167 pub fn finish(self) -> Vec<u8> {
170 self.bytes
171 }
172}
173
174impl Default for Module {
175 fn default() -> Self {
176 Self::new()
177 }
178}