LibJS: Do not generate a format string in Number.prototype.toFixed

We can use the placeholder syntax to specify the precision dynamically.
Note that `fraction_digits` is a double, which we do not support as a
precision argument. It's safe to cast to an integer here because we
guaranteed above that the value is in the range [0, 100], and is not
fractional.
This commit is contained in:
Timothy Flynn 2025-04-08 13:39:21 -04:00 committed by Tim Flynn
parent 64d290447c
commit d5222e9bba
Notes: github-actions[bot] 2025-04-09 00:01:25 +00:00

View file

@ -256,8 +256,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_fixed)
// `number` double. Instead of generating a huge, unwieldy `n`, we format
// the double using our existing formatting code.
auto number_format_string = ByteString::formatted("{{}}{{:.{}f}}", fraction_digits);
return PrimitiveString::create(vm, ByteString::formatted(number_format_string, s, number));
return PrimitiveString::create(vm, MUST(String::formatted("{}{:.{}f}", s, number, static_cast<u32>(fraction_digits))));
}
// 20.2.1 Number.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sup-number.prototype.tolocalestring