Skip to content

Commit

Permalink
Enable more clippy lints for wasmi_core (#438)
Browse files Browse the repository at this point in the history
* use map_or_else

* apply some clippy suggestions

* enable more clippy lints for wasmi_v1 crate

* split {i32,i64}.div and {f32,f64}.div implementations

The reason is that the integer div may trap whereas the float div does not.

* apply clippy suggestions and clean up some macro expansions

* enable some clippy lints for wasmi_core
  • Loading branch information
Robbepop committed Sep 13, 2022
1 parent 61ceb25 commit 11b35ae
Show file tree
Hide file tree
Showing 19 changed files with 320 additions and 129 deletions.
12 changes: 11 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![warn(clippy::cast_lossless)]
#![warn(
clippy::cast_lossless,
clippy::missing_errors_doc,
clippy::used_underscore_binding,
clippy::redundant_closure_for_method_calls,
clippy::type_repetition_in_bounds,
clippy::inconsistent_struct_constructor,
clippy::default_trait_access,
clippy::map_unwrap_or,
clippy::items_after_statements
)]

mod host_error;
mod nan_preserving_float;
Expand Down
2 changes: 1 addition & 1 deletion core/src/trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl Display for Trap {
#[cfg(feature = "std")]
impl StdError for Trap {
fn description(&self) -> &str {
self.as_code().map(|code| code.trap_message()).unwrap_or("")
self.as_code().map_or("", |code| code.trap_message())
}
}

Expand Down
144 changes: 136 additions & 8 deletions core/src/untyped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,41 +217,81 @@ impl UntypedValue {
}

/// Execute `i32.div_s` Wasm operation.
///
/// # Errors
///
/// - If `rhs` is equal to zero.
/// - If the operation result overflows.
pub fn i32_div_s(self, rhs: Self) -> Result<Self, TrapCode> {
self.try_execute_binary(rhs, <i32 as ArithmeticOps<i32>>::div)
self.try_execute_binary(rhs, <i32 as Integer<i32>>::div)
}

/// Execute `i64.div_s` Wasm operation.
///
/// # Errors
///
/// - If `rhs` is equal to zero.
/// - If the operation result overflows.
pub fn i64_div_s(self, rhs: Self) -> Result<Self, TrapCode> {
self.try_execute_binary(rhs, <i64 as ArithmeticOps<i64>>::div)
self.try_execute_binary(rhs, <i64 as Integer<i64>>::div)
}

/// Execute `i32.div_u` Wasm operation.
///
/// # Errors
///
/// - If `rhs` is equal to zero.
/// - If the operation result overflows.
pub fn i32_div_u(self, rhs: Self) -> Result<Self, TrapCode> {
self.try_execute_binary(rhs, <u32 as ArithmeticOps<u32>>::div)
self.try_execute_binary(rhs, <u32 as Integer<u32>>::div)
}

/// Execute `i64.div_u` Wasm operation.
///
/// # Errors
///
/// - If `rhs` is equal to zero.
/// - If the operation result overflows.
pub fn i64_div_u(self, rhs: Self) -> Result<Self, TrapCode> {
self.try_execute_binary(rhs, <u64 as ArithmeticOps<u64>>::div)
self.try_execute_binary(rhs, <u64 as Integer<u64>>::div)
}

/// Execute `i32.rem_s` Wasm operation.
///
/// # Errors
///
/// - If `rhs` is equal to zero.
/// - If the operation result overflows.
pub fn i32_rem_s(self, rhs: Self) -> Result<Self, TrapCode> {
self.try_execute_binary(rhs, <i32 as Integer<i32>>::rem)
}

/// Execute `i64.rem_s` Wasm operation.
///
/// # Errors
///
/// - If `rhs` is equal to zero.
/// - If the operation result overflows.
pub fn i64_rem_s(self, rhs: Self) -> Result<Self, TrapCode> {
self.try_execute_binary(rhs, <i64 as Integer<i64>>::rem)
}

/// Execute `i32.rem_u` Wasm operation.
///
/// # Errors
///
/// - If `rhs` is equal to zero.
/// - If the operation result overflows.
pub fn i32_rem_u(self, rhs: Self) -> Result<Self, TrapCode> {
self.try_execute_binary(rhs, <u32 as Integer<u32>>::rem)
}

/// Execute `i64.rem_u` Wasm operation.
///
/// # Errors
///
/// - If `rhs` is equal to zero.
/// - If the operation result overflows.
pub fn i64_rem_u(self, rhs: Self) -> Result<Self, TrapCode> {
self.try_execute_binary(rhs, <u64 as Integer<u64>>::rem)
}
Expand Down Expand Up @@ -652,13 +692,13 @@ impl UntypedValue {
}

/// Execute `f32.div` Wasm operation.
pub fn f32_div(self, rhs: Self) -> Result<Self, TrapCode> {
self.try_execute_binary(rhs, <F32 as ArithmeticOps<F32>>::div)
pub fn f32_div(self, rhs: Self) -> Self {
self.execute_binary(rhs, <F32 as Float<F32>>::div)
}

/// Execute `f64.div` Wasm operation.
pub fn f64_div(self, rhs: Self) -> Result<Self, TrapCode> {
self.try_execute_binary(rhs, <F64 as ArithmeticOps<F64>>::div)
pub fn f64_div(self, rhs: Self) -> Self {
self.execute_binary(rhs, <F64 as Float<F64>>::div)
}

/// Execute `f64.min` Wasm operation.
Expand All @@ -682,21 +722,65 @@ impl UntypedValue {
}

/// Execute `i32.trunc_f32_s` Wasm operation.
///
/// # Errors
///
/// - If `self` is NaN (not a number).
/// - If `self` is positive or negative infinity.
/// - If the integer value of `self` is out of bounds of the target type.
///
/// Read more about the failure cases in the [WebAssembly specification].
///
/// [WebAssembly specification]:
/// https://webassembly.github.io/spec/core/exec/numerics.html#op-trunc-s
pub fn i32_trunc_f32_s(self) -> Result<Self, TrapCode> {
self.try_execute_unary(<F32 as TryTruncateInto<i32, TrapCode>>::try_truncate_into)
}

/// Execute `i32.trunc_f32_u` Wasm operation.
///
/// # Errors
///
/// - If `self` is NaN (not a number).
/// - If `self` is positive or negative infinity.
/// - If the integer value of `self` is out of bounds of the target type.
///
/// Read more about the failure cases in the [WebAssembly specification].
///
/// [WebAssembly specification]:
/// https://webassembly.github.io/spec/core/exec/numerics.html#op-trunc-s
pub fn i32_trunc_f32_u(self) -> Result<Self, TrapCode> {
self.try_execute_unary(<F32 as TryTruncateInto<u32, TrapCode>>::try_truncate_into)
}

/// Execute `i32.trunc_f64_s` Wasm operation.
///
/// # Errors
///
/// - If `self` is NaN (not a number).
/// - If `self` is positive or negative infinity.
/// - If the integer value of `self` is out of bounds of the target type.
///
/// Read more about the failure cases in the [WebAssembly specification].
///
/// [WebAssembly specification]:
/// https://webassembly.github.io/spec/core/exec/numerics.html#op-trunc-s
pub fn i32_trunc_f64_s(self) -> Result<Self, TrapCode> {
self.try_execute_unary(<F64 as TryTruncateInto<i32, TrapCode>>::try_truncate_into)
}

/// Execute `i32.trunc_f64_u` Wasm operation.
///
/// # Errors
///
/// - If `self` is NaN (not a number).
/// - If `self` is positive or negative infinity.
/// - If the integer value of `self` is out of bounds of the target type.
///
/// Read more about the failure cases in the [WebAssembly specification].
///
/// [WebAssembly specification]:
/// https://webassembly.github.io/spec/core/exec/numerics.html#op-trunc-s
pub fn i32_trunc_f64_u(self) -> Result<Self, TrapCode> {
self.try_execute_unary(<F64 as TryTruncateInto<u32, TrapCode>>::try_truncate_into)
}
Expand All @@ -712,21 +796,65 @@ impl UntypedValue {
}

/// Execute `i64.trunc_f32_s` Wasm operation.
///
/// # Errors
///
/// - If `self` is NaN (not a number).
/// - If `self` is positive or negative infinity.
/// - If the integer value of `self` is out of bounds of the target type.
///
/// Read more about the failure cases in the [WebAssembly specification].
///
/// [WebAssembly specification]:
/// https://webassembly.github.io/spec/core/exec/numerics.html#op-trunc-s
pub fn i64_trunc_f32_s(self) -> Result<Self, TrapCode> {
self.try_execute_unary(<F32 as TryTruncateInto<i64, TrapCode>>::try_truncate_into)
}

/// Execute `i64.trunc_f32_u` Wasm operation.
///
/// # Errors
///
/// - If `self` is NaN (not a number).
/// - If `self` is positive or negative infinity.
/// - If the integer value of `self` is out of bounds of the target type.
///
/// Read more about the failure cases in the [WebAssembly specification].
///
/// [WebAssembly specification]:
/// https://webassembly.github.io/spec/core/exec/numerics.html#op-trunc-s
pub fn i64_trunc_f32_u(self) -> Result<Self, TrapCode> {
self.try_execute_unary(<F32 as TryTruncateInto<u64, TrapCode>>::try_truncate_into)
}

/// Execute `i64.trunc_f64_s` Wasm operation.
///
/// # Errors
///
/// - If `self` is NaN (not a number).
/// - If `self` is positive or negative infinity.
/// - If the integer value of `self` is out of bounds of the target type.
///
/// Read more about the failure cases in the [WebAssembly specification].
///
/// [WebAssembly specification]:
/// https://webassembly.github.io/spec/core/exec/numerics.html#op-trunc-s
pub fn i64_trunc_f64_s(self) -> Result<Self, TrapCode> {
self.try_execute_unary(<F64 as TryTruncateInto<i64, TrapCode>>::try_truncate_into)
}

/// Execute `i64.trunc_f64_u` Wasm operation.
///
/// # Errors
///
/// - If `self` is NaN (not a number).
/// - If `self` is positive or negative infinity.
/// - If the integer value of `self` is out of bounds of the target type.
///
/// Read more about the failure cases in the [WebAssembly specification].
///
/// [WebAssembly specification]:
/// https://webassembly.github.io/spec/core/exec/numerics.html#op-trunc-s
pub fn i64_trunc_f64_u(self) -> Result<Self, TrapCode> {
self.try_execute_unary(<F64 as TryTruncateInto<u64, TrapCode>>::try_truncate_into)
}
Expand Down
Loading

0 comments on commit 11b35ae

Please sign in to comment.