Skip to content

Commit e2001eb

Browse files
chore: add ParserOptions
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent 9dcfeed commit e2001eb

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

.gitmodules

Lines changed: 0 additions & 3 deletions
This file was deleted.

crates/parser/src/lib.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,47 @@ use wasmparser::{Validator, WasmFeaturesInflated};
3939

4040
pub use tinywasm_types::TinyWasmModule;
4141

42+
/// Parser optimization and lowering options.
43+
#[non_exhaustive]
44+
#[derive(Debug, Clone)]
45+
pub struct ParserOptions {
46+
// /// Enable control-flow graph cleanup rewrites.
47+
// pub cfg_cleanup: bool,
48+
// /// Enable dead-code pruning.
49+
// pub dce: bool,
50+
// /// Enable return-call rewrites when safe.
51+
// pub tailcall_rewrite: bool,
52+
}
53+
54+
impl Default for ParserOptions {
55+
fn default() -> Self {
56+
Self {}
57+
}
58+
}
59+
4260
/// A WebAssembly parser
43-
#[derive(Default, Debug)]
44-
pub struct Parser {}
61+
#[derive(Debug, Default)]
62+
pub struct Parser {
63+
options: ParserOptions,
64+
}
4565

4666
impl Parser {
4767
/// Create a new parser instance
4868
pub fn new() -> Self {
49-
Self {}
69+
Self::default()
70+
}
71+
72+
/// Create a new parser with explicit options.
73+
pub fn with_options(options: ParserOptions) -> Self {
74+
Self { options }
75+
}
76+
77+
/// Read back parser options.
78+
pub const fn options(&self) -> &ParserOptions {
79+
&self.options
5080
}
5181

52-
fn create_validator() -> Validator {
82+
fn create_validator(_options: ParserOptions) -> Validator {
5383
let features = WasmFeaturesInflated {
5484
bulk_memory: true,
5585
floats: true,
@@ -98,7 +128,7 @@ impl Parser {
98128
/// Parse a [`TinyWasmModule`] from bytes
99129
pub fn parse_module_bytes(&self, wasm: impl AsRef<[u8]>) -> Result<TinyWasmModule> {
100130
let wasm = wasm.as_ref();
101-
let mut validator = Self::create_validator();
131+
let mut validator = Self::create_validator(self.options.clone());
102132
let mut reader = ModuleReader::new();
103133

104134
for payload in wasmparser::Parser::new(0).parse_all(wasm) {
@@ -128,7 +158,7 @@ impl Parser {
128158
pub fn parse_module_stream(&self, mut stream: impl std::io::Read) -> Result<TinyWasmModule> {
129159
use alloc::format;
130160

131-
let mut validator = Self::create_validator();
161+
let mut validator = Self::create_validator(self.options.clone());
132162
let mut reader = ModuleReader::new();
133163
let mut buffer = alloc::vec::Vec::new();
134164
let mut parser = wasmparser::Parser::new(0);

crates/types/src/instructions.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,13 @@ pub enum Instruction {
255255
I16x8RelaxedDotI8x16I7x16S,
256256
I32x4RelaxedDotI8x16I7x16AddS
257257
}
258+
259+
#[cfg(test)]
260+
mod tests {
261+
use super::Instruction;
262+
263+
#[test]
264+
fn instruction_layout_size_is_stable() {
265+
assert_eq!(core::mem::size_of::<Instruction>(), 16);
266+
}
267+
}

0 commit comments

Comments
 (0)