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

LibJS: Implement Temporal.Calendar.prototype.year

This commit is contained in:
Idan Horowitz 2021-07-23 16:35:36 +03:00 committed by Linus Groh
parent 9fa8f19a0f
commit 3895a8354d
5 changed files with 59 additions and 1 deletions

View file

@ -11,6 +11,7 @@
#include <LibJS/Runtime/Temporal/Calendar.h> #include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/CalendarConstructor.h> #include <LibJS/Runtime/Temporal/CalendarConstructor.h>
#include <LibJS/Runtime/Temporal/PlainDate.h> #include <LibJS/Runtime/Temporal/PlainDate.h>
#include <LibJS/Runtime/Temporal/PlainDateTime.h>
#include <LibJS/Runtime/Value.h> #include <LibJS/Runtime/Value.h>
namespace JS::Temporal { namespace JS::Temporal {
@ -416,4 +417,19 @@ Optional<TemporalDate> iso_date_from_fields(GlobalObject& global_object, Object&
return regulate_iso_date(global_object, year.as_double(), month, day.as_double(), *overflow); return regulate_iso_date(global_object, year.as_double(), month, day.as_double(), *overflow);
} }
// 12.1.41 ISOYear ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isoyear
i32 iso_year(Object& temporal_object)
{
// 1. Assert: temporalObject has an [[ISOYear]] internal slot.
// NOTE: Asserted by the VERIFY_NOT_REACHED at the end
// 2. Return 𝔽(temporalObject.[[ISOYear]]).
// TODO: add the rest of the builtins with a [[ISOYear]] slot (PlainYearMonth, PlainMonthDay)
if (is<PlainDate>(temporal_object))
return static_cast<PlainDate&>(temporal_object).iso_year();
if (is<PlainDateTime>(temporal_object))
return static_cast<PlainDateTime&>(temporal_object).iso_year();
VERIFY_NOT_REACHED();
}
} }

View file

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

View file

@ -27,9 +27,11 @@ void CalendarPrototype::initialize(GlobalObject& global_object)
// 12.4.2 Temporal.Calendar.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype-@@tostringtag // 12.4.2 Temporal.Calendar.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype-@@tostringtag
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.Calendar"), Attribute::Configurable); define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.Calendar"), Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_accessor(vm.names.id, id_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.id, id_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.dateFromFields, date_from_fields, 2, attr); 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.toString, to_string, 0, attr); define_native_function(vm.names.toString, to_string, 0, attr);
define_native_function(vm.names.toJSON, to_json, 0, attr); define_native_function(vm.names.toJSON, to_json, 0, attr);
} }
@ -91,6 +93,33 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::date_from_fields)
return create_temporal_date(global_object, result->year, result->month, result->day, *calendar); return create_temporal_date(global_object, result->year, result->month, result->day, *calendar);
} }
// 12.4.9 Temporal.Calendar.prototype.year ( temporalDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.year
// NOTE: This is the minimum year implementation for engines without ECMA-402.
JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::year)
{
// 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);
auto temporal_date_like = vm.argument(0);
// 4. 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 {};
}
// 5. Return ! ISOYear(temporalDateLike).
return Value(iso_year(temporal_date_like.as_object()));
}
// 12.4.23 Temporal.Calendar.prototype.toString ( ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.tostring // 12.4.23 Temporal.Calendar.prototype.toString ( ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.tostring
JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::to_string) JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::to_string)
{ {

View file

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

View file

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