wasm_encoder/
component.rs

1mod 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
28// Core sorts extended by the component model
29const 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
40/// A WebAssembly component section.
41///
42/// Various builders defined in this crate already implement this trait, but you
43/// can also implement it yourself for your own custom section builders, or use
44/// `RawSection` to use a bunch of raw bytes as a section.
45pub trait ComponentSection: Encode {
46    /// Gets the section identifier for this section.
47    fn id(&self) -> u8;
48
49    /// Appends this section to the specified destination list of bytes.
50    fn append_to_component(&self, dst: &mut Vec<u8>) {
51        dst.push(self.id());
52        self.encode(dst);
53    }
54}
55
56/// Known section identifiers of WebAssembly components.
57///
58/// These sections are supported by the component model proposal.
59#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
60#[repr(u8)]
61pub enum ComponentSectionId {
62    /// The section is a core custom section.
63    CoreCustom = 0,
64    /// The section is a core module section.
65    CoreModule = 1,
66    /// The section is a core instance section.
67    CoreInstance = 2,
68    /// The section is a core type section.
69    CoreType = 3,
70    /// The section is a component section.
71    Component = 4,
72    /// The section is an instance section.
73    Instance = 5,
74    /// The section is an alias section.
75    Alias = 6,
76    /// The section is a type section.
77    Type = 7,
78    /// The section is a canonical function section.
79    CanonicalFunction = 8,
80    /// The section is a start section.
81    Start = 9,
82    /// The section is an import section.
83    Import = 10,
84    /// The section is an export section.
85    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/// Represents a WebAssembly component that is being encoded.
102///
103/// Unlike core WebAssembly modules, the sections of a component
104/// may appear in any order and may be repeated.
105///
106/// Components may also added as a section to other components.
107#[derive(Clone, Debug)]
108pub struct Component {
109    bytes: Vec<u8>,
110}
111
112impl Component {
113    /// The 8-byte header at the beginning of all components.
114    #[rustfmt::skip]
115    pub const HEADER: [u8; 8] = [
116        // Magic
117        0x00, 0x61, 0x73, 0x6D,
118        // Version
119        0x0d, 0x00, 0x01, 0x00,
120    ];
121
122    /// Begin writing a new `Component`.
123    pub fn new() -> Self {
124        Self {
125            bytes: Self::HEADER.to_vec(),
126        }
127    }
128
129    /// Finish writing this component and extract ownership of the encoded bytes.
130    pub fn finish(self) -> Vec<u8> {
131        self.bytes
132    }
133
134    /// Write a section to this component.
135    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    /// View the encoded bytes.
142    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}