LibJS: Implement Temporal.PlainYearMonth.prototype.monthCode

This commit is contained in:
Linus Groh 2021-08-07 23:47:26 +01:00
parent 0edec6578b
commit 5ec70792fd
Notes: sideshowbarker 2024-07-18 07:13:09 +09:00
3 changed files with 32 additions and 0 deletions

View file

@ -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));
}
}

View file

@ -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);
};
}

View file

@ -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");
});
});