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