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

LibJS: Convert Temporal.PlainYearMonth functions to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-10-21 20:16:46 +01:00
parent e4e04528af
commit 15f52c5f8e
4 changed files with 104 additions and 106 deletions

View file

@ -32,8 +32,8 @@ void PlainYearMonthConstructor::initialize(GlobalObject& global_object)
define_direct_property(vm.names.length, Value(2), Attribute::Configurable); define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable; u8 attr = Attribute::Writable | Attribute::Configurable;
define_old_native_function(vm.names.from, from, 1, attr); define_native_function(vm.names.from, from, 1, attr);
define_old_native_function(vm.names.compare, compare, 2, attr); define_native_function(vm.names.compare, compare, 2, attr);
} }
// 9.1.1 Temporal.PlainYearMonth ( isoYear, isoMonth [ , calendarLike [ , referenceISODay ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth // 9.1.1 Temporal.PlainYearMonth ( isoYear, isoMonth [ , calendarLike [ , referenceISODay ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth
@ -85,36 +85,36 @@ ThrowCompletionOr<Object*> PlainYearMonthConstructor::construct(FunctionObject&
} }
// 9.2.2 Temporal.PlainYearMonth.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.from // 9.2.2 Temporal.PlainYearMonth.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.from
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthConstructor::from) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthConstructor::from)
{ {
// 1. Set options to ? GetOptionsObject(options). // 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); auto item = vm.argument(0);
// 2. If Type(item) is Object and item has an [[InitializedTemporalYearMonth]] internal slot, then // 2. If Type(item) is Object and item has an [[InitializedTemporalYearMonth]] internal slot, then
if (item.is_object() && is<PlainYearMonth>(item.as_object())) { if (item.is_object() && is<PlainYearMonth>(item.as_object())) {
// a. Perform ? ToTemporalOverflow(options). // a. Perform ? ToTemporalOverflow(options).
(void)TRY_OR_DISCARD(to_temporal_overflow(global_object, *options)); (void)TRY(to_temporal_overflow(global_object, *options));
auto& plain_year_month_object = static_cast<PlainYearMonth&>(item.as_object()); auto& plain_year_month_object = static_cast<PlainYearMonth&>(item.as_object());
// b. Return ? CreateTemporalYearMonth(item.[[ISOYear]], item.[[ISOMonth]], item.[[Calendar]], item.[[ISODay]]). // b. Return ? CreateTemporalYearMonth(item.[[ISOYear]], item.[[ISOMonth]], item.[[Calendar]], item.[[ISODay]]).
return TRY_OR_DISCARD(create_temporal_year_month(global_object, plain_year_month_object.iso_year(), plain_year_month_object.iso_month(), plain_year_month_object.calendar(), plain_year_month_object.iso_day())); return TRY(create_temporal_year_month(global_object, plain_year_month_object.iso_year(), plain_year_month_object.iso_month(), plain_year_month_object.calendar(), plain_year_month_object.iso_day()));
} }
// 3. Return ? ToTemporalYearMonth(item, options). // 3. Return ? ToTemporalYearMonth(item, options).
return TRY_OR_DISCARD(to_temporal_year_month(global_object, item, options)); return TRY(to_temporal_year_month(global_object, item, options));
} }
// 9.2.3 Temporal.PlainYearMonth.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.compare // 9.2.3 Temporal.PlainYearMonth.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.compare
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthConstructor::compare) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthConstructor::compare)
{ {
// 1. Set one to ? ToTemporalYearMonth(one). // 1. Set one to ? ToTemporalYearMonth(one).
auto* one = TRY_OR_DISCARD(to_temporal_year_month(global_object, vm.argument(0))); auto* one = TRY(to_temporal_year_month(global_object, vm.argument(0)));
// 2. Set two to ? ToTemporalYearMonth(one). // 2. Set two to ? ToTemporalYearMonth(one).
auto* two = TRY_OR_DISCARD(to_temporal_year_month(global_object, vm.argument(1))); auto* two = TRY(to_temporal_year_month(global_object, vm.argument(1)));
// 3. Return 𝔽(! CompareISODate(one.[[ISOYear]], one.[[ISOMonth]], one.[[ISODay]], two.[[ISOYear]], two.[[ISOMonth]], two.[[ISODay]])). // 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())); 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: private:
virtual bool has_constructor() const override { return true; } virtual bool has_constructor() const override { return true; }
JS_DECLARE_OLD_NATIVE_FUNCTION(from); JS_DECLARE_NATIVE_FUNCTION(from);
JS_DECLARE_OLD_NATIVE_FUNCTION(compare); JS_DECLARE_NATIVE_FUNCTION(compare);
}; };
} }

View file

@ -28,173 +28,173 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object)
// 9.3.2 Temporal.PlainYearMonth.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype-@@tostringtag // 9.3.2 Temporal.PlainYearMonth.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype-@@tostringtag
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Temporal.PlainYearMonth"), Attribute::Configurable); define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Temporal.PlainYearMonth"), Attribute::Configurable);
define_old_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.year, year_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.year, year_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.month, month_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.month, month_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.monthCode, month_code_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.monthCode, month_code_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.daysInYear, days_in_year_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.daysInYear, days_in_year_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.daysInMonth, days_in_month_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.daysInMonth, days_in_month_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.monthsInYear, months_in_year_getter, {}, Attribute::Configurable); define_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_native_accessor(vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable);
define_old_native_accessor(vm.names.era, era_getter, {}, Attribute::Configurable); define_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.eraYear, era_year_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable; u8 attr = Attribute::Writable | Attribute::Configurable;
define_old_native_function(vm.names.equals, equals, 1, attr); define_native_function(vm.names.equals, equals, 1, attr);
define_old_native_function(vm.names.toString, to_string, 0, attr); define_native_function(vm.names.toString, to_string, 0, attr);
define_old_native_function(vm.names.toLocaleString, to_locale_string, 0, attr); define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
define_old_native_function(vm.names.toJSON, to_json, 0, attr); define_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.valueOf, value_of, 0, attr);
define_old_native_function(vm.names.toPlainDate, to_plain_date, 1, attr); define_native_function(vm.names.toPlainDate, to_plain_date, 1, attr);
define_old_native_function(vm.names.getISOFields, get_iso_fields, 0, attr); define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr);
} }
// 9.3.3 get Temporal.PlainYearMonth.prototype.calendar, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.calendar // 9.3.3 get Temporal.PlainYearMonth.prototype.calendar, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.calendar
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::calendar_getter) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::calendar_getter)
{ {
// 1. Let yearMonth be the this value. // 1. Let yearMonth be the this value.
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
auto* year_month = TRY_OR_DISCARD(typed_this_object(global_object)); auto* year_month = TRY(typed_this_object(global_object));
// 3. Return yearMonth.[[Calendar]]. // 3. Return yearMonth.[[Calendar]].
return Value(&year_month->calendar()); return Value(&year_month->calendar());
} }
// 9.3.4 get Temporal.PlainYearMonth.prototype.year, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.year // 9.3.4 get Temporal.PlainYearMonth.prototype.year, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.year
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::year_getter) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::year_getter)
{ {
// 1. Let yearMonth be the this value. // 1. Let yearMonth be the this value.
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
auto* year_month = TRY_OR_DISCARD(typed_this_object(global_object)); auto* year_month = TRY(typed_this_object(global_object));
// 3. Let calendar be yearMonth.[[Calendar]]. // 3. Let calendar be yearMonth.[[Calendar]].
auto& calendar = year_month->calendar(); auto& calendar = year_month->calendar();
// 4. Return 𝔽(? CalendarYear(calendar, yearMonth)). // 4. Return 𝔽(? CalendarYear(calendar, yearMonth)).
return Value(TRY_OR_DISCARD(calendar_year(global_object, calendar, *year_month))); return Value(TRY(calendar_year(global_object, calendar, *year_month)));
} }
// 9.3.5 get Temporal.PlainYearMonth.prototype.month, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.month // 9.3.5 get Temporal.PlainYearMonth.prototype.month, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.month
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::month_getter) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::month_getter)
{ {
// 1. Let yearMonth be the this value. // 1. Let yearMonth be the this value.
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
auto* year_month = TRY_OR_DISCARD(typed_this_object(global_object)); auto* year_month = TRY(typed_this_object(global_object));
// 3. Let calendar be yearMonth.[[Calendar]]. // 3. Let calendar be yearMonth.[[Calendar]].
auto& calendar = year_month->calendar(); auto& calendar = year_month->calendar();
// 4. Return 𝔽(? CalendarMonth(calendar, yearMonth)). // 4. Return 𝔽(? CalendarMonth(calendar, yearMonth)).
return Value(TRY_OR_DISCARD(calendar_month(global_object, calendar, *year_month))); return Value(TRY(calendar_month(global_object, calendar, *year_month)));
} }
// 9.3.6 get Temporal.PlainYearMonth.prototype.monthCode, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.monthCode // 9.3.6 get Temporal.PlainYearMonth.prototype.monthCode, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.monthCode
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::month_code_getter) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::month_code_getter)
{ {
// 1. Let yearMonth be the this value. // 1. Let yearMonth be the this value.
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
auto* year_month = TRY_OR_DISCARD(typed_this_object(global_object)); auto* year_month = TRY(typed_this_object(global_object));
// 3. Let calendar be yearMonth.[[Calendar]]. // 3. Let calendar be yearMonth.[[Calendar]].
auto& calendar = year_month->calendar(); auto& calendar = year_month->calendar();
// 4. Return ? CalendarMonthCode(calendar, yearMonth). // 4. Return ? CalendarMonthCode(calendar, yearMonth).
return js_string(vm, TRY_OR_DISCARD(calendar_month_code(global_object, calendar, *year_month))); return js_string(vm, TRY(calendar_month_code(global_object, calendar, *year_month)));
} }
// 9.3.7 get Temporal.PlainYearMonth.prototype.daysInYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.daysinyear // 9.3.7 get Temporal.PlainYearMonth.prototype.daysInYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.daysinyear
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::days_in_year_getter) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::days_in_year_getter)
{ {
// 1. Let yearMonth be the this value. // 1. Let yearMonth be the this value.
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
auto* year_month = TRY_OR_DISCARD(typed_this_object(global_object)); auto* year_month = TRY(typed_this_object(global_object));
// 3. Let calendar be yearMonth.[[Calendar]]. // 3. Let calendar be yearMonth.[[Calendar]].
auto& calendar = year_month->calendar(); auto& calendar = year_month->calendar();
// 4. Return ? CalendarDaysInYear(calendar, yearMonth). // 4. Return ? CalendarDaysInYear(calendar, yearMonth).
return Value(TRY_OR_DISCARD(calendar_days_in_year(global_object, calendar, *year_month))); return Value(TRY(calendar_days_in_year(global_object, calendar, *year_month)));
} }
// 9.3.8 get Temporal.PlainYearMonth.prototype.daysInMonth, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.daysinmonth // 9.3.8 get Temporal.PlainYearMonth.prototype.daysInMonth, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.daysinmonth
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::days_in_month_getter) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::days_in_month_getter)
{ {
// 1. Let yearMonth be the this value. // 1. Let yearMonth be the this value.
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
auto* year_month = TRY_OR_DISCARD(typed_this_object(global_object)); auto* year_month = TRY(typed_this_object(global_object));
// 3. Let calendar be yearMonth.[[Calendar]]. // 3. Let calendar be yearMonth.[[Calendar]].
auto& calendar = year_month->calendar(); auto& calendar = year_month->calendar();
// 4. Return ? CalendarDaysInMonth(calendar, yearMonth). // 4. Return ? CalendarDaysInMonth(calendar, yearMonth).
return Value(TRY_OR_DISCARD(calendar_days_in_month(global_object, calendar, *year_month))); return Value(TRY(calendar_days_in_month(global_object, calendar, *year_month)));
} }
// 9.3.9 get Temporal.PlainYearMonth.prototype.monthsInYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.monthsinyear // 9.3.9 get Temporal.PlainYearMonth.prototype.monthsInYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.monthsinyear
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::months_in_year_getter) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::months_in_year_getter)
{ {
// 1. Let yearMonth be the this value. // 1. Let yearMonth be the this value.
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
auto* year_month = TRY_OR_DISCARD(typed_this_object(global_object)); auto* year_month = TRY(typed_this_object(global_object));
// 3. Let calendar be yearMonth.[[Calendar]]. // 3. Let calendar be yearMonth.[[Calendar]].
auto& calendar = year_month->calendar(); auto& calendar = year_month->calendar();
// 4. Return ? CalendarMonthsInYear(calendar, yearMonth). // 4. Return ? CalendarMonthsInYear(calendar, yearMonth).
return Value(TRY_OR_DISCARD(calendar_months_in_year(global_object, calendar, *year_month))); return Value(TRY(calendar_months_in_year(global_object, calendar, *year_month)));
} }
// 9.3.10 get Temporal.PlainYearMonth.prototype.inLeapYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.inleapyear // 9.3.10 get Temporal.PlainYearMonth.prototype.inLeapYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.inleapyear
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::in_leap_year_getter) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::in_leap_year_getter)
{ {
// 1. Let yearMonth be the this value. // 1. Let yearMonth be the this value.
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
auto* year_month = TRY_OR_DISCARD(typed_this_object(global_object)); auto* year_month = TRY(typed_this_object(global_object));
// 3. Let calendar be yearMonth.[[Calendar]]. // 3. Let calendar be yearMonth.[[Calendar]].
auto& calendar = year_month->calendar(); auto& calendar = year_month->calendar();
// 4. Return ? CalendarInLeapYear(calendar, yearMonth). // 4. Return ? CalendarInLeapYear(calendar, yearMonth).
return Value(TRY_OR_DISCARD(calendar_in_leap_year(global_object, calendar, *year_month))); return Value(TRY(calendar_in_leap_year(global_object, calendar, *year_month)));
} }
// 15.6.9.2 get Temporal.PlainYearMonth.prototype.era, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.era // 15.6.9.2 get Temporal.PlainYearMonth.prototype.era, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.era
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::era_getter) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::era_getter)
{ {
// 1. Let plainYearMonth be the this value. // 1. Let plainYearMonth be the this value.
// 2. Perform ? RequireInternalSlot(plainYearMonth, [[InitializedTemporalYearMonth]]). // 2. Perform ? RequireInternalSlot(plainYearMonth, [[InitializedTemporalYearMonth]]).
auto* plain_year_month = TRY_OR_DISCARD(typed_this_object(global_object)); auto* plain_year_month = TRY(typed_this_object(global_object));
// 3. Let calendar be plainYearMonth.[[Calendar]]. // 3. Let calendar be plainYearMonth.[[Calendar]].
auto& calendar = plain_year_month->calendar(); auto& calendar = plain_year_month->calendar();
// 4. Return ? CalendarEra(calendar, plainYearMonth). // 4. Return ? CalendarEra(calendar, plainYearMonth).
return TRY_OR_DISCARD(calendar_era(global_object, calendar, *plain_year_month)); return TRY(calendar_era(global_object, calendar, *plain_year_month));
} }
// 15.6.9.3 get Temporal.PlainYearMonth.prototype.eraYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.erayear // 15.6.9.3 get Temporal.PlainYearMonth.prototype.eraYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plainyearmonth.prototype.erayear
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::era_year_getter) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::era_year_getter)
{ {
// 1. Let plainYearMonth be the this value. // 1. Let plainYearMonth be the this value.
// 2. Perform ? RequireInternalSlot(plainYearMonth, [[InitializedTemporalYearMonth]]). // 2. Perform ? RequireInternalSlot(plainYearMonth, [[InitializedTemporalYearMonth]]).
auto* plain_year_month = TRY_OR_DISCARD(typed_this_object(global_object)); auto* plain_year_month = TRY(typed_this_object(global_object));
// 3. Let calendar be plainYearMonth.[[Calendar]]. // 3. Let calendar be plainYearMonth.[[Calendar]].
auto& calendar = plain_year_month->calendar(); auto& calendar = plain_year_month->calendar();
// 4. Return ? CalendarEraYear(calendar, plainYearMonth). // 4. Return ? CalendarEraYear(calendar, plainYearMonth).
return TRY_OR_DISCARD(calendar_era_year(global_object, calendar, *plain_year_month)); return TRY(calendar_era_year(global_object, calendar, *plain_year_month));
} }
// 9.3.16 Temporal.PlainYearMonth.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.equals // 9.3.16 Temporal.PlainYearMonth.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.equals
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::equals) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::equals)
{ {
// 1. Let yearMonth be the this value. // 1. Let yearMonth be the this value.
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
auto* year_month = TRY_OR_DISCARD(typed_this_object(global_object)); auto* year_month = TRY(typed_this_object(global_object));
// 3. Set other to ? ToTemporalYearMonth(other). // 3. Set other to ? ToTemporalYearMonth(other).
auto* other = TRY_OR_DISCARD(to_temporal_year_month(global_object, vm.argument(0))); auto* other = TRY(to_temporal_year_month(global_object, vm.argument(0)));
// 4. If yearMonth.[[ISOYear]] ≠ other.[[ISOYear]], return false. // 4. If yearMonth.[[ISOYear]] ≠ other.[[ISOYear]], return false.
if (year_month->iso_year() != other->iso_year()) if (year_month->iso_year() != other->iso_year())
@ -209,90 +209,88 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::equals)
return Value(false); return Value(false);
// 7. Return ? CalendarEquals(yearMonth.[[Calendar]], other.[[Calendar]]). // 7. Return ? CalendarEquals(yearMonth.[[Calendar]], other.[[Calendar]]).
return Value(TRY_OR_DISCARD(calendar_equals(global_object, year_month->calendar(), other->calendar()))); return Value(TRY(calendar_equals(global_object, year_month->calendar(), other->calendar())));
} }
// 9.3.17 Temporal.PlainYearMonth.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.tostring // 9.3.17 Temporal.PlainYearMonth.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.tostring
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::to_string) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_string)
{ {
// 1. Let yearMonth be the this value. // 1. Let yearMonth be the this value.
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
auto* year_month = TRY_OR_DISCARD(typed_this_object(global_object)); auto* year_month = TRY(typed_this_object(global_object));
// 3. Set options to ? GetOptionsObject(options). // 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). // 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 ? TemporalYearMonthToString(yearMonth, showCalendar). // 5. Return ? TemporalYearMonthToString(yearMonth, showCalendar).
return js_string(vm, TRY_OR_DISCARD(temporal_year_month_to_string(global_object, *year_month, show_calendar))); return js_string(vm, TRY(temporal_year_month_to_string(global_object, *year_month, show_calendar)));
} }
// 9.3.18 Temporal.PlainYearMonth.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.tolocalestring // 9.3.18 Temporal.PlainYearMonth.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.tolocalestring
// NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402. // NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402.
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::to_locale_string) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_locale_string)
{ {
// 1. Let yearMonth be the this value. // 1. Let yearMonth be the this value.
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
auto* year_month = TRY_OR_DISCARD(typed_this_object(global_object)); auto* year_month = TRY(typed_this_object(global_object));
// 3. Return ? TemporalYearMonthToString(yearMonth, "auto"). // 3. Return ? TemporalYearMonthToString(yearMonth, "auto").
return js_string(vm, TRY_OR_DISCARD(temporal_year_month_to_string(global_object, *year_month, "auto"sv))); return js_string(vm, TRY(temporal_year_month_to_string(global_object, *year_month, "auto"sv)));
} }
// 9.3.19 Temporal.PlainYearMonth.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.tojson // 9.3.19 Temporal.PlainYearMonth.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.tojson
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::to_json) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_json)
{ {
// 1. Let yearMonth be the this value. // 1. Let yearMonth be the this value.
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
auto* year_month = TRY_OR_DISCARD(typed_this_object(global_object)); auto* year_month = TRY(typed_this_object(global_object));
// 3. Return ? TemporalYearMonthToString(yearMonth, "auto"). // 3. Return ? TemporalYearMonthToString(yearMonth, "auto").
return js_string(vm, TRY_OR_DISCARD(temporal_year_month_to_string(global_object, *year_month, "auto"sv))); return js_string(vm, TRY(temporal_year_month_to_string(global_object, *year_month, "auto"sv)));
} }
// 9.3.20 Temporal.PlainYearMonth.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.valueof // 9.3.20 Temporal.PlainYearMonth.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.valueof
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::value_of) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::value_of)
{ {
// 1. Throw a TypeError exception. // 1. Throw a TypeError exception.
vm.throw_exception<TypeError>(global_object, ErrorType::Convert, "Temporal.PlainYearMonth", "a primitive value"); return vm.throw_completion<TypeError>(global_object, ErrorType::Convert, "Temporal.PlainYearMonth", "a primitive value");
return {};
} }
// 9.3.21 Temporal.PlainYearMonth.prototype.toPlainDate ( item ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.toplaindate // 9.3.21 Temporal.PlainYearMonth.prototype.toPlainDate ( item ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.toplaindate
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::to_plain_date) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_plain_date)
{ {
auto item = vm.argument(0); auto item = vm.argument(0);
// 1. Let yearMonth be the this value. // 1. Let yearMonth be the this value.
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
auto* year_month = TRY_OR_DISCARD(typed_this_object(global_object)); auto* year_month = TRY(typed_this_object(global_object));
// 3. If Type(item) is not Object, then // 3. If Type(item) is not Object, then
if (!item.is_object()) { if (!item.is_object()) {
// a. Throw a TypeError exception. // a. Throw a TypeError exception.
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, item); return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, item);
return {};
} }
// 4. Let calendar be yearMonth.[[Calendar]]. // 4. Let calendar be yearMonth.[[Calendar]].
auto& calendar = year_month->calendar(); auto& calendar = year_month->calendar();
// 5. Let receiverFieldNames be ? CalendarFields(calendar, « "monthCode", "year" »). // 5. Let receiverFieldNames be ? CalendarFields(calendar, « "monthCode", "year" »).
auto receiver_field_names = TRY_OR_DISCARD(calendar_fields(global_object, calendar, { "monthCode"sv, "year"sv })); auto receiver_field_names = TRY(calendar_fields(global_object, calendar, { "monthCode"sv, "year"sv }));
// 6. Let fields be ? PrepareTemporalFields(yearMonth, receiverFieldNames, «»). // 6. Let fields be ? PrepareTemporalFields(yearMonth, receiverFieldNames, «»).
auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *year_month, receiver_field_names, {})); auto* fields = TRY(prepare_temporal_fields(global_object, *year_month, receiver_field_names, {}));
// 7. Let inputFieldNames be ? CalendarFields(calendar, « "day" »). // 7. Let inputFieldNames be ? CalendarFields(calendar, « "day" »).
auto input_field_names = TRY_OR_DISCARD(calendar_fields(global_object, calendar, { "day"sv })); auto input_field_names = TRY(calendar_fields(global_object, calendar, { "day"sv }));
// 8. Let inputFields be ? PrepareTemporalFields(item, inputFieldNames, «»). // 8. Let inputFields be ? PrepareTemporalFields(item, inputFieldNames, «»).
auto* input_fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, item.as_object(), input_field_names, {})); auto* input_fields = TRY(prepare_temporal_fields(global_object, item.as_object(), input_field_names, {}));
// 9. Let mergedFields be ? CalendarMergeFields(calendar, fields, inputFields). // 9. Let mergedFields be ? CalendarMergeFields(calendar, fields, inputFields).
auto* merged_fields = TRY_OR_DISCARD(calendar_merge_fields(global_object, calendar, *fields, *input_fields)); auto* merged_fields = TRY(calendar_merge_fields(global_object, calendar, *fields, *input_fields));
// 10. Let mergedFieldNames be the List containing all the elements of receiverFieldNames followed by all the elements of inputFieldNames, with duplicate elements removed. // 10. Let mergedFieldNames be the List containing all the elements of receiverFieldNames followed by all the elements of inputFieldNames, with duplicate elements removed.
Vector<String> merged_field_names; Vector<String> merged_field_names;
@ -306,7 +304,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::to_plain_date)
} }
// 11. Set mergedFields to ? PrepareTemporalFields(mergedFields, mergedFieldNames, «»). // 11. Set mergedFields to ? PrepareTemporalFields(mergedFields, mergedFieldNames, «»).
merged_fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *merged_fields, merged_field_names, {})); merged_fields = TRY(prepare_temporal_fields(global_object, *merged_fields, merged_field_names, {}));
// 12. Let options be ! OrdinaryObjectCreate(null). // 12. Let options be ! OrdinaryObjectCreate(null).
auto* options = Object::create(global_object, nullptr); auto* options = Object::create(global_object, nullptr);
@ -315,15 +313,15 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::to_plain_date)
MUST(options->create_data_property_or_throw(vm.names.overflow, js_string(vm, vm.names.reject.as_string()))); MUST(options->create_data_property_or_throw(vm.names.overflow, js_string(vm, vm.names.reject.as_string())));
// 14. Return ? DateFromFields(calendar, mergedFields, options). // 14. Return ? DateFromFields(calendar, mergedFields, options).
return TRY_OR_DISCARD(date_from_fields(global_object, calendar, *merged_fields, *options)); return TRY(date_from_fields(global_object, calendar, *merged_fields, *options));
} }
// 9.3.22 Temporal.PlainYearMonth.prototype.getISOFields ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.getisofields // 9.3.22 Temporal.PlainYearMonth.prototype.getISOFields ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.getisofields
JS_DEFINE_OLD_NATIVE_FUNCTION(PlainYearMonthPrototype::get_iso_fields) JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::get_iso_fields)
{ {
// 1. Let yearMonth be the this value. // 1. Let yearMonth be the this value.
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
auto* year_month = TRY_OR_DISCARD(typed_this_object(global_object)); auto* year_month = TRY(typed_this_object(global_object));
// 3. Let fields be ! OrdinaryObjectCreate(%Object.prototype%). // 3. Let fields be ! OrdinaryObjectCreate(%Object.prototype%).
auto* fields = Object::create(global_object, global_object.object_prototype()); auto* fields = Object::create(global_object, global_object.object_prototype());

View file

@ -20,23 +20,23 @@ public:
virtual ~PlainYearMonthPrototype() override = default; virtual ~PlainYearMonthPrototype() override = default;
private: private:
JS_DECLARE_OLD_NATIVE_FUNCTION(calendar_getter); JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(year_getter); JS_DECLARE_NATIVE_FUNCTION(year_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(month_getter); JS_DECLARE_NATIVE_FUNCTION(month_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(month_code_getter); JS_DECLARE_NATIVE_FUNCTION(month_code_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(days_in_year_getter); JS_DECLARE_NATIVE_FUNCTION(days_in_year_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(days_in_month_getter); JS_DECLARE_NATIVE_FUNCTION(days_in_month_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(months_in_year_getter); JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(in_leap_year_getter); JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(era_getter); JS_DECLARE_NATIVE_FUNCTION(era_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(era_year_getter); JS_DECLARE_NATIVE_FUNCTION(era_year_getter);
JS_DECLARE_OLD_NATIVE_FUNCTION(equals); JS_DECLARE_NATIVE_FUNCTION(equals);
JS_DECLARE_OLD_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_OLD_NATIVE_FUNCTION(to_locale_string); JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_OLD_NATIVE_FUNCTION(to_json); JS_DECLARE_NATIVE_FUNCTION(to_json);
JS_DECLARE_OLD_NATIVE_FUNCTION(value_of); JS_DECLARE_NATIVE_FUNCTION(value_of);
JS_DECLARE_OLD_NATIVE_FUNCTION(to_plain_date); JS_DECLARE_NATIVE_FUNCTION(to_plain_date);
JS_DECLARE_OLD_NATIVE_FUNCTION(get_iso_fields); JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
}; };
} }