mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:17:44 +00:00
LibJS: Handle empty strings and arrays in Value::to_number()
- An empty string is converted to 0 - An empty array is converted to 0 - An array with one item is converted to that item's numeric value - An array with more than one item is converted to NaN
This commit is contained in:
parent
2d3634d5f5
commit
bad0556a59
2 changed files with 12 additions and 14 deletions
|
@ -27,7 +27,6 @@
|
||||||
#include <AK/FlyString.h>
|
#include <AK/FlyString.h>
|
||||||
#include <AK/Function.h>
|
#include <AK/Function.h>
|
||||||
#include <LibJS/Interpreter.h>
|
#include <LibJS/Interpreter.h>
|
||||||
#include <LibJS/Runtime/Array.h>
|
|
||||||
#include <LibJS/Runtime/MathObject.h>
|
#include <LibJS/Runtime/MathObject.h>
|
||||||
|
|
||||||
namespace JS {
|
namespace JS {
|
||||||
|
@ -47,18 +46,7 @@ Value MathObject::abs(Interpreter& interpreter)
|
||||||
if (interpreter.call_frame().arguments.is_empty())
|
if (interpreter.call_frame().arguments.is_empty())
|
||||||
return js_nan();
|
return js_nan();
|
||||||
|
|
||||||
auto argument = interpreter.call_frame().arguments[0];
|
auto number = interpreter.call_frame().arguments[0].to_number();
|
||||||
|
|
||||||
if (argument.is_array()) {
|
|
||||||
auto& array = *static_cast<const Array*>(argument.as_object());
|
|
||||||
if (array.length() == 0)
|
|
||||||
return Value(0);
|
|
||||||
if (array.length() > 1)
|
|
||||||
return js_nan();
|
|
||||||
argument = array.elements()[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
auto number = argument.to_number();
|
|
||||||
if (number.is_nan())
|
if (number.is_nan())
|
||||||
return js_nan();
|
return js_nan();
|
||||||
return Value(number.as_double() >= 0 ? number.as_double() : -number.as_double());
|
return Value(number.as_double() >= 0 ? number.as_double() : -number.as_double());
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <LibJS/Heap/Heap.h>
|
#include <LibJS/Heap/Heap.h>
|
||||||
#include <LibJS/Interpreter.h>
|
#include <LibJS/Interpreter.h>
|
||||||
|
#include <LibJS/Runtime/Array.h>
|
||||||
#include <LibJS/Runtime/Error.h>
|
#include <LibJS/Runtime/Error.h>
|
||||||
#include <LibJS/Runtime/Object.h>
|
#include <LibJS/Runtime/Object.h>
|
||||||
#include <LibJS/Runtime/PrimitiveString.h>
|
#include <LibJS/Runtime/PrimitiveString.h>
|
||||||
|
@ -128,7 +129,16 @@ Value Value::to_number() const
|
||||||
case Type::Undefined:
|
case Type::Undefined:
|
||||||
return js_nan();
|
return js_nan();
|
||||||
case Type::Object:
|
case Type::Object:
|
||||||
return m_value.as_object->to_primitive(Object::PreferredType::Number).to_number();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue