LibJS: Add calendar id getter to PlainDatePrototype

This commit is contained in:
Pavel Shliak 2024-10-25 14:47:48 +04:00 committed by Andreas Kling
commit f7cf7382c9
Notes: github-actions[bot] 2024-10-30 09:31:16 +00:00
3 changed files with 29 additions and 0 deletions

View file

@ -52,6 +52,7 @@ void PlainDatePrototype::initialize(Realm& realm)
define_native_accessor(realm, vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable);
define_native_accessor(realm, vm.names.era, era_getter, {}, Attribute::Configurable);
define_native_accessor(realm, vm.names.eraYear, era_year_getter, {}, Attribute::Configurable);
define_native_accessor(realm, vm.names.calendarId, calendar_id_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(realm, vm.names.toPlainYearMonth, to_plain_year_month, 0, attr);
@ -649,4 +650,16 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::value_of)
return vm.throw_completion<TypeError>(ErrorType::Convert, "Temporal.PlainDate", "a primitive value");
}
// 3.3.3 get Temporal.PlainDate.prototype.calendarId, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.calendarid
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::calendar_id_getter)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto temporal_date = TRY(typed_this_object(vm));
// 3. Return temporalDate.[[Calendar]].
auto& calendar = static_cast<Calendar&>(temporal_date->calendar());
return PrimitiveString::create(vm, calendar.identifier());
}
}

View file

@ -23,6 +23,7 @@ private:
explicit PlainDatePrototype(Realm&);
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
JS_DECLARE_NATIVE_FUNCTION(calendar_id_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,15 @@
describe("correct behavior", () => {
test("calendarId basic functionality", () => {
const calendar = "iso8601";
const plainDate = new Temporal.PlainDate(2000, 5, 1, calendar);
expect(plainDate.calendarId).toBe("iso8601");
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainDate object", () => {
expect(() => {
Reflect.get(Temporal.PlainDate.prototype, "calendarId", "foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDate");
});
});