Replace wasmparser::Result with wasmer::WasmResult in middleware

This commit is contained in:
Simon Warta
2020-12-21 22:55:37 +01:00
parent a16701ac6d
commit c41451a547
6 changed files with 20 additions and 26 deletions

View File

@ -17,6 +17,7 @@
* [#1955](https://github.com/wasmerio/wasmer/pull/1955) Set `jit` as a default feature of the `wasmer-wasm-c-api` crate * [#1955](https://github.com/wasmerio/wasmer/pull/1955) Set `jit` as a default feature of the `wasmer-wasm-c-api` crate
* [#1944](https://github.com/wasmerio/wasmer/pull/1944) Require `WasmerEnv` to be `Send + Sync` even in dynamic functions. * [#1944](https://github.com/wasmerio/wasmer/pull/1944) Require `WasmerEnv` to be `Send + Sync` even in dynamic functions.
* [#1963](https://github.com/wasmerio/wasmer/pull/1963) Removed `to_wasm_error` in favour of `impl From<BinaryReaderError> for WasmError` * [#1963](https://github.com/wasmerio/wasmer/pull/1963) Removed `to_wasm_error` in favour of `impl From<BinaryReaderError> for WasmError`
* [#1962](https://github.com/wasmerio/wasmer/pull/1962) Replace `wasmparser::Result` with `wasmer::WasmResult` in middleware
### Fixed ### Fixed

View File

@ -307,7 +307,7 @@ pub use wasmer_compiler::{
wasmparser, CompilerConfig, FunctionMiddleware, MiddlewareReaderState, ModuleMiddleware, wasmparser, CompilerConfig, FunctionMiddleware, MiddlewareReaderState, ModuleMiddleware,
}; };
pub use wasmer_compiler::{ pub use wasmer_compiler::{
CompileError, CpuFeature, Features, ParseCpuFeatureError, Target, WasmError, CompileError, CpuFeature, Features, ParseCpuFeatureError, Target, WasmError, WasmResult,
}; };
pub use wasmer_engine::{ pub use wasmer_engine::{
ChainableNamedResolver, DeserializeError, Engine, Export, FrameInfo, LinkError, NamedResolver, ChainableNamedResolver, DeserializeError, Engine, Export, FrameInfo, LinkError, NamedResolver,

View File

@ -9,7 +9,6 @@ use crate::codegen_x64::{
use crate::config::Singlepass; use crate::config::Singlepass;
use rayon::prelude::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator}; use rayon::prelude::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator};
use std::sync::Arc; use std::sync::Arc;
use wasmer_compiler::wasmparser::BinaryReaderError;
use wasmer_compiler::TrapInformation; use wasmer_compiler::TrapInformation;
use wasmer_compiler::{ use wasmer_compiler::{
Architecture, CompileModuleInfo, CompilerConfig, MiddlewareBinaryReader, ModuleMiddlewareChain, Architecture, CompileModuleInfo, CompilerConfig, MiddlewareBinaryReader, ModuleMiddlewareChain,
@ -92,9 +91,9 @@ impl Compiler for SinglepassCompiler {
// This local list excludes arguments. // This local list excludes arguments.
let mut locals = vec![]; let mut locals = vec![];
let num_locals = reader.read_local_count().map_err(to_compile_error)?; let num_locals = reader.read_local_count()?;
for _ in 0..num_locals { for _ in 0..num_locals {
let (count, ty) = reader.read_local_decl().map_err(to_compile_error)?; let (count, ty) = reader.read_local_decl()?;
for _ in 0..count { for _ in 0..count {
locals.push(ty); locals.push(ty);
} }
@ -113,7 +112,7 @@ impl Compiler for SinglepassCompiler {
while generator.has_control_frames() { while generator.has_control_frames() {
generator.set_srcloc(reader.original_position() as u32); generator.set_srcloc(reader.original_position() as u32);
let op = reader.read_operator().map_err(to_compile_error)?; let op = reader.read_operator()?;
generator.feed_operator(op).map_err(to_compile_error)?; generator.feed_operator(op).map_err(to_compile_error)?;
} }
@ -157,12 +156,6 @@ trait ToCompileError {
fn to_compile_error(self) -> CompileError; fn to_compile_error(self) -> CompileError;
} }
impl ToCompileError for BinaryReaderError {
fn to_compile_error(self) -> CompileError {
CompileError::Codegen(self.message().into())
}
}
impl ToCompileError for CodegenError { impl ToCompileError for CodegenError {
fn to_compile_error(self) -> CompileError { fn to_compile_error(self) -> CompileError {
CompileError::Codegen(self.message) CompileError::Codegen(self.message)

View File

@ -7,7 +7,9 @@ use std::fmt::Debug;
use std::ops::Deref; use std::ops::Deref;
use wasmer_types::LocalFunctionIndex; use wasmer_types::LocalFunctionIndex;
use wasmer_vm::ModuleInfo; use wasmer_vm::ModuleInfo;
use wasmparser::{BinaryReader, Operator, Result as WpResult, Type}; use wasmparser::{BinaryReader, Operator, Type};
use crate::error::WasmResult;
/// A shared builder for function middlewares. /// A shared builder for function middlewares.
pub trait ModuleMiddleware: Debug + Send + Sync { pub trait ModuleMiddleware: Debug + Send + Sync {
@ -32,7 +34,7 @@ pub trait FunctionMiddleware: Debug {
&mut self, &mut self,
operator: Operator<'a>, operator: Operator<'a>,
state: &mut MiddlewareReaderState<'a>, state: &mut MiddlewareReaderState<'a>,
) -> WpResult<()> { ) -> WasmResult<()> {
state.push_operator(operator); state.push_operator(operator);
Ok(()) Ok(())
} }
@ -127,22 +129,22 @@ impl<'a> MiddlewareBinaryReader<'a> {
} }
/// Read a `count` indicating the number of times to call `read_local_decl`. /// Read a `count` indicating the number of times to call `read_local_decl`.
pub fn read_local_count(&mut self) -> WpResult<u32> { pub fn read_local_count(&mut self) -> WasmResult<u32> {
self.state.inner.read_var_u32() Ok(self.state.inner.read_var_u32()?)
} }
/// Read a `(count, value_type)` declaration of local variables of the same type. /// Read a `(count, value_type)` declaration of local variables of the same type.
pub fn read_local_decl(&mut self) -> WpResult<(u32, Type)> { pub fn read_local_decl(&mut self) -> WasmResult<(u32, Type)> {
let count = self.state.inner.read_var_u32()?; let count = self.state.inner.read_var_u32()?;
let ty = self.state.inner.read_type()?; let ty = self.state.inner.read_type()?;
Ok((count, ty)) Ok((count, ty))
} }
/// Reads the next available `Operator`. /// Reads the next available `Operator`.
pub fn read_operator(&mut self) -> WpResult<Operator<'a>> { pub fn read_operator(&mut self) -> WasmResult<Operator<'a>> {
if self.chain.is_empty() { if self.chain.is_empty() {
// We short-circuit in case no chain is used // We short-circuit in case no chain is used
return self.state.inner.read_operator(); return Ok(self.state.inner.read_operator()?);
} }
// Try to fill the `self.pending_operations` buffer, until it is non-empty. // Try to fill the `self.pending_operations` buffer, until it is non-empty.

View File

@ -4,12 +4,10 @@
use std::convert::TryInto; use std::convert::TryInto;
use std::fmt; use std::fmt;
use std::sync::Mutex; use std::sync::Mutex;
use wasmer::wasmparser::{ use wasmer::wasmparser::{Operator, Type as WpType, TypeOrFuncType as WpTypeOrFuncType};
Operator, Result as WpResult, Type as WpType, TypeOrFuncType as WpTypeOrFuncType,
};
use wasmer::{ use wasmer::{
ExportIndex, FunctionMiddleware, GlobalInit, GlobalType, Instance, LocalFunctionIndex, ExportIndex, FunctionMiddleware, GlobalInit, GlobalType, Instance, LocalFunctionIndex,
MiddlewareReaderState, ModuleMiddleware, Mutability, Type, MiddlewareReaderState, ModuleMiddleware, Mutability, Type, WasmResult,
}; };
use wasmer_types::GlobalIndex; use wasmer_types::GlobalIndex;
use wasmer_vm::ModuleInfo; use wasmer_vm::ModuleInfo;
@ -118,7 +116,7 @@ impl<F: Fn(&Operator) -> u64 + Copy + Clone + Send + Sync> FunctionMiddleware
&mut self, &mut self,
operator: Operator<'a>, operator: Operator<'a>,
state: &mut MiddlewareReaderState<'a>, state: &mut MiddlewareReaderState<'a>,
) -> WpResult<()> { ) -> WasmResult<()> {
// Get the cost of the current operator, and add it to the accumulator. // Get the cost of the current operator, and add it to the accumulator.
// This needs to be done before the metering logic, to prevent operators like `Call` from escaping metering in some // This needs to be done before the metering logic, to prevent operators like `Call` from escaping metering in some
// corner cases. // corner cases.

View File

@ -2,7 +2,7 @@ use crate::utils::get_store_with_middlewares;
use anyhow::Result; use anyhow::Result;
use std::sync::Arc; use std::sync::Arc;
use wasmer::wasmparser::{Operator, Result as WpResult}; use wasmer::wasmparser::Operator;
use wasmer::*; use wasmer::*;
#[derive(Debug)] #[derive(Debug)]
@ -28,7 +28,7 @@ impl FunctionMiddleware for Add2Mul {
&mut self, &mut self,
operator: Operator<'a>, operator: Operator<'a>,
state: &mut MiddlewareReaderState<'a>, state: &mut MiddlewareReaderState<'a>,
) -> WpResult<()> { ) -> WasmResult<()> {
match operator { match operator {
Operator::I32Add => { Operator::I32Add => {
state.push_operator(Operator::I32Mul); state.push_operator(Operator::I32Mul);
@ -66,7 +66,7 @@ impl FunctionMiddleware for Fusion {
&mut self, &mut self,
operator: Operator<'a>, operator: Operator<'a>,
state: &mut MiddlewareReaderState<'a>, state: &mut MiddlewareReaderState<'a>,
) -> WpResult<()> { ) -> WasmResult<()> {
match (operator, self.state) { match (operator, self.state) {
(Operator::I32Add, 0) => { (Operator::I32Add, 0) => {
self.state = 1; self.state = 1;