summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-08-27 19:52:41 +0100
committerLinus Groh <mail@linusgroh.de>2021-08-27 23:36:52 +0100
commitc3e0d78ba63971d86f06fb6376d12cf02e083244 (patch)
tree887fa6c72d639d7274b756b5e72aaa121d1145ac
parentf746850d1c81847e194de6a5b17b0ef7b3c8c27e (diff)
downloadserenity-c3e0d78ba63971d86f06fb6376d12cf02e083244.zip
LibJS: Implement Temporal.Calendar.prototype.eraYear()
-rw-r--r--Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp33
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.eraYear.js26
4 files changed, 61 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
index 3e72e5ff55..745052bfdb 100644
--- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
+++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
@@ -136,6 +136,7 @@ namespace JS {
P(epochSeconds) \
P(equals) \
P(era) \
+ P(eraYear) \
P(error) \
P(errors) \
P(escape) \
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp
index 9672c2b749..2410713e1f 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp
@@ -55,6 +55,7 @@ void CalendarPrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.toString, to_string, 0, attr);
define_native_function(vm.names.toJSON, to_json, 0, attr);
define_native_function(vm.names.era, era, 1, attr);
+ define_native_function(vm.names.eraYear, era_year, 1, attr);
}
static Calendar* typed_this(GlobalObject& global_object)
@@ -592,4 +593,36 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::era)
VERIFY_NOT_REACHED();
}
+// 15.6.2.7 Temporal.Calendar.prototype.eraYear ( temporalDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.erayear
+JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::era_year)
+{
+ auto temporal_date_like = vm.argument(0);
+
+ // 1. Let calendar be the this value.
+ // 2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]).
+ auto* calendar = typed_this(global_object);
+ if (vm.exception())
+ return {};
+
+ // 3. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]], or [[InitializedTemporalYearMonth]] internal slot, then
+ if (!temporal_date_like.is_object() || !(is<PlainDate>(temporal_date_like.as_object()) || is<PlainDateTime>(temporal_date_like.as_object()) || is<PlainYearMonth>(temporal_date_like.as_object()))) {
+ // a. Set temporalDateLike to ? ToTemporalDate(temporalDateLike).
+ temporal_date_like = to_temporal_date(global_object, temporal_date_like);
+ if (vm.exception())
+ return {};
+ }
+
+ // 4. If calendar.[[Identifier]] is "iso8601", then
+ if (calendar->identifier() == "iso8601"sv) {
+ // a. Return undefined.
+ return js_undefined();
+ }
+
+ // 5. Let eraYear be the result of implementation-defined processing of temporalDateLike and the value of calendar.[[Identifier]].
+ // 6. Return 𝔽(eraYear).
+
+ // NOTE: No support for non-iso8601 calendars yet.
+ VERIFY_NOT_REACHED();
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h
index 94a8ceeaa5..cbbf458614 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h
@@ -40,6 +40,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_json);
JS_DECLARE_NATIVE_FUNCTION(era);
+ JS_DECLARE_NATIVE_FUNCTION(era_year);
};
}
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.eraYear.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.eraYear.js
new file mode 100644
index 0000000000..9ec905b5f4
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.eraYear.js
@@ -0,0 +1,26 @@
+describe("correct behavior", () => {
+ test("length is 1", () => {
+ expect(Temporal.Calendar.prototype.eraYear).toHaveLength(1);
+ });
+
+ test("basic functionality", () => {
+ const plainDate = new Temporal.PlainDate(2021, 7, 6);
+ const calendar = new Temporal.Calendar("iso8601");
+ expect(calendar.eraYear(plainDate)).toBeUndefined();
+ });
+});
+
+describe("errors", () => {
+ test("this value must be a Temporal.Calendar object", () => {
+ expect(() => {
+ Temporal.Calendar.prototype.eraYear.call("foo");
+ }).toThrowWithMessage(TypeError, "Not a Temporal.Calendar");
+ });
+
+ test("argument must be date-like", () => {
+ const calendar = new Temporal.Calendar("iso8601");
+ expect(() => {
+ calendar.eraYear({});
+ }).toThrowWithMessage(TypeError, "Required property year is missing or undefined");
+ });
+});