Browse Source

LibJS: Implement Temporal.Calendar.prototype.eraYear()

Linus Groh 3 years ago
parent
commit
c3e0d78ba6

+ 1 - 0
Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h

@@ -136,6 +136,7 @@ namespace JS {
     P(epochSeconds)                          \
     P(epochSeconds)                          \
     P(equals)                                \
     P(equals)                                \
     P(era)                                   \
     P(era)                                   \
+    P(eraYear)                               \
     P(error)                                 \
     P(error)                                 \
     P(errors)                                \
     P(errors)                                \
     P(escape)                                \
     P(escape)                                \

+ 33 - 0
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.toString, to_string, 0, attr);
     define_native_function(vm.names.toJSON, to_json, 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.era, era, 1, attr);
+    define_native_function(vm.names.eraYear, era_year, 1, attr);
 }
 }
 
 
 static Calendar* typed_this(GlobalObject& global_object)
 static Calendar* typed_this(GlobalObject& global_object)
@@ -592,4 +593,36 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::era)
     VERIFY_NOT_REACHED();
     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();
+}
+
 }
 }

+ 1 - 0
Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h

@@ -40,6 +40,7 @@ private:
     JS_DECLARE_NATIVE_FUNCTION(to_string);
     JS_DECLARE_NATIVE_FUNCTION(to_string);
     JS_DECLARE_NATIVE_FUNCTION(to_json);
     JS_DECLARE_NATIVE_FUNCTION(to_json);
     JS_DECLARE_NATIVE_FUNCTION(era);
     JS_DECLARE_NATIVE_FUNCTION(era);
+    JS_DECLARE_NATIVE_FUNCTION(era_year);
 };
 };
 
 
 }
 }

+ 26 - 0
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");
+    });
+});