wasm_encoder/
component.rs1mod aliases;
2mod builder;
3mod canonicals;
4mod components;
5mod exports;
6mod imports;
7mod instances;
8mod modules;
9mod names;
10mod start;
11mod types;
12
13pub use self::aliases::*;
14pub use self::builder::*;
15pub use self::canonicals::*;
16pub use self::components::*;
17pub use self::exports::*;
18pub use self::imports::*;
19pub use self::instances::*;
20pub use self::modules::*;
21pub use self::names::*;
22pub use self::start::*;
23pub use self::types::*;
24
25use crate::{CustomSection, Encode, ProducersSection, RawCustomSection};
26use alloc::vec::Vec;
27
28const CORE_TYPE_SORT: u8 = 0x10;
30const CORE_MODULE_SORT: u8 = 0x11;
31const CORE_INSTANCE_SORT: u8 = 0x12;
32
33const CORE_SORT: u8 = 0x00;
34const FUNCTION_SORT: u8 = 0x01;
35const VALUE_SORT: u8 = 0x02;
36const TYPE_SORT: u8 = 0x03;
37const COMPONENT_SORT: u8 = 0x04;
38const INSTANCE_SORT: u8 = 0x05;
39
40pub trait ComponentSection: Encode {
46 fn id(&self) -> u8;
48
49 fn append_to_component(&self, dst: &mut Vec<u8>) {
51 dst.push(self.id());
52 self.encode(dst);
53 }
54}
55
56#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
60#[repr(u8)]
61pub enum ComponentSectionId {
62 CoreCustom = 0,
64 CoreModule = 1,
66 CoreInstance = 2,
68 CoreType = 3,
70 Component = 4,
72 Instance = 5,
74 Alias = 6,
76 Type = 7,
78 CanonicalFunction = 8,
80 Start = 9,
82 Import = 10,
84 Export = 11,
86}
87
88impl From<ComponentSectionId> for u8 {
89 #[inline]
90 fn from(id: ComponentSectionId) -> u8 {
91 id as u8
92 }
93}
94
95impl Encode for ComponentSectionId {
96 fn encode(&self, sink: &mut Vec<u8>) {
97 sink.push(*self as u8);
98 }
99}
100
101#[derive(Clone, Debug)]
108pub struct Component {
109 bytes: Vec<u8>,
110}
111
112impl Component {
113 #[rustfmt::skip]
115 pub const HEADER: [u8; 8] = [
116 0x00, 0x61, 0x73, 0x6D,
118 0x0d, 0x00, 0x01, 0x00,
120 ];
121
122 pub fn new() -> Self {
124 Self {
125 bytes: Self::HEADER.to_vec(),
126 }
127 }
128
129 pub fn finish(self) -> Vec<u8> {
131 self.bytes
132 }
133
134 pub fn section(&mut self, section: &impl ComponentSection) -> &mut Self {
136 self.bytes.push(section.id());
137 section.encode(&mut self.bytes);
138 self
139 }
140
141 pub fn as_slice(&self) -> &[u8] {
143 &self.bytes
144 }
145}
146
147impl Default for Component {
148 fn default() -> Self {
149 Self::new()
150 }
151}
152
153impl ComponentSection for CustomSection<'_> {
154 fn id(&self) -> u8 {
155 ComponentSectionId::CoreCustom.into()
156 }
157}
158
159impl ComponentSection for RawCustomSection<'_> {
160 fn id(&self) -> u8 {
161 ComponentSectionId::CoreCustom.into()
162 }
163}
164
165impl ComponentSection for ProducersSection {
166 fn id(&self) -> u8 {
167 ComponentSectionId::CoreCustom.into()
168 }
169}