mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:37:37 +00:00
LibJS: Handle Infinity in Value::to_number()
This commit is contained in:
parent
477bacddad
commit
f226746394
3 changed files with 17 additions and 0 deletions
|
@ -136,9 +136,14 @@ Value Value::to_number() const
|
||||||
case Type::Null:
|
case Type::Null:
|
||||||
return Value(0);
|
return Value(0);
|
||||||
case Type::String: {
|
case Type::String: {
|
||||||
|
// FIXME: Trim whitespace beforehand
|
||||||
auto& string = as_string()->string();
|
auto& string = as_string()->string();
|
||||||
if (string.is_empty())
|
if (string.is_empty())
|
||||||
return Value(0);
|
return Value(0);
|
||||||
|
if (string == "Infinity" || string == "+Infinity")
|
||||||
|
return js_infinity();
|
||||||
|
if (string == "-Infinity")
|
||||||
|
return Value(-js_infinity().as_double());
|
||||||
bool ok;
|
bool ok;
|
||||||
//FIXME: Parse in a better way
|
//FIXME: Parse in a better way
|
||||||
auto parsed_int = string.to_int(ok);
|
auto parsed_int = string.to_int(ok);
|
||||||
|
|
|
@ -10,6 +10,12 @@ try {
|
||||||
assert(new Number(null).valueOf() === 0);
|
assert(new Number(null).valueOf() === 0);
|
||||||
assert(Number(true) === 1);
|
assert(Number(true) === 1);
|
||||||
assert(new Number(true).valueOf() === 1);
|
assert(new Number(true).valueOf() === 1);
|
||||||
|
assert(Number("Infinity") === Infinity);
|
||||||
|
assert(new Number("Infinity").valueOf() === Infinity);
|
||||||
|
assert(Number("+Infinity") === Infinity);
|
||||||
|
assert(new Number("+Infinity").valueOf() === Infinity);
|
||||||
|
assert(Number("-Infinity") === -Infinity);
|
||||||
|
assert(new Number("-Infinity").valueOf() === -Infinity);
|
||||||
assert(isNaN(Number(undefined)));
|
assert(isNaN(Number(undefined)));
|
||||||
assert(isNaN(new Number(undefined).valueOf()));
|
assert(isNaN(new Number(undefined).valueOf()));
|
||||||
assert(isNaN(Number({})));
|
assert(isNaN(Number({})));
|
||||||
|
|
|
@ -22,6 +22,12 @@ try {
|
||||||
// FIXME: returns NaN
|
// FIXME: returns NaN
|
||||||
// assert(+"1.23" === 1.23)
|
// assert(+"1.23" === 1.23)
|
||||||
// assert(-"1.23" === -1.23)
|
// assert(-"1.23" === -1.23)
|
||||||
|
assert(+"Infinity" === Infinity);
|
||||||
|
assert(+"+Infinity" === Infinity);
|
||||||
|
assert(+"-Infinity" === -Infinity);
|
||||||
|
assert(-"Infinity" === -Infinity);
|
||||||
|
assert(-"+Infinity" === -Infinity);
|
||||||
|
assert(-"-Infinity" === Infinity);
|
||||||
|
|
||||||
assert(isNaN(+undefined));
|
assert(isNaN(+undefined));
|
||||||
assert(isNaN(-undefined));
|
assert(isNaN(-undefined));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue