1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:57:44 +00:00

LibJS: Convert Temporal.PlainDate functions to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-10-21 20:07:40 +01:00
parent df67f4978e
commit 2093128b24
4 changed files with 144 additions and 145 deletions

View file

@ -30,8 +30,8 @@ void PlainDateConstructor::initialize(GlobalObject& global_object)
define_direct_property(vm.names.prototype, global_object.temporal_plain_date_prototype(), 0);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_old_native_function(vm.names.from, from, 1, attr);
define_old_native_function(vm.names.compare, compare, 2, attr);
define_native_function(vm.names.from, from, 1, attr);
define_native_function(vm.names.compare, compare, 2, attr);
define_direct_property(vm.names.length, Value(3), Attribute::Configurable);
}
@ -74,34 +74,34 @@ ThrowCompletionOr<Object*> PlainDateConstructor::construct(FunctionObject& new_t
}
// 3.2.2 Temporal.PlainDate.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.from
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDateConstructor::from)
JS_DEFINE_NATIVE_FUNCTION(PlainDateConstructor::from)
{
// 1. Set options to ? GetOptionsObject(options).
auto* options = TRY_OR_DISCARD(get_options_object(global_object, vm.argument(1)));
auto* options = TRY(get_options_object(global_object, vm.argument(1)));
auto item = vm.argument(0);
// 2. If Type(item) is Object and item has an [[InitializedTemporalDate]] internal slot, then
if (item.is_object() && is<PlainDate>(item.as_object())) {
auto& plain_date_item = static_cast<PlainDate&>(item.as_object());
// a. Perform ? ToTemporalOverflow(options).
(void)TRY_OR_DISCARD(to_temporal_overflow(global_object, *options));
(void)TRY(to_temporal_overflow(global_object, *options));
// b. Return ? CreateTemporalDate(item.[[ISOYear]], item.[[ISOMonth]], item.[[ISODay]], item.[[Calendar]]).
return TRY_OR_DISCARD(create_temporal_date(global_object, plain_date_item.iso_year(), plain_date_item.iso_month(), plain_date_item.iso_day(), plain_date_item.calendar()));
return TRY(create_temporal_date(global_object, plain_date_item.iso_year(), plain_date_item.iso_month(), plain_date_item.iso_day(), plain_date_item.calendar()));
}
// 3. Return ? ToTemporalDate(item, options).
return TRY_OR_DISCARD(to_temporal_date(global_object, item, options));
return TRY(to_temporal_date(global_object, item, options));
}
// 3.2.3 Temporal.PlainDate.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-properties-of-the-temporal-plaindate-constructor
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDateConstructor::compare)
JS_DEFINE_NATIVE_FUNCTION(PlainDateConstructor::compare)
{
// 1. Set one to ? ToTemporalDate(one).
auto* one = TRY_OR_DISCARD(to_temporal_date(global_object, vm.argument(0)));
auto* one = TRY(to_temporal_date(global_object, vm.argument(0)));
// 2. Set two to ? ToTemporalDate(two).
auto* two = TRY_OR_DISCARD(to_temporal_date(global_object, vm.argument(1)));
auto* two = TRY(to_temporal_date(global_object, vm.argument(1)));
// 3. Return 𝔽(! CompareISODate(one.[[ISOYear]], one.[[ISOMonth]], one.[[ISODay]], two.[[ISOYear]], two.[[ISOMonth]], two.[[ISODay]])).
return Value(compare_iso_date(one->iso_year(), one->iso_month(), one->iso_day(), two->iso_year(), two->iso_month(), two->iso_day()));

View file

@ -24,8 +24,8 @@ public:
private:
virtual bool has_constructor() const override { return true; }
JS_DECLARE_OLD_NATIVE_FUNCTION(from);
JS_DECLARE_OLD_NATIVE_FUNCTION(compare);
JS_DECLARE_NATIVE_FUNCTION(from);
JS_DECLARE_NATIVE_FUNCTION(compare);
};
}

View file

@ -33,288 +33,288 @@ void PlainDatePrototype::initialize(GlobalObject& global_object)
// 3.3.2 Temporal.PlainDate.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype-@@tostringtag
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Temporal.PlainDate"), Attribute::Configurable);
define_old_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.year, year_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.month, month_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.monthCode, month_code_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.day, day_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.dayOfWeek, day_of_week_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.dayOfYear, day_of_year_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.weekOfYear, week_of_year_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.daysInWeek, days_in_week_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.daysInMonth, days_in_month_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.daysInYear, days_in_year_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.monthsInYear, months_in_year_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.era, era_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.eraYear, era_year_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.year, year_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.month, month_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.monthCode, month_code_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.day, day_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.dayOfWeek, day_of_week_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.dayOfYear, day_of_year_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.weekOfYear, week_of_year_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.daysInWeek, days_in_week_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.daysInMonth, days_in_month_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.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.era, era_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.eraYear, era_year_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_old_native_function(vm.names.toPlainYearMonth, to_plain_year_month, 0, attr);
define_old_native_function(vm.names.toPlainMonthDay, to_plain_month_day, 0, attr);
define_old_native_function(vm.names.getISOFields, get_iso_fields, 0, attr);
define_old_native_function(vm.names.withCalendar, with_calendar, 1, attr);
define_old_native_function(vm.names.equals, equals, 1, attr);
define_old_native_function(vm.names.toPlainDateTime, to_plain_date_time, 0, attr);
define_old_native_function(vm.names.toString, to_string, 0, attr);
define_old_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
define_old_native_function(vm.names.toJSON, to_json, 0, attr);
define_old_native_function(vm.names.valueOf, value_of, 0, attr);
define_native_function(vm.names.toPlainYearMonth, to_plain_year_month, 0, attr);
define_native_function(vm.names.toPlainMonthDay, to_plain_month_day, 0, attr);
define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr);
define_native_function(vm.names.withCalendar, with_calendar, 1, attr);
define_native_function(vm.names.equals, equals, 1, attr);
define_native_function(vm.names.toPlainDateTime, to_plain_date_time, 0, attr);
define_native_function(vm.names.toString, to_string, 0, attr);
define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
define_native_function(vm.names.toJSON, to_json, 0, attr);
define_native_function(vm.names.valueOf, value_of, 0, attr);
}
// 3.3.3 get Temporal.PlainDate.prototype.calendar, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.calendar
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::calendar_getter)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::calendar_getter)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Return temporalDate.[[Calendar]].
return Value(&temporal_date->calendar());
}
// 3.3.4 get Temporal.PlainDate.prototype.year, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.year
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::year_getter)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::year_getter)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Let calendar be temporalDate.[[Calendar]].
auto& calendar = temporal_date->calendar();
// 4. Return ? CalendarYear(calendar, temporalDate).
return Value(TRY_OR_DISCARD(calendar_year(global_object, calendar, *temporal_date)));
return Value(TRY(calendar_year(global_object, calendar, *temporal_date)));
}
// 3.3.5 get Temporal.PlainDate.prototype.month, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.month
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::month_getter)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::month_getter)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Let calendar be temporalDate.[[Calendar]].
auto& calendar = temporal_date->calendar();
// 4. Return ? CalendarMonth(calendar, temporalDate).
return Value(TRY_OR_DISCARD(calendar_month(global_object, calendar, *temporal_date)));
return Value(TRY(calendar_month(global_object, calendar, *temporal_date)));
}
// 3.3.6 get Temporal.PlainDate.prototype.monthCode, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.monthCode
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::month_code_getter)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::month_code_getter)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Let calendar be temporalDate.[[Calendar]].
auto& calendar = temporal_date->calendar();
// 4. Return ? CalendarMonthCode(calendar, temporalDate).
return js_string(vm, TRY_OR_DISCARD(calendar_month_code(global_object, calendar, *temporal_date)));
return js_string(vm, TRY(calendar_month_code(global_object, calendar, *temporal_date)));
}
// 3.3.7 get Temporal.PlainDate.prototype.day, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.day
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::day_getter)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::day_getter)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Let calendar be temporalDate.[[Calendar]].
auto& calendar = temporal_date->calendar();
// 4. Return ? CalendarDay(calendar, temporalDate).
return Value(TRY_OR_DISCARD(calendar_day(global_object, calendar, *temporal_date)));
return Value(TRY(calendar_day(global_object, calendar, *temporal_date)));
}
// 3.3.8 get Temporal.PlainDate.prototype.dayOfWeek, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.dayofweek
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::day_of_week_getter)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::day_of_week_getter)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Let calendar be temporalDate.[[Calendar]].
auto& calendar = temporal_date->calendar();
// Return ? CalendarDayOfWeek(calendar, temporalDate).
return Value(TRY_OR_DISCARD(calendar_day_of_week(global_object, calendar, *temporal_date)));
return Value(TRY(calendar_day_of_week(global_object, calendar, *temporal_date)));
}
// 3.3.9 get Temporal.PlainDate.prototype.dayOfYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.dayofyear
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::day_of_year_getter)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::day_of_year_getter)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Let calendar be temporalDate.[[Calendar]].
auto& calendar = temporal_date->calendar();
// 4. Return ? CalendarDayOfYear(calendar, temporalDate).
return Value(TRY_OR_DISCARD(calendar_day_of_year(global_object, calendar, *temporal_date)));
return Value(TRY(calendar_day_of_year(global_object, calendar, *temporal_date)));
}
// 3.3.10 get Temporal.PlainDate.prototype.weekOfYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.weekofyear
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::week_of_year_getter)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::week_of_year_getter)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Let calendar be temporalDate.[[Calendar]].
auto& calendar = temporal_date->calendar();
// Return ? CalendarWeekOfYear(calendar, temporalDate).
return Value(TRY_OR_DISCARD(calendar_week_of_year(global_object, calendar, *temporal_date)));
return Value(TRY(calendar_week_of_year(global_object, calendar, *temporal_date)));
}
// 3.3.11 get Temporal.PlainDate.prototype.daysInWeek, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.daysinweek
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::days_in_week_getter)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::days_in_week_getter)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Let calendar be temporalDate.[[Calendar]].
auto& calendar = temporal_date->calendar();
// 4. Return ? CalendarDaysInWeek(calendar, temporalDate).
return Value(TRY_OR_DISCARD(calendar_days_in_week(global_object, calendar, *temporal_date)));
return Value(TRY(calendar_days_in_week(global_object, calendar, *temporal_date)));
}
// 3.3.12 get Temporal.PlainDate.prototype.daysInMonth, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.daysinmonth
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::days_in_month_getter)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::days_in_month_getter)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Let calendar be temporalDate.[[Calendar]].
auto& calendar = temporal_date->calendar();
// 4. Return ? CalendarDaysInMonth(calendar, temporalDate).
return Value(TRY_OR_DISCARD(calendar_days_in_month(global_object, calendar, *temporal_date)));
return Value(TRY(calendar_days_in_month(global_object, calendar, *temporal_date)));
}
// 3.3.13 get Temporal.PlainDate.prototype.daysInYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.daysinyear
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::days_in_year_getter)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::days_in_year_getter)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Let calendar be temporalDate.[[Calendar]].
auto& calendar = temporal_date->calendar();
// 4. Return ? CalendarDaysInYear(calendar, temporalDate).
return Value(TRY_OR_DISCARD(calendar_days_in_year(global_object, calendar, *temporal_date)));
return Value(TRY(calendar_days_in_year(global_object, calendar, *temporal_date)));
}
// 3.3.14 get Temporal.PlainDate.prototype.monthsInYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.monthsinyear
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::months_in_year_getter)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::months_in_year_getter)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Let calendar be temporalDate.[[Calendar]].
auto& calendar = temporal_date->calendar();
// 4. Return ? CalendarMonthsInYear(calendar, temporalDate).
return Value(TRY_OR_DISCARD(calendar_months_in_year(global_object, calendar, *temporal_date)));
return Value(TRY(calendar_months_in_year(global_object, calendar, *temporal_date)));
}
// 3.3.15 get Temporal.PlainDate.prototype.inLeapYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.inleapyear
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::in_leap_year_getter)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::in_leap_year_getter)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Let calendar be temporalDate.[[Calendar]].
auto& calendar = temporal_date->calendar();
// 4. Return ? CalendarInLeapYear(calendar, temporalDate).
return Value(TRY_OR_DISCARD(calendar_in_leap_year(global_object, calendar, *temporal_date)));
return Value(TRY(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_OLD_NATIVE_FUNCTION(PlainDatePrototype::era_getter)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::era_getter)
{
// 1. Let plainDate be the this value.
// 2. Perform ? RequireInternalSlot(plainDate, [[InitializedTemporalDate]]).
auto* plain_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* plain_date = TRY(typed_this_object(global_object));
// 3. Let calendar be plainDate.[[Calendar]].
auto& calendar = plain_date->calendar();
// 4. Return ? CalendarEra(calendar, plainDate).
return TRY_OR_DISCARD(calendar_era(global_object, calendar, *plain_date));
return TRY(calendar_era(global_object, calendar, *plain_date));
}
// 15.6.5.3 get Temporal.PlainDate.prototype.eraYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.erayear
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::era_year_getter)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::era_year_getter)
{
// 1. Let plainDate be the this value.
// 2. Perform ? RequireInternalSlot(plainDate, [[InitializedTemporalDate]]).
auto* plain_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* plain_date = TRY(typed_this_object(global_object));
// 3. Let calendar be plainDate.[[Calendar]].
auto& calendar = plain_date->calendar();
// 4. Return ? CalendarEraYear(calendar, plainDate).
return TRY_OR_DISCARD(calendar_era_year(global_object, calendar, *plain_date));
return TRY(calendar_era_year(global_object, calendar, *plain_date));
}
// 3.3.16 Temporal.PlainDate.prototype.toPlainYearMonth ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.toplainyearmonth
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::to_plain_year_month)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_plain_year_month)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Let calendar be temporalDate.[[Calendar]].
auto& calendar = temporal_date->calendar();
// 4. Let fieldNames be ? CalendarFields(calendar, « "monthCode", "year" »).
auto field_names = TRY_OR_DISCARD(calendar_fields(global_object, calendar, { "monthCode"sv, "year"sv }));
auto field_names = TRY(calendar_fields(global_object, calendar, { "monthCode"sv, "year"sv }));
// 5. Let fields be ? PrepareTemporalFields(temporalDate, fieldNames, «»).
auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *temporal_date, field_names, {}));
auto* fields = TRY(prepare_temporal_fields(global_object, *temporal_date, field_names, {}));
// 6. Return ? YearMonthFromFields(calendar, fields).
return TRY_OR_DISCARD(year_month_from_fields(global_object, calendar, *fields));
return TRY(year_month_from_fields(global_object, calendar, *fields));
}
// 3.3.17 Temporal.PlainDate.prototype.toPlainMonthDay ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.toplainmonthday
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::to_plain_month_day)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_plain_month_day)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Let calendar be temporalDate.[[Calendar]].
auto& calendar = temporal_date->calendar();
// 4. Let fieldNames be ? CalendarFields(calendar, « "day", "monthCode" »).
auto field_names = TRY_OR_DISCARD(calendar_fields(global_object, calendar, { "day"sv, "monthCode"sv }));
auto field_names = TRY(calendar_fields(global_object, calendar, { "day"sv, "monthCode"sv }));
// 5. Let fields be ? PrepareTemporalFields(temporalDate, fieldNames, «»).
auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *temporal_date, field_names, {}));
auto* fields = TRY(prepare_temporal_fields(global_object, *temporal_date, field_names, {}));
// 6. Return ? MonthDayFromFields(calendar, fields).
return TRY_OR_DISCARD(month_day_from_fields(global_object, calendar, *fields));
return TRY(month_day_from_fields(global_object, calendar, *fields));
}
// 3.3.18 Temporal.PlainDate.prototype.getISOFields ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.getisofields
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::get_iso_fields)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::get_iso_fields)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Let fields be ! OrdinaryObjectCreate(%Object.prototype%).
auto* fields = Object::create(global_object, global_object.object_prototype());
@ -336,28 +336,28 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::get_iso_fields)
}
// 3.3.22 Temporal.PlainDate.prototype.withCalendar ( calendar ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.withcalendar
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::with_calendar)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::with_calendar)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Let calendar be ? ToTemporalCalendar(calendar).
auto* calendar = TRY_OR_DISCARD(to_temporal_calendar(global_object, vm.argument(0)));
auto* calendar = TRY(to_temporal_calendar(global_object, vm.argument(0)));
// 4. Return ? CreateTemporalDate(temporalDate.[[ISOYear]], temporalDate.[[ISOMonth]], temporalDate.[[ISODay]], calendar).
return TRY_OR_DISCARD(create_temporal_date(global_object, temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day(), *calendar));
return TRY(create_temporal_date(global_object, temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day(), *calendar));
}
// 3.3.25 Temporal.PlainDate.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.equals
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::equals)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::equals)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Set other to ? ToTemporalDate(other).
auto* other = TRY_OR_DISCARD(to_temporal_date(global_object, vm.argument(0)));
auto* other = TRY(to_temporal_date(global_object, vm.argument(0)));
// 4. If temporalDate.[[ISOYear]] ≠ other.[[ISOYear]], return false.
if (temporal_date->iso_year() != other->iso_year())
@ -369,75 +369,74 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::equals)
if (temporal_date->iso_day() != other->iso_day())
return Value(false);
// 7. Return ? CalendarEquals(temporalDate.[[Calendar]], other.[[Calendar]]).
return Value(TRY_OR_DISCARD(calendar_equals(global_object, temporal_date->calendar(), other->calendar())));
return Value(TRY(calendar_equals(global_object, temporal_date->calendar(), other->calendar())));
}
// 3.3.26 Temporal.PlainDate.prototype.toPlainDateTime ( [ temporalTime ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.toplaindatetime
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::to_plain_date_time)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_plain_date_time)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. If temporalTime is undefined, then
if (vm.argument(0).is_undefined()) {
// a. Return ? CreateTemporalDateTime(temporalDate.[[ISOYear]], temporalDate.[[ISOMonth]], temporalDate.[[ISODay]], 0, 0, 0, 0, 0, 0, temporalDate.[[Calendar]]).
return TRY_OR_DISCARD(create_temporal_date_time(global_object, temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day(), 0, 0, 0, 0, 0, 0, temporal_date->calendar()));
return TRY(create_temporal_date_time(global_object, temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day(), 0, 0, 0, 0, 0, 0, temporal_date->calendar()));
}
// 4. Set temporalTime to ? ToTemporalTime(temporalTime).
auto* temporal_time = TRY_OR_DISCARD(to_temporal_time(global_object, vm.argument(0)));
auto* temporal_time = TRY(to_temporal_time(global_object, vm.argument(0)));
// 5. Return ? CreateTemporalDateTime(temporalDate.[[ISOYear]], temporalDate.[[ISOMonth]], temporalDate.[[ISODay]], temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]], temporalDate.[[Calendar]]).
return TRY_OR_DISCARD(create_temporal_date_time(global_object, temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day(), temporal_time->iso_hour(), temporal_time->iso_minute(), temporal_time->iso_second(), temporal_time->iso_millisecond(), temporal_time->iso_microsecond(), temporal_time->iso_nanosecond(), temporal_date->calendar()));
return TRY(create_temporal_date_time(global_object, temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day(), temporal_time->iso_hour(), temporal_time->iso_minute(), temporal_time->iso_second(), temporal_time->iso_millisecond(), temporal_time->iso_microsecond(), temporal_time->iso_nanosecond(), temporal_date->calendar()));
}
// 3.3.28 Temporal.PlainDate.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.tostring
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::to_string)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_string)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Set options to ? GetOptionsObject(options).
auto* options = TRY_OR_DISCARD(get_options_object(global_object, vm.argument(0)));
auto* options = TRY(get_options_object(global_object, vm.argument(0)));
// 4. Let showCalendar be ? ToShowCalendarOption(options).
auto show_calendar = TRY_OR_DISCARD(to_show_calendar_option(global_object, *options));
auto show_calendar = TRY(to_show_calendar_option(global_object, *options));
// 5. Return ? TemporalDateToString(temporalDate, showCalendar).
return js_string(vm, TRY_OR_DISCARD(temporal_date_to_string(global_object, *temporal_date, show_calendar)));
return js_string(vm, TRY(temporal_date_to_string(global_object, *temporal_date, show_calendar)));
}
// 3.3.29 Temporal.PlainDate.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.tolocalestring
// NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402.
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::to_locale_string)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_locale_string)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Return ? TemporalDateToString(temporalDate, "auto").
return js_string(vm, TRY_OR_DISCARD(temporal_date_to_string(global_object, *temporal_date, "auto"sv)));
return js_string(vm, TRY(temporal_date_to_string(global_object, *temporal_date, "auto"sv)));
}
// 3.3.30 Temporal.PlainDate.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.tojson
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::to_json)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_json)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = TRY_OR_DISCARD(typed_this_object(global_object));
auto* temporal_date = TRY(typed_this_object(global_object));
// 3. Return ? TemporalDateToString(temporalDate, "auto").
return js_string(vm, TRY_OR_DISCARD(temporal_date_to_string(global_object, *temporal_date, "auto"sv)));
return js_string(vm, TRY(temporal_date_to_string(global_object, *temporal_date, "auto"sv)));
}
// 3.3.31 Temporal.PlainDate.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.valueof
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainDatePrototype::value_of)
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::value_of)
{
// 1. Throw a TypeError exception.
vm.throw_exception<TypeError>(global_object, ErrorType::Convert, "Temporal.PlainDate", "a primitive value");
return {};
return vm.throw_completion<TypeError>(global_object, ErrorType::Convert, "Temporal.PlainDate", "a primitive value");
}
}

View file

@ -20,31 +20,31 @@ public:
virtual ~PlainDatePrototype() override = default;
private:
JS_DECLARE_OLD_NATIVE_FUNCTION(calendar_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(year_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(month_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(month_code_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(day_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(day_of_week_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(day_of_year_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(week_of_year_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(days_in_week_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(days_in_month_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(days_in_year_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(months_in_year_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(in_leap_year_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(era_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(era_year_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(to_plain_year_month);
JS_DECLARE_OLD_NATIVE_FUNCTION(to_plain_month_day);
JS_DECLARE_OLD_NATIVE_FUNCTION(get_iso_fields);
JS_DECLARE_OLD_NATIVE_FUNCTION(with_calendar);
JS_DECLARE_OLD_NATIVE_FUNCTION(equals);
JS_DECLARE_OLD_NATIVE_FUNCTION(to_plain_date_time);
JS_DECLARE_OLD_NATIVE_FUNCTION(to_string);
JS_DECLARE_OLD_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_OLD_NATIVE_FUNCTION(to_json);
JS_DECLARE_OLD_NATIVE_FUNCTION(value_of);
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
JS_DECLARE_NATIVE_FUNCTION(year_getter);
JS_DECLARE_NATIVE_FUNCTION(month_getter);
JS_DECLARE_NATIVE_FUNCTION(month_code_getter);
JS_DECLARE_NATIVE_FUNCTION(day_getter);
JS_DECLARE_NATIVE_FUNCTION(day_of_week_getter);
JS_DECLARE_NATIVE_FUNCTION(day_of_year_getter);
JS_DECLARE_NATIVE_FUNCTION(week_of_year_getter);
JS_DECLARE_NATIVE_FUNCTION(days_in_week_getter);
JS_DECLARE_NATIVE_FUNCTION(days_in_month_getter);
JS_DECLARE_NATIVE_FUNCTION(days_in_year_getter);
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
JS_DECLARE_NATIVE_FUNCTION(era_getter);
JS_DECLARE_NATIVE_FUNCTION(era_year_getter);
JS_DECLARE_NATIVE_FUNCTION(to_plain_year_month);
JS_DECLARE_NATIVE_FUNCTION(to_plain_month_day);
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
JS_DECLARE_NATIVE_FUNCTION(equals);
JS_DECLARE_NATIVE_FUNCTION(to_plain_date_time);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(to_json);
JS_DECLARE_NATIVE_FUNCTION(value_of);
};
}