Skip to content

Commit

Permalink
Add javascript Number consts. (#1965)
Browse files Browse the repository at this point in the history
* Add javascript Number consts.

* Add tests
  • Loading branch information
derekdreery authored and alexcrichton committed Jan 21, 2020
1 parent 450c477 commit bb066e6
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
38 changes: 38 additions & 0 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#![doc(html_root_url = "https://docs.rs/js-sys/0.2")]

use std::f64;
use std::fmt;
use std::mem;

Expand Down Expand Up @@ -1901,6 +1902,43 @@ extern "C" {
pub fn value_of(this: &Number) -> f64;
}

impl Number {
/// The smallest interval between two representable numbers.
///
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/EPSILON)
pub const EPSILON: f64 = f64::EPSILON;
/// The maximum safe integer in JavaScript (2^53 - 1).
///
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)
pub const MAX_SAFE_INTEGER: f64 = 9007199254740991.0;
/// The largest positive representable number.
///
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_VALUE)
pub const MAX_VALUE: f64 = f64::MAX;
/// The minimum safe integer in JavaScript (-(2^53 - 1)).
///
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_SAFE_INTEGER)
pub const MIN_SAFE_INTEGER: f64 = -9007199254740991.0;
/// The smallest positive representable number—that is, the positive number closest to zero
/// (without actually being zero).
///
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_VALUE)
// Cannot use f64::MIN_POSITIVE since that is the smallest **normal** postitive number.
pub const MIN_VALUE: f64 = 5E-324;
/// Special "Not a Number" value.
///
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/NaN)
pub const NAN: f64 = f64::NAN;
/// Special value representing negative infinity. Returned on overflow.
///
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/NEGATIVE_INFINITY)
pub const NEGATIVE_INFINITY: f64 = f64::NEG_INFINITY;
/// Special value representing infinity. Returned on overflow.
///
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/POSITIVE_INFINITY)
pub const POSITIVE_INFINITY: f64 = f64::INFINITY;
}

macro_rules! number_from {
($($x:ident)*) => ($(
impl From<$x> for Number {
Expand Down
22 changes: 22 additions & 0 deletions crates/js-sys/tests/wasm/Number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

exports.const_epsilon = function() {
return Number.EPSILON;
};
exports.const_max_safe_integer = function() {
return Number.MAX_SAFE_INTEGER;
};
exports.const_max_value = function() {
return Number.MAX_VALUE;
};
exports.const_min_safe_integer = function() {
return Number.MIN_SAFE_INTEGER;
};
exports.const_min_value = function() {
return Number.MIN_VALUE;
};
exports.const_negative_infinity = function() {
return Number.NEGATIVE_INFINITY;
};
exports.const_positive_infinity = function() {
return Number.POSITIVE_INFINITY;
};
40 changes: 39 additions & 1 deletion crates/js-sys/tests/wasm/Number.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
use std::f64::{INFINITY, NAN};

use js_sys::*;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;

#[wasm_bindgen(module = "tests/wasm/Number.js")]
extern "C" {
fn const_epsilon() -> f64;
fn const_max_safe_integer() -> f64;
fn const_max_value() -> f64;
fn const_min_safe_integer() -> f64;
fn const_min_value() -> f64;
fn const_negative_infinity() -> f64;
fn const_positive_infinity() -> f64;
}

#[wasm_bindgen_test]
fn is_finite() {
assert!(Number::is_finite(&42.into()));
Expand Down Expand Up @@ -118,3 +129,30 @@ fn number_inheritance() {
assert!(n.is_instance_of::<Object>());
let _: &Object = n.as_ref();
}

#[wasm_bindgen_test]
fn consts() {
assert_eq!(const_epsilon(), Number::EPSILON, "EPSILON");
assert_eq!(
const_max_safe_integer(),
Number::MAX_SAFE_INTEGER,
"MAX_SAFE_INTEGER"
);
assert_eq!(const_max_value(), Number::MAX_VALUE, "MAX_VALUE");
assert_eq!(
const_min_safe_integer(),
Number::MIN_SAFE_INTEGER,
"MIN_SAFE_INTEGER"
);
assert_eq!(const_min_value(), Number::MIN_VALUE, "MIN_VALUE");
assert_eq!(
const_negative_infinity(),
Number::NEGATIVE_INFINITY,
"NEGATIVE_INFINITY"
);
assert_eq!(
const_positive_infinity(),
Number::POSITIVE_INFINITY,
"POSITIVE_INFINITY"
);
}

0 comments on commit bb066e6

Please sign in to comment.