mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 12:05:15 +00:00
LibJS: Handle Infinity in Value::to_number()
This commit is contained in:
parent
477bacddad
commit
f226746394
Notes:
sideshowbarker
2024-07-19 07:41:11 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/f2267463943 Pull-request: https://github.com/SerenityOS/serenity/pull/1757
3 changed files with 17 additions and 0 deletions
|
@ -136,9 +136,14 @@ Value Value::to_number() const
|
|||
case Type::Null:
|
||||
return Value(0);
|
||||
case Type::String: {
|
||||
// FIXME: Trim whitespace beforehand
|
||||
auto& string = as_string()->string();
|
||||
if (string.is_empty())
|
||||
return Value(0);
|
||||
if (string == "Infinity" || string == "+Infinity")
|
||||
return js_infinity();
|
||||
if (string == "-Infinity")
|
||||
return Value(-js_infinity().as_double());
|
||||
bool ok;
|
||||
//FIXME: Parse in a better way
|
||||
auto parsed_int = string.to_int(ok);
|
||||
|
|
|
@ -10,6 +10,12 @@ try {
|
|||
assert(new Number(null).valueOf() === 0);
|
||||
assert(Number(true) === 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(new Number(undefined).valueOf()));
|
||||
assert(isNaN(Number({})));
|
||||
|
|
|
@ -22,6 +22,12 @@ try {
|
|||
// FIXME: returns NaN
|
||||
// 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));
|
||||
|
|
Loading…
Add table
Reference in a new issue