1use anyhow::{Result, bail, ensure};
2pub use wasmparser::FunctionBody;
3
4use super::Ind;
5use crate::index::{FuncTypeId, IdVec, InputFuncId};
6
7#[derive(Debug)]
8pub enum InputFunction<'a> {
9 Import {},
10 Defined(FunctionBody<'a>),
11}
12#[derive(Debug, Clone)]
13pub struct FunctionWithBody<'a> {
14 pub type_id: FuncTypeId,
15 pub body: FunctionBody<'a>,
16}
17
18#[derive(Debug, Default)]
19pub struct CodeSection<'a> {
20 pub start_func: Option<InputFuncId>,
21 pub defined_funcs: IdVec<FunctionWithBody<'a>>,
23}
24impl<'a> CodeSection<'a> {
25 pub fn new(
26 start: Option<InputFuncId>,
27 funcs: Vec<FunctionBody<'a>>,
28 func_types: Vec<FuncTypeId>,
29 code_header: Option<(usize, usize, u32)>,
30 ) -> Result<Ind<Self>> {
31 let Some((code_start, section_index, count)) = code_header else {
32 bail!("No code section start");
33 };
34 ensure!(
35 count as usize == funcs.len(),
36 "Function count mismatch: {} != {}",
37 count,
38 funcs.len()
39 );
40 ensure!(
41 count as usize == func_types.len(),
42 "Function types count mismatch: {} != {}",
43 count,
44 func_types.len()
45 );
46 Ok(Ind {
47 starting_offset: code_start,
48 section_index,
49 section_payload: CodeSection {
50 start_func: start,
51 defined_funcs: funcs
52 .into_iter()
53 .zip(&func_types)
54 .map(|(body, ty)| FunctionWithBody { type_id: *ty, body })
55 .collect(),
56 },
57 })
58 }
59}