summaryrefslogtreecommitdiff
path: root/crates/parser/src/conversion.rs
diff options
context:
space:
mode:
authorHenry Gressmann <mail@henrygressmann.de>2023-12-19 14:37:51 +0100
committerHenry Gressmann <mail@henrygressmann.de>2023-12-19 14:37:51 +0100
commit375f9aa32b117a42c7a9c7f29d3c592b81d966be (patch)
tree4673abc196c01435578d6594c30e5987298dd325 /crates/parser/src/conversion.rs
parent820ecb429f57053098684e209b8a3a00cb0ac973 (diff)
feat: parse memory and table sections
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
Diffstat (limited to 'crates/parser/src/conversion.rs')
-rw-r--r--crates/parser/src/conversion.rs43
1 files changed, 42 insertions, 1 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>> {