diff options
author | Linus Groh <mail@linusgroh.de> | 2020-04-28 23:58:23 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-29 01:30:59 +0200 |
commit | da0ab16f011bec0c4da862db80dc53c2122e04f2 (patch) | |
tree | ed378661f204e5246c1e91b8beb09c5f0e38d35a /Libraries/LibJS/Runtime/Value.cpp | |
parent | 6d6cd64689097260e376e5f7df0234dbcdb9fc0b (diff) | |
download | serenity-da0ab16f011bec0c4da862db80dc53c2122e04f2.zip |
LibJS: Don't handle arrays separately in Value::to_number()
Now that Array.prototype.join() is producing the correct results we
can remove the separate code path for arrays in Value::to_number()
and treat them like all other objects - using to_primitive() with
number as the preferred type and then calling to_number() on the
result.
This is how the spec descibes it.
This also means we don't crash anymore when trying to coerce
[<empty>] to a number - it now does the following:
[<empty>] - to string - "" - to number - 0
[<empty>, <empty>] - to string - "," - to number - NaN
Diffstat (limited to 'Libraries/LibJS/Runtime/Value.cpp')
-rw-r--r-- | Libraries/LibJS/Runtime/Value.cpp | 11 |
1 files changed, 1 insertions, 10 deletions
diff --git a/Libraries/LibJS/Runtime/Value.cpp b/Libraries/LibJS/Runtime/Value.cpp index e2457d5ce4..d0998e4ec1 100644 --- a/Libraries/LibJS/Runtime/Value.cpp +++ b/Libraries/LibJS/Runtime/Value.cpp @@ -162,16 +162,7 @@ Value Value::to_number() const case Type::Undefined: return js_nan(); case Type::Object: - if (m_value.as_object->is_array()) { - auto& array = *static_cast<Array*>(m_value.as_object); - if (array.length() == 0) - return Value(0); - if (array.length() > 1) - return js_nan(); - return array.elements()[0].to_number(); - } else { - return m_value.as_object->to_primitive(Object::PreferredType::Number).to_number(); - } + return m_value.as_object->to_primitive(Object::PreferredType::Number).to_number(); } ASSERT_NOT_REACHED(); |