diff options
6 files changed, 42 insertions, 70 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp index b59ecb60db..a28ee4e9ba 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp @@ -578,9 +578,7 @@ Array* supported_locales(GlobalObject& global_object, Vector<String> const& requ return {}; // 2. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit"). - auto matcher = get_option(global_object, *options_object, vm.names.localeMatcher, Value::Type::String, { "lookup"sv, "best fit"sv }, "best fit"sv); - if (vm.exception()) - return {}; + auto matcher = TRY_OR_DISCARD(get_option(global_object, *options_object, vm.names.localeMatcher, Value::Type::String, { "lookup"sv, "best fit"sv }, "best fit"sv)); Vector<String> supported_locales; @@ -613,15 +611,15 @@ Object* coerce_options_to_object(GlobalObject& global_object, Value options) } // 9.2.13 GetOption ( options, property, type, values, fallback ), https://tc39.es/ecma402/#sec-getoption -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) { auto& vm = global_object.vm(); // 1. Assert: Type(options) is Object. // 2. Let value be ? Get(options, property). auto value = options.get(property); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // 3. If value is undefined, return fallback. if (value.is_undefined()) { @@ -643,18 +641,16 @@ Value get_option(GlobalObject& global_object, Object const& options, PropertyNam else { // a. Let value be ? ToString(value). value = value.to_primitive_string(global_object); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); } // 7. If values is not undefined and values does not contain an element equal to value, throw a RangeError exception. if (!values.is_empty()) { // Note: Every location in the spec that invokes GetOption with type=boolean also has values=undefined. VERIFY(value.is_string()); - if (!values.contains_slow(value.as_string().string())) { - vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, value.to_string_without_side_effects(), property.as_string()); - return {}; - } + if (!values.contains_slow(value.as_string().string())) + return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, value.to_string_without_side_effects(), property.as_string()); } // 8. Return value. diff --git a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h index 7513e9580a..bf837ed8d5 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h @@ -46,7 +46,7 @@ Vector<String> lookup_supported_locales(Vector<String> const& requested_locales) Vector<String> best_fit_supported_locales(Vector<String> const& requested_locales); Array* supported_locales(GlobalObject&, Vector<String> const& requested_locales, Value options); Object* coerce_options_to_object(GlobalObject& global_object, Value options); -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); 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); diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesConstructor.cpp index 5824d44577..6c543ba582 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesConstructor.cpp @@ -74,9 +74,7 @@ Value DisplayNamesConstructor::construct(FunctionObject& new_target) // 7. Let localeData be %DisplayNames%.[[LocaleData]]. // 8. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit"). - auto matcher = get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, { "lookup"sv, "best fit"sv }, "best fit"sv); - if (vm.exception()) - return {}; + auto matcher = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, { "lookup"sv, "best fit"sv }, "best fit"sv)); // 9. Set opt.[[localeMatcher]] to matcher. opt.locale_matcher = matcher; @@ -85,17 +83,13 @@ Value DisplayNamesConstructor::construct(FunctionObject& new_target) auto result = resolve_locale(requested_locales, opt, {}); // 11. Let style be ? GetOption(options, "style", "string", « "narrow", "short", "long" », "long"). - auto style = get_option(global_object, *options, vm.names.style, Value::Type::String, { "narrow"sv, "short"sv, "long"sv }, "long"sv); - if (vm.exception()) - return {}; + auto style = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.style, Value::Type::String, { "narrow"sv, "short"sv, "long"sv }, "long"sv)); // 12. Set displayNames.[[Style]] to style. display_names->set_style(style.as_string().string()); // 13. Let type be ? GetOption(options, "type", "string", « "language", "region", "script", "currency" », undefined). - auto type = get_option(global_object, *options, vm.names.type, Value::Type::String, { "language"sv, "region"sv, "script"sv, "currency"sv }, Empty {}); - if (vm.exception()) - return {}; + auto type = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.type, Value::Type::String, { "language"sv, "region"sv, "script"sv, "currency"sv }, Empty {})); // 14. If type is undefined, throw a TypeError exception. if (type.is_undefined()) { @@ -107,9 +101,7 @@ Value DisplayNamesConstructor::construct(FunctionObject& new_target) display_names->set_type(type.as_string().string()); // 16. Let fallback be ? GetOption(options, "fallback", "string", « "code", "none" », "code"). - auto fallback = get_option(global_object, *options, vm.names.fallback, Value::Type::String, { "code"sv, "none"sv }, "code"sv); - if (vm.exception()) - return {}; + auto fallback = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.fallback, Value::Type::String, { "code"sv, "none"sv }, "code"sv)); // 17. Set displayNames.[[Fallback]] to fallback. display_names->set_fallback(fallback.as_string().string()); diff --git a/Userland/Libraries/LibJS/Runtime/Intl/ListFormatConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/ListFormatConstructor.cpp index f1c4eaf7c6..5764f6b80c 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/ListFormatConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/ListFormatConstructor.cpp @@ -65,9 +65,7 @@ Value ListFormatConstructor::construct(FunctionObject& new_target) LocaleOptions opt {}; // 6. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit"). - auto matcher = get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, { "lookup"sv, "best fit"sv }, "best fit"sv); - if (vm.exception()) - return {}; + auto matcher = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, { "lookup"sv, "best fit"sv }, "best fit"sv)); // 7. Set opt.[[localeMatcher]] to matcher. opt.locale_matcher = matcher; @@ -81,17 +79,13 @@ Value ListFormatConstructor::construct(FunctionObject& new_target) list_format->set_locale(move(result.locale)); // 11. Let type be ? GetOption(options, "type", "string", « "conjunction", "disjunction", "unit" », "conjunction"). - auto type = get_option(global_object, *options, vm.names.type, Value::Type::String, { "conjunction"sv, "disjunction"sv, "unit"sv }, "conjunction"sv); - if (vm.exception()) - return {}; + auto type = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.type, Value::Type::String, { "conjunction"sv, "disjunction"sv, "unit"sv }, "conjunction"sv)); // 12. Set listFormat.[[Type]] to type. list_format->set_type(type.as_string().string()); // 13. Let style be ? GetOption(options, "style", "string", « "long", "short", "narrow" », "long"). - auto style = get_option(global_object, *options, vm.names.style, Value::Type::String, { "long"sv, "short"sv, "narrow"sv }, "long"sv); - if (vm.exception()) - return {}; + auto style = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.style, Value::Type::String, { "long"sv, "short"sv, "narrow"sv }, "long"sv)); // 14. Set listFormat.[[Style]] to style. list_format->set_style(style.as_string().string()); diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.cpp index 0d8e56adb0..3919b9c094 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.cpp @@ -30,9 +30,7 @@ static Optional<String> get_string_option(GlobalObject& global_object, Object co { auto& vm = global_object.vm(); - auto option = get_option(global_object, options, property, Value::Type::String, values, Empty {}); - if (vm.exception()) - return {}; + auto option = TRY_OR_DISCARD(get_option(global_object, options, property, Value::Type::String, values, Empty {})); if (option.is_undefined()) return {}; @@ -342,9 +340,7 @@ Value LocaleConstructor::construct(FunctionObject& new_target) return {}; // 23. Let kn be ? GetOption(options, "numeric", "boolean", undefined, undefined). - auto kn = get_option(global_object, *options, vm.names.numeric, Value::Type::Boolean, {}, Empty {}); - if (vm.exception()) - return {}; + auto kn = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.numeric, Value::Type::Boolean, {}, Empty {})); // 24. If kn is not undefined, set kn to ! ToString(kn). // 25. Set opt.[[kn]] to kn. diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp index 2b722aeb83..e53dd58e73 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp @@ -368,17 +368,13 @@ NumberFormat* initialize_number_format(GlobalObject& global_object, NumberFormat LocaleOptions opt {}; // 4. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit"). - auto matcher = get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, { "lookup"sv, "best fit"sv }, "best fit"sv); - if (vm.exception()) - return {}; + auto matcher = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, { "lookup"sv, "best fit"sv }, "best fit"sv)); // 5. Set opt.[[localeMatcher]] to matcher. opt.locale_matcher = matcher; // 6. Let numberingSystem be ? GetOption(options, "numberingSystem", "string", undefined, undefined). - auto numbering_system = get_option(global_object, *options, vm.names.numberingSystem, Value::Type::String, {}, Empty {}); - if (vm.exception()) - return {}; + auto numbering_system = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.numberingSystem, Value::Type::String, {}, Empty {})); // 7. If numberingSystem is not undefined, then if (!numbering_system.is_undefined()) { @@ -443,9 +439,7 @@ NumberFormat* initialize_number_format(GlobalObject& global_object, NumberFormat } // 18. Let notation be ? GetOption(options, "notation", "string", « "standard", "scientific", "engineering", "compact" », "standard"). - auto notation = get_option(global_object, *options, vm.names.notation, Value::Type::String, { "standard"sv, "scientific"sv, "engineering"sv, "compact"sv }, "standard"sv); - if (vm.exception()) - return {}; + auto notation = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.notation, Value::Type::String, { "standard"sv, "scientific"sv, "engineering"sv, "compact"sv }, "standard"sv)); // 19. Set numberFormat.[[Notation]] to notation. number_format.set_notation(notation.as_string().string()); @@ -456,9 +450,7 @@ NumberFormat* initialize_number_format(GlobalObject& global_object, NumberFormat return {}; // 21. Let compactDisplay be ? GetOption(options, "compactDisplay", "string", « "short", "long" », "short"). - auto compact_display = get_option(global_object, *options, vm.names.compactDisplay, Value::Type::String, { "short"sv, "long"sv }, "short"sv); - if (vm.exception()) - return {}; + auto compact_display = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.compactDisplay, Value::Type::String, { "short"sv, "long"sv }, "short"sv)); // 22. If notation is "compact", then if (number_format.notation() == NumberFormat::Notation::Compact) { @@ -467,17 +459,13 @@ NumberFormat* initialize_number_format(GlobalObject& global_object, NumberFormat } // 23. Let useGrouping be ? GetOption(options, "useGrouping", "boolean", undefined, true). - auto use_grouping = get_option(global_object, *options, vm.names.useGrouping, Value::Type::Boolean, {}, true); - if (vm.exception()) - return {}; + auto use_grouping = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.useGrouping, Value::Type::Boolean, {}, true)); // 24. Set numberFormat.[[UseGrouping]] to useGrouping. number_format.set_use_grouping(use_grouping.as_bool()); // 25. Let signDisplay be ? GetOption(options, "signDisplay", "string", « "auto", "never", "always", "exceptZero" », "auto"). - auto sign_display = get_option(global_object, *options, vm.names.signDisplay, Value::Type::String, { "auto"sv, "never"sv, "always"sv, "exceptZero"sv }, "auto"sv); - if (vm.exception()) - return {}; + auto sign_display = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.signDisplay, Value::Type::String, { "auto"sv, "never"sv, "always"sv, "exceptZero"sv }, "auto"sv)); // 26. Set numberFormat.[[SignDisplay]] to signDisplay. number_format.set_sign_display(sign_display.as_string().string()); @@ -505,17 +493,19 @@ void set_number_format_unit_options(GlobalObject& global_object, NumberFormat& i // 2. Assert: Type(options) is Object. // 3. Let style be ? GetOption(options, "style", "string", « "decimal", "percent", "currency", "unit" », "decimal"). - auto style = get_option(global_object, options, vm.names.style, Value::Type::String, { "decimal"sv, "percent"sv, "currency"sv, "unit"sv }, "decimal"sv); - if (vm.exception()) + auto style_or_error = get_option(global_object, options, vm.names.style, Value::Type::String, { "decimal"sv, "percent"sv, "currency"sv, "unit"sv }, "decimal"sv); + if (style_or_error.is_error()) return; + auto style = style_or_error.release_value(); // 4. Set intlObj.[[Style]] to style. intl_object.set_style(style.as_string().string()); // 5. Let currency be ? GetOption(options, "currency", "string", undefined, undefined). - auto currency = get_option(global_object, options, vm.names.currency, Value::Type::String, {}, Empty {}); - if (vm.exception()) + auto currency_or_error = get_option(global_object, options, vm.names.currency, Value::Type::String, {}, Empty {}); + if (currency_or_error.is_error()) return; + auto currency = currency_or_error.release_value(); // 6. If currency is undefined, then if (currency.is_undefined()) { @@ -533,19 +523,22 @@ void set_number_format_unit_options(GlobalObject& global_object, NumberFormat& i } // 8. Let currencyDisplay be ? GetOption(options, "currencyDisplay", "string", « "code", "symbol", "narrowSymbol", "name" », "symbol"). - auto currency_display = get_option(global_object, options, vm.names.currencyDisplay, Value::Type::String, { "code"sv, "symbol"sv, "narrowSymbol"sv, "name"sv }, "symbol"sv); - if (vm.exception()) + auto currency_display_or_error = get_option(global_object, options, vm.names.currencyDisplay, Value::Type::String, { "code"sv, "symbol"sv, "narrowSymbol"sv, "name"sv }, "symbol"sv); + if (currency_display_or_error.is_error()) return; + auto currency_display = currency_display_or_error.release_value(); // 9. Let currencySign be ? GetOption(options, "currencySign", "string", « "standard", "accounting" », "standard"). - auto currency_sign = get_option(global_object, options, vm.names.currencySign, Value::Type::String, { "standard"sv, "accounting"sv }, "standard"sv); - if (vm.exception()) + auto currency_sign_or_error = get_option(global_object, options, vm.names.currencySign, Value::Type::String, { "standard"sv, "accounting"sv }, "standard"sv); + if (currency_sign_or_error.is_error()) return; + auto currency_sign = currency_sign_or_error.release_value(); // 10. Let unit be ? GetOption(options, "unit", "string", undefined, undefined). - auto unit = get_option(global_object, options, vm.names.unit, Value::Type::String, {}, Empty {}); - if (vm.exception()) + auto unit_or_error = get_option(global_object, options, vm.names.unit, Value::Type::String, {}, Empty {}); + if (unit_or_error.is_error()) return; + auto unit = unit_or_error.release_value(); // 11. If unit is undefined, then if (unit.is_undefined()) { @@ -563,9 +556,10 @@ void set_number_format_unit_options(GlobalObject& global_object, NumberFormat& i } // 13. Let unitDisplay be ? GetOption(options, "unitDisplay", "string", « "short", "narrow", "long" », "short"). - auto unit_display = get_option(global_object, options, vm.names.unitDisplay, Value::Type::String, { "short"sv, "narrow"sv, "long"sv }, "short"sv); - if (vm.exception()) + auto unit_display_or_error = get_option(global_object, options, vm.names.unitDisplay, Value::Type::String, { "short"sv, "narrow"sv, "long"sv }, "short"sv); + if (unit_display_or_error.is_error()) return; + auto unit_display = unit_display_or_error.release_value(); // 14. If style is "currency", then if (intl_object.style() == NumberFormat::Style::Currency) { |