| 1 |
mod branch_hints; |
| 2 |
mod code; |
| 3 |
mod custom; |
| 4 |
mod data; |
| 5 |
mod dump; |
| 6 |
mod elements; |
| 7 |
mod exports; |
| 8 |
mod functions; |
| 9 |
mod globals; |
| 10 |
mod imports; |
| 11 |
mod instructions; |
| 12 |
mod linking; |
| 13 |
mod memories; |
| 14 |
mod names; |
| 15 |
mod producers; |
| 16 |
mod start; |
| 17 |
mod tables; |
| 18 |
mod tags; |
| 19 |
mod types; |
| 20 |
|
| 21 |
pub use branch_hints::*; |
| 22 |
pub use code::*; |
| 23 |
pub use custom::*; |
| 24 |
pub use data::*; |
| 25 |
pub use dump::*; |
| 26 |
pub use elements::*; |
| 27 |
pub use exports::*; |
| 28 |
pub use functions::*; |
| 29 |
pub use globals::*; |
| 30 |
pub use imports::*; |
| 31 |
pub use instructions::*; |
| 32 |
pub use linking::*; |
| 33 |
pub use memories::*; |
| 34 |
pub use names::*; |
| 35 |
pub use producers::*; |
| 36 |
pub use start::*; |
| 37 |
pub use tables::*; |
| 38 |
pub use tags::*; |
| 39 |
pub use types::*; |
| 40 |
|
| 41 |
use crate::Encode; |
| 42 |
use alloc::vec::Vec; |
| 43 |
|
| 44 |
pub(crate) const CORE_FUNCTION_SORT: u8 = 0x00; |
| 45 |
pub(crate) const CORE_TABLE_SORT: u8 = 0x01; |
| 46 |
pub(crate) const CORE_MEMORY_SORT: u8 = 0x02; |
| 47 |
pub(crate) const CORE_GLOBAL_SORT: u8 = 0x03; |
| 48 |
pub(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. |
| 55 |
pub 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)] |
| 69 |
pub 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 |
|
| 102 |
impl From<SectionId> for u8 { |
| 103 |
#[inline] |
| 104 |
fn from(id: SectionId) -> u8 { |
| 105 |
id as u8 |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
impl 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)] |
| 121 |
pub struct Module { |
| 122 |
pub(crate) bytes: Vec<u8>, |
| 123 |
} |
| 124 |
|
| 125 |
impl 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 |
|
| 174 |
impl Default for Module { |
| 175 |
fn default() -> Self { |
| 176 |
Self::new() |
| 177 |
} |
| 178 |
} |