mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 01:17:35 +00:00
LibJS: Convert get_string_or_number_option() to ThrowCompletionOr
This commit is contained in:
parent
8d6ac267dc
commit
b1e7e62657
2 changed files with 13 additions and 19 deletions
|
@ -171,38 +171,34 @@ ThrowCompletionOr<Value> get_option(GlobalObject& global_object, Object const& o
|
||||||
|
|
||||||
// 13.4 GetStringOrNumberOption ( options, property, stringValues, minimum, maximum, fallback ), https://tc39.es/proposal-temporal/#sec-getstringornumberoption
|
// 13.4 GetStringOrNumberOption ( options, property, stringValues, minimum, maximum, fallback ), https://tc39.es/proposal-temporal/#sec-getstringornumberoption
|
||||||
template<typename NumberType>
|
template<typename NumberType>
|
||||||
Optional<Variant<String, NumberType>> get_string_or_number_option(GlobalObject& global_object, Object const& options, PropertyName const& property, Vector<StringView> const& string_values, NumberType minimum, NumberType maximum, Value fallback)
|
ThrowCompletionOr<Variant<String, NumberType>> get_string_or_number_option(GlobalObject& global_object, Object const& options, PropertyName const& property, Vector<StringView> const& string_values, NumberType minimum, NumberType maximum, Value fallback)
|
||||||
{
|
{
|
||||||
auto& vm = global_object.vm();
|
auto& vm = global_object.vm();
|
||||||
|
|
||||||
// 1. Assert: Type(options) is Object.
|
// 1. Assert: Type(options) is Object.
|
||||||
|
|
||||||
// 2. Let value be ? GetOption(options, property, « Number, String », empty, fallback).
|
// 2. Let value be ? GetOption(options, property, « Number, String », empty, fallback).
|
||||||
auto value = TRY_OR_DISCARD(get_option(global_object, options, property, { OptionType::Number, OptionType::String }, {}, fallback));
|
auto value = TRY(get_option(global_object, options, property, { OptionType::Number, OptionType::String }, {}, fallback));
|
||||||
|
|
||||||
// 3. If Type(value) is Number, then
|
// 3. If Type(value) is Number, then
|
||||||
if (value.is_number()) {
|
if (value.is_number()) {
|
||||||
// a. If value < minimum or value > maximum, throw a RangeError exception.
|
// a. If value < minimum or value > maximum, throw a RangeError exception.
|
||||||
if (value.as_double() < minimum || value.as_double() > maximum) {
|
if (value.as_double() < minimum || value.as_double() > maximum)
|
||||||
vm.template throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, value.as_double(), property.as_string());
|
return vm.template throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, value.as_double(), property.as_string());
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
// b. Return floor(ℝ(value)).
|
// b. Return floor(ℝ(value)).
|
||||||
return floor(value.as_double());
|
return { static_cast<NumberType>(floor(value.as_double())) };
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Assert: Type(value) is String.
|
// 4. Assert: Type(value) is String.
|
||||||
VERIFY(value.is_string());
|
VERIFY(value.is_string());
|
||||||
|
|
||||||
// 5. If stringValues does not contain value, throw a RangeError exception.
|
// 5. If stringValues does not contain value, throw a RangeError exception.
|
||||||
if (!string_values.contains_slow(value.as_string().string())) {
|
if (!string_values.contains_slow(value.as_string().string()))
|
||||||
vm.template throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, value.as_string().string(), property.as_string());
|
return vm.template throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, value.as_string().string(), property.as_string());
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
// 6. Return value.
|
// 6. Return value.
|
||||||
return value.as_string().string();
|
return { value.as_string().string() };
|
||||||
}
|
}
|
||||||
|
|
||||||
// 13.6 ToTemporalOverflow ( normalizedOptions ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaloverflow
|
// 13.6 ToTemporalOverflow ( normalizedOptions ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaloverflow
|
||||||
|
@ -337,18 +333,16 @@ Optional<SecondsStringPrecision> to_seconds_string_precision(GlobalObject& globa
|
||||||
VERIFY(!smallest_unit.has_value());
|
VERIFY(!smallest_unit.has_value());
|
||||||
|
|
||||||
// 8. Let digits be ? GetStringOrNumberOption(normalizedOptions, "fractionalSecondDigits", « "auto" », 0, 9, "auto").
|
// 8. Let digits be ? GetStringOrNumberOption(normalizedOptions, "fractionalSecondDigits", « "auto" », 0, 9, "auto").
|
||||||
auto digits_variant = get_string_or_number_option<u8>(global_object, normalized_options, vm.names.fractionalSecondDigits, { "auto"sv }, 0, 9, js_string(vm, "auto"sv));
|
auto digits_variant = TRY_OR_DISCARD(get_string_or_number_option<u8>(global_object, normalized_options, vm.names.fractionalSecondDigits, { "auto"sv }, 0, 9, js_string(vm, "auto"sv)));
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
|
|
||||||
// 9. If digits is "auto", then
|
// 9. If digits is "auto", then
|
||||||
if (digits_variant->has<String>()) {
|
if (digits_variant.has<String>()) {
|
||||||
VERIFY(digits_variant->get<String>() == "auto"sv);
|
VERIFY(digits_variant.get<String>() == "auto"sv);
|
||||||
// a. Return the Record { [[Precision]]: "auto", [[Unit]]: "nanosecond", [[Increment]]: 1 }.
|
// a. Return the Record { [[Precision]]: "auto", [[Unit]]: "nanosecond", [[Increment]]: 1 }.
|
||||||
return SecondsStringPrecision { .precision = "auto"sv, .unit = "nanosecond"sv, .increment = 1 };
|
return SecondsStringPrecision { .precision = "auto"sv, .unit = "nanosecond"sv, .increment = 1 };
|
||||||
}
|
}
|
||||||
|
|
||||||
auto digits = digits_variant->get<u8>();
|
auto digits = digits_variant.get<u8>();
|
||||||
|
|
||||||
// 10. If digits is 0, then
|
// 10. If digits is 0, then
|
||||||
if (digits == 0) {
|
if (digits == 0) {
|
||||||
|
|
|
@ -88,7 +88,7 @@ ThrowCompletionOr<MarkedValueList> iterable_to_list_of_type(GlobalObject&, Value
|
||||||
ThrowCompletionOr<Object*> get_options_object(GlobalObject&, Value options);
|
ThrowCompletionOr<Object*> get_options_object(GlobalObject&, Value options);
|
||||||
ThrowCompletionOr<Value> get_option(GlobalObject&, Object const& options, PropertyName const& property, Vector<OptionType> const& types, Vector<StringView> const& values, Value fallback);
|
ThrowCompletionOr<Value> get_option(GlobalObject&, Object const& options, PropertyName const& property, Vector<OptionType> const& types, Vector<StringView> const& values, Value fallback);
|
||||||
template<typename NumberType>
|
template<typename NumberType>
|
||||||
Optional<Variant<String, NumberType>> get_string_or_number_option(GlobalObject&, Object const& options, PropertyName const& property, Vector<StringView> const& string_values, NumberType minimum, NumberType maximum, Value fallback);
|
ThrowCompletionOr<Variant<String, NumberType>> get_string_or_number_option(GlobalObject&, Object const& options, PropertyName const& property, Vector<StringView> const& string_values, NumberType minimum, NumberType maximum, Value fallback);
|
||||||
Optional<String> to_temporal_overflow(GlobalObject&, Object const& normalized_options);
|
Optional<String> to_temporal_overflow(GlobalObject&, Object const& normalized_options);
|
||||||
Optional<String> to_temporal_rounding_mode(GlobalObject&, Object const& normalized_options, String const& fallback);
|
Optional<String> to_temporal_rounding_mode(GlobalObject&, Object const& normalized_options, String const& fallback);
|
||||||
Optional<String> to_show_calendar_option(GlobalObject&, Object const& normalized_options);
|
Optional<String> to_show_calendar_option(GlobalObject&, Object const& normalized_options);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue