Skip to content

Commit

Permalink
Number constants (#420)
Browse files Browse the repository at this point in the history
Co-authored-by: HalidOdat <[email protected]>
  • Loading branch information
Paul Lancaster and HalidOdat authored May 26, 2020
1 parent d42e413 commit d837e04
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
15 changes: 14 additions & 1 deletion boa/src/builtins/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions boa/src/builtins/number/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

0 comments on commit d837e04

Please sign in to comment.