LibJS: Implement Temporal.PlainYearMonth.prototype.era

This commit is contained in:
Linus Groh 2021-08-27 20:21:37 +01:00
parent f2f671f340
commit b11ea98648
Notes: sideshowbarker 2024-07-18 05:11:38 +09:00
3 changed files with 42 additions and 0 deletions

View File

@ -36,6 +36,7 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.daysInMonth, days_in_month_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.monthsInYear, months_in_year_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.era, era_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.toString, to_string, 0, attr);
@ -183,6 +184,22 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::in_leap_year_getter)
return Value(calendar_in_leap_year(global_object, calendar, *year_month));
}
// 15.6.9.2 get Temporal.PlainYearMonth.prototype.era, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.era
JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::era_getter)
{
// 1. Let plainYearMonth be the this value.
// 2. Perform ? RequireInternalSlot(plainYearMonth, [[InitializedTemporalYearMonth]]).
auto* plain_year_month = typed_this(global_object);
if (vm.exception())
return {};
// 3. Let calendar be plainYearMonth.[[Calendar]].
auto& calendar = plain_year_month->calendar();
// 4. Return ? CalendarEra(calendar, plainYearMonth).
return calendar_era(global_object, calendar, *plain_year_month);
}
// 9.3.17 Temporal.PlainYearMonth.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.tostring
JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_string)
{

View File

@ -27,6 +27,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(days_in_month_getter);
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
JS_DECLARE_NATIVE_FUNCTION(era_getter);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(to_json);

View File

@ -0,0 +1,24 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
expect(plainYearMonth.era).toBeUndefined();
});
test("calendar with custom era function", () => {
const calendar = {
era() {
return "foo";
},
};
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7, calendar);
expect(plainYearMonth.era).toBe("foo");
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainYearMonth object", () => {
expect(() => {
Reflect.get(Temporal.PlainYearMonth.prototype, "era", "foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.PlainYearMonth");
});
});