Skip to content

Commit

Permalink
add Infinity gloabal property (boa-dev#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnirudhKonduru committed Jun 16, 2020
1 parent df13272 commit 5c324a9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
33 changes: 33 additions & 0 deletions boa/src/builtins/infinity/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//! This module implements the global `Infinity` property.
//!
//! The global `Infinity` is a property of the global object. In other words,
//! it is a variable in global scope.
//!
//! 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 `NaN` 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))
}
}
10 changes: 10 additions & 0 deletions boa/src/builtins/infinity/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::exec;

#[test]
fn infinity_exists_on_global_object_and_evaluates_to_infinity_value() {
let scenario = r#"
Infinity;
"#;

assert_eq!(&exec(scenario), "Infinity");
}
3 changes: 3 additions & 0 deletions boa/src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -57,6 +59,7 @@ pub fn init(global: &Value) {
TypeError::init(global),
// Global properties.
NaN::init(global),
Infinity::init(global),
GlobalThis::init(global),
];

Expand Down

0 comments on commit 5c324a9

Please sign in to comment.