mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:07:36 +00:00
LibJS: Convert this_number_value() to ThrowCompletionOr
This commit is contained in:
parent
f6cf44c3db
commit
233bb89929
1 changed files with 6 additions and 12 deletions
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
#include <AK/Function.h>
|
#include <AK/Function.h>
|
||||||
#include <AK/TypeCasts.h>
|
#include <AK/TypeCasts.h>
|
||||||
|
#include <LibJS/Runtime/Completion.h>
|
||||||
#include <LibJS/Runtime/Error.h>
|
#include <LibJS/Runtime/Error.h>
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
#include <LibJS/Runtime/NumberObject.h>
|
#include <LibJS/Runtime/NumberObject.h>
|
||||||
|
@ -45,24 +46,20 @@ NumberPrototype::~NumberPrototype()
|
||||||
}
|
}
|
||||||
|
|
||||||
// thisNumberValue ( value ), https://tc39.es/ecma262/#thisnumbervalue
|
// thisNumberValue ( value ), https://tc39.es/ecma262/#thisnumbervalue
|
||||||
static Value this_number_value(GlobalObject& global_object, Value value)
|
static ThrowCompletionOr<Value> this_number_value(GlobalObject& global_object, Value value)
|
||||||
{
|
{
|
||||||
if (value.is_number())
|
if (value.is_number())
|
||||||
return value;
|
return value;
|
||||||
if (value.is_object() && is<NumberObject>(value.as_object()))
|
if (value.is_object() && is<NumberObject>(value.as_object()))
|
||||||
return static_cast<NumberObject&>(value.as_object()).value_of();
|
return static_cast<NumberObject&>(value.as_object()).value_of();
|
||||||
auto& vm = global_object.vm();
|
auto& vm = global_object.vm();
|
||||||
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObjectOfType, "Number");
|
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObjectOfType, "Number");
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 21.1.3.3 Number.prototype.toFixed ( fractionDigits ), https://tc39.es/ecma262/#sec-number.prototype.tofixed
|
// 21.1.3.3 Number.prototype.toFixed ( fractionDigits ), https://tc39.es/ecma262/#sec-number.prototype.tofixed
|
||||||
JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_fixed)
|
JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_fixed)
|
||||||
{
|
{
|
||||||
auto number_value = this_number_value(global_object, vm.this_value(global_object));
|
auto number_value = TRY_OR_DISCARD(this_number_value(global_object, vm.this_value(global_object)));
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
|
|
||||||
auto fraction_digits = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object));
|
auto fraction_digits = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object));
|
||||||
if (!vm.argument(0).is_finite_number()) {
|
if (!vm.argument(0).is_finite_number()) {
|
||||||
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidFractionDigits);
|
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidFractionDigits);
|
||||||
|
@ -87,10 +84,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_fixed)
|
||||||
// 21.1.3.6 Number.prototype.toString ( [ radix ] ), https://tc39.es/ecma262/#sec-number.prototype.tostring
|
// 21.1.3.6 Number.prototype.toString ( [ radix ] ), https://tc39.es/ecma262/#sec-number.prototype.tostring
|
||||||
JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_string)
|
JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_string)
|
||||||
{
|
{
|
||||||
auto number_value = this_number_value(global_object, vm.this_value(global_object));
|
auto number_value = TRY_OR_DISCARD(this_number_value(global_object, vm.this_value(global_object)));
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
|
|
||||||
double radix_argument = 10;
|
double radix_argument = 10;
|
||||||
auto argument = vm.argument(0);
|
auto argument = vm.argument(0);
|
||||||
if (!vm.argument(0).is_undefined())
|
if (!vm.argument(0).is_undefined())
|
||||||
|
@ -162,7 +156,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_string)
|
||||||
// 21.1.3.7 Number.prototype.valueOf ( ), https://tc39.es/ecma262/#sec-number.prototype.valueof
|
// 21.1.3.7 Number.prototype.valueOf ( ), https://tc39.es/ecma262/#sec-number.prototype.valueof
|
||||||
JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::value_of)
|
JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::value_of)
|
||||||
{
|
{
|
||||||
return this_number_value(global_object, vm.this_value(global_object));
|
return TRY_OR_DISCARD(this_number_value(global_object, vm.this_value(global_object)));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue