From c1cc1a2c3e005bebd35091596fff97eb07970988 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 21 Dec 2020 22:55:37 +0100 Subject: [PATCH] Replace `wasmparser::Result` with `wasmer::WasmResult` in middleware --- CHANGELOG.md | 2 +- lib/api/src/lib.rs | 2 +- lib/compiler/src/translator/middleware.rs | 16 +++++++++------- lib/middlewares/src/metering.rs | 8 +++----- tests/compilers/middlewares.rs | 6 +++--- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b48e4166315..4142f00937a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ * [#1941](https://github.com/wasmerio/wasmer/pull/1941) Turn `get_remaining_points`/`set_remaining_points` of the `Metering` middleware into free functions to allow using them in an ahead-of-time compilation setup * [#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. -* Deprecated `to_wasm_error` in favour of `impl From for WasmError` +* Deprecated `to_wasm_error` in favour of `impl From for WasmError`. Replace `wasmparser::Result` with `wasmer::WasmResult` in middleware. ### Fixed diff --git a/lib/api/src/lib.rs b/lib/api/src/lib.rs index 8adef3b737f..738925df8b2 100644 --- a/lib/api/src/lib.rs +++ b/lib/api/src/lib.rs @@ -307,7 +307,7 @@ pub use wasmer_compiler::{ wasmparser, CompilerConfig, FunctionMiddleware, MiddlewareReaderState, ModuleMiddleware, }; pub use wasmer_compiler::{ - CompileError, CpuFeature, Features, ParseCpuFeatureError, Target, WasmError, + CompileError, CpuFeature, Features, ParseCpuFeatureError, Target, WasmError, WasmResult, }; pub use wasmer_engine::{ ChainableNamedResolver, DeserializeError, Engine, Export, FrameInfo, LinkError, NamedResolver, diff --git a/lib/compiler/src/translator/middleware.rs b/lib/compiler/src/translator/middleware.rs index e7f21c574b7..e7f7f300aa5 100644 --- a/lib/compiler/src/translator/middleware.rs +++ b/lib/compiler/src/translator/middleware.rs @@ -7,7 +7,9 @@ use std::fmt::Debug; use std::ops::Deref; use wasmer_types::LocalFunctionIndex; 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. pub trait ModuleMiddleware: Debug + Send + Sync { @@ -32,7 +34,7 @@ pub trait FunctionMiddleware: Debug { &mut self, operator: Operator<'a>, state: &mut MiddlewareReaderState<'a>, - ) -> WpResult<()> { + ) -> WasmResult<()> { state.push_operator(operator); Ok(()) } @@ -127,22 +129,22 @@ impl<'a> MiddlewareBinaryReader<'a> { } /// Read a `count` indicating the number of times to call `read_local_decl`. - pub fn read_local_count(&mut self) -> WpResult { - self.state.inner.read_var_u32() + pub fn read_local_count(&mut self) -> WasmResult { + Ok(self.state.inner.read_var_u32()?) } /// 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 ty = self.state.inner.read_type()?; Ok((count, ty)) } /// Reads the next available `Operator`. - pub fn read_operator(&mut self) -> WpResult> { + pub fn read_operator(&mut self) -> WasmResult> { if self.chain.is_empty() { // 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. diff --git a/lib/middlewares/src/metering.rs b/lib/middlewares/src/metering.rs index a0a2919d31f..130c39961e5 100644 --- a/lib/middlewares/src/metering.rs +++ b/lib/middlewares/src/metering.rs @@ -4,12 +4,10 @@ use std::convert::TryInto; use std::fmt; use std::sync::Mutex; -use wasmer::wasmparser::{ - Operator, Result as WpResult, Type as WpType, TypeOrFuncType as WpTypeOrFuncType, -}; +use wasmer::wasmparser::{Operator, Type as WpType, TypeOrFuncType as WpTypeOrFuncType}; use wasmer::{ ExportIndex, FunctionMiddleware, GlobalInit, GlobalType, Instance, LocalFunctionIndex, - MiddlewareReaderState, ModuleMiddleware, Mutability, Type, + MiddlewareReaderState, ModuleMiddleware, Mutability, Type, WasmResult, }; use wasmer_types::GlobalIndex; use wasmer_vm::ModuleInfo; @@ -118,7 +116,7 @@ impl u64 + Copy + Clone + Send + Sync> FunctionMiddleware &mut self, operator: Operator<'a>, state: &mut MiddlewareReaderState<'a>, - ) -> WpResult<()> { + ) -> WasmResult<()> { // 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 // corner cases. diff --git a/tests/compilers/middlewares.rs b/tests/compilers/middlewares.rs index 939c72b090f..4564cf50ff5 100644 --- a/tests/compilers/middlewares.rs +++ b/tests/compilers/middlewares.rs @@ -2,7 +2,7 @@ use crate::utils::get_store_with_middlewares; use anyhow::Result; use std::sync::Arc; -use wasmer::wasmparser::{Operator, Result as WpResult}; +use wasmer::wasmparser::Operator; use wasmer::*; #[derive(Debug)] @@ -28,7 +28,7 @@ impl FunctionMiddleware for Add2Mul { &mut self, operator: Operator<'a>, state: &mut MiddlewareReaderState<'a>, - ) -> WpResult<()> { + ) -> WasmResult<()> { match operator { Operator::I32Add => { state.push_operator(Operator::I32Mul); @@ -66,7 +66,7 @@ impl FunctionMiddleware for Fusion { &mut self, operator: Operator<'a>, state: &mut MiddlewareReaderState<'a>, - ) -> WpResult<()> { + ) -> WasmResult<()> { match (operator, self.state) { (Operator::I32Add, 0) => { self.state = 1;