1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 14:57:35 +00:00

LibJS: Start implementing the stage 3 Intl.DurationFormat proposal

This commit is contained in:
Idan Horowitz 2022-06-30 00:58:40 +03:00
parent e1ee33ba7c
commit 97fe37bcc2
14 changed files with 998 additions and 0 deletions

View file

@ -0,0 +1,182 @@
/*
* Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Intl/AbstractOperations.h>
#include <LibJS/Runtime/Intl/DurationFormat.h>
namespace JS::Intl {
// 1 DurationFormat Objects, https://tc39.es/proposal-intl-duration-format/#durationformat-objects
DurationFormat::DurationFormat(Object& prototype)
: Object(prototype)
{
}
DurationFormat::Style DurationFormat::style_from_string(StringView style)
{
if (style == "long"sv)
return Style::Long;
if (style == "short"sv)
return Style::Short;
if (style == "narrow"sv)
return Style::Narrow;
if (style == "digital"sv)
return Style::Digital;
VERIFY_NOT_REACHED();
}
StringView DurationFormat::style_to_string(Style style)
{
switch (style) {
case Style::Long:
return "long"sv;
case Style::Short:
return "short"sv;
case Style::Narrow:
return "narrow"sv;
case Style::Digital:
return "digital"sv;
default:
VERIFY_NOT_REACHED();
}
}
DurationFormat::ValueStyle DurationFormat::date_style_from_string(StringView date_style)
{
if (date_style == "long"sv)
return ValueStyle::Long;
if (date_style == "short"sv)
return ValueStyle::Short;
if (date_style == "narrow"sv)
return ValueStyle::Narrow;
VERIFY_NOT_REACHED();
}
DurationFormat::ValueStyle DurationFormat::time_style_from_string(StringView time_style)
{
if (time_style == "long"sv)
return ValueStyle::Long;
if (time_style == "short"sv)
return ValueStyle::Short;
if (time_style == "narrow"sv)
return ValueStyle::Narrow;
if (time_style == "numeric"sv)
return ValueStyle::Numeric;
if (time_style == "2-digit"sv)
return ValueStyle::TwoDigit;
VERIFY_NOT_REACHED();
}
DurationFormat::ValueStyle DurationFormat::sub_second_style_from_string(StringView sub_second_style)
{
if (sub_second_style == "long"sv)
return ValueStyle::Long;
if (sub_second_style == "short"sv)
return ValueStyle::Short;
if (sub_second_style == "narrow"sv)
return ValueStyle::Narrow;
if (sub_second_style == "numeric"sv)
return ValueStyle::Numeric;
VERIFY_NOT_REACHED();
}
DurationFormat::Display DurationFormat::display_from_string(StringView display)
{
if (display == "auto"sv)
return Display::Auto;
if (display == "always"sv)
return Display::Always;
VERIFY_NOT_REACHED();
}
StringView DurationFormat::value_style_to_string(ValueStyle value_style)
{
switch (value_style) {
case ValueStyle::Long:
return "long"sv;
case ValueStyle::Short:
return "short"sv;
case ValueStyle::Narrow:
return "narrow"sv;
case ValueStyle::Numeric:
return "numeric"sv;
case ValueStyle::TwoDigit:
return "2-digit"sv;
default:
VERIFY_NOT_REACHED();
}
}
StringView DurationFormat::display_to_string(Display display)
{
switch (display) {
case Display::Auto:
return "auto"sv;
case Display::Always:
return "always"sv;
default:
VERIFY_NOT_REACHED();
}
}
// 1.1.2 GetDurationUnitOptions ( unit, options, baseStyle, stylesList, digitalBase, prevStyle ), https://tc39.es/proposal-intl-duration-format/#sec-getdurationunitoptions
ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(GlobalObject& global_object, String const& unit, Object const& options, StringView base_style, Span<StringView const> styles_list, StringView digital_base, Optional<String> const& previous_style)
{
auto& vm = global_object.vm();
// 1. Let style be ? GetOption(options, unit, "string", stylesList, undefined).
auto style_value = TRY(get_option(global_object, options, unit, OptionType::String, styles_list, Empty {}));
// 2. Let displayDefault be "always".
auto display_default = "always"sv;
String style;
// 3. If style is undefined, then
if (style_value.is_undefined()) {
// a. Set displayDefault to "auto".
display_default = "auto"sv;
// b. If baseStyle is "digital", then
if (base_style == "digital"sv) {
// i. Set style to digitalBase.
style = digital_base;
}
// c. Else,
else {
// i. Set style to baseStyle.
style = base_style;
}
} else {
style = style_value.as_string().string();
}
// 4. Let displayField be the string-concatenation of unit and "Display".
auto display_field = String::formatted("{}Display", unit);
// 5. Let display be ? GetOption(options, displayField, "string", « "auto", "always" », displayDefault).
auto display = TRY(get_option(global_object, options, display_field, OptionType::String, { "auto"sv, "always"sv }, display_default));
// 6. If prevStyle is "numeric" or "2-digit", then
if (previous_style == "numeric"sv || previous_style == "2-digit"sv) {
// a. If style is not "numeric" or "2-digit", then
if (style != "numeric"sv && style != "2-digit"sv) {
// i. Throw a RangeError exception.
return vm.throw_completion<RangeError>(global_object, ErrorType::IntlNonNumericOr2DigitAfterNumericOr2Digit);
}
// b. Else if unit is "minutes" or "seconds", then
else if (unit == "minutes"sv || unit == "seconds"sv) {
// i. Set style to "2-digit".
style = "2-digit"sv;
}
}
// 7. Return the Record { [[Style]]: style, [[Display]]: display }.
return DurationUnitOptions { .style = move(style), .display = display.as_string().string() };
}
}

View file

@ -0,0 +1,194 @@
/*
* Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Array.h>
#include <AK/String.h>
#include <LibJS/Runtime/Object.h>
namespace JS::Intl {
class DurationFormat final : public Object {
JS_OBJECT(DurationFormat, Object);
public:
enum class Style {
Long,
Short,
Narrow,
Digital
};
enum class ValueStyle {
Long,
Short,
Narrow,
Numeric,
TwoDigit
};
enum class Display {
Auto,
Always
};
static constexpr auto relevant_extension_keys()
{
// 1.3.3 Internal slots, https://tc39.es/proposal-intl-duration-format/#sec-Intl.DurationFormat-internal-slots
// The value of the [[RelevantExtensionKeys]] internal slot is « "nu" ».
return AK::Array { "nu"sv };
}
explicit DurationFormat(Object& prototype);
virtual ~DurationFormat() override = default;
void set_locale(String locale) { m_locale = move(locale); }
String const& locale() const { return m_locale; }
void set_data_locale(String data_locale) { m_data_locale = move(data_locale); }
String const& data_locale() const { return m_data_locale; }
void set_numbering_system(String numbering_system) { m_numbering_system = move(numbering_system); }
String const& numbering_system() const { return m_numbering_system; }
void set_style(StringView style) { m_style = style_from_string(style); }
String style_string() const { return style_to_string(m_style); }
void set_years_style(StringView years_style) { m_years_style = date_style_from_string(years_style); }
StringView years_style_string() const { return value_style_to_string(m_years_style); }
void set_years_display(StringView years_display) { m_years_display = display_from_string(years_display); }
StringView years_display_string() const { return display_to_string(m_years_display); }
void set_months_style(StringView months_style) { m_months_style = date_style_from_string(months_style); }
StringView months_style_string() const { return value_style_to_string(m_months_style); }
void set_months_display(StringView months_display) { m_months_display = display_from_string(months_display); }
StringView months_display_string() const { return display_to_string(m_months_display); }
void set_weeks_style(StringView weeks_style) { m_weeks_style = date_style_from_string(weeks_style); }
StringView weeks_style_string() const { return value_style_to_string(m_weeks_style); }
void set_weeks_display(StringView weeks_display) { m_weeks_display = display_from_string(weeks_display); }
StringView weeks_display_string() const { return display_to_string(m_weeks_display); }
void set_days_style(StringView days_style) { m_days_style = date_style_from_string(days_style); }
StringView days_style_string() const { return value_style_to_string(m_days_style); }
void set_days_display(StringView days_display) { m_days_display = display_from_string(days_display); }
StringView days_display_string() const { return display_to_string(m_days_display); }
void set_hours_style(StringView hours_style) { m_hours_style = time_style_from_string(hours_style); }
StringView hours_style_string() const { return value_style_to_string(m_hours_style); }
void set_hours_display(StringView hours_display) { m_hours_display = display_from_string(hours_display); }
StringView hours_display_string() const { return display_to_string(m_hours_display); }
void set_minutes_style(StringView minutes_style) { m_minutes_style = time_style_from_string(minutes_style); }
StringView minutes_style_string() const { return value_style_to_string(m_minutes_style); }
void set_minutes_display(StringView minutes_display) { m_minutes_display = display_from_string(minutes_display); }
StringView minutes_display_string() const { return display_to_string(m_minutes_display); }
void set_seconds_style(StringView seconds_style) { m_seconds_style = time_style_from_string(seconds_style); }
StringView seconds_style_string() const { return value_style_to_string(m_seconds_style); }
void set_seconds_display(StringView seconds_display) { m_seconds_display = display_from_string(seconds_display); }
StringView seconds_display_string() const { return display_to_string(m_seconds_display); }
void set_milliseconds_style(StringView milliseconds_style) { m_milliseconds_style = sub_second_style_from_string(milliseconds_style); }
StringView milliseconds_style_string() const { return value_style_to_string(m_milliseconds_style); }
void set_milliseconds_display(StringView milliseconds_display) { m_milliseconds_display = display_from_string(milliseconds_display); }
StringView milliseconds_display_string() const { return display_to_string(m_milliseconds_display); }
void set_microseconds_style(StringView microseconds_style) { m_microseconds_style = sub_second_style_from_string(microseconds_style); }
StringView microseconds_style_string() const { return value_style_to_string(m_microseconds_style); }
void set_microseconds_display(StringView microseconds_display) { m_microseconds_display = display_from_string(microseconds_display); }
StringView microseconds_display_string() const { return display_to_string(m_microseconds_display); }
void set_nanoseconds_style(StringView nanoseconds_style) { m_nanoseconds_style = sub_second_style_from_string(nanoseconds_style); }
StringView nanoseconds_style_string() const { return value_style_to_string(m_nanoseconds_style); }
void set_nanoseconds_display(StringView nanoseconds_display) { m_nanoseconds_display = display_from_string(nanoseconds_display); }
StringView nanoseconds_display_string() const { return display_to_string(m_nanoseconds_display); }
void set_fractional_digits(Optional<u8> fractional_digits) { m_fractional_digits = move(fractional_digits); }
bool has_fractional_digits() const { return m_fractional_digits.has_value(); }
u8 fractional_digits() const { return m_fractional_digits.value(); }
private:
static Style style_from_string(StringView style);
static StringView style_to_string(Style);
static ValueStyle date_style_from_string(StringView date_style);
static ValueStyle time_style_from_string(StringView time_style);
static ValueStyle sub_second_style_from_string(StringView sub_second_style);
static StringView value_style_to_string(ValueStyle);
static Display display_from_string(StringView display);
static StringView display_to_string(Display);
String m_locale; // [[Locale]]
String m_data_locale; // [[DataLocale]]
String m_numbering_system; // [[NumberingSystem]]
Style m_style; // [[Style]]
ValueStyle m_years_style { ValueStyle::Long }; // [[YearsStyle]]
Display m_years_display { Display::Auto }; // [[YearsDisplay]]
ValueStyle m_months_style { ValueStyle::Long }; // [[MonthsStyle]]
Display m_months_display { Display::Auto }; // [[MonthsDisplay]]
ValueStyle m_weeks_style { ValueStyle::Long }; // [[WeeksStyle]]
Display m_weeks_display { Display::Auto }; // [[WeeksDisplay]]
ValueStyle m_days_style { ValueStyle::Long }; // [[DaysStyle]]
Display m_days_display { Display::Auto }; // [[DaysDisplay]]
ValueStyle m_hours_style { ValueStyle::Long }; // [[HoursStyle]]
Display m_hours_display { Display::Auto }; // [[HoursDisplay]]
ValueStyle m_minutes_style { ValueStyle::Long }; // [[MinutesStyle]]
Display m_minutes_display { Display::Auto }; // [[MinutesDisplay]]
ValueStyle m_seconds_style { ValueStyle::Long }; // [[SecondsStyle]]
Display m_seconds_display { Display::Auto }; // [[SecondsDisplay]]
ValueStyle m_milliseconds_style { ValueStyle::Long }; // [[MillisecondsStyle]]
Display m_milliseconds_display { Display::Auto }; // [[MillisecondsDisplay]]
ValueStyle m_microseconds_style { ValueStyle::Long }; // [[MicrosecondsStyle]]
Display m_microseconds_display { Display::Auto }; // [[MicrosecondsDisplay]]
ValueStyle m_nanoseconds_style { ValueStyle::Long }; // [[NanosecondsStyle]]
Display m_nanoseconds_display { Display::Auto }; // [[NanosecondsDisplay]]
Optional<u8> m_fractional_digits; // [[FractionalDigits]]
};
struct DurationInstanceComponent {
void (DurationFormat::*set_style_slot)(StringView);
void (DurationFormat::*set_display_slot)(StringView);
StringView unit;
Span<StringView const> values;
StringView digital_default;
};
// Table 1: Components of Duration Instances, https://tc39.es/proposal-intl-duration-format/#table-duration-component
static constexpr AK::Array<StringView, 3> date_values = { "long"sv, "short"sv, "narrow"sv };
static constexpr AK::Array<StringView, 5> time_values = { "long"sv, "short"sv, "narrow"sv, "numeric"sv, "2-digit"sv };
static constexpr AK::Array<StringView, 4> sub_second_values = { "long"sv, "short"sv, "narrow"sv, "numeric"sv };
static constexpr AK::Array<DurationInstanceComponent, 10> duration_instances_components {
DurationInstanceComponent { &DurationFormat::set_years_style, &DurationFormat::set_years_display, "years"sv, date_values, "narrow"sv },
DurationInstanceComponent { &DurationFormat::set_months_style, &DurationFormat::set_months_display, "months"sv, date_values, "narrow"sv },
DurationInstanceComponent { &DurationFormat::set_weeks_style, &DurationFormat::set_weeks_display, "weeks"sv, date_values, "narrow"sv },
DurationInstanceComponent { &DurationFormat::set_days_style, &DurationFormat::set_days_display, "days"sv, date_values, "narrow"sv },
DurationInstanceComponent { &DurationFormat::set_hours_style, &DurationFormat::set_hours_display, "hours"sv, time_values, "numeric"sv },
DurationInstanceComponent { &DurationFormat::set_minutes_style, &DurationFormat::set_minutes_display, "minutes"sv, time_values, "numeric"sv },
DurationInstanceComponent { &DurationFormat::set_seconds_style, &DurationFormat::set_seconds_display, "seconds"sv, time_values, "numeric"sv },
DurationInstanceComponent { &DurationFormat::set_milliseconds_style, &DurationFormat::set_milliseconds_display, "milliseconds"sv, sub_second_values, "numeric"sv },
DurationInstanceComponent { &DurationFormat::set_microseconds_style, &DurationFormat::set_microseconds_display, "microseconds"sv, sub_second_values, "numeric"sv },
DurationInstanceComponent { &DurationFormat::set_nanoseconds_style, &DurationFormat::set_nanoseconds_display, "nanoseconds"sv, sub_second_values, "numeric"sv },
};
struct DurationUnitOptions {
String style;
String display;
};
ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(GlobalObject& global_object, String const& unit, Object const& options, StringView base_style, Span<StringView const> styles_list, StringView digital_base, Optional<String> const& previous_style);
}

View file

@ -0,0 +1,140 @@
/*
* Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Intl/AbstractOperations.h>
#include <LibJS/Runtime/Intl/DurationFormat.h>
#include <LibJS/Runtime/Intl/DurationFormatConstructor.h>
namespace JS::Intl {
// 1.2 The Intl.DurationFormat Constructor, https://tc39.es/proposal-intl-duration-format/#sec-intl-durationformat-constructor
DurationFormatConstructor::DurationFormatConstructor(GlobalObject& global_object)
: NativeFunction(vm().names.DurationFormat.as_string(), *global_object.function_prototype())
{
}
void DurationFormatConstructor::initialize(GlobalObject& global_object)
{
NativeFunction::initialize(global_object);
auto& vm = this->vm();
// 1.3.1 Intl.DurationFormat.prototype, https://tc39.es/proposal-intl-duration-format/#sec-Intl.DurationFormat.prototype
define_direct_property(vm.names.prototype, global_object.intl_duration_format_prototype(), 0);
define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
}
// 1.2.1 Intl.DurationFormat ( [ locales [ , options ] ] ), https://tc39.es/proposal-intl-duration-format/#sec-Intl.DurationFormat
ThrowCompletionOr<Value> DurationFormatConstructor::call()
{
// 1. If NewTarget is undefined, throw a TypeError exception.
return vm().throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Intl.DurationFormat");
}
// 1.2.1 Intl.DurationFormat ( [ locales [ , options ] ] ), https://tc39.es/proposal-intl-duration-format/#sec-Intl.DurationFormat
ThrowCompletionOr<Object*> DurationFormatConstructor::construct(FunctionObject& new_target)
{
auto& vm = this->vm();
auto& global_object = this->global_object();
auto locales = vm.argument(0);
auto options_value = vm.argument(1);
// 2. Let durationFormat be ? OrdinaryCreateFromConstructor(NewTarget, "%DurationFormatPrototype%", « [[InitializedDurationFormat]], [[Locale]], [[DataLocale]], [[NumberingSystem]], [[Style]], [[YearsStyle]], [[YearsDisplay]], [[MonthsStyle]], [[MonthsDisplay]] , [[WeeksStyle]], [[WeeksDisplay]] , [[DaysStyle]], [[DaysDisplay]] , [[HoursStyle]], [[HoursDisplay]] , [[MinutesStyle]], [[MinutesDisplay]] , [[SecondsStyle]], [[SecondsDisplay]] , [[MillisecondsStyle]], [[MillisecondsDisplay]] , [[MicrosecondsStyle]], [[MicrosecondsDisplay]] , [[NanosecondsStyle]], [[NanosecondsDisplay]], [[FractionalDigits]] »).
auto* duration_format = TRY(ordinary_create_from_constructor<DurationFormat>(global_object, new_target, &GlobalObject::intl_duration_format_prototype));
// 3. Let requestedLocales be ? CanonicalizeLocaleList(locales).
auto requested_locales = TRY(canonicalize_locale_list(global_object, locales));
// 4. Let options be ? GetOptionsObject(options).
auto* options = TRY(Temporal::get_options_object(global_object, options_value));
// 5. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
auto matcher = TRY(get_option(global_object, *options, vm.names.localeMatcher, OptionType::String, { "lookup"sv, "best fit"sv }, "best fit"sv));
// 6. Let numberingSystem be ? GetOption(options, "numberingSystem", "string", undefined, undefined).
auto numbering_system = TRY(get_option(global_object, *options, vm.names.numberingSystem, OptionType::String, {}, Empty {}));
// FIXME: Missing spec step - If numberingSystem is not undefined, then
if (!numbering_system.is_undefined()) {
// 7. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
if (numbering_system.is_undefined() || !Unicode::is_type_identifier(numbering_system.as_string().string()))
return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv);
}
// 8. Let opt be the Record { [[localeMatcher]]: matcher, [[nu]]: numberingSystem }.
LocaleOptions opt {};
opt.locale_matcher = matcher;
opt.nu = numbering_system.is_undefined() ? Optional<String>() : numbering_system.as_string().string();
// 9. Let r be ResolveLocale(%DurationFormat%.[[AvailableLocales]], requestedLocales, opt, %DurationFormat%.[[RelevantExtensionKeys]], %DurationFormat%.[[LocaleData]]).
auto result = resolve_locale(requested_locales, opt, DurationFormat::relevant_extension_keys());
// 10. Let locale be r.[[locale]].
auto locale = move(result.locale);
// 11. Set durationFormat.[[Locale]] to locale.
duration_format->set_locale(move(locale));
// 12. Set durationFormat.[[NumberingSystem]] to r.[[nu]].
if (result.nu.has_value())
duration_format->set_numbering_system(result.nu.release_value());
// 13. Let style be ? GetOption(options, "style", "string", « "long", "short", "narrow", "digital" », "long").
auto style = TRY(get_option(global_object, *options, vm.names.style, OptionType::String, { "long"sv, "short"sv, "narrow"sv, "digital"sv }, "long"sv));
// 14. Set durationFormat.[[Style]] to style.
duration_format->set_style(style.as_string().string());
// 15. Set durationFormat.[[DataLocale]] to r.[[dataLocale]].
duration_format->set_data_locale(move(result.data_locale));
// 16. Let prevStyle be undefined.
Optional<String> previous_style;
// 17. For each row in Table 1, except the header row, in table order, do
for (auto const& duration_instances_component : duration_instances_components) {
// a. Let styleSlot be the Style Slot value.
auto style_slot = duration_instances_component.set_style_slot;
// b. Let displaySlot be the Display Slot value.
auto display_slot = duration_instances_component.set_display_slot;
// c. Let unit be the Unit value.
auto unit = duration_instances_component.unit;
// d. Let valueList be the Values value.
auto value_list = duration_instances_component.values;
// e. Let digitalBase be the Digital Default value.
auto digital_base = duration_instances_component.digital_default;
// f. Let unitOptions be ? GetDurationUnitOptions(unit, options, style, valueList, digitalBase, prevStyle).
auto unit_options = TRY(get_duration_unit_options(global_object, unit, *options, style.as_string().string(), value_list, digital_base, previous_style));
// g. Set the value of the styleSlot slot of durationFormat to unitOptions.[[Style]].
(duration_format->*style_slot)(unit_options.style);
// h. Set the value of the displaySlot slot of durationFormat to unitOptions.[[Display]].
(duration_format->*display_slot)(unit_options.display);
// i. If unit is one of "hours", "minutes", "seconds", "milliseconds", or "microseconds", then
if (unit.is_one_of("hours"sv, "minutes"sv, "seconds"sv, "milliseconds"sv, "microseconds"sv)) {
// i. Set prevStyle to unitOptions.[[Style]].
previous_style = unit_options.style;
}
}
// 18. Set durationFormat.[[FractionalDigits]] to ? GetNumberOption(options, "fractionalDigits", 0, 9, undefined).
duration_format->set_fractional_digits(Optional<u8>(TRY(get_number_option(global_object, *options, vm.names.fractionalDigits, 0, 9, {}))));
// 19. Return durationFormat.
return duration_format;
}
}

View file

@ -0,0 +1,28 @@
/*
* Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/NativeFunction.h>
namespace JS::Intl {
class DurationFormatConstructor final : public NativeFunction {
JS_OBJECT(DurationFormatConstructor, NativeFunction);
public:
explicit DurationFormatConstructor(GlobalObject&);
virtual void initialize(GlobalObject&) override;
virtual ~DurationFormatConstructor() override = default;
virtual ThrowCompletionOr<Value> call() override;
virtual ThrowCompletionOr<Object*> construct(FunctionObject& new_target) override;
private:
virtual bool has_constructor() const override { return true; }
};
}

View file

@ -0,0 +1,28 @@
/*
* Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Intl/DurationFormatPrototype.h>
namespace JS::Intl {
// 1.4 Properties of the Intl.DurationFormat Prototype Object, https://tc39.es/proposal-intl-duration-format/#sec-properties-of-intl-durationformat-prototype-object
DurationFormatPrototype::DurationFormatPrototype(GlobalObject& global_object)
: PrototypeObject(*global_object.object_prototype())
{
}
void DurationFormatPrototype::initialize(GlobalObject& global_object)
{
Object::initialize(global_object);
auto& vm = this->vm();
// 1.4.2 Intl.DurationFormat.prototype [ @@toStringTag ], https://tc39.es/proposal-intl-duration-format/#sec-Intl.DurationFormat.prototype-@@tostringtag
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Intl.DurationFormat"), Attribute::Configurable);
}
}

View file

@ -0,0 +1,23 @@
/*
* Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/Intl/DurationFormat.h>
#include <LibJS/Runtime/PrototypeObject.h>
namespace JS::Intl {
class DurationFormatPrototype final : public PrototypeObject<DurationFormatPrototype, DurationFormat> {
JS_PROTOTYPE_OBJECT(DurationFormatPrototype, DurationFormat, Intl.DurationFormat);
public:
explicit DurationFormatPrototype(GlobalObject&);
virtual void initialize(GlobalObject&) override;
virtual ~DurationFormatPrototype() override = default;
};
}

View file

@ -11,6 +11,7 @@
#include <LibJS/Runtime/Intl/CollatorConstructor.h>
#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
#include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
#include <LibJS/Runtime/Intl/DurationFormatConstructor.h>
#include <LibJS/Runtime/Intl/Intl.h>
#include <LibJS/Runtime/Intl/ListFormatConstructor.h>
#include <LibJS/Runtime/Intl/LocaleConstructor.h>
@ -45,6 +46,7 @@ void Intl::initialize(GlobalObject& global_object)
define_direct_property(vm.names.Collator, global_object.intl_collator_constructor(), attr);
define_direct_property(vm.names.DateTimeFormat, global_object.intl_date_time_format_constructor(), attr);
define_direct_property(vm.names.DisplayNames, global_object.intl_display_names_constructor(), attr);
define_direct_property(vm.names.DurationFormat, global_object.intl_duration_format_constructor(), attr);
define_direct_property(vm.names.ListFormat, global_object.intl_list_format_constructor(), attr);
define_direct_property(vm.names.Locale, global_object.intl_locale_constructor(), attr);
define_direct_property(vm.names.NumberFormat, global_object.intl_number_format_constructor(), attr);