1 | mod aliases; |
2 | mod builder; |
3 | mod canonicals; |
4 | mod components; |
5 | mod exports; |
6 | mod imports; |
7 | mod instances; |
8 | mod modules; |
9 | mod names; |
10 | mod start; |
11 | mod types; |
12 | |
13 | pub use self::aliases::*; |
14 | pub use self::builder::*; |
15 | pub use self::canonicals::*; |
16 | pub use self::components::*; |
17 | pub use self::exports::*; |
18 | pub use self::imports::*; |
19 | pub use self::instances::*; |
20 | pub use self::modules::*; |
21 | pub use self::names::*; |
22 | pub use self::start::*; |
23 | pub use self::types::*; |
24 | |
25 | use crate::{CustomSection, Encode, ProducersSection, RawCustomSection}; |
26 | use alloc::vec::Vec; |
27 | |
28 | // Core sorts extended by the component model |
29 | const CORE_TYPE_SORT: u8 = 0x10; |
30 | const CORE_MODULE_SORT: u8 = 0x11; |
31 | const CORE_INSTANCE_SORT: u8 = 0x12; |
32 | |
33 | const CORE_SORT: u8 = 0x00; |
34 | const FUNCTION_SORT: u8 = 0x01; |
35 | const VALUE_SORT: u8 = 0x02; |
36 | const TYPE_SORT: u8 = 0x03; |
37 | const COMPONENT_SORT: u8 = 0x04; |
38 | const 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. |
45 | pub 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(sink: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)] |
61 | pub 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 | |
88 | impl From<ComponentSectionId> for u8 { |
89 | #[inline ] |
90 | fn from(id: ComponentSectionId) -> u8 { |
91 | id as u8 |
92 | } |
93 | } |
94 | |
95 | impl 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)] |
108 | pub struct Component { |
109 | bytes: Vec<u8>, |
110 | } |
111 | |
112 | impl 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 | |
147 | impl Default for Component { |
148 | fn default() -> Self { |
149 | Self::new() |
150 | } |
151 | } |
152 | |
153 | impl ComponentSection for CustomSection<'_> { |
154 | fn id(&self) -> u8 { |
155 | ComponentSectionId::CoreCustom.into() |
156 | } |
157 | } |
158 | |
159 | impl ComponentSection for RawCustomSection<'_> { |
160 | fn id(&self) -> u8 { |
161 | ComponentSectionId::CoreCustom.into() |
162 | } |
163 | } |
164 | |
165 | impl ComponentSection for ProducersSection { |
166 | fn id(&self) -> u8 { |
167 | ComponentSectionId::CoreCustom.into() |
168 | } |
169 | } |
170 | |