mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-13 04:51:23 +00:00
Add javascript Number consts. (#1965)
* Add javascript Number consts. * Add tests
This commit is contained in:
committed by
Alex Crichton
parent
450c477197
commit
bb066e68a5
@ -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,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 {
|
||||
|
22
crates/js-sys/tests/wasm/Number.js
Normal file
22
crates/js-sys/tests/wasm/Number.js
Normal file
@ -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;
|
||||
};
|
@ -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::<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"
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user