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

Commit 4ee0bac

Browse files
chore: parse element sections, improve contributing.md
Signed-off-by: Henry <[email protected]>
1 parent 154d5fe commit 4ee0bac

File tree

7 files changed

+95
-18
lines changed

7 files changed

+95
-18
lines changed

CONTRIBUTING.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
# Common Commands
1+
# Scripts and Commands
22

3-
> To improve the development experience, a number of aliases have been added to the `.cargo/config.toml` file. These can be run using `cargo <command>`.
3+
> To improve the development experience, a number of custom commands and aliases have been added to the `.cargo/config.toml` file. These can be run using `cargo <command>`.
44
5-
- **`cargo dev`**\
5+
- **`cargo dev [args]`**\
66
e.g. `cargo dev -f check ./examples/wasm/call.wat -a i32:0`\
7-
Run the development version of the tinywasm-cli. This is the main command used for developing new features.
7+
Run the development version of the tinywasm-cli. This is the main command used for developing new features.\
8+
See [tinywasm-cli](./crates/cli) for more information.
89

910
- **`cargo generate-charts`**\
10-
Generate test result charts
11+
Generate test result charts from the previous test runs. This is used to generate the charts in the [README](./README.md).
1112

1213
- **`cargo test-mvp`**\
1314
Run the WebAssembly MVP (1.0) test suite. Be sure to cloned this repo with `--recursive` or initialize the submodules with `git submodule update --init --recursive`
1415

16+
- **`cargo test-wast <path>`**\
17+
Run a single WAST test file. e.g. `cargo test-wast ./examples/wast/i32.wast`
18+
1519
- **`cargo version-dev`**\
1620
Bump the version to the next dev version. This should be used after a release so test results are not overwritten. Does not create a new github release.
1721

crates/parser/src/conversion.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,62 @@
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, GlobalType, Import, ImportKind, Instruction,
5-
MemArg, MemoryArch, MemoryType, TableType, ValType,
4+
BlockArgs, ConstInstruction, ElementItem, Export, ExternalKind, FuncType, Global, GlobalType, Import, ImportKind,
5+
Instruction, MemArg, MemoryArch, MemoryType, TableType, ValType,
66
};
77
use wasmparser::{FuncValidator, OperatorsReader, ValidatorResources};
88

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

11+
pub(crate) fn convert_module_elements<'a, T: IntoIterator<Item = wasmparser::Result<wasmparser::Element<'a>>>>(
12+
elements: T,
13+
) -> Result<Vec<tinywasm_types::Element>> {
14+
let elements = elements
15+
.into_iter()
16+
.map(|element| convert_module_element(element?))
17+
.collect::<Result<Vec<_>>>()?;
18+
Ok(elements)
19+
}
20+
21+
pub(crate) fn convert_module_element(element: wasmparser::Element<'_>) -> Result<tinywasm_types::Element> {
22+
let kind = match element.kind {
23+
wasmparser::ElementKind::Active {
24+
table_index,
25+
offset_expr,
26+
} => tinywasm_types::ElementKind::Active {
27+
table: table_index,
28+
offset: process_const_operators(offset_expr.get_operators_reader())?,
29+
},
30+
wasmparser::ElementKind::Passive => tinywasm_types::ElementKind::Passive,
31+
wasmparser::ElementKind::Declared => tinywasm_types::ElementKind::Declared,
32+
};
33+
34+
let items = match element.items {
35+
wasmparser::ElementItems::Functions(funcs) => funcs
36+
.into_iter()
37+
.map(|func| Ok(ElementItem::Func(func?)))
38+
.collect::<Result<Vec<_>>>()?
39+
.into_boxed_slice(),
40+
41+
wasmparser::ElementItems::Expressions(exprs) => exprs
42+
.into_iter()
43+
.map(|expr| {
44+
Ok(ElementItem::Expr(process_const_operators(
45+
expr?.get_operators_reader(),
46+
)?))
47+
})
48+
.collect::<Result<Vec<_>>>()?
49+
.into_boxed_slice(),
50+
};
51+
52+
Ok(tinywasm_types::Element {
53+
kind,
54+
items,
55+
ty: convert_valtype(&element.ty),
56+
range: element.range,
57+
})
58+
}
59+
1160
pub(crate) fn convert_module_data_sections<'a, T: IntoIterator<Item = wasmparser::Result<wasmparser::Data<'a>>>>(
1261
data_sections: T,
1362
) -> Result<Vec<tinywasm_types::Data>> {

crates/parser/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ impl TryFrom<ModuleReader> for TinyWasmModule {
128128
memory_types: reader.memory_types.into_boxed_slice(),
129129
imports: reader.imports.into_boxed_slice(),
130130
data: reader.data.into_boxed_slice(),
131+
elements: reader.elements.into_boxed_slice(),
131132
})
132133
}
133134
}

crates/parser/src/module.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::log::debug;
22
use crate::{conversion, ParseError, Result};
33
use alloc::{boxed::Box, format, vec::Vec};
44
use core::fmt::Debug;
5-
use tinywasm_types::{Data, Export, FuncType, Global, Import, Instruction, MemoryType, TableType, ValType};
5+
use tinywasm_types::{Data, Element, Export, FuncType, Global, Import, Instruction, MemoryType, TableType, ValType};
66
use wasmparser::{Payload, Validator};
77

88
#[derive(Debug, Clone, PartialEq)]
@@ -25,6 +25,7 @@ pub struct ModuleReader {
2525
pub memory_types: Vec<MemoryType>,
2626
pub imports: Vec<Import>,
2727
pub data: Vec<Data>,
28+
pub elements: Vec<Element>,
2829

2930
// pub element_section: Option<ElementSectionReader<'a>>,
3031
pub end_reached: bool,
@@ -122,11 +123,10 @@ impl ModuleReader {
122123
validator.memory_section(&reader)?;
123124
self.memory_types = conversion::convert_module_memories(reader)?;
124125
}
125-
ElementSection(_reader) => {
126-
return Err(ParseError::UnsupportedSection("Element section".into()));
127-
// debug!("Found element section");
128-
// validator.element_section(&reader)?;
129-
// self.element_section = Some(reader);
126+
ElementSection(reader) => {
127+
debug!("Found element section");
128+
validator.element_section(&reader)?;
129+
self.elements = conversion::convert_module_elements(reader)?;
130130
}
131131
DataSection(reader) => {
132132
if !self.data.is_empty() {

crates/tinywasm/tests/generated/mvp.csv

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

crates/tinywasm/tests/generated/progress-mvp.svg

Lines changed: 3 additions & 3 deletions
Loading

crates/types/src/lib.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ pub struct TinyWasmModule {
6767

6868
/// Data segments of the WebAssembly module.
6969
pub data: Box<[Data]>,
70-
// pub elements: Option<ElementSectionReader<'a>>,
70+
71+
/// Element segments of the WebAssembly module.
72+
pub elements: Box<[Element]>,
7173
}
7274

7375
/// A WebAssembly value.
@@ -366,3 +368,24 @@ pub enum DataKind {
366368
Active { mem: MemAddr, offset: ConstInstruction },
367369
Passive,
368370
}
371+
372+
#[derive(Debug, Clone)]
373+
pub struct Element {
374+
pub kind: ElementKind,
375+
pub items: Box<[ElementItem]>,
376+
pub range: Range<usize>,
377+
pub ty: ValType,
378+
}
379+
380+
#[derive(Debug, Clone)]
381+
pub enum ElementKind {
382+
Passive,
383+
Active { table: TableAddr, offset: ConstInstruction },
384+
Declared,
385+
}
386+
387+
#[derive(Debug, Clone)]
388+
pub enum ElementItem {
389+
Func(FuncAddr),
390+
Expr(ConstInstruction),
391+
}

0 commit comments

Comments
 (0)