From 4ee0bacbf79c41661b24e6cb9e4420cca33dc039 Mon Sep 17 00:00:00 2001 From: Henry Date: Sat, 23 Dec 2023 18:35:49 +0100 Subject: chore: parse element sections, improve contributing.md Signed-off-by: Henry --- crates/parser/src/conversion.rs | 53 +++++++++++++++++++++++++++++++++++++++-- crates/parser/src/lib.rs | 1 + crates/parser/src/module.rs | 12 +++++----- 3 files changed, 58 insertions(+), 8 deletions(-) (limited to 'crates/parser') diff --git a/crates/parser/src/conversion.rs b/crates/parser/src/conversion.rs index 700cb0c..2c78e67 100644 --- a/crates/parser/src/conversion.rs +++ b/crates/parser/src/conversion.rs @@ -1,13 +1,62 @@ use alloc::{boxed::Box, format, string::ToString, vec::Vec}; use log::info; use tinywasm_types::{ - BlockArgs, ConstInstruction, Export, ExternalKind, FuncType, Global, GlobalType, Import, ImportKind, Instruction, - MemArg, MemoryArch, MemoryType, TableType, ValType, + BlockArgs, ConstInstruction, ElementItem, Export, ExternalKind, FuncType, Global, GlobalType, Import, ImportKind, + Instruction, MemArg, MemoryArch, MemoryType, TableType, ValType, }; use wasmparser::{FuncValidator, OperatorsReader, ValidatorResources}; use crate::{module::CodeSection, Result}; +pub(crate) fn convert_module_elements<'a, T: IntoIterator>>>( + elements: T, +) -> Result> { + let elements = elements + .into_iter() + .map(|element| convert_module_element(element?)) + .collect::>>()?; + Ok(elements) +} + +pub(crate) fn convert_module_element(element: wasmparser::Element<'_>) -> Result { + let kind = match element.kind { + wasmparser::ElementKind::Active { + table_index, + offset_expr, + } => tinywasm_types::ElementKind::Active { + table: table_index, + offset: process_const_operators(offset_expr.get_operators_reader())?, + }, + wasmparser::ElementKind::Passive => tinywasm_types::ElementKind::Passive, + wasmparser::ElementKind::Declared => tinywasm_types::ElementKind::Declared, + }; + + let items = match element.items { + wasmparser::ElementItems::Functions(funcs) => funcs + .into_iter() + .map(|func| Ok(ElementItem::Func(func?))) + .collect::>>()? + .into_boxed_slice(), + + wasmparser::ElementItems::Expressions(exprs) => exprs + .into_iter() + .map(|expr| { + Ok(ElementItem::Expr(process_const_operators( + expr?.get_operators_reader(), + )?)) + }) + .collect::>>()? + .into_boxed_slice(), + }; + + Ok(tinywasm_types::Element { + kind, + items, + ty: convert_valtype(&element.ty), + range: element.range, + }) +} + pub(crate) fn convert_module_data_sections<'a, T: IntoIterator>>>( data_sections: T, ) -> Result> { diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index f1477f9..22e2661 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -128,6 +128,7 @@ impl TryFrom for TinyWasmModule { memory_types: reader.memory_types.into_boxed_slice(), imports: reader.imports.into_boxed_slice(), data: reader.data.into_boxed_slice(), + elements: reader.elements.into_boxed_slice(), }) } } diff --git a/crates/parser/src/module.rs b/crates/parser/src/module.rs index fdad900..660a702 100644 --- a/crates/parser/src/module.rs +++ b/crates/parser/src/module.rs @@ -2,7 +2,7 @@ use crate::log::debug; use crate::{conversion, ParseError, Result}; use alloc::{boxed::Box, format, vec::Vec}; use core::fmt::Debug; -use tinywasm_types::{Data, Export, FuncType, Global, Import, Instruction, MemoryType, TableType, ValType}; +use tinywasm_types::{Data, Element, Export, FuncType, Global, Import, Instruction, MemoryType, TableType, ValType}; use wasmparser::{Payload, Validator}; #[derive(Debug, Clone, PartialEq)] @@ -25,6 +25,7 @@ pub struct ModuleReader { pub memory_types: Vec, pub imports: Vec, pub data: Vec, + pub elements: Vec, // pub element_section: Option>, pub end_reached: bool, @@ -122,11 +123,10 @@ impl ModuleReader { 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"); - // validator.element_section(&reader)?; - // self.element_section = Some(reader); + ElementSection(reader) => { + debug!("Found element section"); + validator.element_section(&reader)?; + self.elements = conversion::convert_module_elements(reader)?; } DataSection(reader) => { if !self.data.is_empty() { -- cgit v1.3.1