LibJS: Implement Temporal.PlainDateTime.prototype.era

This commit is contained in:
Linus Groh 2021-08-27 20:16:44 +01:00
parent 418c22f9b3
commit 276d3f5089
Notes: sideshowbarker 2024-07-18 05:11:45 +09:00
3 changed files with 42 additions and 0 deletions

View File

@ -49,6 +49,7 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.daysInYear, days_in_year_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.withPlainTime, with_plain_time, 1, attr);
@ -359,6 +360,22 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::in_leap_year_getter)
return calendar_in_leap_year(global_object, calendar, *date_time);
}
// 15.6.6.2 get Temporal.PlainDateTime.prototype.era, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindatetime.prototype.era
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::era_getter)
{
// 1. Let plainDateTime be the this value.
// 2. Perform ? RequireInternalSlot(plainDateTime, [[InitializedTemporalDateTime]]).
auto* plain_date_time = typed_this(global_object);
if (vm.exception())
return {};
// 3. Let calendar be plainDateTime.[[Calendar]].
auto& calendar = plain_date_time->calendar();
// 4. Return ? CalendarEra(calendar, plainDateTime).
return calendar_era(global_object, calendar, *plain_date_time);
}
// 5.3.23 Temporal.PlainDateTime.prototype.withPlainTime ( [ plainTimeLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.withplaintime
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::with_plain_time)
{

View File

@ -38,6 +38,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(days_in_year_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(with_plain_time);
JS_DECLARE_NATIVE_FUNCTION(with_plain_date);
JS_DECLARE_NATIVE_FUNCTION(with_calendar);

View File

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