diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-03-15 11:09:11 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-03-15 17:30:58 +0100 |
commit | 04bb17ca94f5537a29e5d7996970cabf6410a51d (patch) | |
tree | ebcbd2734437e0f8be76bcd1de8de619e3084036 /Userland/Libraries/LibJS | |
parent | 8941d0b49e4d434127680c0d93f33dea24328e35 (diff) | |
download | serenity-04bb17ca94f5537a29e5d7996970cabf6410a51d.zip |
LibJS: Reorganize spec steps for Intl.RelativeTimeFormat
This is an editorial change in the Intl spec:
https://github.com/tc39/ecma402/commit/a3ae343
Diffstat (limited to 'Userland/Libraries/LibJS')
5 files changed, 94 insertions, 90 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormat.cpp index 2da5156f8a..ed8c52450f 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormat.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormat.cpp @@ -51,80 +51,7 @@ StringView RelativeTimeFormat::numeric_string() const } } -// 17.1.1 InitializeRelativeTimeFormat ( relativeTimeFormat, locales, options ), https://tc39.es/ecma402/#sec-InitializeRelativeTimeFormat -ThrowCompletionOr<RelativeTimeFormat*> initialize_relative_time_format(GlobalObject& global_object, RelativeTimeFormat& relative_time_format, Value locales_value, Value options_value) -{ - auto& vm = global_object.vm(); - - // 1. Let requestedLocales be ? CanonicalizeLocaleList(locales). - auto requested_locales = TRY(canonicalize_locale_list(global_object, locales_value)); - - // 2. Set options to ? CoerceOptionsToObject(options). - auto* options = TRY(coerce_options_to_object(global_object, options_value)); - - // 3. Let opt be a new Record. - LocaleOptions opt {}; - - // 4. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit"). - auto matcher = TRY(get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, AK::Array { "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 = TRY(get_option(global_object, *options, vm.names.numberingSystem, Value::Type::String, {}, Empty {})); - - // 7. If numberingSystem is not undefined, then - if (!numbering_system.is_undefined()) { - // a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception. - if (!Unicode::is_type_identifier(numbering_system.as_string().string())) - return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv); - - // 8. Set opt.[[nu]] to numberingSystem. - opt.nu = numbering_system.as_string().string(); - } - - // 9. Let localeData be %RelativeTimeFormat%.[[LocaleData]]. - // 10. Let r be ResolveLocale(%RelativeTimeFormat%.[[AvailableLocales]], requestedLocales, opt, %RelativeTimeFormat%.[[RelevantExtensionKeys]], localeData). - auto result = resolve_locale(requested_locales, opt, RelativeTimeFormat::relevant_extension_keys()); - - // 11. Let locale be r.[[locale]]. - auto locale = move(result.locale); - - // 12. Set relativeTimeFormat.[[Locale]] to locale. - relative_time_format.set_locale(locale); - - // 13. Set relativeTimeFormat.[[DataLocale]] to r.[[dataLocale]]. - relative_time_format.set_data_locale(move(result.data_locale)); - - // 14. Set relativeTimeFormat.[[NumberingSystem]] to r.[[nu]]. - if (result.nu.has_value()) - relative_time_format.set_numbering_system(result.nu.release_value()); - - // 15. Let style be ? GetOption(options, "style", "string", « "long", "short", "narrow" », "long"). - auto style = TRY(get_option(global_object, *options, vm.names.style, Value::Type::String, { "long"sv, "short"sv, "narrow"sv }, "long"sv)); - - // 16. Set relativeTimeFormat.[[Style]] to style. - relative_time_format.set_style(style.as_string().string()); - - // 17. Let numeric be ? GetOption(options, "numeric", "string", « "always", "auto" », "always"). - auto numeric = TRY(get_option(global_object, *options, vm.names.numeric, Value::Type::String, { "always"sv, "auto"sv }, "always"sv)); - - // 18. Set relativeTimeFormat.[[Numeric]] to numeric. - relative_time_format.set_numeric(numeric.as_string().string()); - - // 19. Let relativeTimeFormat.[[NumberFormat]] be ! Construct(%NumberFormat%, « locale »). - auto* number_format = MUST(construct(global_object, *global_object.intl_number_format_constructor(), js_string(vm, locale))); - relative_time_format.set_number_format(static_cast<NumberFormat*>(number_format)); - - // 20. Let relativeTimeFormat.[[PluralRules]] be ! Construct(%PluralRules%, « locale »). - // FIXME: We do not yet support Intl.PluralRules. - - // 21. Return relativeTimeFormat. - return &relative_time_format; -} - -// 17.1.2 SingularRelativeTimeUnit ( unit ), https://tc39.es/ecma402/#sec-singularrelativetimeunit +// 17.5.1 SingularRelativeTimeUnit ( unit ), https://tc39.es/ecma402/#sec-singularrelativetimeunit ThrowCompletionOr<Unicode::TimeUnit> singular_relative_time_unit(GlobalObject& global_object, StringView unit) { auto& vm = global_object.vm(); @@ -163,7 +90,7 @@ ThrowCompletionOr<Unicode::TimeUnit> singular_relative_time_unit(GlobalObject& g return vm.throw_completion<RangeError>(global_object, ErrorType::IntlInvalidUnit, unit); } -// 17.1.3 PartitionRelativeTimePattern ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-PartitionRelativeTimePattern +// 17.5.2 PartitionRelativeTimePattern ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-PartitionRelativeTimePattern ThrowCompletionOr<Vector<PatternPartitionWithUnit>> partition_relative_time_pattern(GlobalObject& global_object, RelativeTimeFormat& relative_time_format, double value, StringView unit) { auto& vm = global_object.vm(); @@ -262,7 +189,7 @@ ThrowCompletionOr<Vector<PatternPartitionWithUnit>> partition_relative_time_patt return make_parts_list(pattern->pattern, Unicode::time_unit_to_string(time_unit), move(value_partitions)); } -// 17.1.4 MakePartsList ( pattern, unit, parts ), https://tc39.es/ecma402/#sec-makepartslist +// 17.5.3 MakePartsList ( pattern, unit, parts ), https://tc39.es/ecma402/#sec-makepartslist Vector<PatternPartitionWithUnit> make_parts_list(StringView pattern, StringView unit, Vector<PatternPartition> parts) { // 1. Let patternParts be PartitionPattern(pattern). @@ -295,7 +222,7 @@ Vector<PatternPartitionWithUnit> make_parts_list(StringView pattern, StringView return result; } -// 17.1.5 FormatRelativeTime ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTime +// 17.5.4 FormatRelativeTime ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTime ThrowCompletionOr<String> format_relative_time(GlobalObject& global_object, RelativeTimeFormat& relative_time_format, double value, StringView unit) { // 1. Let parts be ? PartitionRelativeTimePattern(relativeTimeFormat, value, unit). @@ -314,7 +241,7 @@ ThrowCompletionOr<String> format_relative_time(GlobalObject& global_object, Rela return result.build(); } -// 17.1.6 FormatRelativeTimeToParts ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTimeToParts +// 17.5.5 FormatRelativeTimeToParts ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTimeToParts ThrowCompletionOr<Array*> format_relative_time_to_parts(GlobalObject& global_object, RelativeTimeFormat& relative_time_format, double value, StringView unit) { auto& vm = global_object.vm(); diff --git a/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormat.h b/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormat.h index 675db2f62f..61ee1904fa 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormat.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormat.h @@ -28,7 +28,7 @@ public: static constexpr auto relevant_extension_keys() { - // 17.3.3 Internal slots, https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat-internal-slots + // 17.2.3 Internal slots, https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat-internal-slots // The value of the [[RelevantExtensionKeys]] internal slot is « "nu" ». return AK::Array { "nu"sv }; } @@ -77,7 +77,6 @@ struct PatternPartitionWithUnit : public PatternPartition { StringView unit; }; -ThrowCompletionOr<RelativeTimeFormat*> initialize_relative_time_format(GlobalObject& global_object, RelativeTimeFormat& relative_time_format, Value locales_value, Value options_value); ThrowCompletionOr<Unicode::TimeUnit> singular_relative_time_unit(GlobalObject& global_object, StringView unit); ThrowCompletionOr<Vector<PatternPartitionWithUnit>> partition_relative_time_pattern(GlobalObject& global_object, RelativeTimeFormat& relative_time_format, double value, StringView unit); Vector<PatternPartitionWithUnit> make_parts_list(StringView pattern, StringView unit, Vector<PatternPartition> parts); diff --git a/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatConstructor.cpp index a87ed428be..48b6421550 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatConstructor.cpp @@ -8,12 +8,15 @@ #include <LibJS/Runtime/Array.h> #include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/Intl/AbstractOperations.h> +#include <LibJS/Runtime/Intl/NumberFormat.h> +#include <LibJS/Runtime/Intl/NumberFormatConstructor.h> #include <LibJS/Runtime/Intl/RelativeTimeFormat.h> #include <LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h> +#include <LibUnicode/Locale.h> namespace JS::Intl { -// 17.2 The Intl.RelativeTimeFormat Constructor, https://tc39.es/ecma402/#sec-intl-relativetimeformat-constructor +// 17.1 The Intl.RelativeTimeFormat Constructor, https://tc39.es/ecma402/#sec-intl-relativetimeformat-constructor RelativeTimeFormatConstructor::RelativeTimeFormatConstructor(GlobalObject& global_object) : NativeFunction(vm().names.RelativeTimeFormat.as_string(), *global_object.function_prototype()) { @@ -25,7 +28,7 @@ void RelativeTimeFormatConstructor::initialize(GlobalObject& global_object) auto& vm = this->vm(); - // 17.3.1 Intl.RelativeTimeFormat.prototype, https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.prototype + // 17.2.1 Intl.RelativeTimeFormat.prototype, https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.prototype define_direct_property(vm.names.prototype, global_object.intl_relative_time_format_prototype(), 0); define_direct_property(vm.names.length, Value(0), Attribute::Configurable); @@ -33,14 +36,14 @@ void RelativeTimeFormatConstructor::initialize(GlobalObject& global_object) define_native_function(vm.names.supportedLocalesOf, supported_locales_of, 1, attr); } -// 17.2.1 Intl.RelativeTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat +// 17.1.1 Intl.RelativeTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat ThrowCompletionOr<Value> RelativeTimeFormatConstructor::call() { // 1. If NewTarget is undefined, throw a TypeError exception. return vm().throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Intl.RelativeTimeFormat"); } -// 17.2.1 Intl.RelativeTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat +// 17.1.1 Intl.RelativeTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat ThrowCompletionOr<Object*> RelativeTimeFormatConstructor::construct(FunctionObject& new_target) { auto& vm = this->vm(); @@ -56,7 +59,7 @@ ThrowCompletionOr<Object*> RelativeTimeFormatConstructor::construct(FunctionObje return TRY(initialize_relative_time_format(global_object, *relative_time_format, locales, options)); } -// 17.3.2 Intl.RelativeTimeFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.supportedLocalesOf +// 17.2.2 Intl.RelativeTimeFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.supportedLocalesOf JS_DEFINE_NATIVE_FUNCTION(RelativeTimeFormatConstructor::supported_locales_of) { auto locales = vm.argument(0); @@ -71,4 +74,77 @@ JS_DEFINE_NATIVE_FUNCTION(RelativeTimeFormatConstructor::supported_locales_of) return TRY(supported_locales(global_object, requested_locales, options)); } +// 17.1.2 InitializeRelativeTimeFormat ( relativeTimeFormat, locales, options ), https://tc39.es/ecma402/#sec-InitializeRelativeTimeFormat +ThrowCompletionOr<RelativeTimeFormat*> initialize_relative_time_format(GlobalObject& global_object, RelativeTimeFormat& relative_time_format, Value locales_value, Value options_value) +{ + auto& vm = global_object.vm(); + + // 1. Let requestedLocales be ? CanonicalizeLocaleList(locales). + auto requested_locales = TRY(canonicalize_locale_list(global_object, locales_value)); + + // 2. Set options to ? CoerceOptionsToObject(options). + auto* options = TRY(coerce_options_to_object(global_object, options_value)); + + // 3. Let opt be a new Record. + LocaleOptions opt {}; + + // 4. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit"). + auto matcher = TRY(get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, AK::Array { "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 = TRY(get_option(global_object, *options, vm.names.numberingSystem, Value::Type::String, {}, Empty {})); + + // 7. If numberingSystem is not undefined, then + if (!numbering_system.is_undefined()) { + // a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception. + if (!Unicode::is_type_identifier(numbering_system.as_string().string())) + return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv); + + // 8. Set opt.[[nu]] to numberingSystem. + opt.nu = numbering_system.as_string().string(); + } + + // 9. Let localeData be %RelativeTimeFormat%.[[LocaleData]]. + // 10. Let r be ResolveLocale(%RelativeTimeFormat%.[[AvailableLocales]], requestedLocales, opt, %RelativeTimeFormat%.[[RelevantExtensionKeys]], localeData). + auto result = resolve_locale(requested_locales, opt, RelativeTimeFormat::relevant_extension_keys()); + + // 11. Let locale be r.[[locale]]. + auto locale = move(result.locale); + + // 12. Set relativeTimeFormat.[[Locale]] to locale. + relative_time_format.set_locale(locale); + + // 13. Set relativeTimeFormat.[[DataLocale]] to r.[[dataLocale]]. + relative_time_format.set_data_locale(move(result.data_locale)); + + // 14. Set relativeTimeFormat.[[NumberingSystem]] to r.[[nu]]. + if (result.nu.has_value()) + relative_time_format.set_numbering_system(result.nu.release_value()); + + // 15. Let style be ? GetOption(options, "style", "string", « "long", "short", "narrow" », "long"). + auto style = TRY(get_option(global_object, *options, vm.names.style, Value::Type::String, { "long"sv, "short"sv, "narrow"sv }, "long"sv)); + + // 16. Set relativeTimeFormat.[[Style]] to style. + relative_time_format.set_style(style.as_string().string()); + + // 17. Let numeric be ? GetOption(options, "numeric", "string", « "always", "auto" », "always"). + auto numeric = TRY(get_option(global_object, *options, vm.names.numeric, Value::Type::String, { "always"sv, "auto"sv }, "always"sv)); + + // 18. Set relativeTimeFormat.[[Numeric]] to numeric. + relative_time_format.set_numeric(numeric.as_string().string()); + + // 19. Let relativeTimeFormat.[[NumberFormat]] be ! Construct(%NumberFormat%, « locale »). + auto* number_format = MUST(construct(global_object, *global_object.intl_number_format_constructor(), js_string(vm, locale))); + relative_time_format.set_number_format(static_cast<NumberFormat*>(number_format)); + + // 20. Let relativeTimeFormat.[[PluralRules]] be ! Construct(%PluralRules%, « locale »). + // FIXME: We do not yet support Intl.PluralRules. + + // 21. Return relativeTimeFormat. + return &relative_time_format; +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h b/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h index fce4363cf8..aceae05a70 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h @@ -27,4 +27,6 @@ private: JS_DECLARE_NATIVE_FUNCTION(supported_locales_of); }; +ThrowCompletionOr<RelativeTimeFormat*> initialize_relative_time_format(GlobalObject& global_object, RelativeTimeFormat& relative_time_format, Value locales_value, Value options_value); + } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatPrototype.cpp index be1fefa2b7..063d4ea964 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatPrototype.cpp @@ -10,7 +10,7 @@ namespace JS::Intl { -// 17.4 Properties of the Intl.RelativeTimeFormat Prototype Object, https://tc39.es/ecma402/#sec-properties-of-intl-relativetimeformat-prototype-object +// 17.3 Properties of the Intl.RelativeTimeFormat Prototype Object, https://tc39.es/ecma402/#sec-properties-of-intl-relativetimeformat-prototype-object RelativeTimeFormatPrototype::RelativeTimeFormatPrototype(GlobalObject& global_object) : PrototypeObject(*global_object.object_prototype()) { @@ -22,7 +22,7 @@ void RelativeTimeFormatPrototype::initialize(GlobalObject& global_object) auto& vm = this->vm(); - // 17.4.2 Intl.RelativeTimeFormat.prototype[ @@toStringTag ], https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.prototype-toStringTag + // 17.3.2 Intl.RelativeTimeFormat.prototype[ @@toStringTag ], https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.prototype-toStringTag define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Intl.RelativeTimeFormat"sv), Attribute::Configurable); u8 attr = Attribute::Writable | Attribute::Configurable; @@ -31,7 +31,7 @@ void RelativeTimeFormatPrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.resolvedOptions, resolved_options, 0, attr); } -// 17.4.3 Intl.RelativeTimeFormat.prototype.format ( value, unit ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.prototype.format +// 17.3.3 Intl.RelativeTimeFormat.prototype.format ( value, unit ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.prototype.format JS_DEFINE_NATIVE_FUNCTION(RelativeTimeFormatPrototype::format) { // 1. Let relativeTimeFormat be the this value. @@ -49,7 +49,7 @@ JS_DEFINE_NATIVE_FUNCTION(RelativeTimeFormatPrototype::format) return js_string(vm, move(formatted)); } -// 17.4.4 Intl.RelativeTimeFormat.prototype.formatToParts ( value, unit ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.prototype.formatToParts +// 17.3.4 Intl.RelativeTimeFormat.prototype.formatToParts ( value, unit ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.prototype.formatToParts JS_DEFINE_NATIVE_FUNCTION(RelativeTimeFormatPrototype::format_to_parts) { // 1. Let relativeTimeFormat be the this value. @@ -66,7 +66,7 @@ JS_DEFINE_NATIVE_FUNCTION(RelativeTimeFormatPrototype::format_to_parts) return TRY(format_relative_time_to_parts(global_object, *relative_time_format, value.as_double(), unit)); } -// 17.4.5 Intl.RelativeTimeFormat.prototype.resolvedOptions ( ), https://tc39.es/ecma402/#sec-intl.relativetimeformat.prototype.resolvedoptions +// 17.3.5 Intl.RelativeTimeFormat.prototype.resolvedOptions ( ), https://tc39.es/ecma402/#sec-intl.relativetimeformat.prototype.resolvedoptions JS_DEFINE_NATIVE_FUNCTION(RelativeTimeFormatPrototype::resolved_options) { // 1. Let relativeTimeFormat be the this value. |