diff options
| author | Henry Gressmann <mail@henrygressmann.de> | 2023-12-19 17:42:00 +0100 |
|---|---|---|
| committer | Henry Gressmann <mail@henrygressmann.de> | 2023-12-19 17:42:00 +0100 |
| commit | a8269d7c0bf5e4c21d5d5dbb728e0ba4c63fa50e (patch) | |
| tree | c4d176e65d756ab2e0ec527a2ed82308133e69fd /crates/parser/src | |
| parent | 45b11e3260c10afa45338525413f609100b76940 (diff) | |
feat: import section
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
Diffstat (limited to 'crates/parser/src')
| -rw-r--r-- | crates/parser/src/conversion.rs | 84 | ||||
| -rw-r--r-- | crates/parser/src/module.rs | 20 |
2 files changed, 70 insertions, 34 deletions
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<Item = wasmparser::Result<wasmparser::Import<'a>>>>( + imports: T, +) -> Result<Vec<Import>> { + let imports = imports + .into_iter() + .map(|import| convert_module_import(import?)) + .collect::<Result<Vec<_>>>()?; + Ok(imports) +} + +pub(crate) fn convert_module_import<'a>(import: wasmparser::Import<'a>) -> Result<Import> { + 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<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, - }) - }) + .map(|memory| convert_module_memory(memory?)) .collect::<Result<Vec<_>>>()?; Ok(memory_type) } +pub(crate) fn convert_module_memory(memory: wasmparser::MemoryType) -> Result<MemoryType> { + 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<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, - }) - }) + .map(|table| convert_module_table(table?)) .collect::<Result<Vec<_>>>()?; Ok(table_type) } +pub(crate) fn convert_module_table(table: wasmparser::TableType) -> Result<TableType> { + 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<Item = wasmparser::Result<wasmparser::Global<'a>>>>( globals: T, ) -> Result<Vec<Global>> { @@ -70,9 +104,11 @@ pub(crate) fn convert_module_globals<'a, T: IntoIterator<Item = wasmparser::Resu assert!(matches!(ops[ops.len() - 1], wasmparser::Operator::End)); Ok(Global { - ty, init: process_const_operator(ops[ops.len() - 2].clone())?, - mutable: global.ty.mutable, + ty: GlobalType { + mutable: global.ty.mutable, + ty, + }, }) }) .collect::<Result<Vec<_>>>()?; diff --git a/crates/parser/src/module.rs b/crates/parser/src/module.rs index ea6aaad..790b9a2 100644 --- a/crates/parser/src/module.rs +++ b/crates/parser/src/module.rs @@ -1,7 +1,7 @@ use crate::log::debug; use alloc::{boxed::Box, format, vec::Vec}; use core::fmt::Debug; -use tinywasm_types::{Export, FuncType, Global, Instruction, MemoryType, TableType, ValType}; +use tinywasm_types::{Export, FuncType, Global, Import, Instruction, MemoryType, TableType, ValType}; use wasmparser::{Payload, Validator}; use crate::{conversion, ParseError, Result}; @@ -24,10 +24,10 @@ pub struct ModuleReader { pub globals: Vec<Global>, pub table_types: Vec<TableType>, pub memory_types: Vec<MemoryType>, + pub imports: Vec<Import>, // pub element_section: Option<ElementSectionReader<'a>>, // pub data_section: Option<DataSectionReader<'a>>, - // pub import_section: Option<ImportSectionReader<'a>>, pub end_reached: bool, } @@ -42,9 +42,9 @@ impl Debug for ModuleReader { .field("globals", &self.globals) .field("table_types", &self.table_types) .field("memory_types", &self.memory_types) + .field("import_section", &self.imports) // .field("element_section", &self.element_section) // .field("data_section", &self.data_section) - // .field("import_section", &self.import_section) .finish() } } @@ -116,7 +116,6 @@ impl ModuleReader { if !self.code.is_empty() { return Err(ParseError::DuplicateSection("Code section".into())); } - validator.code_section_start(count, &range)?; } CodeSectionEntry(function) => { @@ -127,12 +126,13 @@ impl ModuleReader { self.code .push(conversion::convert_module_code(function, func_validator)?); } - ImportSection(_reader) => { - return Err(ParseError::UnsupportedSection("Import section".into())); - - // debug!("Found import section"); - // validator.import_section(&reader)?; - // self.import_section = Some(reader); + ImportSection(reader) => { + debug!("Found import section"); + validator.import_section(&reader)?; + self.imports = reader + .into_iter() + .map(|i| conversion::convert_module_import(i?)) + .collect::<Result<Vec<_>>>()?; } ExportSection(reader) => { debug!("Found export section"); |
