diff options
| author | Henry Gressmann <mail@henrygressmann.de> | 2024-02-25 17:51:41 +0100 |
|---|---|---|
| committer | Henry Gressmann <mail@henrygressmann.de> | 2024-02-25 17:51:41 +0100 |
| commit | 062515c39154b29bb0406b207e3c67bae399a449 (patch) | |
| tree | 722520f15d62714c003f8dad36651d30ffba4e47 /crates/parser/src/module.rs | |
| parent | a0368bc39ac620b867a105ca0c9bc12824abcd9d (diff) | |
feat: new parser architecture
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
Diffstat (limited to 'crates/parser/src/module.rs')
| -rw-r--r-- | crates/parser/src/module.rs | 27 |
1 files changed, 11 insertions, 16 deletions
diff --git a/crates/parser/src/module.rs b/crates/parser/src/module.rs index a18d343..813a65d 100644 --- a/crates/parser/src/module.rs +++ b/crates/parser/src/module.rs @@ -1,16 +1,9 @@ use crate::log::debug; use crate::{conversion, ParseError, Result}; use alloc::{boxed::Box, format, vec::Vec}; -use core::fmt::Debug; use tinywasm_types::{Data, Element, Export, FuncType, Global, Import, Instruction, MemoryType, TableType, ValType}; use wasmparser::{Payload, Validator}; -#[derive(Debug, Clone)] -pub(crate) struct CodeSection { - pub(crate) locals: Box<[ValType]>, - pub(crate) body: Box<[Instruction]>, -} - #[derive(Default)] pub(crate) struct ModuleReader { pub(crate) version: Option<u16>, @@ -18,7 +11,7 @@ pub(crate) struct ModuleReader { pub(crate) func_types: Vec<FuncType>, pub(crate) code_type_addrs: Vec<u32>, pub(crate) exports: Vec<Export>, - pub(crate) code: Vec<CodeSection>, + pub(crate) code: Vec<(Box<[Instruction]>, Box<[ValType]>)>, pub(crate) globals: Vec<Global>, pub(crate) table_types: Vec<TableType>, pub(crate) memory_types: Vec<MemoryType>, @@ -66,15 +59,7 @@ impl ModuleReader { .map(|t| conversion::convert_module_type(t?)) .collect::<Result<Vec<FuncType>>>()?; } - FunctionSection(reader) => { - if !self.code_type_addrs.is_empty() { - return Err(ParseError::DuplicateSection("Function section".into())); - } - debug!("Found function section"); - validator.function_section(&reader)?; - self.code_type_addrs = reader.into_iter().map(|f| Ok(f?)).collect::<Result<Vec<_>>>()?; - } GlobalSection(reader) => { if !self.globals.is_empty() { return Err(ParseError::DuplicateSection("Global section".into())); @@ -122,11 +107,21 @@ impl ModuleReader { } validator.data_count_section(count, &range)?; } + FunctionSection(reader) => { + if !self.code_type_addrs.is_empty() { + return Err(ParseError::DuplicateSection("Function section".into())); + } + + debug!("Found function section"); + validator.function_section(&reader)?; + self.code_type_addrs = reader.into_iter().map(|f| Ok(f?)).collect::<Result<Vec<_>>>()?; + } CodeSectionStart { count, range, .. } => { debug!("Found code section ({} functions)", count); if !self.code.is_empty() { return Err(ParseError::DuplicateSection("Code section".into())); } + self.code.reserve(count as usize); validator.code_section_start(count, &range)?; } CodeSectionEntry(function) => { |
