Browse Source

Number constants (#420)

Co-authored-by: HalidOdat <halidodat@gmail.com>
pull/424/head
Paul Lancaster 5 years ago committed by GitHub
parent
commit
d837e040c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      boa/src/builtins/number/mod.rs
  2. 28
      boa/src/builtins/number/tests.rs

15
boa/src/builtins/number/mod.rs

@ -402,7 +402,20 @@ impl Number {
make_builtin_fn(Self::to_string, "toString", &prototype, 1);
make_builtin_fn(Self::value_of, "valueOf", &prototype, 0);
make_constructor_fn("Number", 1, Self::make_number, global, prototype, true)
let number = make_constructor_fn("Number", 1, Self::make_number, global, prototype, true);
// Constants from:
// https://tc39.es/ecma262/#sec-properties-of-the-number-constructor
number.set_field("EPSILON", Value::from(std::f64::EPSILON));
number.set_field("MAX_SAFE_INTEGER", Value::from(9_007_199_254_740_991_f64));
number.set_field("MIN_SAFE_INTEGER", Value::from(-9_007_199_254_740_991_f64));
number.set_field("MAX_VALUE", Value::from(std::f64::MAX));
number.set_field("MIN_VALUE", Value::from(std::f64::MIN));
number.set_field("NEGATIVE_INFINITY", Value::from(f64::NEG_INFINITY));
number.set_field("POSITIVE_INFINITY", Value::from(f64::INFINITY));
number.set_field("NaN", Value::from(f64::NAN));
number
}
/// Initialise the `Number` object on the global object.

28
boa/src/builtins/number/tests.rs

@ -442,3 +442,31 @@ fn from_bigint() {
assert_eq!(&forward(&mut engine, "Number(100000n)"), "100000",);
assert_eq!(&forward(&mut engine, "Number(1n << 1240n)"), "Infinity",);
}
#[test]
fn number_constants() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);
assert!(!forward_val(&mut engine, "Number.EPSILON")
.unwrap()
.is_null_or_undefined());
assert!(!forward_val(&mut engine, "Number.MAX_SAFE_INTEGER")
.unwrap()
.is_null_or_undefined());
assert!(!forward_val(&mut engine, "Number.MIN_SAFE_INTEGER")
.unwrap()
.is_null_or_undefined());
assert!(!forward_val(&mut engine, "Number.MAX_VALUE")
.unwrap()
.is_null_or_undefined());
assert!(!forward_val(&mut engine, "Number.MIN_VALUE")
.unwrap()
.is_null_or_undefined());
assert!(!forward_val(&mut engine, "Number.NEGATIVE_INFINITY")
.unwrap()
.is_null_or_undefined());
assert!(!forward_val(&mut engine, "Number.POSITIVE_INFINITY")
.unwrap()
.is_null_or_undefined());
}

Loading…
Cancel
Save