Skip to content

Commit

Permalink
Use the new wasmparser::VisitOperator API (#420)
Browse files Browse the repository at this point in the history
* move FuncValidator into FunctionBuilder type

* add initial skeleton for VisitOperator impl of FunctionBuilder

* make use of TranslationError instead of ModuleError in FunctionBuilder

* move TranslationError into its own submodule

* silence warning

* silence fewer warnings in visit.rs

* add TOTO annotation

* implement VisitOperator for FunctionBuilder

* use new VisitOperator impl

* remove no longer used code

The removed code has been superseeded by the new VisitOperator based implemenation.

* fix no_std build error

* clean up code in VisitOperator impl

* implement rest of the VisitOperator trait

* unsilence warnings in visit.rs

* unsilence warnings in error.rs

* rename internal method and fix doc links

* apply rustfmt

* use macro to implement most unsupported Wasm operators

* rename FunctionBuilder -> FuncBuilder

This creates more parity with the wasmparser::FuncValidator.

* fix doc links

* remove validator getter on FuncBuilder
  • Loading branch information
Robbepop committed Aug 30, 2022
1 parent e9b0463 commit 225c822
Show file tree
Hide file tree
Showing 14 changed files with 2,451 additions and 1,539 deletions.
58 changes: 58 additions & 0 deletions wasmi_v1/src/engine/func_builder/error.rs
@@ -0,0 +1,58 @@
use alloc::boxed::Box;
use core::fmt::{self, Display};

/// An error that may occur upon parsing, validating and translating Wasm.
#[derive(Debug)]
pub struct TranslationError {
/// The inner error type encapsulating internal error state.
inner: Box<TranslationErrorInner>,
}

impl TranslationError {
/// Creates a new error indicating an unsupported Wasm block type.
pub fn unsupported_block_type(block_type: wasmparser::BlockType) -> Self {
Self {
inner: Box::new(TranslationErrorInner::UnsupportedBlockType(block_type)),
}
}

/// Creates a new error indicating an unsupported Wasm value type.
pub fn unsupported_value_type(value_type: wasmparser::ValType) -> Self {
Self {
inner: Box::new(TranslationErrorInner::UnsupportedValueType(value_type)),
}
}
}

impl From<wasmparser::BinaryReaderError> for TranslationError {
fn from(error: wasmparser::BinaryReaderError) -> Self {
Self {
inner: Box::new(TranslationErrorInner::Validate(error)),
}
}
}

impl Display for TranslationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &*self.inner {
TranslationErrorInner::Validate(error) => error.fmt(f),
TranslationErrorInner::UnsupportedBlockType(error) => {
write!(f, "encountered unsupported Wasm block type: {:?}", error)
}
TranslationErrorInner::UnsupportedValueType(error) => {
write!(f, "encountered unsupported Wasm value type: {:?}", error)
}
}
}
}

/// The inner error type encapsulating internal [`TranslationError`] state.
#[derive(Debug)]
enum TranslationErrorInner {
/// There was either a problem parsing a Wasm input OR validating a Wasm input.
Validate(wasmparser::BinaryReaderError),
/// Encountered an unsupported Wasm block type.
UnsupportedBlockType(wasmparser::BlockType),
/// Encountered an unsupported Wasm value type.
UnsupportedValueType(wasmparser::ValType),
}

0 comments on commit 225c822

Please sign in to comment.