pub(crate) fn parse<'a>(full_wasm: &'a [u8]) -> anyhow::Result<ModuleContext<'a>> {
    log::debug!("Parsing the input Wasm");

    let mut cx = ModuleContext::new();

    // The wasm we are currently parsing. This is advanced as the parser
    // consumes input.
    let mut wasm = full_wasm;

    let mut stack = vec![StackEntry {
        parser: wasmparser::Parser::new(0),
        module: cx.root(),
    }];

    loop {
        let (payload, consumed) = match stack
            .top_mut()
            .parser
            .parse(wasm, true)
            .context("failed to parse Wasm")?
        {
            wasmparser::Chunk::NeedMoreData(_) => unreachable!(),
            wasmparser::Chunk::Parsed { payload, consumed } => (payload, consumed),
        };
        wasm = &wasm[consumed..];

        use wasmparser::Payload::*;
        match payload {
            Version { .. } => {}
            TypeSection(types) => type_section(&mut cx, &mut stack, full_wasm, types)?,
            ImportSection(imports) => import_section(&mut cx, &mut stack, full_wasm, imports)?,
            AliasSection(aliases) => alias_section(&mut cx, &mut stack, full_wasm, aliases)?,
            InstanceSection(instances) => {
                instance_section(&mut cx, &mut stack, full_wasm, instances)?
            }
            ModuleSectionStart {
                range,
                size: _,
                count: _,
            } => {
                stack.top_mut().module.add_raw_section(
                    &mut cx,
                    SectionId::Module,
                    range,
                    full_wasm,
                );
            }
            ModuleSectionEntry { parser, range: _ } => {
                stack.push(StackEntry {
                    parser,
                    module: Module::new_defined(&mut cx),
                });
            }
            FunctionSection(funcs) => function_section(&mut cx, &mut stack, full_wasm, funcs)?,
            TableSection(tables) => table_section(&mut cx, &mut stack, full_wasm, tables)?,
            MemorySection(mems) => memory_section(&mut cx, &mut stack, full_wasm, mems)?,
            GlobalSection(globals) => global_section(&mut cx, &mut stack, full_wasm, globals)?,
            ExportSection(exports) => export_section(&mut cx, &mut stack, full_wasm, exports)?,
            StartSection { func: _, range } => {
                stack
                    .top_mut()
                    .module
                    .add_raw_section(&mut cx, SectionId::Start, range, full_wasm)
            }
            ElementSection(elems) => stack.top_mut().module.add_raw_section(
                &mut cx,
                SectionId::Element,
                elems.range(),
                full_wasm,
            ),
            DataCountSection { .. } => unreachable!("validation rejects bulk memory"),
            DataSection(data) => stack.top_mut().module.add_raw_section(
                &mut cx,
                SectionId::Data,
                data.range(),
                full_wasm,
            ),
            CustomSection { range, .. } => {
                stack
                    .top_mut()
                    .module
                    .add_raw_section(&mut cx, SectionId::Custom, range, full_wasm)
            }
            CodeSectionStart {
                range,
                count: _,
                size,
            } => {
                wasm = &wasm[usize::try_from(size).unwrap()..];
                let entry = stack.top_mut();
                entry.parser.skip_section();
                entry
                    .module
                    .add_raw_section(&mut cx, SectionId::Code, range, full_wasm)
            }
            CodeSectionEntry(_) => unreachable!(),
            UnknownSection { .. } => anyhow::bail!("unknown section"),
            EventSection(_) => anyhow::bail!("exceptions are not supported yet"),
            End => {
                let entry = stack.pop().unwrap();

                // If we finished parsing the root Wasm module, then we're done.
                if entry.module.is_root() {
                    assert!(stack.is_empty());
                    return Ok(cx);
                }

                // Otherwise, we need to add this module to its parent's module
                // section.
                let parent = stack.top_mut();
                parent.module.push_child_module(&mut cx, entry.module);
            }
        }
    }
}
/// Parse the given Wasm bytes into a `ModuleInfo` tree.
Parse the given Wasm bytes into a `ModuleInfo` tree.
Parse the given Wasm bytes into a ` ModuleInfo ` tree .
pub(crate) fn parse<'a>(full_wasm: &'a [u8]) -> anyhow::Result<ModuleContext<'a>> {
    log::debug!("Parsing the input Wasm");
    let mut cx = ModuleContext::new();
    // The wasm we are currently parsing. This is advanced as the parser
    // consumes input.
    let mut wasm = full_wasm;
    let mut stack = vec![StackEntry {
        parser: wasmparser::Parser::new(0),
        module: cx.root(),
    }];
    loop {
        let (payload, consumed) = match stack
            .top_mut()
            .parser
            .parse(wasm, true)
            .context("failed to parse Wasm")?
        {
            wasmparser::Chunk::NeedMoreData(_) => unreachable!(),
            wasmparser::Chunk::Parsed { payload, consumed } => (payload, consumed),
        };
        wasm = &wasm[consumed..];
        use wasmparser::Payload::*;
        match payload {
            Version { .. } => {}
            TypeSection(types) => type_section(&mut cx, &mut stack, full_wasm, types)?,
            ImportSection(imports) => import_section(&mut cx, &mut stack, full_wasm, imports)?,
            AliasSection(aliases) => alias_section(&mut cx, &mut stack, full_wasm, aliases)?,
            InstanceSection(instances) => {
                instance_section(&mut cx, &mut stack, full_wasm, instances)?
            }
            ModuleSectionStart {
                range,
                size: _,
                count: _,
            } => {
                stack.top_mut().module.add_raw_section(
                    &mut cx,
                    SectionId::Module,
                    range,
                    full_wasm,
                );
            }
            ModuleSectionEntry { parser, range: _ } => {
                stack.push(StackEntry {
                    parser,
                    module: Module::new_defined(&mut cx),
                });
            }
            FunctionSection(funcs) => function_section(&mut cx, &mut stack, full_wasm, funcs)?,
            TableSection(tables) => table_section(&mut cx, &mut stack, full_wasm, tables)?,
            MemorySection(mems) => memory_section(&mut cx, &mut stack, full_wasm, mems)?,
            GlobalSection(globals) => global_section(&mut cx, &mut stack, full_wasm, globals)?,
            ExportSection(exports) => export_section(&mut cx, &mut stack, full_wasm, exports)?,
            StartSection { func: _, range } => {
                stack
                    .top_mut()
                    .module
                    .add_raw_section(&mut cx, SectionId::Start, range, full_wasm)
            }
            ElementSection(elems) => stack.top_mut().module.add_raw_section(
                &mut cx,
                SectionId::Element,
                elems.range(),
                full_wasm,
            ),
            DataCountSection { .. } => unreachable!("validation rejects bulk memory"),
            DataSection(data) => stack.top_mut().module.add_raw_section(
                &mut cx,
                SectionId::Data,
                data.range(),
                full_wasm,
            ),
            CustomSection { range, .. } => {
                stack
                    .top_mut()
                    .module
                    .add_raw_section(&mut cx, SectionId::Custom, range, full_wasm)
            }
            CodeSectionStart {
                range,
                count: _,
                size,
            } => {
                wasm = &wasm[usize::try_from(size).unwrap()..];
                let entry = stack.top_mut();
                entry.parser.skip_section();
                entry
                    .module
                    .add_raw_section(&mut cx, SectionId::Code, range, full_wasm)
            }
            CodeSectionEntry(_) => unreachable!(),
            UnknownSection { .. } => anyhow::bail!("unknown section"),
            EventSection(_) => anyhow::bail!("exceptions are not supported yet"),
            End => {
                let entry = stack.pop().unwrap();
                // If we finished parsing the root Wasm module, then we're done.
                if entry.module.is_root() {
                    assert!(stack.is_empty());
                    return Ok(cx);
                }
                // Otherwise, we need to add this module to its parent's module
                // section.
                let parent = stack.top_mut();
                parent.module.push_child_module(&mut cx, entry.module);
            }
        }
    }
}
pub ( crate ) fn parse < ' a > ( full_wasm : & ' a [ u8 ] ) -> anyhow :: Result < ModuleContext < ' a > > { log :: debug ! ( " " ) ; let mut cx = ModuleContext :: new ( ) ; // The wasm we are currently parsing. This is advanced as the parser // consumes input. let mut wasm = full_wasm ; let mut stack = vec ! [ StackEntry { parser wasmparser Parser new ( 0 ) module cx root ( ) } ] ; loop { let ( payload , consumed ) = match stack . top_mut ( ) . parser . parse ( wasm , true ) . context ( " " ) ? { wasmparser :: Chunk :: NeedMoreData ( _ ) => unreachable ! ( ) , wasmparser :: Chunk :: Parsed { payload , consumed } => ( payload , consumed ) , } ; wasm = & wasm [ consumed .. ] ; use wasmparser :: Payload :: * ; match payload { Version { .. } => { } TypeSection ( types ) => type_section ( & mut cx , & mut stack , full_wasm , types ) ? , ImportSection ( imports ) => import_section ( & mut cx , & mut stack , full_wasm , imports ) ? , AliasSection ( aliases ) => alias_section ( & mut cx , & mut stack , full_wasm , aliases ) ? , InstanceSection ( instances ) => { instance_section ( & mut cx , & mut stack , full_wasm , instances ) ? } ModuleSectionStart { range , size : _ , count : _ , } => { stack . top_mut ( ) . module . add_raw_section ( & mut cx , SectionId :: Module , range , full_wasm , ) ; } ModuleSectionEntry { parser , range : _ } => { stack . push ( StackEntry { parser , module : Module :: new_defined ( & mut cx ) , } ) ; } FunctionSection ( funcs ) => function_section ( & mut cx , & mut stack , full_wasm , funcs ) ? , TableSection ( tables ) => table_section ( & mut cx , & mut stack , full_wasm , tables ) ? , MemorySection ( mems ) => memory_section ( & mut cx , & mut stack , full_wasm , mems ) ? , GlobalSection ( globals ) => global_section ( & mut cx , & mut stack , full_wasm , globals ) ? , ExportSection ( exports ) => export_section ( & mut cx , & mut stack , full_wasm , exports ) ? , StartSection { func : _ , range } => { stack . top_mut ( ) . module . add_raw_section ( & mut cx , SectionId :: Start , range , full_wasm ) } ElementSection ( elems ) => stack . top_mut ( ) . module . add_raw_section ( & mut cx , SectionId :: Element , elems . range ( ) , full_wasm , ) , DataCountSection { .. } => unreachable ! ( " " ) , DataSection ( data ) => stack . top_mut ( ) . module . add_raw_section ( & mut cx , SectionId :: Data , data . range ( ) , full_wasm , ) , CustomSection { range , .. } => { stack . top_mut ( ) . module . add_raw_section ( & mut cx , SectionId :: Custom , range , full_wasm ) } CodeSectionStart { range , count : _ , size , } => { wasm = & wasm [ usize :: try_from ( size ) . unwrap ( ) .. ] ; let entry = stack . top_mut ( ) ; entry . parser . skip_section ( ) ; entry . module . add_raw_section ( & mut cx , SectionId :: Code , range , full_wasm ) } CodeSectionEntry ( _ ) => unreachable ! ( ) , UnknownSection { .. } => anyhow :: bail ! ( " " ) , EventSection ( _ ) => anyhow :: bail ! ( " " ) , End => { let entry = stack . pop ( ) . unwrap ( ) ; // If we finished parsing the root Wasm module, then we're done. if entry . module . is_root ( ) { assert ! ( stack is_empty ( ) ) ; return Ok ( cx ) ; } // Otherwise, we need to add this module to its parent's module // section. let parent = stack . top_mut ( ) ; parent . module . push_child_module ( & mut cx , entry . module ) ; } } } }
Parse the given Wasm bytes into a `ModuleInfo` tree.
Parse the given Wasm bytes into a ` ModuleInfo ` tree .
full_wasm
&'a [u8]
full_wasm
&'a [u8]