Browse Source

LibJS: Implement a nearly empty Intl.DateTimeFormat object

This adds plumbing for the Intl.DateTimeFormat object, constructor, and
prototype.

Note that unlike other Intl objects, the Intl.DateTimeFormat object has
a LibUnicode structure as a base. This is to prevent wild amounts of
code duplication between LibUnicode, Intl.DateTimeFormat, and other
not-yet-defined Intl structures, because there's 12 fields shared
between them.
Timothy Flynn 3 years ago
parent
commit
75b2a09a2f

+ 3 - 0
Userland/Libraries/LibJS/CMakeLists.txt

@@ -86,6 +86,9 @@ set(SOURCES
     Runtime/GlobalObject.cpp
     Runtime/IndexedProperties.cpp
     Runtime/Intl/AbstractOperations.cpp
+    Runtime/Intl/DateTimeFormat.cpp
+    Runtime/Intl/DateTimeFormatConstructor.cpp
+    Runtime/Intl/DateTimeFormatPrototype.cpp
     Runtime/Intl/DisplayNames.cpp
     Runtime/Intl/DisplayNamesConstructor.cpp
     Runtime/Intl/DisplayNamesPrototype.cpp

+ 5 - 4
Userland/Libraries/LibJS/Forward.h

@@ -67,10 +67,11 @@
     __JS_ENUMERATE(Float32Array, float32_array, Float32ArrayPrototype, Float32ArrayConstructor, float)                          \
     __JS_ENUMERATE(Float64Array, float64_array, Float64ArrayPrototype, Float64ArrayConstructor, double)
 
-#define JS_ENUMERATE_INTL_OBJECTS                                                               \
-    __JS_ENUMERATE(DisplayNames, display_names, DisplayNamesPrototype, DisplayNamesConstructor) \
-    __JS_ENUMERATE(ListFormat, list_format, ListFormatPrototype, ListFormatConstructor)         \
-    __JS_ENUMERATE(Locale, locale, LocalePrototype, LocaleConstructor)                          \
+#define JS_ENUMERATE_INTL_OBJECTS                                                                        \
+    __JS_ENUMERATE(DateTimeFormat, date_time_format, DateTimeFormatPrototype, DateTimeFormatConstructor) \
+    __JS_ENUMERATE(DisplayNames, display_names, DisplayNamesPrototype, DisplayNamesConstructor)          \
+    __JS_ENUMERATE(ListFormat, list_format, ListFormatPrototype, ListFormatConstructor)                  \
+    __JS_ENUMERATE(Locale, locale, LocalePrototype, LocaleConstructor)                                   \
     __JS_ENUMERATE(NumberFormat, number_format, NumberFormatPrototype, NumberFormatConstructor)
 
 #define JS_ENUMERATE_TEMPORAL_OBJECTS                                                                    \

+ 2 - 0
Userland/Libraries/LibJS/Runtime/GlobalObject.cpp

@@ -47,6 +47,8 @@
 #include <LibJS/Runtime/GeneratorObjectPrototype.h>
 #include <LibJS/Runtime/GlobalEnvironment.h>
 #include <LibJS/Runtime/GlobalObject.h>
+#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
+#include <LibJS/Runtime/Intl/DateTimeFormatPrototype.h>
 #include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
 #include <LibJS/Runtime/Intl/DisplayNamesPrototype.h>
 #include <LibJS/Runtime/Intl/Intl.h>

+ 54 - 0
Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.cpp

@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibJS/Runtime/Intl/DateTimeFormat.h>
+
+namespace JS::Intl {
+
+Vector<StringView> const& DateTimeFormat::relevant_extension_keys()
+{
+    // 11.3.3 Internal slots, https://tc39.es/ecma402/#sec-intl.datetimeformat-internal-slots
+    // The value of the [[RelevantExtensionKeys]] internal slot is « "ca", "hc", "nu" ».
+    static Vector<StringView> relevant_extension_keys { "ca"sv, "hc"sv, "nu"sv };
+    return relevant_extension_keys;
+}
+
+// 11 DateTimeFormat Objects, https://tc39.es/ecma402/#datetimeformat-objects
+DateTimeFormat::DateTimeFormat(Object& prototype)
+    : Object(prototype)
+{
+}
+
+DateTimeFormat::Style DateTimeFormat::style_from_string(StringView style)
+{
+    if (style == "full"sv)
+        return Style::Full;
+    if (style == "long"sv)
+        return Style::Long;
+    if (style == "medium"sv)
+        return Style::Medium;
+    if (style == "short"sv)
+        return Style::Short;
+    VERIFY_NOT_REACHED();
+}
+
+StringView DateTimeFormat::style_to_string(Style style)
+{
+    switch (style) {
+    case Style::Full:
+        return "full"sv;
+    case Style::Long:
+        return "long"sv;
+    case Style::Medium:
+        return "medium"sv;
+    case Style::Short:
+        return "short"sv;
+    default:
+        VERIFY_NOT_REACHED();
+    }
+}
+
+}

+ 126 - 0
Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.h

@@ -0,0 +1,126 @@
+/*
+ * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/String.h>
+#include <AK/StringView.h>
+#include <AK/Types.h>
+#include <AK/Vector.h>
+#include <LibJS/Runtime/Object.h>
+#include <LibUnicode/DateTimeFormat.h>
+
+namespace JS::Intl {
+
+class DateTimeFormat final
+    : public Object
+    , public Unicode::CalendarPattern {
+    JS_OBJECT(DateTimeFormat, Object);
+
+    using Patterns = Unicode::CalendarPattern;
+
+public:
+    enum class Style {
+        Full,
+        Long,
+        Medium,
+        Short,
+    };
+
+    static Vector<StringView> const& relevant_extension_keys(); // [[RelevantExtensionKeys]]
+
+    DateTimeFormat(Object& prototype);
+    virtual ~DateTimeFormat() override = default;
+
+    String const& locale() const { return m_locale; }
+    void set_locale(String locale) { m_locale = move(locale); }
+
+    String const& calendar() const { return m_calendar; }
+    void set_calendar(String calendar) { m_calendar = move(calendar); }
+
+    String const& numbering_system() const { return m_numbering_system; }
+    void set_numbering_system(String numbering_system) { m_numbering_system = move(numbering_system); }
+
+    bool has_hour_cycle() const { return m_hour_cycle.has_value(); }
+    Unicode::HourCycle hour_cycle() const { return *m_hour_cycle; }
+    StringView hour_cycle_string() const { return Unicode::hour_cycle_to_string(*m_hour_cycle); }
+    void set_hour_cycle(StringView hour_cycle) { m_hour_cycle = Unicode::hour_cycle_from_string(hour_cycle); }
+    void set_hour_cycle(Unicode::HourCycle hour_cycle) { m_hour_cycle = hour_cycle; }
+    void clear_hour_cycle() { m_hour_cycle.clear(); }
+
+    String const& time_zone() const { return m_time_zone; }
+    void set_time_zone(String time_zone) { m_time_zone = move(time_zone); }
+
+    bool has_date_style() const { return m_date_style.has_value(); }
+    Style date_style() const { return *m_date_style; };
+    StringView date_style_string() const { return style_to_string(*m_date_style); };
+    void set_date_style(StringView style) { m_date_style = style_from_string(style); };
+
+    bool has_time_style() const { return m_time_style.has_value(); }
+    Style time_style() const { return *m_time_style; };
+    StringView time_style_string() const { return style_to_string(*m_time_style); };
+    void set_time_style(StringView style) { m_time_style = style_from_string(style); };
+
+    String const& pattern() const { return Patterns::pattern; };
+    void set_pattern(String pattern) { Patterns::pattern = move(pattern); }
+
+    bool has_era() const { return Patterns::era.has_value(); }
+    Unicode::CalendarPatternStyle era() const { return *Patterns::era; };
+    StringView era_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::era); }
+
+    bool has_year() const { return Patterns::year.has_value(); }
+    Unicode::CalendarPatternStyle year() const { return *Patterns::year; };
+    StringView year_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::year); }
+
+    bool has_month() const { return Patterns::month.has_value(); }
+    Unicode::CalendarPatternStyle month() const { return *Patterns::month; };
+    StringView month_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::month); }
+
+    bool has_weekday() const { return Patterns::weekday.has_value(); }
+    Unicode::CalendarPatternStyle weekday() const { return *Patterns::weekday; };
+    StringView weekday_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::weekday); }
+
+    bool has_day() const { return Patterns::day.has_value(); }
+    Unicode::CalendarPatternStyle day() const { return *Patterns::day; };
+    StringView day_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::day); }
+
+    bool has_day_period() const { return Patterns::day_period.has_value(); }
+    Unicode::CalendarPatternStyle day_period() const { return *Patterns::day_period; };
+    StringView day_period_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::day_period); }
+
+    bool has_hour() const { return Patterns::hour.has_value(); }
+    Unicode::CalendarPatternStyle hour() const { return *Patterns::hour; };
+    StringView hour_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::hour); }
+
+    bool has_minute() const { return Patterns::minute.has_value(); }
+    Unicode::CalendarPatternStyle minute() const { return *Patterns::minute; };
+    StringView minute_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::minute); }
+
+    bool has_second() const { return Patterns::second.has_value(); }
+    Unicode::CalendarPatternStyle second() const { return *Patterns::second; };
+    StringView second_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::second); }
+
+    bool has_fractional_second_digits() const { return Patterns::fractional_second_digits.has_value(); }
+    u8 fractional_second_digits() const { return *Patterns::fractional_second_digits; };
+
+    bool has_time_zone_name() const { return Patterns::time_zone_name.has_value(); }
+    Unicode::CalendarPatternStyle time_zone_name() const { return *Patterns::time_zone_name; };
+    StringView time_zone_name_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::time_zone_name); }
+
+private:
+    static Style style_from_string(StringView style);
+    static StringView style_to_string(Style style);
+
+    String m_locale;                           // [[Locale]]
+    String m_calendar;                         // [[Calendar]]
+    String m_numbering_system;                 // [[NumberingSystem]]
+    Optional<Unicode::HourCycle> m_hour_cycle; // [[HourCycle]]
+    String m_time_zone;                        // [[TimeZone]]
+    Optional<Style> m_date_style;              // [[DateStyle]]
+    Optional<Style> m_time_style;              // [[TimeStyle]]
+};
+
+}

+ 50 - 0
Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp

@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibJS/Runtime/AbstractOperations.h>
+#include <LibJS/Runtime/GlobalObject.h>
+#include <LibJS/Runtime/Intl/DateTimeFormat.h>
+#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
+
+namespace JS::Intl {
+
+// 11.2 The Intl.DateTimeFormat Constructor, https://tc39.es/ecma402/#sec-intl-datetimeformat-constructor
+DateTimeFormatConstructor::DateTimeFormatConstructor(GlobalObject& global_object)
+    : NativeFunction(vm().names.DateTimeFormat.as_string(), *global_object.function_prototype())
+{
+}
+
+void DateTimeFormatConstructor::initialize(GlobalObject& global_object)
+{
+    NativeFunction::initialize(global_object);
+
+    auto& vm = this->vm();
+
+    // 11.3.1 Intl.DateTimeFormat.prototype, https://tc39.es/ecma402/#sec-intl.datetimeformat.prototype
+    define_direct_property(vm.names.prototype, global_object.intl_date_time_format_prototype(), 0);
+    define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
+}
+
+// 11.2.1 Intl.DateTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.datetimeformat
+ThrowCompletionOr<Value> DateTimeFormatConstructor::call()
+{
+    // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
+    return TRY(construct(*this));
+}
+
+// 11.2.1 Intl.DateTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.datetimeformat
+ThrowCompletionOr<Object*> DateTimeFormatConstructor::construct(FunctionObject& new_target)
+{
+    auto& global_object = this->global_object();
+
+    // 2. Let dateTimeFormat be ? OrdinaryCreateFromConstructor(newTarget, "%DateTimeFormat.prototype%", « [[InitializedDateTimeFormat]], [[Locale]], [[Calendar]], [[NumberingSystem]], [[TimeZone]], [[Weekday]], [[Era]], [[Year]], [[Month]], [[Day]], [[DayPeriod]], [[Hour]], [[Minute]], [[Second]], [[FractionalSecondDigits]], [[TimeZoneName]], [[HourCycle]], [[Pattern]], [[BoundFormat]] »).
+    auto* date_time_format = TRY(ordinary_create_from_constructor<DateTimeFormat>(global_object, new_target, &GlobalObject::intl_date_time_format_prototype));
+
+    // 5. Return dateTimeFormat.
+    return date_time_format;
+}
+
+}

+ 28 - 0
Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.h

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

+ 28 - 0
Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatPrototype.cpp

@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibJS/Runtime/GlobalObject.h>
+#include <LibJS/Runtime/Intl/DateTimeFormatPrototype.h>
+
+namespace JS::Intl {
+
+// 11.4 Properties of the Intl.DateTimeFormat Prototype Object, https://tc39.es/ecma402/#sec-properties-of-intl-datetimeformat-prototype-object
+DateTimeFormatPrototype::DateTimeFormatPrototype(GlobalObject& global_object)
+    : PrototypeObject(*global_object.object_prototype())
+{
+}
+
+void DateTimeFormatPrototype::initialize(GlobalObject& global_object)
+{
+    Object::initialize(global_object);
+
+    auto& vm = this->vm();
+
+    // 11.4.2 Intl.DateTimeFormat.prototype [ @@toStringTag ], https://tc39.es/ecma402/#sec-intl.datetimeformat.prototype-@@tostringtag
+    define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Intl.DateTimeFormat"), Attribute::Configurable);
+}
+
+}

+ 23 - 0
Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatPrototype.h

@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibJS/Runtime/Intl/DateTimeFormat.h>
+#include <LibJS/Runtime/PrototypeObject.h>
+
+namespace JS::Intl {
+
+class DateTimeFormatPrototype final : public PrototypeObject<DateTimeFormatPrototype, DateTimeFormat> {
+    JS_PROTOTYPE_OBJECT(DateTimeFormatPrototype, DateTimeFormat, Intl.DateTimeFormat);
+
+public:
+    explicit DateTimeFormatPrototype(GlobalObject&);
+    virtual void initialize(GlobalObject&) override;
+    virtual ~DateTimeFormatPrototype() override = default;
+};
+
+}

+ 2 - 0
Userland/Libraries/LibJS/Runtime/Intl/Intl.cpp

@@ -7,6 +7,7 @@
 #include <LibJS/Runtime/Array.h>
 #include <LibJS/Runtime/GlobalObject.h>
 #include <LibJS/Runtime/Intl/AbstractOperations.h>
+#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
 #include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
 #include <LibJS/Runtime/Intl/Intl.h>
 #include <LibJS/Runtime/Intl/ListFormatConstructor.h>
@@ -31,6 +32,7 @@ void Intl::initialize(GlobalObject& global_object)
     define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Intl"), Attribute::Configurable);
 
     u8 attr = Attribute::Writable | Attribute::Configurable;
+    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.ListFormat, global_object.intl_list_format_constructor(), attr);
     define_direct_property(vm.names.Locale, global_object.intl_locale_constructor(), attr);

+ 3 - 0
Userland/Libraries/LibJS/Tests/builtins/Intl/DateTimeFormat/DateTimeFormat.@@toStringTag.js

@@ -0,0 +1,3 @@
+test("basic functionality", () => {
+    expect(Intl.DateTimeFormat.prototype[Symbol.toStringTag]).toBe("Intl.DateTimeFormat");
+});

+ 5 - 0
Userland/Libraries/LibJS/Tests/builtins/Intl/DateTimeFormat/DateTimeFormat.js

@@ -0,0 +1,5 @@
+describe("normal behavior", () => {
+    test("length is 0", () => {
+        expect(Intl.DateTimeFormat).toHaveLength(0);
+    });
+});