Browse Source

add Infinity gloabal property (#480) (#499)

pull/500/head
Anirudh Konduru 5 years ago committed by GitHub
parent
commit
64fca0c162
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 32
      boa/src/builtins/infinity/mod.rs
  2. 19
      boa/src/builtins/infinity/tests.rs
  3. 3
      boa/src/builtins/mod.rs

32
boa/src/builtins/infinity/mod.rs

@ -0,0 +1,32 @@
//! This module implements the global `Infinity` property.
//!
//! The global property `Infinity` is a numeric value representing infinity.
//!
//! More information:
//! - [MDN documentation][mdn]
//! - [ECMAScript reference][spec]
//!
//! [spec]: https://tc39.es/ecma262/#sec-value-properties-of-the-global-object-infinity
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Infinity
#[cfg(test)]
mod tests;
use crate::{builtins::value::Value, BoaProfiler};
/// JavaScript global `Infinity` property.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct Infinity;
impl Infinity {
/// The binding name of the property.
pub(crate) const NAME: &'static str = "Infinity";
/// Initialize the `Infinity` property on the global object.
#[inline]
pub(crate) fn init(_: &Value) -> (&str, Value) {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");
(Self::NAME, Value::from(f64::INFINITY))
}
}

19
boa/src/builtins/infinity/tests.rs

@ -0,0 +1,19 @@
use crate::exec;
#[test]
fn infinity_exists_on_global_object_and_evaluates_to_infinity_value() {
let scenario = r#"
Infinity;
"#;
assert_eq!(&exec(scenario), "Infinity");
}
#[test]
fn infinity_exists_and_equals_to_number_positive_infinity_value() {
let scenario = r#"
Number.POSITIVE_INFINITY === Infinity;
"#;
assert_eq!(&exec(scenario), "true");
}

3
boa/src/builtins/mod.rs

@ -7,6 +7,7 @@ pub mod console;
pub mod error;
pub mod function;
pub mod global_this;
pub mod infinity;
pub mod json;
pub mod math;
pub mod nan;
@ -24,6 +25,7 @@ pub(crate) use self::{
boolean::Boolean,
error::{Error, RangeError, TypeError},
global_this::GlobalThis,
infinity::Infinity,
json::Json,
math::Math,
nan::NaN,
@ -57,6 +59,7 @@ pub fn init(global: &Value) {
TypeError::init(global),
// Global properties.
NaN::init(global),
Infinity::init(global),
GlobalThis::init(global),
];

Loading…
Cancel
Save