Skip to content

Commit bb066e6

Browse files
richard-uk1alexcrichton
authored andcommitted
Add javascript Number consts. (#1965)
* Add javascript Number consts. * Add tests
1 parent 450c477 commit bb066e6

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

crates/js-sys/src/lib.rs

+38
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
1919
#![doc(html_root_url = "https://docs.rs/js-sys/0.2")]
2020

21+
use std::f64;
2122
use std::fmt;
2223
use std::mem;
2324

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

1905+
impl Number {
1906+
/// The smallest interval between two representable numbers.
1907+
///
1908+
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/EPSILON)
1909+
pub const EPSILON: f64 = f64::EPSILON;
1910+
/// The maximum safe integer in JavaScript (2^53 - 1).
1911+
///
1912+
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)
1913+
pub const MAX_SAFE_INTEGER: f64 = 9007199254740991.0;
1914+
/// The largest positive representable number.
1915+
///
1916+
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_VALUE)
1917+
pub const MAX_VALUE: f64 = f64::MAX;
1918+
/// The minimum safe integer in JavaScript (-(2^53 - 1)).
1919+
///
1920+
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_SAFE_INTEGER)
1921+
pub const MIN_SAFE_INTEGER: f64 = -9007199254740991.0;
1922+
/// The smallest positive representable number—that is, the positive number closest to zero
1923+
/// (without actually being zero).
1924+
///
1925+
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_VALUE)
1926+
// Cannot use f64::MIN_POSITIVE since that is the smallest **normal** postitive number.
1927+
pub const MIN_VALUE: f64 = 5E-324;
1928+
/// Special "Not a Number" value.
1929+
///
1930+
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/NaN)
1931+
pub const NAN: f64 = f64::NAN;
1932+
/// Special value representing negative infinity. Returned on overflow.
1933+
///
1934+
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/NEGATIVE_INFINITY)
1935+
pub const NEGATIVE_INFINITY: f64 = f64::NEG_INFINITY;
1936+
/// Special value representing infinity. Returned on overflow.
1937+
///
1938+
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/POSITIVE_INFINITY)
1939+
pub const POSITIVE_INFINITY: f64 = f64::INFINITY;
1940+
}
1941+
19041942
macro_rules! number_from {
19051943
($($x:ident)*) => ($(
19061944
impl From<$x> for Number {

crates/js-sys/tests/wasm/Number.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
exports.const_epsilon = function() {
3+
return Number.EPSILON;
4+
};
5+
exports.const_max_safe_integer = function() {
6+
return Number.MAX_SAFE_INTEGER;
7+
};
8+
exports.const_max_value = function() {
9+
return Number.MAX_VALUE;
10+
};
11+
exports.const_min_safe_integer = function() {
12+
return Number.MIN_SAFE_INTEGER;
13+
};
14+
exports.const_min_value = function() {
15+
return Number.MIN_VALUE;
16+
};
17+
exports.const_negative_infinity = function() {
18+
return Number.NEGATIVE_INFINITY;
19+
};
20+
exports.const_positive_infinity = function() {
21+
return Number.POSITIVE_INFINITY;
22+
};

crates/js-sys/tests/wasm/Number.rs

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
use std::f64::{INFINITY, NAN};
22

33
use js_sys::*;
4+
use wasm_bindgen::prelude::*;
45
use wasm_bindgen::JsCast;
5-
use wasm_bindgen::JsValue;
66
use wasm_bindgen_test::*;
77

8+
#[wasm_bindgen(module = "tests/wasm/Number.js")]
9+
extern "C" {
10+
fn const_epsilon() -> f64;
11+
fn const_max_safe_integer() -> f64;
12+
fn const_max_value() -> f64;
13+
fn const_min_safe_integer() -> f64;
14+
fn const_min_value() -> f64;
15+
fn const_negative_infinity() -> f64;
16+
fn const_positive_infinity() -> f64;
17+
}
18+
819
#[wasm_bindgen_test]
920
fn is_finite() {
1021
assert!(Number::is_finite(&42.into()));
@@ -118,3 +129,30 @@ fn number_inheritance() {
118129
assert!(n.is_instance_of::<Object>());
119130
let _: &Object = n.as_ref();
120131
}
132+
133+
#[wasm_bindgen_test]
134+
fn consts() {
135+
assert_eq!(const_epsilon(), Number::EPSILON, "EPSILON");
136+
assert_eq!(
137+
const_max_safe_integer(),
138+
Number::MAX_SAFE_INTEGER,
139+
"MAX_SAFE_INTEGER"
140+
);
141+
assert_eq!(const_max_value(), Number::MAX_VALUE, "MAX_VALUE");
142+
assert_eq!(
143+
const_min_safe_integer(),
144+
Number::MIN_SAFE_INTEGER,
145+
"MIN_SAFE_INTEGER"
146+
);
147+
assert_eq!(const_min_value(), Number::MIN_VALUE, "MIN_VALUE");
148+
assert_eq!(
149+
const_negative_infinity(),
150+
Number::NEGATIVE_INFINITY,
151+
"NEGATIVE_INFINITY"
152+
);
153+
assert_eq!(
154+
const_positive_infinity(),
155+
Number::POSITIVE_INFINITY,
156+
"POSITIVE_INFINITY"
157+
);
158+
}

0 commit comments

Comments
 (0)