mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 01:20:25 +00:00
LibJS: Implement Temporal.Calendar.prototype.daysInMonth
This commit is contained in:
parent
2150f5b374
commit
69c8b8e8bd
Notes:
sideshowbarker
2024-07-18 08:27:21 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/69c8b8e8bd4 Pull-request: https://github.com/SerenityOS/serenity/pull/8961 Reviewed-by: https://github.com/linusg ✅
4 changed files with 42 additions and 0 deletions
|
@ -109,6 +109,7 @@ namespace JS {
|
|||
P(dayOfWeek) \
|
||||
P(dayOfYear) \
|
||||
P(days) \
|
||||
P(daysInMonth) \
|
||||
P(daysInWeek) \
|
||||
P(debug) \
|
||||
P(decodeURI) \
|
||||
|
|
|
@ -39,6 +39,7 @@ void CalendarPrototype::initialize(GlobalObject& global_object)
|
|||
define_native_function(vm.names.dayOfYear, day_of_year, 1, attr);
|
||||
define_native_function(vm.names.weekOfYear, week_of_year, 1, attr);
|
||||
define_native_function(vm.names.daysInWeek, days_in_week, 1, attr);
|
||||
define_native_function(vm.names.daysInMonth, days_in_month, 1, attr);
|
||||
define_native_function(vm.names.toString, to_string, 0, attr);
|
||||
define_native_function(vm.names.toJSON, to_json, 0, attr);
|
||||
}
|
||||
|
@ -300,6 +301,34 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::days_in_week)
|
|||
return Value(7);
|
||||
}
|
||||
|
||||
// 12.4.16 Temporal.Calendar.prototype.daysInMonth ( temporalDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.daysinweek
|
||||
// NOTE: This is the minimum daysInMonth implementation for engines without ECMA-402.
|
||||
JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::days_in_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 not Object or temporalDateLike does not have an [[InitializedTemporalDate]] or [[InitializedTemporalYearMonth]] internal slots, then
|
||||
auto temporal_date_like = vm.argument(0);
|
||||
// 4. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]] or [[InitializedTemporalMonthDay]] internal slot, then
|
||||
// TODO PlainMonthDay 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 {};
|
||||
}
|
||||
|
||||
// 5. Return 𝔽(! ISODaysInMonth(temporalDateLike.[[ISOYear]], temporalDateLike.[[ISOMonth]])).
|
||||
return Value(iso_days_in_month(iso_year(temporal_date_like.as_object()), 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)
|
||||
{
|
||||
|
|
|
@ -29,6 +29,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(day_of_year);
|
||||
JS_DECLARE_NATIVE_FUNCTION(week_of_year);
|
||||
JS_DECLARE_NATIVE_FUNCTION(days_in_week);
|
||||
JS_DECLARE_NATIVE_FUNCTION(days_in_month);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_string);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_json);
|
||||
};
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.Calendar.prototype.daysInMonth).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const calendar = new Temporal.Calendar("iso8601");
|
||||
const date = new Temporal.PlainDate(2021, 7, 23);
|
||||
expect(calendar.daysInMonth(date)).toBe(31);
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue