Skip to content

Commit

Permalink
Fix wasmi no_std support (wasmi-labs#218)
Browse files Browse the repository at this point in the history
Two issues are fixed:

 1. num-bigint 0.2 requires std, and is used to perform float to int conversions
    since wasmi-labs#186. This patch disables this change in no_std environments.
    This change can be reverted once num-bigint 0.3 is released, which will
    support no_std.

 2. The `f{32,64}::fract` method is currently not implemented by core, only std.
    This patch uses the no_std supporting implementation from num-trait's
    FloatCore trait.
  • Loading branch information
mystor committed Feb 18, 2020
1 parent e8d5fb6 commit 7c1d75a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ wasmi-validation = { version = "0.3", path = "validation", default-features = fa
parity-wasm = { version = "0.41.0", 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"
num-rational = { version = "0.2.2", default-features = false }
num-traits = { version = "0.2.8", default-features = false }
libc = "0.2.58"
errno = { version = "0.2.4", optional = true }

Expand All @@ -32,6 +32,7 @@ std = [
"parity-wasm/std",
"wasmi-validation/std",
"num-rational/std",
"num-rational/bigint-std",
"num-traits/std"
]
# Enable for no_std support
Expand Down
3 changes: 2 additions & 1 deletion src/nan_preserving_float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use core::cmp::{Ordering, PartialEq, PartialOrd};
use core::ops::{Add, Div, Mul, Neg, Rem, Sub};
use num_traits::float::FloatCore;

macro_rules! impl_binop {
($for:ident, $is:ident, $op:ident, $func_name:ident) => {
Expand Down Expand Up @@ -63,7 +64,7 @@ macro_rules! float {
}

pub fn fract(self) -> Self {
self.to_float().fract().into()
FloatCore::fract(self.to_float()).into()
}

pub fn min(self, other: Self) -> Self {
Expand Down
9 changes: 9 additions & 0 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,22 @@ impl WrapInto<F32> for F64 {
macro_rules! impl_try_truncate_into {
(@primitive $from: ident, $into: ident, $to_primitive:path) => {
impl TryTruncateInto<$into, TrapKind> for $from {
// FIXME: BigRational uses the num-bigint crate, which requires std
// as of version `0.2.6`. `num-bigint` 0.3 will have no_std support.
#[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)
.map(|val| val.to_integer())
.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
$to_primitive(&self).ok_or(TrapKind::InvalidConversionToInt)
}
}
};
(@wrapped $from:ident, $intermediate:ident, $into:ident) => {
Expand Down

0 comments on commit 7c1d75a

Please sign in to comment.