From 5a45ab532e61cf3e1ec9d6a87d407569323f3afb Mon Sep 17 00:00:00 2001 From: croraf Date: Thu, 11 Jun 2020 18:49:08 +0200 Subject: [PATCH] [NaN] handle NaN token as identifier (#475) --- boa/src/builtins/array/tests.rs | 11 +++++------ boa/src/builtins/mod.rs | 2 ++ boa/src/builtins/nan/mod.rs | 11 +++++++++++ boa/src/builtins/nan/tests.rs | 10 ++++++++++ boa/src/syntax/lexer/mod.rs | 1 - boa/src/syntax/lexer/tests.rs | 13 ------------- 6 files changed, 28 insertions(+), 20 deletions(-) create mode 100644 boa/src/builtins/nan/mod.rs create mode 100644 boa/src/builtins/nan/tests.rs diff --git a/boa/src/builtins/array/tests.rs b/boa/src/builtins/array/tests.rs index 3794361817..c828940f03 100644 --- a/boa/src/builtins/array/tests.rs +++ b/boa/src/builtins/array/tests.rs @@ -452,12 +452,11 @@ fn fill() { String::from("4,2,3") ); - // TODO: uncomment when NaN support is added - // forward(&mut engine, "a = [1, 2, 3];"); - // assert_eq!( - // forward(&mut engine, "a.fill(4, NaN, NaN).join()"), - // String::from("1,2,3") - // ); + forward(&mut engine, "a = [1, 2, 3];"); + assert_eq!( + forward(&mut engine, "a.fill(4, NaN, NaN).join()"), + String::from("1,2,3") + ); forward(&mut engine, "a = [1, 2, 3];"); assert_eq!( diff --git a/boa/src/builtins/mod.rs b/boa/src/builtins/mod.rs index ad89b6b33c..4e43b5268f 100644 --- a/boa/src/builtins/mod.rs +++ b/boa/src/builtins/mod.rs @@ -8,6 +8,7 @@ pub mod error; pub mod function; pub mod json; pub mod math; +pub mod nan; pub mod number; pub mod object; pub mod property; @@ -36,6 +37,7 @@ pub fn init(global: &Value) { Boolean::init(global); json::init(global); math::init(global); + nan::init(global); Number::init(global); object::init(global); function::init(global); diff --git a/boa/src/builtins/nan/mod.rs b/boa/src/builtins/nan/mod.rs new file mode 100644 index 0000000000..41ddfcca82 --- /dev/null +++ b/boa/src/builtins/nan/mod.rs @@ -0,0 +1,11 @@ +#[cfg(test)] +mod tests; + +use crate::{builtins::value::Value, BoaProfiler}; + +/// Initialize the `NaN` property on the global object. +#[inline] +pub fn init(global: &Value) { + let _timer = BoaProfiler::global().start_event("NaN", "init"); + global.set_field("NaN", Value::from(f64::NAN)); +} diff --git a/boa/src/builtins/nan/tests.rs b/boa/src/builtins/nan/tests.rs new file mode 100644 index 0000000000..627d694ffc --- /dev/null +++ b/boa/src/builtins/nan/tests.rs @@ -0,0 +1,10 @@ +use crate::exec; + +#[test] +fn nan_exists_on_global_object_and_evaluates_to_nan_value() { + let scenario = r#" + NaN; + "#; + + assert_eq!(&exec(scenario), "NaN"); +} diff --git a/boa/src/syntax/lexer/mod.rs b/boa/src/syntax/lexer/mod.rs index 568b54d526..73976881f8 100644 --- a/boa/src/syntax/lexer/mod.rs +++ b/boa/src/syntax/lexer/mod.rs @@ -660,7 +660,6 @@ impl<'a> Lexer<'a> { "true" => TokenKind::BooleanLiteral(true), "false" => TokenKind::BooleanLiteral(false), "null" => TokenKind::NullLiteral, - "NaN" => TokenKind::NumericLiteral(NumericLiteral::Rational(f64::NAN)), slice => { if let Ok(keyword) = FromStr::from_str(slice) { TokenKind::Keyword(keyword) diff --git a/boa/src/syntax/lexer/tests.rs b/boa/src/syntax/lexer/tests.rs index 4129bb3c17..5571db6a7c 100644 --- a/boa/src/syntax/lexer/tests.rs +++ b/boa/src/syntax/lexer/tests.rs @@ -387,19 +387,6 @@ fn check_decrement_advances_lexer_2_places() { ); } -#[test] -fn check_nan() { - let mut lexer = Lexer::new("let a = NaN;"); - lexer.lex().expect("failed to lex"); - - match lexer.tokens[3].kind { - TokenKind::NumericLiteral(NumericLiteral::Rational(a)) => { - assert!(a.is_nan()); - } - ref other => panic!("Incorrect token kind found for NaN: {}", other), - } -} - #[test] fn numbers() { let mut lexer = Lexer::new(