@@ -39,17 +39,47 @@ use wasmparser::{Validator, WasmFeaturesInflated};
3939
4040pub 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
4666impl 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 ) ;
0 commit comments