From 34eb2d426c784d2a02ebf61cb24a7dd0df60d92d Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 2 Dec 2024 08:45:37 -0800 Subject: [PATCH] Add `simd_visitor` helper methods to `FuncValidator` The previous `visitor` return method only return `impl VisitOperator`, so this adds a second method for when the `simd` feature is enabled to return a similar structure which also is guaranteed to implement `VisitSimdOperator`. --- crates/wasmparser/src/validator/func.rs | 11 +++++++++++ crates/wasmparser/src/validator/operators.rs | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/crates/wasmparser/src/validator/func.rs b/crates/wasmparser/src/validator/func.rs index 7a230ef27d..51907e33b4 100644 --- a/crates/wasmparser/src/validator/func.rs +++ b/crates/wasmparser/src/validator/func.rs @@ -177,6 +177,17 @@ impl FuncValidator { self.validator.with_resources(&self.resources, offset) } + /// Same as [`FuncValidator::visit`] except that the returned type + /// implements the [`VisitSimdOperator`](crate::VisitSimdOperator) trait as + /// well. + #[cfg(feature = "simd")] + pub fn simd_visitor<'this, 'a: 'this>( + &'this mut self, + offset: usize, + ) -> impl crate::VisitSimdOperator<'a, Output = Result<()>> + ModuleArity + 'this { + self.validator.with_resources_simd(&self.resources, offset) + } + /// Function that must be called after the last opcode has been processed. /// /// This will validate that the function was properly terminated with the diff --git a/crates/wasmparser/src/validator/operators.rs b/crates/wasmparser/src/validator/operators.rs index 9a3a8c391f..249bc070d9 100644 --- a/crates/wasmparser/src/validator/operators.rs +++ b/crates/wasmparser/src/validator/operators.rs @@ -493,6 +493,25 @@ impl OperatorValidator { }) } + /// Same as `with_resources` above but guarantees it's able to visit simd + /// operators as well. + #[cfg(feature = "simd")] + pub fn with_resources_simd<'a, 'validator, 'resources, T>( + &'validator mut self, + resources: &'resources T, + offset: usize, + ) -> impl VisitSimdOperator<'a, Output = Result<()>> + ModuleArity + 'validator + where + T: WasmModuleResources, + 'resources: 'validator, + { + WasmProposalValidator(OperatorValidatorTemp { + offset, + inner: self, + resources, + }) + } + pub fn finish(&mut self, offset: usize) -> Result<()> { if self.control.last().is_some() { bail!(