Skip to content
This repository was archived by the owner on Oct 3, 2025. It is now read-only.

Commit a8269d7

Browse files
feat: import section
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
1 parent 45b11e3 commit a8269d7

File tree

4 files changed

+94
-37
lines changed

4 files changed

+94
-37
lines changed

crates/parser/src/conversion.rs

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,87 @@
11
use alloc::{boxed::Box, format, string::ToString, vec::Vec};
22
use log::info;
33
use tinywasm_types::{
4-
BlockArgs, ConstInstruction, Export, ExternalKind, FuncType, Global, Instruction, MemArg, MemoryArch, MemoryType,
5-
TableType, ValType,
4+
BlockArgs, ConstInstruction, Export, ExternalKind, FuncType, Global, GlobalType, Import, ImportKind, Instruction,
5+
MemArg, MemoryArch, MemoryType, TableType, ValType,
66
};
77
use wasmparser::{FuncValidator, ValidatorResources};
88

99
use crate::{module::CodeSection, Result};
1010

11+
pub(crate) fn convert_module_imports<'a, T: IntoIterator<Item = wasmparser::Result<wasmparser::Import<'a>>>>(
12+
imports: T,
13+
) -> Result<Vec<Import>> {
14+
let imports = imports
15+
.into_iter()
16+
.map(|import| convert_module_import(import?))
17+
.collect::<Result<Vec<_>>>()?;
18+
Ok(imports)
19+
}
20+
21+
pub(crate) fn convert_module_import<'a>(import: wasmparser::Import<'a>) -> Result<Import> {
22+
Ok(Import {
23+
module: import.module.to_string(),
24+
name: import.name.to_string(),
25+
kind: match import.ty {
26+
wasmparser::TypeRef::Func(ty) => ImportKind::Func(ty),
27+
wasmparser::TypeRef::Table(ty) => ImportKind::Table(convert_module_table(ty)?),
28+
wasmparser::TypeRef::Memory(ty) => ImportKind::Mem(convert_module_memory(ty)?),
29+
wasmparser::TypeRef::Global(ty) => ImportKind::Global(GlobalType {
30+
mutable: ty.mutable,
31+
ty: convert_valtype(&ty.content_type),
32+
}),
33+
wasmparser::TypeRef::Tag(ty) => {
34+
return Err(crate::ParseError::UnsupportedOperator(format!(
35+
"Unsupported import kind: {:?}",
36+
ty
37+
)))
38+
}
39+
},
40+
})
41+
}
42+
1143
pub(crate) fn convert_module_memories<T: IntoIterator<Item = wasmparser::Result<wasmparser::MemoryType>>>(
1244
memory_types: T,
1345
) -> Result<Vec<MemoryType>> {
1446
let memory_type = memory_types
1547
.into_iter()
16-
.map(|memory| {
17-
let memory = memory?;
18-
Ok(MemoryType {
19-
arch: match memory.memory64 {
20-
true => MemoryArch::I64,
21-
false => MemoryArch::I32,
22-
},
23-
page_count_initial: memory.initial,
24-
page_count_max: memory.maximum,
25-
})
26-
})
48+
.map(|memory| convert_module_memory(memory?))
2749
.collect::<Result<Vec<_>>>()?;
2850

2951
Ok(memory_type)
3052
}
3153

54+
pub(crate) fn convert_module_memory(memory: wasmparser::MemoryType) -> Result<MemoryType> {
55+
Ok(MemoryType {
56+
arch: match memory.memory64 {
57+
true => MemoryArch::I64,
58+
false => MemoryArch::I32,
59+
},
60+
page_count_initial: memory.initial,
61+
page_count_max: memory.maximum,
62+
})
63+
}
64+
3265
pub(crate) fn convert_module_tables<T: IntoIterator<Item = wasmparser::Result<wasmparser::TableType>>>(
3366
table_types: T,
3467
) -> Result<Vec<TableType>> {
3568
let table_type = table_types
3669
.into_iter()
37-
.map(|table| {
38-
let table = table?;
39-
let ty = convert_valtype(&table.element_type);
40-
Ok(TableType {
41-
element_type: ty,
42-
size_initial: table.initial,
43-
size_max: table.maximum,
44-
})
45-
})
70+
.map(|table| convert_module_table(table?))
4671
.collect::<Result<Vec<_>>>()?;
4772

4873
Ok(table_type)
4974
}
5075

76+
pub(crate) fn convert_module_table(table: wasmparser::TableType) -> Result<TableType> {
77+
let ty = convert_valtype(&table.element_type);
78+
Ok(TableType {
79+
element_type: ty,
80+
size_initial: table.initial,
81+
size_max: table.maximum,
82+
})
83+
}
84+
5185
pub(crate) fn convert_module_globals<'a, T: IntoIterator<Item = wasmparser::Result<wasmparser::Global<'a>>>>(
5286
globals: T,
5387
) -> Result<Vec<Global>> {
@@ -70,9 +104,11 @@ pub(crate) fn convert_module_globals<'a, T: IntoIterator<Item = wasmparser::Resu
70104
assert!(matches!(ops[ops.len() - 1], wasmparser::Operator::End));
71105

72106
Ok(Global {
73-
ty,
74107
init: process_const_operator(ops[ops.len() - 2].clone())?,
75-
mutable: global.ty.mutable,
108+
ty: GlobalType {
109+
mutable: global.ty.mutable,
110+
ty,
111+
},
76112
})
77113
})
78114
.collect::<Result<Vec<_>>>()?;

crates/parser/src/module.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::log::debug;
22
use alloc::{boxed::Box, format, vec::Vec};
33
use core::fmt::Debug;
4-
use tinywasm_types::{Export, FuncType, Global, Instruction, MemoryType, TableType, ValType};
4+
use tinywasm_types::{Export, FuncType, Global, Import, Instruction, MemoryType, TableType, ValType};
55
use wasmparser::{Payload, Validator};
66

77
use crate::{conversion, ParseError, Result};
@@ -24,10 +24,10 @@ pub struct ModuleReader {
2424
pub globals: Vec<Global>,
2525
pub table_types: Vec<TableType>,
2626
pub memory_types: Vec<MemoryType>,
27+
pub imports: Vec<Import>,
2728

2829
// pub element_section: Option<ElementSectionReader<'a>>,
2930
// pub data_section: Option<DataSectionReader<'a>>,
30-
// pub import_section: Option<ImportSectionReader<'a>>,
3131
pub end_reached: bool,
3232
}
3333

@@ -42,9 +42,9 @@ impl Debug for ModuleReader {
4242
.field("globals", &self.globals)
4343
.field("table_types", &self.table_types)
4444
.field("memory_types", &self.memory_types)
45+
.field("import_section", &self.imports)
4546
// .field("element_section", &self.element_section)
4647
// .field("data_section", &self.data_section)
47-
// .field("import_section", &self.import_section)
4848
.finish()
4949
}
5050
}
@@ -116,7 +116,6 @@ impl ModuleReader {
116116
if !self.code.is_empty() {
117117
return Err(ParseError::DuplicateSection("Code section".into()));
118118
}
119-
120119
validator.code_section_start(count, &range)?;
121120
}
122121
CodeSectionEntry(function) => {
@@ -127,12 +126,13 @@ impl ModuleReader {
127126
self.code
128127
.push(conversion::convert_module_code(function, func_validator)?);
129128
}
130-
ImportSection(_reader) => {
131-
return Err(ParseError::UnsupportedSection("Import section".into()));
132-
133-
// debug!("Found import section");
134-
// validator.import_section(&reader)?;
135-
// self.import_section = Some(reader);
129+
ImportSection(reader) => {
130+
debug!("Found import section");
131+
validator.import_section(&reader)?;
132+
self.imports = reader
133+
.into_iter()
134+
.map(|i| conversion::convert_module_import(i?))
135+
.collect::<Result<Vec<_>>>()?;
136136
}
137137
ExportSection(reader) => {
138138
debug!("Found export section");

0 commit comments

Comments
 (0)