Skip to content

Commit 75b2a09

Browse files
trflynn89linusg
authored andcommitted
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.
1 parent 6dbdfb6 commit 75b2a09

File tree

12 files changed

+329
-4
lines changed

12 files changed

+329
-4
lines changed

Userland/Libraries/LibJS/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ set(SOURCES
8686
Runtime/GlobalObject.cpp
8787
Runtime/IndexedProperties.cpp
8888
Runtime/Intl/AbstractOperations.cpp
89+
Runtime/Intl/DateTimeFormat.cpp
90+
Runtime/Intl/DateTimeFormatConstructor.cpp
91+
Runtime/Intl/DateTimeFormatPrototype.cpp
8992
Runtime/Intl/DisplayNames.cpp
9093
Runtime/Intl/DisplayNamesConstructor.cpp
9194
Runtime/Intl/DisplayNamesPrototype.cpp

Userland/Libraries/LibJS/Forward.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@
6767
__JS_ENUMERATE(Float32Array, float32_array, Float32ArrayPrototype, Float32ArrayConstructor, float) \
6868
__JS_ENUMERATE(Float64Array, float64_array, Float64ArrayPrototype, Float64ArrayConstructor, double)
6969

70-
#define JS_ENUMERATE_INTL_OBJECTS \
71-
__JS_ENUMERATE(DisplayNames, display_names, DisplayNamesPrototype, DisplayNamesConstructor) \
72-
__JS_ENUMERATE(ListFormat, list_format, ListFormatPrototype, ListFormatConstructor) \
73-
__JS_ENUMERATE(Locale, locale, LocalePrototype, LocaleConstructor) \
70+
#define JS_ENUMERATE_INTL_OBJECTS \
71+
__JS_ENUMERATE(DateTimeFormat, date_time_format, DateTimeFormatPrototype, DateTimeFormatConstructor) \
72+
__JS_ENUMERATE(DisplayNames, display_names, DisplayNamesPrototype, DisplayNamesConstructor) \
73+
__JS_ENUMERATE(ListFormat, list_format, ListFormatPrototype, ListFormatConstructor) \
74+
__JS_ENUMERATE(Locale, locale, LocalePrototype, LocaleConstructor) \
7475
__JS_ENUMERATE(NumberFormat, number_format, NumberFormatPrototype, NumberFormatConstructor)
7576

7677
#define JS_ENUMERATE_TEMPORAL_OBJECTS \

Userland/Libraries/LibJS/Runtime/GlobalObject.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
#include <LibJS/Runtime/GeneratorObjectPrototype.h>
4848
#include <LibJS/Runtime/GlobalEnvironment.h>
4949
#include <LibJS/Runtime/GlobalObject.h>
50+
#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
51+
#include <LibJS/Runtime/Intl/DateTimeFormatPrototype.h>
5052
#include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
5153
#include <LibJS/Runtime/Intl/DisplayNamesPrototype.h>
5254
#include <LibJS/Runtime/Intl/Intl.h>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include <LibJS/Runtime/Intl/DateTimeFormat.h>
8+
9+
namespace JS::Intl {
10+
11+
Vector<StringView> const& DateTimeFormat::relevant_extension_keys()
12+
{
13+
// 11.3.3 Internal slots, https://tc39.es/ecma402/#sec-intl.datetimeformat-internal-slots
14+
// The value of the [[RelevantExtensionKeys]] internal slot is « "ca", "hc", "nu" ».
15+
static Vector<StringView> relevant_extension_keys { "ca"sv, "hc"sv, "nu"sv };
16+
return relevant_extension_keys;
17+
}
18+
19+
// 11 DateTimeFormat Objects, https://tc39.es/ecma402/#datetimeformat-objects
20+
DateTimeFormat::DateTimeFormat(Object& prototype)
21+
: Object(prototype)
22+
{
23+
}
24+
25+
DateTimeFormat::Style DateTimeFormat::style_from_string(StringView style)
26+
{
27+
if (style == "full"sv)
28+
return Style::Full;
29+
if (style == "long"sv)
30+
return Style::Long;
31+
if (style == "medium"sv)
32+
return Style::Medium;
33+
if (style == "short"sv)
34+
return Style::Short;
35+
VERIFY_NOT_REACHED();
36+
}
37+
38+
StringView DateTimeFormat::style_to_string(Style style)
39+
{
40+
switch (style) {
41+
case Style::Full:
42+
return "full"sv;
43+
case Style::Long:
44+
return "long"sv;
45+
case Style::Medium:
46+
return "medium"sv;
47+
case Style::Short:
48+
return "short"sv;
49+
default:
50+
VERIFY_NOT_REACHED();
51+
}
52+
}
53+
54+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <AK/String.h>
10+
#include <AK/StringView.h>
11+
#include <AK/Types.h>
12+
#include <AK/Vector.h>
13+
#include <LibJS/Runtime/Object.h>
14+
#include <LibUnicode/DateTimeFormat.h>
15+
16+
namespace JS::Intl {
17+
18+
class DateTimeFormat final
19+
: public Object
20+
, public Unicode::CalendarPattern {
21+
JS_OBJECT(DateTimeFormat, Object);
22+
23+
using Patterns = Unicode::CalendarPattern;
24+
25+
public:
26+
enum class Style {
27+
Full,
28+
Long,
29+
Medium,
30+
Short,
31+
};
32+
33+
static Vector<StringView> const& relevant_extension_keys(); // [[RelevantExtensionKeys]]
34+
35+
DateTimeFormat(Object& prototype);
36+
virtual ~DateTimeFormat() override = default;
37+
38+
String const& locale() const { return m_locale; }
39+
void set_locale(String locale) { m_locale = move(locale); }
40+
41+
String const& calendar() const { return m_calendar; }
42+
void set_calendar(String calendar) { m_calendar = move(calendar); }
43+
44+
String const& numbering_system() const { return m_numbering_system; }
45+
void set_numbering_system(String numbering_system) { m_numbering_system = move(numbering_system); }
46+
47+
bool has_hour_cycle() const { return m_hour_cycle.has_value(); }
48+
Unicode::HourCycle hour_cycle() const { return *m_hour_cycle; }
49+
StringView hour_cycle_string() const { return Unicode::hour_cycle_to_string(*m_hour_cycle); }
50+
void set_hour_cycle(StringView hour_cycle) { m_hour_cycle = Unicode::hour_cycle_from_string(hour_cycle); }
51+
void set_hour_cycle(Unicode::HourCycle hour_cycle) { m_hour_cycle = hour_cycle; }
52+
void clear_hour_cycle() { m_hour_cycle.clear(); }
53+
54+
String const& time_zone() const { return m_time_zone; }
55+
void set_time_zone(String time_zone) { m_time_zone = move(time_zone); }
56+
57+
bool has_date_style() const { return m_date_style.has_value(); }
58+
Style date_style() const { return *m_date_style; };
59+
StringView date_style_string() const { return style_to_string(*m_date_style); };
60+
void set_date_style(StringView style) { m_date_style = style_from_string(style); };
61+
62+
bool has_time_style() const { return m_time_style.has_value(); }
63+
Style time_style() const { return *m_time_style; };
64+
StringView time_style_string() const { return style_to_string(*m_time_style); };
65+
void set_time_style(StringView style) { m_time_style = style_from_string(style); };
66+
67+
String const& pattern() const { return Patterns::pattern; };
68+
void set_pattern(String pattern) { Patterns::pattern = move(pattern); }
69+
70+
bool has_era() const { return Patterns::era.has_value(); }
71+
Unicode::CalendarPatternStyle era() const { return *Patterns::era; };
72+
StringView era_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::era); }
73+
74+
bool has_year() const { return Patterns::year.has_value(); }
75+
Unicode::CalendarPatternStyle year() const { return *Patterns::year; };
76+
StringView year_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::year); }
77+
78+
bool has_month() const { return Patterns::month.has_value(); }
79+
Unicode::CalendarPatternStyle month() const { return *Patterns::month; };
80+
StringView month_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::month); }
81+
82+
bool has_weekday() const { return Patterns::weekday.has_value(); }
83+
Unicode::CalendarPatternStyle weekday() const { return *Patterns::weekday; };
84+
StringView weekday_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::weekday); }
85+
86+
bool has_day() const { return Patterns::day.has_value(); }
87+
Unicode::CalendarPatternStyle day() const { return *Patterns::day; };
88+
StringView day_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::day); }
89+
90+
bool has_day_period() const { return Patterns::day_period.has_value(); }
91+
Unicode::CalendarPatternStyle day_period() const { return *Patterns::day_period; };
92+
StringView day_period_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::day_period); }
93+
94+
bool has_hour() const { return Patterns::hour.has_value(); }
95+
Unicode::CalendarPatternStyle hour() const { return *Patterns::hour; };
96+
StringView hour_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::hour); }
97+
98+
bool has_minute() const { return Patterns::minute.has_value(); }
99+
Unicode::CalendarPatternStyle minute() const { return *Patterns::minute; };
100+
StringView minute_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::minute); }
101+
102+
bool has_second() const { return Patterns::second.has_value(); }
103+
Unicode::CalendarPatternStyle second() const { return *Patterns::second; };
104+
StringView second_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::second); }
105+
106+
bool has_fractional_second_digits() const { return Patterns::fractional_second_digits.has_value(); }
107+
u8 fractional_second_digits() const { return *Patterns::fractional_second_digits; };
108+
109+
bool has_time_zone_name() const { return Patterns::time_zone_name.has_value(); }
110+
Unicode::CalendarPatternStyle time_zone_name() const { return *Patterns::time_zone_name; };
111+
StringView time_zone_name_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::time_zone_name); }
112+
113+
private:
114+
static Style style_from_string(StringView style);
115+
static StringView style_to_string(Style style);
116+
117+
String m_locale; // [[Locale]]
118+
String m_calendar; // [[Calendar]]
119+
String m_numbering_system; // [[NumberingSystem]]
120+
Optional<Unicode::HourCycle> m_hour_cycle; // [[HourCycle]]
121+
String m_time_zone; // [[TimeZone]]
122+
Optional<Style> m_date_style; // [[DateStyle]]
123+
Optional<Style> m_time_style; // [[TimeStyle]]
124+
};
125+
126+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include <LibJS/Runtime/AbstractOperations.h>
8+
#include <LibJS/Runtime/GlobalObject.h>
9+
#include <LibJS/Runtime/Intl/DateTimeFormat.h>
10+
#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
11+
12+
namespace JS::Intl {
13+
14+
// 11.2 The Intl.DateTimeFormat Constructor, https://tc39.es/ecma402/#sec-intl-datetimeformat-constructor
15+
DateTimeFormatConstructor::DateTimeFormatConstructor(GlobalObject& global_object)
16+
: NativeFunction(vm().names.DateTimeFormat.as_string(), *global_object.function_prototype())
17+
{
18+
}
19+
20+
void DateTimeFormatConstructor::initialize(GlobalObject& global_object)
21+
{
22+
NativeFunction::initialize(global_object);
23+
24+
auto& vm = this->vm();
25+
26+
// 11.3.1 Intl.DateTimeFormat.prototype, https://tc39.es/ecma402/#sec-intl.datetimeformat.prototype
27+
define_direct_property(vm.names.prototype, global_object.intl_date_time_format_prototype(), 0);
28+
define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
29+
}
30+
31+
// 11.2.1 Intl.DateTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.datetimeformat
32+
ThrowCompletionOr<Value> DateTimeFormatConstructor::call()
33+
{
34+
// 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
35+
return TRY(construct(*this));
36+
}
37+
38+
// 11.2.1 Intl.DateTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.datetimeformat
39+
ThrowCompletionOr<Object*> DateTimeFormatConstructor::construct(FunctionObject& new_target)
40+
{
41+
auto& global_object = this->global_object();
42+
43+
// 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]] »).
44+
auto* date_time_format = TRY(ordinary_create_from_constructor<DateTimeFormat>(global_object, new_target, &GlobalObject::intl_date_time_format_prototype));
45+
46+
// 5. Return dateTimeFormat.
47+
return date_time_format;
48+
}
49+
50+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <LibJS/Runtime/NativeFunction.h>
10+
11+
namespace JS::Intl {
12+
13+
class DateTimeFormatConstructor final : public NativeFunction {
14+
JS_OBJECT(DateTimeFormatConstructor, NativeFunction);
15+
16+
public:
17+
explicit DateTimeFormatConstructor(GlobalObject&);
18+
virtual void initialize(GlobalObject&) override;
19+
virtual ~DateTimeFormatConstructor() override = default;
20+
21+
virtual ThrowCompletionOr<Value> call() override;
22+
virtual ThrowCompletionOr<Object*> construct(FunctionObject& new_target) override;
23+
24+
private:
25+
virtual bool has_constructor() const override { return true; }
26+
};
27+
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include <LibJS/Runtime/GlobalObject.h>
8+
#include <LibJS/Runtime/Intl/DateTimeFormatPrototype.h>
9+
10+
namespace JS::Intl {
11+
12+
// 11.4 Properties of the Intl.DateTimeFormat Prototype Object, https://tc39.es/ecma402/#sec-properties-of-intl-datetimeformat-prototype-object
13+
DateTimeFormatPrototype::DateTimeFormatPrototype(GlobalObject& global_object)
14+
: PrototypeObject(*global_object.object_prototype())
15+
{
16+
}
17+
18+
void DateTimeFormatPrototype::initialize(GlobalObject& global_object)
19+
{
20+
Object::initialize(global_object);
21+
22+
auto& vm = this->vm();
23+
24+
// 11.4.2 Intl.DateTimeFormat.prototype [ @@toStringTag ], https://tc39.es/ecma402/#sec-intl.datetimeformat.prototype-@@tostringtag
25+
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Intl.DateTimeFormat"), Attribute::Configurable);
26+
}
27+
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <LibJS/Runtime/Intl/DateTimeFormat.h>
10+
#include <LibJS/Runtime/PrototypeObject.h>
11+
12+
namespace JS::Intl {
13+
14+
class DateTimeFormatPrototype final : public PrototypeObject<DateTimeFormatPrototype, DateTimeFormat> {
15+
JS_PROTOTYPE_OBJECT(DateTimeFormatPrototype, DateTimeFormat, Intl.DateTimeFormat);
16+
17+
public:
18+
explicit DateTimeFormatPrototype(GlobalObject&);
19+
virtual void initialize(GlobalObject&) override;
20+
virtual ~DateTimeFormatPrototype() override = default;
21+
};
22+
23+
}

Userland/Libraries/LibJS/Runtime/Intl/Intl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <LibJS/Runtime/Array.h>
88
#include <LibJS/Runtime/GlobalObject.h>
99
#include <LibJS/Runtime/Intl/AbstractOperations.h>
10+
#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
1011
#include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
1112
#include <LibJS/Runtime/Intl/Intl.h>
1213
#include <LibJS/Runtime/Intl/ListFormatConstructor.h>
@@ -31,6 +32,7 @@ void Intl::initialize(GlobalObject& global_object)
3132
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Intl"), Attribute::Configurable);
3233

3334
u8 attr = Attribute::Writable | Attribute::Configurable;
35+
define_direct_property(vm.names.DateTimeFormat, global_object.intl_date_time_format_constructor(), attr);
3436
define_direct_property(vm.names.DisplayNames, global_object.intl_display_names_constructor(), attr);
3537
define_direct_property(vm.names.ListFormat, global_object.intl_list_format_constructor(), attr);
3638
define_direct_property(vm.names.Locale, global_object.intl_locale_constructor(), attr);

0 commit comments

Comments
 (0)