mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 13:17:35 +00:00
LibJS: Convert default_number_option() to ThrowCompletionOr
This commit is contained in:
parent
b9c7a629f8
commit
6d3de03549
3 changed files with 20 additions and 18 deletions
|
@ -661,7 +661,7 @@ ThrowCompletionOr<Value> get_option(GlobalObject& global_object, Object const& o
|
||||||
}
|
}
|
||||||
|
|
||||||
// 9.2.14 DefaultNumberOption ( value, minimum, maximum, fallback ), https://tc39.es/ecma402/#sec-defaultnumberoption
|
// 9.2.14 DefaultNumberOption ( value, minimum, maximum, fallback ), https://tc39.es/ecma402/#sec-defaultnumberoption
|
||||||
Optional<int> default_number_option(GlobalObject& global_object, Value value, int minimum, int maximum, Optional<int> fallback)
|
ThrowCompletionOr<Optional<int>> default_number_option(GlobalObject& global_object, Value value, int minimum, int maximum, Optional<int> fallback)
|
||||||
{
|
{
|
||||||
auto& vm = global_object.vm();
|
auto& vm = global_object.vm();
|
||||||
|
|
||||||
|
@ -671,17 +671,15 @@ Optional<int> default_number_option(GlobalObject& global_object, Value value, in
|
||||||
|
|
||||||
// 2. Let value be ? ToNumber(value).
|
// 2. Let value be ? ToNumber(value).
|
||||||
value = value.to_number(global_object);
|
value = value.to_number(global_object);
|
||||||
if (vm.exception())
|
if (auto* exception = vm.exception())
|
||||||
return {};
|
return throw_completion(exception->value());
|
||||||
|
|
||||||
// 3. If value is NaN or less than minimum or greater than maximum, throw a RangeError exception.
|
// 3. If value is NaN or less than minimum or greater than maximum, throw a RangeError exception.
|
||||||
if (value.is_nan() || (value.as_double() < minimum) || (value.as_double() > maximum)) {
|
if (value.is_nan() || (value.as_double() < minimum) || (value.as_double() > maximum))
|
||||||
vm.throw_exception<RangeError>(global_object, ErrorType::IntlNumberIsNaNOrOutOfRange, value, minimum, maximum);
|
return vm.throw_completion<RangeError>(global_object, ErrorType::IntlNumberIsNaNOrOutOfRange, value, minimum, maximum);
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
// 4. Return floor(value).
|
// 4. Return floor(value).
|
||||||
return floor(value.as_double());
|
return { floor(value.as_double()) };
|
||||||
}
|
}
|
||||||
|
|
||||||
// 9.2.15 GetNumberOption ( options, property, minimum, maximum, fallback ), https://tc39.es/ecma402/#sec-getnumberoption
|
// 9.2.15 GetNumberOption ( options, property, minimum, maximum, fallback ), https://tc39.es/ecma402/#sec-getnumberoption
|
||||||
|
@ -696,7 +694,7 @@ Optional<int> get_number_option(GlobalObject& global_object, Object const& optio
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
// 3. Return ? DefaultNumberOption(value, minimum, maximum, fallback).
|
// 3. Return ? DefaultNumberOption(value, minimum, maximum, fallback).
|
||||||
return default_number_option(global_object, value, minimum, maximum, fallback);
|
return TRY_OR_DISCARD(default_number_option(global_object, value, minimum, maximum, move(fallback)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 9.2.16 PartitionPattern ( pattern ), https://tc39.es/ecma402/#sec-partitionpattern
|
// 9.2.16 PartitionPattern ( pattern ), https://tc39.es/ecma402/#sec-partitionpattern
|
||||||
|
|
|
@ -47,7 +47,7 @@ Vector<String> best_fit_supported_locales(Vector<String> const& requested_locale
|
||||||
ThrowCompletionOr<Array*> supported_locales(GlobalObject&, Vector<String> const& requested_locales, Value options);
|
ThrowCompletionOr<Array*> supported_locales(GlobalObject&, Vector<String> const& requested_locales, Value options);
|
||||||
ThrowCompletionOr<Object*> coerce_options_to_object(GlobalObject& global_object, Value options);
|
ThrowCompletionOr<Object*> coerce_options_to_object(GlobalObject& global_object, Value options);
|
||||||
ThrowCompletionOr<Value> get_option(GlobalObject& global_object, Object const& options, PropertyName const& property, Value::Type type, Vector<StringView> const& values, Fallback fallback);
|
ThrowCompletionOr<Value> get_option(GlobalObject& global_object, Object const& options, PropertyName const& property, Value::Type type, Vector<StringView> const& values, Fallback fallback);
|
||||||
Optional<int> default_number_option(GlobalObject& global_object, Value value, int minimum, int maximum, Optional<int> fallback);
|
ThrowCompletionOr<Optional<int>> default_number_option(GlobalObject& global_object, Value value, int minimum, int maximum, Optional<int> fallback);
|
||||||
Optional<int> get_number_option(GlobalObject& global_object, Object const& options, PropertyName const& property, int minimum, int maximum, Optional<int> fallback);
|
Optional<int> get_number_option(GlobalObject& global_object, Object const& options, PropertyName const& property, int minimum, int maximum, Optional<int> fallback);
|
||||||
Vector<PatternPartition> partition_pattern(StringView pattern);
|
Vector<PatternPartition> partition_pattern(StringView pattern);
|
||||||
|
|
||||||
|
|
|
@ -282,14 +282,16 @@ void set_number_format_digit_options(GlobalObject& global_object, NumberFormat&
|
||||||
intl_object.set_rounding_type(NumberFormat::RoundingType::SignificantDigits);
|
intl_object.set_rounding_type(NumberFormat::RoundingType::SignificantDigits);
|
||||||
|
|
||||||
// b. Let mnsd be ? DefaultNumberOption(mnsd, 1, 21, 1).
|
// b. Let mnsd be ? DefaultNumberOption(mnsd, 1, 21, 1).
|
||||||
auto min_digits = default_number_option(global_object, min_significant_digits, 1, 21, 1);
|
auto min_digits_or_error = default_number_option(global_object, min_significant_digits, 1, 21, 1);
|
||||||
if (vm.exception())
|
if (min_digits_or_error.is_error())
|
||||||
return;
|
return;
|
||||||
|
auto min_digits = min_digits_or_error.release_value();
|
||||||
|
|
||||||
// c. Let mxsd be ? DefaultNumberOption(mxsd, mnsd, 21, 21).
|
// c. Let mxsd be ? DefaultNumberOption(mxsd, mnsd, 21, 21).
|
||||||
auto max_digits = default_number_option(global_object, max_significant_digits, *min_digits, 21, 21);
|
auto max_digits_or_error = default_number_option(global_object, max_significant_digits, *min_digits, 21, 21);
|
||||||
if (vm.exception())
|
if (max_digits_or_error.is_error())
|
||||||
return;
|
return;
|
||||||
|
auto max_digits = max_digits_or_error.release_value();
|
||||||
|
|
||||||
// d. Set intlObj.[[MinimumSignificantDigits]] to mnsd.
|
// d. Set intlObj.[[MinimumSignificantDigits]] to mnsd.
|
||||||
intl_object.set_min_significant_digits(*min_digits);
|
intl_object.set_min_significant_digits(*min_digits);
|
||||||
|
@ -303,14 +305,16 @@ void set_number_format_digit_options(GlobalObject& global_object, NumberFormat&
|
||||||
intl_object.set_rounding_type(NumberFormat::RoundingType::FractionDigits);
|
intl_object.set_rounding_type(NumberFormat::RoundingType::FractionDigits);
|
||||||
|
|
||||||
// b. Let mnfd be ? DefaultNumberOption(mnfd, 0, 20, undefined).
|
// b. Let mnfd be ? DefaultNumberOption(mnfd, 0, 20, undefined).
|
||||||
auto min_digits = default_number_option(global_object, min_fraction_digits, 0, 20, {});
|
auto min_digits_or_error = default_number_option(global_object, min_fraction_digits, 0, 20, {});
|
||||||
if (vm.exception())
|
if (min_digits_or_error.is_error())
|
||||||
return;
|
return;
|
||||||
|
auto min_digits = min_digits_or_error.release_value();
|
||||||
|
|
||||||
// c. Let mxfd be ? DefaultNumberOption(mxfd, 0, 20, undefined).
|
// c. Let mxfd be ? DefaultNumberOption(mxfd, 0, 20, undefined).
|
||||||
auto max_digits = default_number_option(global_object, max_fraction_digits, 0, 20, {});
|
auto max_digits_or_error = default_number_option(global_object, max_fraction_digits, 0, 20, {});
|
||||||
if (vm.exception())
|
if (max_digits_or_error.is_error())
|
||||||
return;
|
return;
|
||||||
|
auto max_digits = max_digits_or_error.release_value();
|
||||||
|
|
||||||
// d. If mnfd is undefined, set mnfd to min(mnfdDefault, mxfd).
|
// d. If mnfd is undefined, set mnfd to min(mnfdDefault, mxfd).
|
||||||
if (!min_digits.has_value()) {
|
if (!min_digits.has_value()) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue