diff options
Diffstat (limited to 'crates/parser')
| -rw-r--r-- | crates/parser/src/conversion.rs | 43 | ||||
| -rw-r--r-- | crates/parser/src/lib.rs | 13 | ||||
| -rw-r--r-- | crates/parser/src/module.rs | 65 |
3 files changed, 83 insertions, 38 deletions
diff --git a/crates/parser/src/conversion.rs b/crates/parser/src/conversion.rs index 9f4930a..fb48321 100644 --- a/crates/parser/src/conversion.rs +++ b/crates/parser/src/conversion.rs @@ -1,12 +1,53 @@ use alloc::{boxed::Box, format, string::ToString, vec::Vec}; use log::info; use tinywasm_types::{ - BlockArgs, ConstInstruction, Export, ExternalKind, FuncType, Global, Instruction, MemArg, ValType, + BlockArgs, ConstInstruction, Export, ExternalKind, FuncType, Global, Instruction, MemArg, MemoryArch, MemoryType, + TableType, ValType, }; use wasmparser::{FuncValidator, ValidatorResources}; use crate::{module::CodeSection, Result}; +pub(crate) fn convert_module_memories<T: IntoIterator<Item = wasmparser::Result<wasmparser::MemoryType>>>( + memory_types: T, +) -> Result<Vec<MemoryType>> { + let memory_type = memory_types + .into_iter() + .map(|memory| { + let memory = memory?; + Ok(MemoryType { + arch: match memory.memory64 { + true => MemoryArch::I64, + false => MemoryArch::I32, + }, + page_count_initial: memory.initial, + page_count_max: memory.maximum, + }) + }) + .collect::<Result<Vec<_>>>()?; + + Ok(memory_type) +} + +pub(crate) fn convert_module_tables<T: IntoIterator<Item = wasmparser::Result<wasmparser::TableType>>>( + table_types: T, +) -> Result<Vec<TableType>> { + let table_type = table_types + .into_iter() + .map(|table| { + let table = table?; + let ty = convert_valtype(&table.element_type); + Ok(TableType { + element_type: ty, + size_initial: table.initial, + size_max: table.maximum, + }) + }) + .collect::<Result<Vec<_>>>()?; + + Ok(table_type) +} + pub(crate) fn convert_module_globals<'a, T: IntoIterator<Item = wasmparser::Result<wasmparser::Global<'a>>>>( globals: T, ) -> Result<Vec<Global>> { diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index bcaef76..36deb7f 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -102,9 +102,9 @@ impl TryFrom<ModuleReader> for TinyWasmModule { return Err(ParseError::EndNotReached); } - let func_types = reader.function_section; + let func_types = reader.func_addrs; let funcs = reader - .code_section + .code .into_iter() .zip(func_types) .map(|(f, ty)| Function { @@ -114,15 +114,18 @@ impl TryFrom<ModuleReader> for TinyWasmModule { }) .collect::<Vec<_>>(); - let globals = reader.global_section; + let globals = reader.globals; + let table_types = reader.table_types; Ok(TinyWasmModule { version: reader.version, start_func: reader.start_func, - types: reader.type_section.into_boxed_slice(), + func_types: reader.func_types.into_boxed_slice(), funcs: funcs.into_boxed_slice(), - exports: reader.export_section.into_boxed_slice(), + exports: reader.exports.into_boxed_slice(), globals: globals.into_boxed_slice(), + table_types: table_types.into_boxed_slice(), + memory_types: reader.memory_types.into_boxed_slice(), }) } } diff --git a/crates/parser/src/module.rs b/crates/parser/src/module.rs index 6f823de..ea6aaad 100644 --- a/crates/parser/src/module.rs +++ b/crates/parser/src/module.rs @@ -1,7 +1,7 @@ use crate::log::debug; use alloc::{boxed::Box, format, vec::Vec}; use core::fmt::Debug; -use tinywasm_types::{Export, FuncType, Global, Instruction, ValType}; +use tinywasm_types::{Export, FuncType, Global, Instruction, MemoryType, TableType, ValType}; use wasmparser::{Payload, Validator}; use crate::{conversion, ParseError, Result}; @@ -17,14 +17,14 @@ pub struct ModuleReader { pub version: Option<u16>, pub start_func: Option<u32>, - pub type_section: Vec<FuncType>, - pub function_section: Vec<u32>, - pub export_section: Vec<Export>, - pub code_section: Vec<CodeSection>, - pub global_section: Vec<Global>, + pub func_types: Vec<FuncType>, + pub func_addrs: Vec<u32>, + pub exports: Vec<Export>, + pub code: Vec<CodeSection>, + pub globals: Vec<Global>, + pub table_types: Vec<TableType>, + pub memory_types: Vec<MemoryType>, - // pub table_section: Option<TableSectionReader<'a>>, - // pub memory_section: Option<MemorySectionReader<'a>>, // pub element_section: Option<ElementSectionReader<'a>>, // pub data_section: Option<DataSectionReader<'a>>, // pub import_section: Option<ImportSectionReader<'a>>, @@ -35,13 +35,13 @@ impl Debug for ModuleReader { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { f.debug_struct("ModuleReader") .field("version", &self.version) - .field("type_section", &self.type_section) - .field("function_section", &self.function_section) - .field("code_section", &self.code_section) - .field("export_section", &self.export_section) - .field("global_section", &self.global_section) - // .field("table_section", &self.table_section) - // .field("memory_section", &self.memory_section) + .field("func_types", &self.func_types) + .field("func_addrs", &self.func_addrs) + .field("code", &self.code) + .field("exports", &self.exports) + .field("globals", &self.globals) + .field("table_types", &self.table_types) + .field("memory_types", &self.memory_types) // .field("element_section", &self.element_section) // .field("data_section", &self.data_section) // .field("import_section", &self.import_section) @@ -74,7 +74,7 @@ impl ModuleReader { TypeSection(reader) => { debug!("Found type section"); validator.type_section(&reader)?; - self.type_section = reader + self.func_types = reader .into_iter() .map(|t| conversion::convert_module_type(t?)) .collect::<Result<Vec<FuncType>>>()?; @@ -82,26 +82,23 @@ impl ModuleReader { FunctionSection(reader) => { debug!("Found function section"); validator.function_section(&reader)?; - self.function_section = reader.into_iter().map(|f| Ok(f?)).collect::<Result<Vec<_>>>()?; + self.func_addrs = reader.into_iter().map(|f| Ok(f?)).collect::<Result<Vec<_>>>()?; } GlobalSection(reader) => { debug!("Found global section"); validator.global_section(&reader)?; - self.global_section = conversion::convert_module_globals(reader)?; + self.globals = conversion::convert_module_globals(reader)?; } - TableSection(_reader) => { - return Err(ParseError::UnsupportedSection("Table section".into())); - // debug!("Found table section"); - // validator.table_section(&reader)?; - // self.table_section = Some(reader); + TableSection(reader) => { + debug!("Found table section"); + validator.table_section(&reader)?; + self.table_types = conversion::convert_module_tables(reader)?; } - MemorySection(_reader) => { - return Err(ParseError::UnsupportedSection("Memory section".into())); - // debug!("Found memory section"); - // validator.memory_section(&reader)?; - // self.memory_section = Some(reader); + MemorySection(reader) => { + debug!("Found memory section"); + validator.memory_section(&reader)?; + self.memory_types = conversion::convert_module_memories(reader)?; } - ElementSection(_reader) => { return Err(ParseError::UnsupportedSection("Element section".into())); // debug!("Found element section"); @@ -116,7 +113,7 @@ impl ModuleReader { } CodeSectionStart { count, range, .. } => { debug!("Found code section ({} functions)", count); - if !self.code_section.is_empty() { + if !self.code.is_empty() { return Err(ParseError::DuplicateSection("Code section".into())); } @@ -127,7 +124,7 @@ impl ModuleReader { let v = validator.code_section_entry(&function)?; let func_validator = v.into_validator(Default::default()); - self.code_section + self.code .push(conversion::convert_module_code(function, func_validator)?); } ImportSection(_reader) => { @@ -140,7 +137,7 @@ impl ModuleReader { ExportSection(reader) => { debug!("Found export section"); validator.export_section(&reader)?; - self.export_section = reader + self.exports = reader .into_iter() .map(|e| conversion::convert_module_export(e?)) .collect::<Result<Vec<_>>>()?; @@ -158,6 +155,10 @@ impl ModuleReader { debug!("Found custom section"); debug!("Skipping custom section: {:?}", reader.name()); } + // TagSection(tag) => { + // debug!("Found tag section"); + // validator.tag_section(&tag)?; + // } UnknownSection { .. } => return Err(ParseError::UnsupportedSection("Unknown section".into())), section => { return Err(ParseError::UnsupportedSection(format!( |
