From d837e040c9f12ceac4284f4fb557808f006f742d Mon Sep 17 00:00:00 2001 From: Paul Lancaster Date: Tue, 26 May 2020 15:20:40 +0100 Subject: [PATCH] Number constants (#420) Co-authored-by: HalidOdat --- boa/src/builtins/number/mod.rs | 15 ++++++++++++++- boa/src/builtins/number/tests.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/boa/src/builtins/number/mod.rs b/boa/src/builtins/number/mod.rs index e3dae7bf893..05cde1dcf13 100644 --- a/boa/src/builtins/number/mod.rs +++ b/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. diff --git a/boa/src/builtins/number/tests.rs b/boa/src/builtins/number/tests.rs index 4e01da96c87..39725ba5554 100644 --- a/boa/src/builtins/number/tests.rs +++ b/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()); +}