1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:48:10 +00:00

LibJS: Pass Interpreter& to Value::to_number() et al.

This patch is unfortunately rather large and might make some things feel
bloated, but it is necessary to fix a few flaws in LibJS, primarily
blindly coercing values to numbers without exception checks - i.e.

interpreter.argument(0).to_i32();  // can fail!!!

Some examples where the interpreter would actually crash:

var o = { toString: () => { throw Error() } };
+o;
o - 1;
"foo".charAt(o);
"bar".repeat(o);

To fix this, we now have the following...

to_double(Interpreter&)
to_i32()
to_i32(Interpreter&)
to_size_t()
to_size_t(Interpreter&)

...and a whole lot of exception checking.

There's intentionally no to_double(), use as_double() directly instead.

This way we still can use these convenient utility functions but don't
need to check for exceptions if we are sure the value already is a
number.

Fixes #2267.
This commit is contained in:
Linus Groh 2020-05-18 00:28:00 +01:00 committed by Andreas Kling
parent 1a1394f7a2
commit 476094922b
17 changed files with 491 additions and 187 deletions

View file

@ -64,16 +64,17 @@ Value NumberConstructor::call(Interpreter& interpreter)
{
if (!interpreter.argument_count())
return Value(0);
return interpreter.argument(0).to_number();
return interpreter.argument(0).to_number(interpreter);
}
Value NumberConstructor::construct(Interpreter& interpreter)
{
double number;
if (!interpreter.argument_count())
number = 0;
else
number = interpreter.argument(0).to_number().as_double();
double number = 0;
if (interpreter.argument_count()) {
number = interpreter.argument(0).to_double(interpreter);
if (interpreter.exception())
return {};
}
return NumberObject::create(interpreter.global_object(), number);
}
@ -96,7 +97,7 @@ Value NumberConstructor::is_safe_integer(Interpreter& interpreter)
{
if (!interpreter.argument(0).is_number())
return Value(false);
auto value = interpreter.argument(0).to_number().as_double();
auto value = interpreter.argument(0).as_double();
return Value((int64_t)value == value && value >= MIN_SAFE_INTEGER && value <= MAX_SAFE_INTEGER);
}