diff options
author | Andreas Kling <kling@serenityos.org> | 2020-03-27 12:26:37 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-03-27 12:40:22 +0100 |
commit | 9a78b4af2c3a9a77810dfb960ed8c2cca1870809 (patch) | |
tree | d1a415833979835fcfbef4bd499ec95028a06f0c | |
parent | 23c5323a7096e85a7ca73b746e6f38ec0e51b4e5 (diff) | |
download | serenity-9a78b4af2c3a9a77810dfb960ed8c2cca1870809.zip |
LibJS: Basic NaN support
This patch adds js_nan() for constructing a NaN value. You can check
if a Value is NaN with Value::is_nan().
-rw-r--r-- | Libraries/LibJS/Runtime/Value.cpp | 14 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Value.h | 7 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/NaN-basic.js | 9 |
3 files changed, 23 insertions, 7 deletions
diff --git a/Libraries/LibJS/Runtime/Value.cpp b/Libraries/LibJS/Runtime/Value.cpp index 675a938f14..66582e4622 100644 --- a/Libraries/LibJS/Runtime/Value.cpp +++ b/Libraries/LibJS/Runtime/Value.cpp @@ -49,9 +49,13 @@ String Value::to_string() const if (is_undefined()) return "undefined"; - if (is_number()) + if (is_number()) { + if (is_nan()) + return "NaN"; + // FIXME: This needs improvement. return String::number((i32)as_double()); + } if (is_object()) return as_object()->to_primitive(Object::PreferredType::String).to_string(); @@ -108,14 +112,10 @@ Value Value::to_number() const if (ok) return Value(parsed_int); - //FIXME: Implement 'NaN' - ASSERT_NOT_REACHED(); - - break; + return js_nan(); } case Type::Undefined: - //FIXME: Implement 'NaN' - ASSERT_NOT_REACHED(); + return js_nan(); case Type::Object: return m_value.as_object->to_primitive(Object::PreferredType::Number).to_number(); } diff --git a/Libraries/LibJS/Runtime/Value.h b/Libraries/LibJS/Runtime/Value.h index 5dfe600101..8e2a1daeaf 100644 --- a/Libraries/LibJS/Runtime/Value.h +++ b/Libraries/LibJS/Runtime/Value.h @@ -53,6 +53,8 @@ public: bool is_cell() const { return is_string() || is_object(); } bool is_array() const; + bool is_nan() const { return is_number() && __builtin_isnan( as_double()); } + Value() : m_type(Type::Undefined) { @@ -166,6 +168,11 @@ inline Value js_null() return Value(Value::Type::Null); } +inline Value js_nan() +{ + return Value(__builtin_nan("")); +} + Value greater_than(Value lhs, Value rhs); Value greater_than_equals(Value lhs, Value rhs); Value less_than(Value lhs, Value rhs); diff --git a/Libraries/LibJS/Tests/NaN-basic.js b/Libraries/LibJS/Tests/NaN-basic.js new file mode 100644 index 0000000000..1f849beefe --- /dev/null +++ b/Libraries/LibJS/Tests/NaN-basic.js @@ -0,0 +1,9 @@ +function assert(x) { if (!x) throw 1; } + +try { + var nan = undefined + 1; + assert(nan + "" == "NaN"); + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} |