1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:27:43 +00:00

LibJS: Implement Temporal.PlainDateTime.prototype.eraYear

This commit is contained in:
Linus Groh 2021-08-27 20:18:37 +01:00
parent 276d3f5089
commit f2f671f340
3 changed files with 42 additions and 0 deletions

View file

@ -50,6 +50,7 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object)
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);
define_native_accessor(vm.names.eraYear, era_year_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.withPlainTime, with_plain_time, 1, attr);
@ -376,6 +377,22 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::era_getter)
return calendar_era(global_object, calendar, *plain_date_time);
}
// 15.6.6.3 get Temporal.PlainDateTime.prototype.eraYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindatetime.prototype.erayear
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::era_year_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 ? CalendarEraYear(calendar, plainDateTime).
return calendar_era_year(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

@ -39,6 +39,7 @@ private:
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(era_year_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.eraYear).toBeUndefined();
});
test("calendar with custom eraYear function", () => {
const calendar = {
eraYear() {
return 123;
},
};
const plainDateTime = new Temporal.PlainDateTime(2021, 7, 6, 18, 14, 47, 0, 0, 0, calendar);
expect(plainDateTime.eraYear).toBe(123);
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainDateTime object", () => {
expect(() => {
Reflect.get(Temporal.PlainDateTime.prototype, "eraYear", "foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.PlainDateTime");
});
});