wasm_encoder/
core.rs

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
50/// A WebAssembly module section.
51///
52/// Various builders defined in this crate already implement this trait, but you
53/// can also implement it yourself for your own custom section builders, or use
54/// `RawSection` to use a bunch of raw bytes as a section.
55pub trait Section: Encode {
56    /// Gets the section identifier for this section.
57    fn id(&self) -> u8;
58
59    /// Appends this section to the specified destination list of bytes.
60    fn append_to(&self, dst: &mut Vec<u8>) {
61        dst.push(self.id());
62        self.encode(dst);
63    }
64}
65
66/// Known section identifiers of WebAssembly modules.
67#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
68#[repr(u8)]
69pub enum SectionId {
70    /// The custom section.
71    Custom = 0,
72    /// The type section.
73    Type = 1,
74    /// The import section.
75    Import = 2,
76    /// The function section.
77    Function = 3,
78    /// The table section.
79    Table = 4,
80    /// The memory section.
81    Memory = 5,
82    /// The global section.
83    Global = 6,
84    /// The export section.
85    Export = 7,
86    /// The start section.
87    Start = 8,
88    /// The element section.
89    Element = 9,
90    /// The code section.
91    Code = 10,
92    /// The data section.
93    Data = 11,
94    /// The data count section.
95    DataCount = 12,
96    /// The tag section.
97    ///
98    /// This section is supported by the exception handling proposal.
99    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/// Represents a WebAssembly component that is being encoded.
116///
117/// Sections within a WebAssembly module are encoded in a specific order.
118///
119/// Modules may also added as a section to a WebAssembly component.
120#[derive(Clone, Debug)]
121pub struct Module {
122    pub(crate) bytes: Vec<u8>,
123}
124
125impl Module {
126    /// The 8-byte header at the beginning of all core wasm modules.
127    #[rustfmt::skip]
128    pub const HEADER: [u8; 8] = [
129        // Magic
130        0x00, 0x61, 0x73, 0x6D,
131        // Version
132        0x01, 0x00, 0x00, 0x00,
133    ];
134
135    /// Begin writing a new `Module`.
136    #[rustfmt::skip]
137    pub fn new() -> Self {
138        Module {
139            bytes: Self::HEADER.to_vec(),
140        }
141    }
142
143    /// Write a section into this module.
144    ///
145    /// It is your responsibility to define the sections in the [proper
146    /// order](https://webassembly.github.io/spec/core/binary/modules.html#binary-module),
147    /// and to ensure that each kind of section (other than custom sections) is
148    /// only defined once. While this is a potential footgun, it also allows you
149    /// to use this crate to easily construct test cases for bad Wasm module
150    /// encodings.
151    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    /// Get the encoded Wasm module as a slice.
158    pub fn as_slice(&self) -> &[u8] {
159        &self.bytes
160    }
161
162    /// Give the current size of the module in bytes.
163    pub fn len(&self) -> usize {
164        self.bytes.len()
165    }
166
167    /// Finish writing this Wasm module and extract ownership of the encoded
168    /// bytes.
169    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}