wacko/sections/
function_section.rs1use crate::Error;
2use leb128::write;
3use std::io::Write;
4
5pub struct FunctionSection {
7 declarations: Vec<u32>,
8}
9
10impl FunctionSection {
11 pub fn new() -> Self {
12 Self {
13 declarations: Vec::new(),
14 }
15 }
16
17 pub fn add_fn_decl(&mut self, type_index: u32) -> usize {
18 self.declarations.push(type_index);
19 self.declarations.len() - 1
20 }
21
22 pub fn compile(self, block_count: usize, writer: &mut impl Write) -> Result<(), Error> {
23 writer.write_all(&[Self::id()])?;
24 let mut buff = Vec::new();
25 write::unsigned(&mut buff, block_count as u64)?;
26 for i in self.declarations.len() - block_count..self.declarations.len() {
27 write::unsigned(&mut buff, self.declarations[i] as u64)?;
28 }
29
30 write::unsigned(writer, buff.len() as u64)?;
31 writer.write_all(&buff)?;
32 Ok(())
33 }
34
35 fn id() -> u8 {
36 0x03
37 }
38}
39
40impl Default for FunctionSection {
41 fn default() -> Self {
42 Self::new()
43 }
44}