1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-10 18:10:56 +09:00

LibJS: Stop propagating small OOM errors from Intl.MathematicalValue

This commit is contained in:
Timothy Flynn 2023-08-30 11:55:14 -04:00 committed by Andreas Kling
parent 746ce6f9a1
commit 30a812b77b
Notes: sideshowbarker 2024-07-17 18:08:55 +09:00
3 changed files with 25 additions and 27 deletions

View file

@ -229,18 +229,18 @@ bool MathematicalValue::modulo_is_zero(Checked<i32> mod) const
[](auto) -> bool { VERIFY_NOT_REACHED(); });
}
ThrowCompletionOr<int> MathematicalValue::logarithmic_floor(VM& vm) const
int MathematicalValue::logarithmic_floor() const
{
return m_value.visit(
[](double value) -> ThrowCompletionOr<int> {
[](double value) {
return static_cast<int>(floor(log10(value)));
},
[&](Crypto::SignedBigInteger const& value) -> ThrowCompletionOr<int> {
[&](Crypto::SignedBigInteger const& value) {
// FIXME: Can we do this without string conversion?
auto value_as_string = TRY_OR_THROW_OOM(vm, value.to_base(10));
auto value_as_string = MUST(value.to_base(10));
return static_cast<int>(value_as_string.bytes_as_string_view().length() - 1);
},
[](auto) -> ThrowCompletionOr<int> { VERIFY_NOT_REACHED(); });
[](auto) -> int { VERIFY_NOT_REACHED(); });
}
bool MathematicalValue::is_equal_to(MathematicalValue const& other) const
@ -294,16 +294,16 @@ bool MathematicalValue::is_zero() const
[](auto) { return false; });
}
ThrowCompletionOr<String> MathematicalValue::to_string(VM& vm) const
String MathematicalValue::to_string() const
{
return m_value.visit(
[&](double value) -> ThrowCompletionOr<String> {
[&](double value) {
return number_to_string(value, NumberToStringMode::WithoutExponent);
},
[&](Crypto::SignedBigInteger const& value) -> ThrowCompletionOr<String> {
return TRY_OR_THROW_OOM(vm, value.to_base(10));
[&](Crypto::SignedBigInteger const& value) {
return MUST(value.to_base(10));
},
[&](auto) -> ThrowCompletionOr<String> { VERIFY_NOT_REACHED(); });
[&](auto) -> String { VERIFY_NOT_REACHED(); });
}
Value MathematicalValue::to_value(VM& vm) const

View file

@ -80,7 +80,7 @@ public:
bool modulo_is_zero(Checked<i32> mod) const;
ThrowCompletionOr<int> logarithmic_floor(VM&) const;
int logarithmic_floor() const;
bool is_equal_to(MathematicalValue const&) const;
bool is_less_than(MathematicalValue const&) const;
@ -89,7 +89,7 @@ public:
bool is_positive() const;
bool is_zero() const;
ThrowCompletionOr<String> to_string(VM&) const;
String to_string() const;
Value to_value(VM&) const;
private:

View file

@ -1011,10 +1011,10 @@ struct RawPrecisionResult {
};
// ToRawPrecisionFn, https://tc39.es/ecma402/#eqn-ToRawPrecisionFn
static ThrowCompletionOr<RawPrecisionResult> to_raw_precision_function(VM& vm, MathematicalValue const& number, int precision, PreferredResult mode)
static RawPrecisionResult to_raw_precision_function(MathematicalValue const& number, int precision, PreferredResult mode)
{
RawPrecisionResult result {};
result.exponent = MUST_OR_THROW_OOM(number.logarithmic_floor(vm));
result.exponent = number.logarithmic_floor();
if (number.is_number()) {
result.number = number.divided_by_power(result.exponent - precision + 1);
@ -1033,7 +1033,7 @@ static ThrowCompletionOr<RawPrecisionResult> to_raw_precision_function(VM& vm, M
result.number = number.divided_by_power(result.exponent - precision);
// FIXME: Can we do this without string conversion?
auto digits = MUST_OR_THROW_OOM(result.number.to_string(vm));
auto digits = result.number.to_string();
auto digit = digits.bytes_as_string_view().substring_view(digits.bytes_as_string_view().length() - 1);
result.number = result.number.divided_by(10);
@ -1069,10 +1069,10 @@ ThrowCompletionOr<RawFormatResult> to_raw_precision(VM& vm, MathematicalValue co
// 3. Else,
else {
// a. Let n1 and e1 each be an integer and r1 a mathematical value, with r1 = ToRawPrecisionFn(n1, e1, p), such that r1 ≤ x and r1 is maximized.
auto [number1, exponent1, rounded1] = MUST_OR_THROW_OOM(to_raw_precision_function(vm, number, precision, PreferredResult::LessThanNumber));
auto [number1, exponent1, rounded1] = to_raw_precision_function(number, precision, PreferredResult::LessThanNumber);
// b. Let n2 and e2 each be an integer and r2 a mathematical value, with r2 = ToRawPrecisionFn(n2, e2, p), such that r2 ≥ x and r2 is minimized.
auto [number2, exponent2, rounded2] = MUST_OR_THROW_OOM(to_raw_precision_function(vm, number, precision, PreferredResult::GreaterThanNumber));
auto [number2, exponent2, rounded2] = to_raw_precision_function(number, precision, PreferredResult::GreaterThanNumber);
// c. Let r be ApplyUnsignedRoundingMode(x, r1, r2, unsignedRoundingMode).
auto rounded = apply_unsigned_rounding_mode(number, rounded1, rounded2, unsigned_rounding_mode);
@ -1103,7 +1103,7 @@ ThrowCompletionOr<RawFormatResult> to_raw_precision(VM& vm, MathematicalValue co
}
// f. Let m be the String consisting of the digits of the decimal representation of n (in order, with no leading zeroes).
result.formatted_string = MUST_OR_THROW_OOM(n.to_string(vm));
result.formatted_string = n.to_string();
}
// 4. If e ≥ (p 1), then
@ -1164,7 +1164,7 @@ struct RawFixedResult {
};
// ToRawFixedFn, https://tc39.es/ecma402/#eqn-ToRawFixedFn
static ThrowCompletionOr<RawFixedResult> to_raw_fixed_function(VM& vm, MathematicalValue const& number, int fraction, int rounding_increment, PreferredResult mode)
static RawFixedResult to_raw_fixed_function(MathematicalValue const& number, int fraction, int rounding_increment, PreferredResult mode)
{
RawFixedResult result {};
@ -1185,7 +1185,7 @@ static ThrowCompletionOr<RawFixedResult> to_raw_fixed_function(VM& vm, Mathemati
result.number = number.multiplied_by_power(fraction - 1);
// FIXME: Can we do this without string conversion?
auto digits = MUST_OR_THROW_OOM(result.number.to_string(vm));
auto digits = result.number.to_string();
auto digit = digits.bytes_as_string_view().substring_view(digits.bytes_as_string_view().length() - 1);
result.number = result.number.multiplied_by(10);
@ -1218,10 +1218,10 @@ ThrowCompletionOr<RawFormatResult> to_raw_fixed(VM& vm, MathematicalValue const&
int fraction = max_fraction;
// 2. Let n1 be an integer and r1 a mathematical value, with r1 = ToRawFixedFn(n1, f), such that n1 modulo roundingIncrement = 0, r1 ≤ x, and r1 is maximized.
auto [number1, rounded1] = MUST_OR_THROW_OOM(to_raw_fixed_function(vm, number, fraction, rounding_increment, PreferredResult::LessThanNumber));
auto [number1, rounded1] = to_raw_fixed_function(number, fraction, rounding_increment, PreferredResult::LessThanNumber);
// 3. Let n2 be an integer and r2 a mathematical value, with r2 = ToRawFixedFn(n2, f), such that n2 modulo roundingIncrement = 0, r2 ≥ x, and r2 is minimized.
auto [number2, rounded2] = MUST_OR_THROW_OOM(to_raw_fixed_function(vm, number, fraction, rounding_increment, PreferredResult::GreaterThanNumber));
auto [number2, rounded2] = to_raw_fixed_function(number, fraction, rounding_increment, PreferredResult::GreaterThanNumber);
// 4. Let r be ApplyUnsignedRoundingMode(x, r1, r2, unsignedRoundingMode).
auto rounded = apply_unsigned_rounding_mode(number, rounded1, rounded2, unsigned_rounding_mode);
@ -1246,9 +1246,7 @@ ThrowCompletionOr<RawFormatResult> to_raw_fixed(VM& vm, MathematicalValue const&
}
// 7. If n = 0, let m be "0". Otherwise, let m be the String consisting of the digits of the decimal representation of n (in order, with no leading zeroes).
result.formatted_string = n.is_zero()
? "0"_string
: MUST_OR_THROW_OOM(n.to_string(vm));
result.formatted_string = n.is_zero() ? "0"_string : n.to_string();
// 8. If f ≠ 0, then
if (fraction != 0) {
@ -1573,7 +1571,7 @@ ThrowCompletionOr<int> compute_exponent(VM& vm, NumberFormat& number_format, Mat
}
// 3. Let magnitude be the base 10 logarithm of x rounded down to the nearest integer.
int magnitude = MUST_OR_THROW_OOM(number.logarithmic_floor(vm));
int magnitude = number.logarithmic_floor();
// 4. Let exponent be ComputeExponentForMagnitude(numberFormat, magnitude).
int exponent = compute_exponent_for_magnitude(number_format, magnitude);
@ -1591,7 +1589,7 @@ ThrowCompletionOr<int> compute_exponent(VM& vm, NumberFormat& number_format, Mat
}
// 8. Let newMagnitude be the base 10 logarithm of formatNumberResult.[[RoundedNumber]] rounded down to the nearest integer.
int new_magnitude = MUST_OR_THROW_OOM(format_number_result.rounded_number.logarithmic_floor(vm));
int new_magnitude = format_number_result.rounded_number.logarithmic_floor();
// 9. If newMagnitude is magnitude - exponent, then
if (new_magnitude == magnitude - exponent) {