From e2001ebe6ce6f1c5f3843feac772adf999dae873 Mon Sep 17 00:00:00 2001 From: Henry Date: Sat, 4 Apr 2026 20:42:15 +0200 Subject: chore: add ParserOptions Signed-off-by: Henry --- crates/parser/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++------ crates/types/src/instructions.rs | 10 ++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) (limited to 'crates') diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index f7e713b..eabeeab 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -39,17 +39,47 @@ use wasmparser::{Validator, WasmFeaturesInflated}; pub use tinywasm_types::TinyWasmModule; +/// Parser optimization and lowering options. +#[non_exhaustive] +#[derive(Debug, Clone)] +pub struct ParserOptions { + // /// Enable control-flow graph cleanup rewrites. + // pub cfg_cleanup: bool, + // /// Enable dead-code pruning. + // pub dce: bool, + // /// Enable return-call rewrites when safe. + // pub tailcall_rewrite: bool, +} + +impl Default for ParserOptions { + fn default() -> Self { + Self {} + } +} + /// A WebAssembly parser -#[derive(Default, Debug)] -pub struct Parser {} +#[derive(Debug, Default)] +pub struct Parser { + options: ParserOptions, +} impl Parser { /// Create a new parser instance pub fn new() -> Self { - Self {} + Self::default() + } + + /// Create a new parser with explicit options. + pub fn with_options(options: ParserOptions) -> Self { + Self { options } + } + + /// Read back parser options. + pub const fn options(&self) -> &ParserOptions { + &self.options } - fn create_validator() -> Validator { + fn create_validator(_options: ParserOptions) -> Validator { let features = WasmFeaturesInflated { bulk_memory: true, floats: true, @@ -98,7 +128,7 @@ impl Parser { /// Parse a [`TinyWasmModule`] from bytes pub fn parse_module_bytes(&self, wasm: impl AsRef<[u8]>) -> Result { let wasm = wasm.as_ref(); - let mut validator = Self::create_validator(); + let mut validator = Self::create_validator(self.options.clone()); let mut reader = ModuleReader::new(); for payload in wasmparser::Parser::new(0).parse_all(wasm) { @@ -128,7 +158,7 @@ impl Parser { pub fn parse_module_stream(&self, mut stream: impl std::io::Read) -> Result { use alloc::format; - let mut validator = Self::create_validator(); + let mut validator = Self::create_validator(self.options.clone()); let mut reader = ModuleReader::new(); let mut buffer = alloc::vec::Vec::new(); let mut parser = wasmparser::Parser::new(0); diff --git a/crates/types/src/instructions.rs b/crates/types/src/instructions.rs index efb7172..d013734 100644 --- a/crates/types/src/instructions.rs +++ b/crates/types/src/instructions.rs @@ -255,3 +255,13 @@ pub enum Instruction { I16x8RelaxedDotI8x16I7x16S, I32x4RelaxedDotI8x16I7x16AddS } + +#[cfg(test)] +mod tests { + use super::Instruction; + + #[test] + fn instruction_layout_size_is_stable() { + assert_eq!(core::mem::size_of::(), 16); + } +} -- cgit v1.3.1