From 375f9aa32b117a42c7a9c7f29d3c592b81d966be Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Tue, 19 Dec 2023 14:37:51 +0100 Subject: feat: parse memory and table sections Signed-off-by: Henry Gressmann --- crates/parser/src/conversion.rs | 43 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'crates/parser/src/conversion.rs') 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>>( + memory_types: T, +) -> Result> { + 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::>>()?; + + Ok(memory_type) +} + +pub(crate) fn convert_module_tables>>( + table_types: T, +) -> Result> { + 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::>>()?; + + Ok(table_type) +} + pub(crate) fn convert_module_globals<'a, T: IntoIterator>>>( globals: T, ) -> Result> { -- cgit v1.3.1