1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 15:57:35 +00:00

LibJS: Add the Number.prototype.toFixed method

This commit is contained in:
Idan Horowitz 2021-06-19 17:02:42 +03:00 committed by Linus Groh
parent 5e53a690ac
commit c31392510a
5 changed files with 79 additions and 0 deletions

View file

@ -297,6 +297,7 @@ namespace JS {
P(test) \
P(then) \
P(toDateString) \
P(toFixed) \
P(toGMTString) \
P(toISOString) \
P(toJSON) \

View file

@ -32,6 +32,7 @@
M(InstanceOfOperatorBadPrototype, "'prototype' property of {} is not an object") \
M(InvalidAssignToConst, "Invalid assignment to const variable") \
M(InvalidCodePoint, "Invalid code point {}, must be an integer no less than 0 and no greater than 0x10FFFF") \
M(InvalidFractionDigits, "Fraction Digits must be an integer no less than 0, and no greater than 100") \
M(InvalidHint, "Invalid hint: \"{}\"") \
M(InvalidIndex, "Index must be a positive integer") \
M(InvalidLeftHandAssignment, "Invalid left-hand side in assignment") \

View file

@ -34,6 +34,7 @@ void NumberPrototype::initialize(GlobalObject& object)
auto& vm = this->vm();
Object::initialize(object);
u8 attr = Attribute::Configurable | Attribute::Writable;
define_native_function(vm.names.toFixed, to_fixed, 1, attr);
define_native_function(vm.names.toString, to_string, 1, attr);
define_native_function(vm.names.valueOf, value_of, 0, attr);
}
@ -54,6 +55,37 @@ static Value this_number_value(GlobalObject& global_object, Value value)
return {};
}
// 21.1.3.3 Number.prototype.toFixed ( fractionDigits ), https://tc39.es/ecma262/#sec-number.prototype.tofixed
JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_fixed)
{
auto number_value = this_number_value(global_object, vm.this_value(global_object));
if (vm.exception())
return {};
auto fraction_digits = vm.argument(0).to_integer_or_infinity(global_object);
if (vm.exception())
return {};
if (!vm.argument(0).is_finite_number()) {
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidFractionDigits);
return {};
}
if (fraction_digits < 0 || fraction_digits > 100) {
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidFractionDigits);
return {};
}
if (!number_value.is_finite_number())
return js_string(vm, number_value.to_string(global_object));
auto number = number_value.as_double();
if (fabs(number) >= 1e+21)
return js_string(vm, number_value.to_string(global_object));
return js_string(vm, String::formatted("{:0.{1}}", number, static_cast<size_t>(fraction_digits)));
}
// 21.1.3.6 Number.prototype.toString ( [ radix ] ), https://tc39.es/ecma262/#sec-number.prototype.tostring
JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_string)
{

View file

@ -18,6 +18,7 @@ public:
virtual void initialize(GlobalObject&) override;
virtual ~NumberPrototype() override;
JS_DECLARE_NATIVE_FUNCTION(to_fixed);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(value_of);
};