Skip to content

Commit

Permalink
Fix wasmi no_std support (#230)
Browse files Browse the repository at this point in the history
* Fix wasmi no_std support (#218)

Two issues are fixed:

 1. num-bigint 0.2 requires std, and is used to perform float to int conversions
    since #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.

* Ensure wasmi builds without std in CI

The no-std-check cargo subcommand uses a custom sysroot to prevent the crate
from using std while checking a local target. This should help ensure that
changes which introduce std dependencies are caught at review time.
  • Loading branch information
mystor authored Feb 18, 2020
1 parent e8d5fb6 commit d186a3d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ matrix:

install:
- if [ "$TRAVIS_RUST_VERSION" == "nightly" ]; then rustup target add wasm32-unknown-unknown; fi
- if [ "$TRAVIS_RUST_VERSION" == "nightly" ]; then cargo install cargo-no-std-check; fi
- if [ -n "$TARGET" ]; then rustup target add "$TARGET" && sudo apt-get install --yes qemu-user-static; fi
- if [ "$TARGET" == "armv7-unknown-linux-gnueabihf" ]; then sudo apt-get install --yes crossbuild-essential-armhf && export QEMU_LD_PREFIX=/usr/arm-linux-gnueabihf; fi
- rustup component add rustfmt
Expand All @@ -25,7 +26,7 @@ script:
- if [ "$TRAVIS_RUST_VERSION" == "nightly" ]; then cargo check --tests --manifest-path=fuzz/Cargo.toml; fi
- if [ "$TRAVIS_RUST_VERSION" == "nightly" ]; then cargo check --benches --manifest-path=benches/Cargo.toml; fi
# Make sure `no_std` version checks.
- if [ "$TRAVIS_RUST_VERSION" == "nightly" ]; then cargo +nightly check --no-default-features --features core; fi
- if [ "$TRAVIS_RUST_VERSION" == "nightly" ]; then cargo no-std-check --no-default-features --features core; fi
# Check that `vec_memory` feature works.
- cargo check --features vec_memory
- travis_wait 60 ./test.sh
Expand Down
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 d186a3d

Please sign in to comment.