summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-08-07 23:47:26 +0100
committerLinus Groh <mail@linusgroh.de>2021-08-08 17:45:06 +0100
commit5ec70792fdad13eae791fb3bd56ec6132db6c2c4 (patch)
treeba687c31ad692c8fc1216d357d6ffe107a246088
parent0edec6578b8dbfed22982f61f7e8c5aead419859 (diff)
downloadserenity-5ec70792fdad13eae791fb3bd56ec6132db6c2c4.zip
LibJS: Implement Temporal.PlainYearMonth.prototype.monthCode
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp17
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.monthCode.js14
3 files changed, 32 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp
index d6151d6775..5f473c13c8 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp
@@ -30,6 +30,7 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.year, year_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.month, month_getter, {}, Attribute::Configurable);
+ define_native_accessor(vm.names.monthCode, month_code_getter, {}, Attribute::Configurable);
}
static PlainYearMonth* typed_this(GlobalObject& global_object)
@@ -90,4 +91,20 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::month_getter)
return Value(calendar_month(global_object, calendar, *year_month));
}
+// 9.3.6 get Temporal.PlainYearMonth.prototype.monthCode, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.monthCode
+JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::month_code_getter)
+{
+ // 1. Let yearMonth be the this value.
+ // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
+ auto* year_month = typed_this(global_object);
+ if (vm.exception())
+ return {};
+
+ // 3. Let calendar be yearMonth.[[Calendar]].
+ auto& calendar = year_month->calendar();
+
+ // 4. Return ? CalendarMonthCode(calendar, yearMonth).
+ return js_string(vm, calendar_month_code(global_object, calendar, *year_month));
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h
index 680ed8dd2d..5b9399a6c7 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h
@@ -22,6 +22,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
JS_DECLARE_NATIVE_FUNCTION(year_getter);
JS_DECLARE_NATIVE_FUNCTION(month_getter);
+ JS_DECLARE_NATIVE_FUNCTION(month_code_getter);
};
}
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.monthCode.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.monthCode.js
new file mode 100644
index 0000000000..f2e5adb2f0
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.monthCode.js
@@ -0,0 +1,14 @@
+describe("correct behavior", () => {
+ test("basic functionality", () => {
+ const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
+ expect(plainYearMonth.monthCode).toBe("M07");
+ });
+});
+
+describe("errors", () => {
+ test("this value must be a Temporal.PlainYearMonth object", () => {
+ expect(() => {
+ Reflect.get(Temporal.PlainYearMonth.prototype, "monthCode", "foo");
+ }).toThrowWithMessage(TypeError, "Not a Temporal.PlainYearMonth");
+ });
+});