From c5c5c52df02ec84fab60236bfea7f5173bdb407b Mon Sep 17 00:00:00 2001 From: Richard Dodd Date: Sat, 18 Jan 2020 18:02:35 +0000 Subject: [PATCH 1/2] Add javascript Number consts. --- crates/js-sys/src/lib.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 90fb9083803..0d9ec1b4a81 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -18,6 +18,7 @@ #![doc(html_root_url = "https://docs.rs/js-sys/0.2")] +use std::f64; use std::fmt; use std::mem; @@ -1901,6 +1902,42 @@ 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) + pub const MIN_VALUE: f64 = f64::MIN_POSITIVE; + /// 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 { From 7935100368b6788db14bbb95d31f830602e64f54 Mon Sep 17 00:00:00 2001 From: Richard Dodd Date: Sat, 18 Jan 2020 18:51:01 +0000 Subject: [PATCH 2/2] Add tests --- crates/js-sys/src/lib.rs | 3 ++- crates/js-sys/tests/wasm/Number.js | 22 ++++++++++++++++ crates/js-sys/tests/wasm/Number.rs | 40 +++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 crates/js-sys/tests/wasm/Number.js diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 0d9ec1b4a81..61eca1717a5 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -1923,7 +1923,8 @@ impl Number { /// (without actually being zero). /// /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_VALUE) - pub const MIN_VALUE: f64 = f64::MIN_POSITIVE; + // 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) diff --git a/crates/js-sys/tests/wasm/Number.js b/crates/js-sys/tests/wasm/Number.js new file mode 100644 index 00000000000..c3258885aee --- /dev/null +++ b/crates/js-sys/tests/wasm/Number.js @@ -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; +}; diff --git a/crates/js-sys/tests/wasm/Number.rs b/crates/js-sys/tests/wasm/Number.rs index 60ce21b3ad6..6c3416eda5b 100644 --- a/crates/js-sys/tests/wasm/Number.rs +++ b/crates/js-sys/tests/wasm/Number.rs @@ -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())); @@ -118,3 +129,30 @@ fn number_inheritance() { assert!(n.is_instance_of::()); 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" + ); +}