From a8269d7c0bf5e4c21d5d5dbb728e0ba4c63fa50e Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Tue, 19 Dec 2023 17:42:00 +0100 Subject: feat: import section Signed-off-by: Henry Gressmann --- crates/parser/src/conversion.rs | 84 +++++++++++++++++++++++++++++------------ 1 file changed, 60 insertions(+), 24 deletions(-) (limited to 'crates/parser/src/conversion.rs') diff --git a/crates/parser/src/conversion.rs b/crates/parser/src/conversion.rs index fb48321..1594bf7 100644 --- a/crates/parser/src/conversion.rs +++ b/crates/parser/src/conversion.rs @@ -1,53 +1,87 @@ use alloc::{boxed::Box, format, string::ToString, vec::Vec}; use log::info; use tinywasm_types::{ - BlockArgs, ConstInstruction, Export, ExternalKind, FuncType, Global, Instruction, MemArg, MemoryArch, MemoryType, - TableType, ValType, + BlockArgs, ConstInstruction, Export, ExternalKind, FuncType, Global, GlobalType, Import, ImportKind, Instruction, + MemArg, MemoryArch, MemoryType, TableType, ValType, }; use wasmparser::{FuncValidator, ValidatorResources}; use crate::{module::CodeSection, Result}; +pub(crate) fn convert_module_imports<'a, T: IntoIterator>>>( + imports: T, +) -> Result> { + let imports = imports + .into_iter() + .map(|import| convert_module_import(import?)) + .collect::>>()?; + Ok(imports) +} + +pub(crate) fn convert_module_import<'a>(import: wasmparser::Import<'a>) -> Result { + Ok(Import { + module: import.module.to_string(), + name: import.name.to_string(), + kind: match import.ty { + wasmparser::TypeRef::Func(ty) => ImportKind::Func(ty), + wasmparser::TypeRef::Table(ty) => ImportKind::Table(convert_module_table(ty)?), + wasmparser::TypeRef::Memory(ty) => ImportKind::Mem(convert_module_memory(ty)?), + wasmparser::TypeRef::Global(ty) => ImportKind::Global(GlobalType { + mutable: ty.mutable, + ty: convert_valtype(&ty.content_type), + }), + wasmparser::TypeRef::Tag(ty) => { + return Err(crate::ParseError::UnsupportedOperator(format!( + "Unsupported import kind: {:?}", + ty + ))) + } + }, + }) +} + 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, - }) - }) + .map(|memory| convert_module_memory(memory?)) .collect::>>()?; Ok(memory_type) } +pub(crate) fn convert_module_memory(memory: wasmparser::MemoryType) -> Result { + Ok(MemoryType { + arch: match memory.memory64 { + true => MemoryArch::I64, + false => MemoryArch::I32, + }, + page_count_initial: memory.initial, + page_count_max: memory.maximum, + }) +} + 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, - }) - }) + .map(|table| convert_module_table(table?)) .collect::>>()?; Ok(table_type) } +pub(crate) fn convert_module_table(table: wasmparser::TableType) -> Result { + let ty = convert_valtype(&table.element_type); + Ok(TableType { + element_type: ty, + size_initial: table.initial, + size_max: table.maximum, + }) +} + pub(crate) fn convert_module_globals<'a, T: IntoIterator>>>( globals: T, ) -> Result> { @@ -70,9 +104,11 @@ pub(crate) fn convert_module_globals<'a, T: IntoIterator>>()?; -- cgit v1.3.1