1mod aliases;
2mod builder;
3mod canonicals;
4mod components;
5mod exports;
6mod imports;
7mod instances;
8mod modules;
9mod names;
10mod start;
11mod types;
1213pub 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::*;
2425use crate::{CustomSection, Encode, ProducersSection, RawCustomSection};
26use alloc::vec::Vec;
2728// 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;
3233const 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;
3940/// 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.
47fn id(&self) -> u8;
4849/// Appends this section to the specified destination list of bytes.
50fn append_to_component(&self, dst: &mut Vec<u8>) {
51 dst.push(self.id());
52self.encode(dst);
53 }
54}
5556/// 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.
63CoreCustom = 0,
64/// The section is a core module section.
65CoreModule = 1,
66/// The section is a core instance section.
67CoreInstance = 2,
68/// The section is a core type section.
69CoreType = 3,
70/// The section is a component section.
71Component = 4,
72/// The section is an instance section.
73Instance = 5,
74/// The section is an alias section.
75Alias = 6,
76/// The section is a type section.
77Type = 7,
78/// The section is a canonical function section.
79CanonicalFunction = 8,
80/// The section is a start section.
81Start = 9,
82/// The section is an import section.
83Import = 10,
84/// The section is an export section.
85Export = 11,
86}
8788impl From<ComponentSectionId> for u8 {
89#[inline]
90fn from(id: ComponentSectionId) -> u8 {
91 id as u8
92 }
93}
9495impl Encode for ComponentSectionId {
96fn encode(&self, sink: &mut Vec<u8>) {
97 sink.push(*self as u8);
98 }
99}
100101/// 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}
111112impl Component {
113/// The 8-byte header at the beginning of all components.
114#[rustfmt::skip]
115pub const HEADER: [u8; 8] = [
116// Magic
1170x00, 0x61, 0x73, 0x6D,
118// Version
1190x0d, 0x00, 0x01, 0x00,
120 ];
121122/// Begin writing a new `Component`.
123pub fn new() -> Self {
124Self {
125 bytes: Self::HEADER.to_vec(),
126 }
127 }
128129/// Finish writing this component and extract ownership of the encoded bytes.
130pub fn finish(self) -> Vec<u8> {
131self.bytes
132 }
133134/// Write a section to this component.
135pub fn section(&mut self, section: &impl ComponentSection) -> &mut Self {
136self.bytes.push(section.id());
137 section.encode(&mut self.bytes);
138self
139}
140141/// View the encoded bytes.
142pub fn as_slice(&self) -> &[u8] {
143&self.bytes
144 }
145}
146147impl Default for Component {
148fn default() -> Self {
149Self::new()
150 }
151}
152153impl ComponentSection for CustomSection<'_> {
154fn id(&self) -> u8 {
155 ComponentSectionId::CoreCustom.into()
156 }
157}
158159impl ComponentSection for RawCustomSection<'_> {
160fn id(&self) -> u8 {
161 ComponentSectionId::CoreCustom.into()
162 }
163}
164165impl ComponentSection for ProducersSection {
166fn id(&self) -> u8 {
167 ComponentSectionId::CoreCustom.into()
168 }
169}