From 0b6dd64219cc73e8fc58cfdfdd89a314b1d41596 Mon Sep 17 00:00:00 2001 From: Within Date: Thu, 24 Oct 2019 10:29:04 -0400 Subject: [PATCH] fix no_std building --- Cargo.toml | 6 +++--- src/nan_preserving_float.rs | 3 +++ src/value.rs | 27 ++++++++++++++++++--------- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0c86dc6..72975f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,9 +15,8 @@ wasmi-validation = { version = "0.2", path = "validation", default-features = fa parity-wasm = { version = "0.40.1", default-features = false } memory_units = "0.3.0" libm = { version = "0.1.2", optional = true } -num-rational = "0.2.2" -num-traits = "0.2.8" -libc = "0.2.58" +num-rational = { version = "0.2.2", default-features = false } +num-traits = { version = "0.2.8", default-features = false } [dev-dependencies] assert_matches = "1.1" @@ -31,6 +30,7 @@ std = [ "parity-wasm/std", "wasmi-validation/std", "num-rational/std", + "num-rational/bigint-std", "num-traits/std" ] # Enable for no_std support diff --git a/src/nan_preserving_float.rs b/src/nan_preserving_float.rs index 994c91a..e63d476 100644 --- a/src/nan_preserving_float.rs +++ b/src/nan_preserving_float.rs @@ -1,5 +1,8 @@ #![allow(missing_docs)] +#[cfg(not(feature = "std"))] +use libm::{F32Ext, F64Ext}; + use core::cmp::{Ordering, PartialEq, PartialOrd}; use core::ops::{Add, Div, Mul, Neg, Rem, Sub}; diff --git a/src/value.rs b/src/value.rs index 8d5061a..31698e3 100644 --- a/src/value.rs +++ b/src/value.rs @@ -368,6 +368,7 @@ impl WrapInto for F64 { macro_rules! impl_try_truncate_into { (@primitive $from: ident, $into: ident, $to_primitive:path) => { impl TryTruncateInto<$into, TrapKind> for $from { + #[cfg(feature = "std")] fn try_truncate_into(self) -> Result<$into, TrapKind> { // Casting from a float to an integer will round the float towards zero num_rational::BigRational::from_float(self) @@ -375,6 +376,23 @@ macro_rules! impl_try_truncate_into { .and_then(|val| $to_primitive(&val)) .ok_or(TrapKind::InvalidConversionToInt) } + #[cfg(not(feature = "std"))] + fn try_truncate_into(self) -> Result<$into, TrapKind> { + // Casting from a float to an integer will round the float towards zero + // NOTE: currently this will cause Undefined Behavior if the rounded value cannot be represented by the + // target integer type. This includes Inf and NaN. This is a bug and will be fixed. + if self.is_nan() || self.is_infinite() { + return Err(TrapKind::InvalidConversionToInt); + } + + // range check + let result = self as $into; + if result as $from != self.trunc() { + return Err(TrapKind::InvalidConversionToInt); + } + + Ok(self as $into) + } } }; (@wrapped $from:ident, $intermediate:ident, $into:ident) => { @@ -819,15 +837,6 @@ impl_integer!(u32); impl_integer!(i64); impl_integer!(u64); -// Use std float functions in std environment. -// And libm's implementation in no_std -#[cfg(feature = "std")] -macro_rules! call_math { - ($op:ident, $e:expr, $fXX:ident, $FXXExt:ident) => { - $fXX::$op($e) - }; -} -#[cfg(not(feature = "std"))] macro_rules! call_math { ($op:ident, $e:expr, $fXX:ident, $FXXExt:ident) => { ::libm::$FXXExt::$op($e)