summaryrefslogtreecommitdiff
path: root/crates/parser/src
diff options
context:
space:
mode:
authorHenry <mail@henrygressmann.de>2023-12-23 18:35:49 +0100
committerHenry <mail@henrygressmann.de>2023-12-23 18:35:49 +0100
commit4ee0bacbf79c41661b24e6cb9e4420cca33dc039 (patch)
treeacdfdade801fa435472293ff46279dd0a0898828 /crates/parser/src
parent154d5fe5311ede2be44036900ad7598fe5fc8664 (diff)
chore: parse element sections, improve contributing.md
Signed-off-by: Henry <mail@henrygressmann.de>
Diffstat (limited to 'crates/parser/src')
-rw-r--r--crates/parser/src/conversion.rs53
-rw-r--r--crates/parser/src/lib.rs1
-rw-r--r--crates/parser/src/module.rs12
3 files changed, 58 insertions, 8 deletions
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<Item = wasmparser::Result<wasmparser::Element<'a>>>>(
+ elements: T,
+) -> Result<Vec<tinywasm_types::Element>> {
+ let elements = elements
+ .into_iter()
+ .map(|element| convert_module_element(element?))
+ .collect::<Result<Vec<_>>>()?;
+ Ok(elements)
+}
+
+pub(crate) fn convert_module_element(element: wasmparser::Element<'_>) -> Result<tinywasm_types::Element> {
+ 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::<Result<Vec<_>>>()?
+ .into_boxed_slice(),
+
+ wasmparser::ElementItems::Expressions(exprs) => exprs
+ .into_iter()
+ .map(|expr| {
+ Ok(ElementItem::Expr(process_const_operators(
+ expr?.get_operators_reader(),
+ )?))
+ })
+ .collect::<Result<Vec<_>>>()?
+ .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<Item = wasmparser::Result<wasmparser::Data<'a>>>>(
data_sections: T,
) -> Result<Vec<tinywasm_types::Data>> {
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<ModuleReader> 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<MemoryType>,
pub imports: Vec<Import>,
pub data: Vec<Data>,
+ pub elements: Vec<Element>,
// pub element_section: Option<ElementSectionReader<'a>>,
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() {