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

LibJS: Implement Temporal.Calendar.prototype.month

This commit is contained in:
Idan Horowitz 2021-07-23 16:46:46 +03:00 committed by Linus Groh
parent 8434ca6c4b
commit 3bec18432a
5 changed files with 60 additions and 0 deletions

View file

@ -453,4 +453,19 @@ i32 iso_year(Object& temporal_object)
VERIFY_NOT_REACHED();
}
// 12.1.42 ISOMonth ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isomonth
u8 iso_month(Object& temporal_object)
{
// 1. Assert: temporalObject has an [[ISOMonth]] internal slot.
// NOTE: Asserted by the VERIFY_NOT_REACHED at the end
// 2. Return 𝔽(temporalObject.[[ISOMonth]]).
// TODO: add the rest of the builtins with a [[ISOMonth]] slot (PlainYearMonth, PlainMonthDay)
if (is<PlainDate>(temporal_object))
return static_cast<PlainDate&>(temporal_object).iso_month();
if (is<PlainDateTime>(temporal_object))
return static_cast<PlainDateTime&>(temporal_object).iso_month();
VERIFY_NOT_REACHED();
}
}

View file

@ -46,5 +46,6 @@ String build_iso_month_code(i32 month);
double resolve_iso_month(GlobalObject&, Object& fields);
Optional<TemporalDate> iso_date_from_fields(GlobalObject&, Object& fields, Object& options);
i32 iso_year(Object& temporal_object);
u8 iso_month(Object& temporal_object);
}

View file

@ -32,6 +32,7 @@ void CalendarPrototype::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.dateFromFields, date_from_fields, 2, attr);
define_native_function(vm.names.year, year, 1, attr);
define_native_function(vm.names.month, month, 1, attr);
define_native_function(vm.names.toString, to_string, 0, attr);
define_native_function(vm.names.toJSON, to_json, 0, attr);
}
@ -120,6 +121,37 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::year)
return Value(iso_year(temporal_date_like.as_object()));
}
// 12.4.10 Temporal.Calendar.prototype.month ( temporalDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.month
// NOTE: This is the minimum month implementation for engines without ECMA-402.
JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::month)
{
// 1. Let calendar be the this value.
// 2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]).
auto* calendar = typed_this(global_object);
if (vm.exception())
return {};
// 3. Assert: calendar.[[Identifier]] is "iso8601".
VERIFY(calendar->identifier() == "iso8601"sv);
// 4. If Type(temporalDateLike) is Object and temporalDateLike has an [[InitializedTemporalMonthDay]] internal slot, then
// a. Throw a TypeError exception.
// TODO
auto temporal_date_like = vm.argument(0);
// 5. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]] or [[InitializedTemporalYearMonth]] internal slot, then
// TODO PlainYearMonth objects
if (!temporal_date_like.is_object() || !is<PlainDate>(temporal_date_like.as_object())) {
// a. Set temporalDateLike to ? ToTemporalDate(temporalDateLike).
temporal_date_like = to_temporal_date(global_object, temporal_date_like);
if (vm.exception())
return {};
}
// 6. Return ! ISOMonth(temporalDateLike).
return Value(iso_month(temporal_date_like.as_object()));
}
// 12.4.23 Temporal.Calendar.prototype.toString ( ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.tostring
JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::to_string)
{

View file

@ -22,6 +22,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(id_getter);
JS_DECLARE_NATIVE_FUNCTION(date_from_fields);
JS_DECLARE_NATIVE_FUNCTION(year);
JS_DECLARE_NATIVE_FUNCTION(month);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_json);
};

View file

@ -0,0 +1,11 @@
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.Calendar.prototype.month).toHaveLength(1);
});
test("basic functionality", () => {
const calendar = new Temporal.Calendar("iso8601");
const date = new Temporal.PlainDate(2021, 7, 23);
expect(calendar.month(date)).toBe(7);
});
});