mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 11:57:35 +00:00
LibJS: Implement Temporal.PlainDate.prototype.era
This commit is contained in:
parent
c3e0d78ba6
commit
6f7d6d917e
5 changed files with 67 additions and 0 deletions
|
@ -288,6 +288,30 @@ Value calendar_in_leap_year(GlobalObject& global_object, Object& calendar, Objec
|
||||||
return Value(&calendar).invoke(global_object, vm.names.inLeapYear, &date_like);
|
return Value(&calendar).invoke(global_object, vm.names.inLeapYear, &date_like);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 15.6.1.2 CalendarEra ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarera
|
||||||
|
Value calendar_era(GlobalObject& global_object, Object& calendar, Object& date_like)
|
||||||
|
{
|
||||||
|
auto& vm = global_object.vm();
|
||||||
|
|
||||||
|
// 1. Assert: Type(calendar) is Object.
|
||||||
|
|
||||||
|
// 2. Let result be ? Invoke(calendar, "era", « dateLike »).
|
||||||
|
auto result = Value(&calendar).invoke(global_object, vm.names.era, &date_like);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// 3. If result is not undefined, set result to ? ToString(result).
|
||||||
|
if (!result.is_undefined()) {
|
||||||
|
auto result_string = result.to_string(global_object);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
result = js_string(vm, move(result_string));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Return result.
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
// 12.1.21 ToTemporalCalendar ( temporalCalendarLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalcalendar
|
// 12.1.21 ToTemporalCalendar ( temporalCalendarLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalcalendar
|
||||||
Object* to_temporal_calendar(GlobalObject& global_object, Value temporal_calendar_like)
|
Object* to_temporal_calendar(GlobalObject& global_object, Value temporal_calendar_like)
|
||||||
{
|
{
|
||||||
|
|
|
@ -46,6 +46,7 @@ Value calendar_days_in_month(GlobalObject&, Object& calendar, Object& date_like)
|
||||||
Value calendar_days_in_year(GlobalObject&, Object& calendar, Object& date_like);
|
Value calendar_days_in_year(GlobalObject&, Object& calendar, Object& date_like);
|
||||||
Value calendar_months_in_year(GlobalObject&, Object& calendar, Object& date_like);
|
Value calendar_months_in_year(GlobalObject&, Object& calendar, Object& date_like);
|
||||||
Value calendar_in_leap_year(GlobalObject&, Object& calendar, Object& date_like);
|
Value calendar_in_leap_year(GlobalObject&, Object& calendar, Object& date_like);
|
||||||
|
Value calendar_era(GlobalObject&, Object& calendar, Object& date_like);
|
||||||
Object* to_temporal_calendar(GlobalObject&, Value);
|
Object* to_temporal_calendar(GlobalObject&, Value);
|
||||||
Object* to_temporal_calendar_with_iso_default(GlobalObject&, Value);
|
Object* to_temporal_calendar_with_iso_default(GlobalObject&, Value);
|
||||||
Object* get_temporal_calendar_with_iso_default(GlobalObject&, Object&);
|
Object* get_temporal_calendar_with_iso_default(GlobalObject&, Object&);
|
||||||
|
|
|
@ -46,6 +46,7 @@ void PlainDatePrototype::initialize(GlobalObject& global_object)
|
||||||
define_native_accessor(vm.names.daysInYear, days_in_year_getter, {}, Attribute::Configurable);
|
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.monthsInYear, months_in_year_getter, {}, Attribute::Configurable);
|
||||||
define_native_accessor(vm.names.inLeapYear, in_leap_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;
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||||
define_native_function(vm.names.toPlainYearMonth, to_plain_year_month, 0, attr);
|
define_native_function(vm.names.toPlainYearMonth, to_plain_year_month, 0, attr);
|
||||||
|
@ -278,6 +279,22 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::in_leap_year_getter)
|
||||||
return Value(calendar_in_leap_year(global_object, calendar, *temporal_date));
|
return Value(calendar_in_leap_year(global_object, calendar, *temporal_date));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 15.6.5.2 get Temporal.PlainDate.prototype.era, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.era
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::era_getter)
|
||||||
|
{
|
||||||
|
// 1. Let plainDate be the this value.
|
||||||
|
// 2. Perform ? RequireInternalSlot(plainDate, [[InitializedTemporalDate]]).
|
||||||
|
auto* plain_date = typed_this(global_object);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// 3. Let calendar be plainDate.[[Calendar]].
|
||||||
|
auto& calendar = plain_date->calendar();
|
||||||
|
|
||||||
|
// 4. Return ? CalendarEra(calendar, plainDate).
|
||||||
|
return calendar_era(global_object, calendar, *plain_date);
|
||||||
|
}
|
||||||
|
|
||||||
// 3.3.16 Temporal.PlainDate.prototype.toPlainYearMonth ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.toplainyearmonth
|
// 3.3.16 Temporal.PlainDate.prototype.toPlainYearMonth ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.toplainyearmonth
|
||||||
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_plain_year_month)
|
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_plain_year_month)
|
||||||
{
|
{
|
||||||
|
|
|
@ -32,6 +32,7 @@ private:
|
||||||
JS_DECLARE_NATIVE_FUNCTION(days_in_year_getter);
|
JS_DECLARE_NATIVE_FUNCTION(days_in_year_getter);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
|
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
|
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(era_getter);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(to_plain_year_month);
|
JS_DECLARE_NATIVE_FUNCTION(to_plain_year_month);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(to_plain_month_day);
|
JS_DECLARE_NATIVE_FUNCTION(to_plain_month_day);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
|
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
describe("correct behavior", () => {
|
||||||
|
test("basic functionality", () => {
|
||||||
|
const plainDate = new Temporal.PlainDate(2021, 7, 6);
|
||||||
|
expect(plainDate.era).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("calendar with custom era function", () => {
|
||||||
|
const calendar = {
|
||||||
|
era() {
|
||||||
|
return "foo";
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const plainDate = new Temporal.PlainDate(2021, 7, 6, calendar);
|
||||||
|
expect(plainDate.era).toBe("foo");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("errors", () => {
|
||||||
|
test("this value must be a Temporal.PlainDate object", () => {
|
||||||
|
expect(() => {
|
||||||
|
Reflect.get(Temporal.PlainDate.prototype, "era", "foo");
|
||||||
|
}).toThrowWithMessage(TypeError, "Not a Temporal.PlainDate");
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue