1 | use crate::{ComponentSection, ComponentSectionId, Encode}; |
2 | |
3 | /// An encoder for the start section of WebAssembly components. |
4 | /// |
5 | /// # Example |
6 | /// |
7 | /// ``` |
8 | /// use wasm_encoder::{Component, ComponentStartSection}; |
9 | /// |
10 | /// let start = ComponentStartSection { function_index: 0, args: [0, 1], results: 1 }; |
11 | /// |
12 | /// let mut component = Component::new(); |
13 | /// component.section(&start); |
14 | /// |
15 | /// let bytes = component.finish(); |
16 | /// ``` |
17 | #[derive (Clone, Debug)] |
18 | pub struct ComponentStartSection<A> { |
19 | /// The index to the start function. |
20 | pub function_index: u32, |
21 | /// The arguments to pass to the start function. |
22 | /// |
23 | /// An argument is an index to a value. |
24 | pub args: A, |
25 | /// The number of expected results for the start function. |
26 | /// |
27 | /// This should match the number of results for the type of |
28 | /// the function referenced by `function_index`. |
29 | pub results: u32, |
30 | } |
31 | |
32 | impl<A> Encode for ComponentStartSection<A> |
33 | where |
34 | A: AsRef<[u32]>, |
35 | { |
36 | fn encode(&self, sink: &mut Vec<u8>) { |
37 | let mut bytes: Vec = Vec::new(); |
38 | self.function_index.encode(&mut bytes); |
39 | self.args.as_ref().encode(&mut bytes); |
40 | self.results.encode(&mut bytes); |
41 | bytes.encode(sink); |
42 | } |
43 | } |
44 | |
45 | impl<A> ComponentSection for ComponentStartSection<A> |
46 | where |
47 | A: AsRef<[u32]>, |
48 | { |
49 | fn id(&self) -> u8 { |
50 | ComponentSectionId::Start.into() |
51 | } |
52 | } |
53 | |