@@ -49,12 +49,12 @@ pub use tinywasm_types::Module;
4949#[ non_exhaustive]
5050#[ derive( Debug , Clone ) ]
5151pub struct ParserOptions {
52+ /// Whether to validate modules while parsing.
53+ pub validation : bool ,
5254 /// Whether to optimize local memory allocation by skipping allocation of unused local memories.
5355 pub optimize_local_memory_allocation : bool ,
5456 /// Whether to run the peephole rewrite optimizer.
5557 pub optimize_rewrite : bool ,
56- /// Whether to remove `Nop` and `MergeBarrier` instructions after rewriting.
57- pub optimize_remove_nop : bool ,
5858
5959 #[ cfg( parallel_parser) ]
6060 /// Number of threads to use for parallel parsing.
@@ -70,16 +70,27 @@ pub struct ParserOptions {
7070impl Default for ParserOptions {
7171 fn default ( ) -> Self {
7272 Self {
73+ validation : true ,
7374 optimize_local_memory_allocation : true ,
7475 optimize_rewrite : true ,
75- optimize_remove_nop : true ,
7676 #[ cfg( parallel_parser) ]
7777 parser_threads : None ,
7878 }
7979 }
8080}
8181
8282impl ParserOptions {
83+ /// Enable or disable WebAssembly validation.
84+ pub const fn with_validation ( mut self , enabled : bool ) -> Self {
85+ self . validation = enabled;
86+ self
87+ }
88+
89+ /// Returns whether WebAssembly validation is enabled.
90+ pub const fn validation ( & self ) -> bool {
91+ self . validation
92+ }
93+
8394 /// Enable or disable the optimization that skips allocating unused local memories.
8495 pub const fn with_local_memory_allocation_optimization ( mut self , enabled : bool ) -> Self {
8596 self . optimize_local_memory_allocation = enabled;
@@ -102,17 +113,6 @@ impl ParserOptions {
102113 self . optimize_rewrite
103114 }
104115
105- /// Enable or disable `Nop`/`MergeBarrier` removal after rewriting.
106- pub const fn with_nop_removal_optimization ( mut self , enabled : bool ) -> Self {
107- self . optimize_remove_nop = enabled;
108- self
109- }
110-
111- /// Returns whether `Nop`/`MergeBarrier` removal is enabled.
112- pub const fn optimize_remove_nop ( & self ) -> bool {
113- self . optimize_remove_nop
114- }
115-
116116 #[ cfg( parallel_parser) ]
117117 /// Set the number of threads for parallel parsing.
118118 ///
@@ -188,18 +188,18 @@ impl Parser {
188188 /// Parse a [`Module`] from bytes
189189 pub fn parse_module_bytes ( & self , wasm : impl AsRef < [ u8 ] > ) -> Result < Module > {
190190 let wasm = wasm. as_ref ( ) ;
191- let mut validator = Self :: create_validator ( self . options . clone ( ) ) ;
191+ let mut validator = self . options . validation ( ) . then ( || Self :: create_validator ( self . options . clone ( ) ) ) ;
192192 let mut reader = ModuleReader :: default ( ) ;
193193
194194 for payload in wasmparser:: Parser :: new ( 0 ) . parse_all ( wasm) {
195195 match payload? {
196196 wasmparser:: Payload :: CodeSectionStart { count, range, size } => {
197- reader. begin_code_section ( count, range, size, & mut validator, & self . options ) ?;
197+ reader. begin_code_section ( count, range, size, validator. as_mut ( ) , & self . options ) ?;
198198 }
199199 wasmparser:: Payload :: CodeSectionEntry ( function) => {
200- reader. process_borrowed_code_section_entry ( function, & mut validator, & self . options ) ?;
200+ reader. process_borrowed_code_section_entry ( function, validator. as_mut ( ) , & self . options ) ?;
201201 }
202- payload => reader. process_payload ( payload, & mut validator) ?,
202+ payload => reader. process_payload ( payload, validator. as_mut ( ) ) ?,
203203 }
204204 }
205205
@@ -222,7 +222,7 @@ impl Parser {
222222 #[ cfg( feature = "std" ) ]
223223 /// Parse a [`Module`] from a stream. Requires `std` feature.
224224 pub fn parse_module_stream ( & self , mut stream : impl std:: io:: Read ) -> Result < Module > {
225- let mut validator = Self :: create_validator ( self . options . clone ( ) ) ;
225+ let mut validator = self . options . validation ( ) . then ( || Self :: create_validator ( self . options . clone ( ) ) ) ;
226226 let mut reader = ModuleReader :: default ( ) ;
227227 let mut buffer = alloc:: vec:: Vec :: new ( ) ;
228228 let mut parser = wasmparser:: Parser :: new ( 0 ) ;
@@ -240,8 +240,13 @@ impl Parser {
240240
241241 match payload {
242242 wasmparser:: Payload :: CodeSectionStart { count, range, size } => {
243- let defer =
244- reader. begin_code_section ( count, range. clone ( ) , size, & mut validator, & self . options ) ?;
243+ let defer = reader. begin_code_section (
244+ count,
245+ range. clone ( ) ,
246+ size,
247+ validator. as_mut ( ) ,
248+ & self . options ,
249+ ) ?;
245250
246251 #[ cfg( parallel_parser) ]
247252 if defer {
@@ -252,10 +257,10 @@ impl Parser {
252257 let _ = defer;
253258 }
254259 wasmparser:: Payload :: CodeSectionEntry ( function) => {
255- reader. process_inline_code_section_entry ( function, & mut validator, & self . options ) ?;
260+ reader. process_inline_code_section_entry ( function, validator. as_mut ( ) , & self . options ) ?;
256261 }
257262 payload => {
258- reader. process_payload ( payload, & mut validator) ?;
263+ reader. process_payload ( payload, validator. as_mut ( ) ) ?;
259264 }
260265 }
261266 buffer. drain ( ..consumed) ;
@@ -274,7 +279,7 @@ impl Parser {
274279 }
275280
276281 let section_bytes = alloc:: sync:: Arc :: < [ u8 ] > :: from ( buffer[ ..section_size] . to_vec ( ) ) ;
277- reader. queue_owned_code_section ( count, body_offset, section_bytes, & mut validator) ?;
282+ reader. queue_owned_code_section ( count, body_offset, section_bytes, validator. as_mut ( ) ) ?;
278283 parser. skip_section ( ) ;
279284 buffer. drain ( ..section_size) ;
280285 continue ;
0 commit comments