diff options
author | Linus Groh <mail@linusgroh.de> | 2021-09-17 21:02:51 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-09-17 23:43:01 +0200 |
commit | 35bba1c98d7a5e17eb3da6d5f317562b9ebf8b54 (patch) | |
tree | 3feb6da73412fce5561241a2470707a3c9cc5808 | |
parent | 433725fef20e47ab5642acd6fc211e349e983730 (diff) | |
download | serenity-35bba1c98d7a5e17eb3da6d5f317562b9ebf8b54.zip |
LibJS: Convert PlainDate AOs to ThrowCompletionOr
10 files changed, 81 insertions, 147 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp index 540d4842b7..69698655bf 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp @@ -827,10 +827,7 @@ ThrowCompletionOr<ISODate> iso_date_from_fields(GlobalObject& global_object, Obj return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalMissingRequiredProperty, vm.names.day.as_string()); // 9. Return ? RegulateISODate(year, month, day, overflow). - auto iso_date = regulate_iso_date(global_object, year.as_double(), month, day.as_double(), overflow); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); - return *iso_date; + return regulate_iso_date(global_object, year.as_double(), month, day.as_double(), overflow); } // 12.1.39 ISOYearMonthFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-isoyearmonthfromfields @@ -919,15 +916,13 @@ ThrowCompletionOr<ISOMonthDay> iso_month_day_from_fields(GlobalObject& global_ob // 12. If monthCode is undefined, then if (month_code.is_undefined()) { // a. Let result be ? RegulateISODate(year, month, day, overflow). - result = regulate_iso_date(global_object, year.as_double(), month, day.as_double(), overflow); + result = TRY(regulate_iso_date(global_object, year.as_double(), month, day.as_double(), overflow)); } // 13. Else, else { // a. Let result be ? RegulateISODate(referenceISOYear, month, day, overflow). - result = regulate_iso_date(global_object, reference_iso_year, month, day.as_double(), overflow); + result = TRY(regulate_iso_date(global_object, reference_iso_year, month, day.as_double(), overflow)); } - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); // 14. Return the Record { [[Month]]: result.[[Month]], [[Day]]: result.[[Day]], [[ReferenceISOYear]]: referenceISOYear }. return ISOMonthDay { .month = result->month, .day = result->day, .reference_iso_year = reference_iso_year }; diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp index a7b4f7df68..5f579299a8 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp @@ -98,7 +98,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::date_from_fields) auto result = TRY_OR_DISCARD(iso_date_from_fields(global_object, fields.as_object(), *options)); // 7. Return ? CreateTemporalDate(result.[[Year]], result.[[Month]], result.[[Day]], calendar). - return create_temporal_date(global_object, result.year, result.month, result.day, *calendar); + return TRY_OR_DISCARD(create_temporal_date(global_object, result.year, result.month, result.day, *calendar)); } // 12.4.5 Temporal.Calendar.prototype.yearMonthFromFields ( fields [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.yearmonthfromfields @@ -175,9 +175,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::date_add) VERIFY(calendar->identifier() == "iso8601"sv); // 4. Set date to ? ToTemporalDate(date). - auto* date = to_temporal_date(global_object, vm.argument(0)); - if (vm.exception()) - return {}; + auto* date = TRY_OR_DISCARD(to_temporal_date(global_object, vm.argument(0))); // 5. Set duration to ? ToTemporalDuration(duration). auto* duration = TRY_OR_DISCARD(to_temporal_duration(global_object, vm.argument(1))); @@ -195,12 +193,10 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::date_add) auto balance_result = balance_duration(global_object, duration->days(), duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), *nanoseconds, "day"sv).release_value(); // 9. Let result be ? AddISODate(date.[[ISOYear]], date.[[ISOMonth]], date.[[ISODay]], duration.[[Years]], duration.[[Months]], duration.[[Weeks]], balanceResult.[[Days]], overflow). - auto result = add_iso_date(global_object, date->iso_year(), date->iso_month(), date->iso_day(), duration->years(), duration->months(), duration->weeks(), balance_result.days, overflow); - if (vm.exception()) - return {}; + auto result = TRY_OR_DISCARD(add_iso_date(global_object, date->iso_year(), date->iso_month(), date->iso_day(), duration->years(), duration->months(), duration->weeks(), balance_result.days, overflow)); // 10. Return ? CreateTemporalDate(result.[[Year]], result.[[Month]], result.[[Day]], calendar). - return create_temporal_date(global_object, result->year, result->month, result->day, *calendar); + return TRY_OR_DISCARD(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 @@ -220,9 +216,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::year) // 4. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]] or [[InitializedTemporalYearMonth]] internal slot, then if (!temporal_date_like.is_object() || !(is<PlainDate>(temporal_date_like.as_object()) || is<PlainDateTime>(temporal_date_like.as_object()) || is<PlainYearMonth>(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 {}; + temporal_date_like = TRY_OR_DISCARD(to_temporal_date(global_object, temporal_date_like)); } // 5. Return ! ISOYear(temporalDateLike). @@ -254,9 +248,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::month) // 5. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]] or [[InitializedTemporalYearMonth]] internal slot, then if (!temporal_date_like.is_object() || !(is<PlainDate>(temporal_date_like.as_object()) || is<PlainDateTime>(temporal_date_like.as_object()) || is<PlainYearMonth>(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 {}; + temporal_date_like = TRY_OR_DISCARD(to_temporal_date(global_object, temporal_date_like)); } // 6. Return ! ISOMonth(temporalDateLike). @@ -280,9 +272,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::month_code) // 4. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]], [[InitializedTemporalMonthDay]], or [[InitializedTemporalYearMonth]] internal slot, then if (!temporal_date_like.is_object() || !(is<PlainDate>(temporal_date_like.as_object()) || is<PlainDateTime>(temporal_date_like.as_object()) || is<PlainMonthDay>(temporal_date_like.as_object()) || is<PlainYearMonth>(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 {}; + temporal_date_like = TRY_OR_DISCARD(to_temporal_date(global_object, temporal_date_like)); } // 5. Return ! ISOMonthCode(temporalDateLike). @@ -306,9 +296,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::day) // 4. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]] or [[InitializedTemporalMonthDay]] internal slot, then if (!temporal_date_like.is_object() || !(is<PlainDate>(temporal_date_like.as_object()) || is<PlainDateTime>(temporal_date_like.as_object()) || is<PlainMonthDay>(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 {}; + temporal_date_like = TRY_OR_DISCARD(to_temporal_date(global_object, temporal_date_like)); } // 5. Return ! ISODay(temporalDateLike). @@ -329,9 +317,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::day_of_week) VERIFY(calendar->identifier() == "iso8601"sv); // 4. Let temporalDate be ? ToTemporalDate(temporalDateLike). - auto* temporal_date = to_temporal_date(global_object, vm.argument(0)); - if (vm.exception()) - return {}; + auto* temporal_date = TRY_OR_DISCARD(to_temporal_date(global_object, vm.argument(0))); // 5. Return ๐ฝ(! ToISODayOfWeek(temporalDate.[[ISOYear]], temporalDate.[[ISOMonth]], temporalDate.[[ISODay]])). return Value(to_iso_day_of_week(temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day())); @@ -351,9 +337,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::day_of_year) VERIFY(calendar->identifier() == "iso8601"sv); // 4. Let temporalDate be ? ToTemporalDate(temporalDateLike). - auto* temporal_date = to_temporal_date(global_object, vm.argument(0)); - if (vm.exception()) - return {}; + auto* temporal_date = TRY_OR_DISCARD(to_temporal_date(global_object, vm.argument(0))); // 5. Return ๐ฝ(! ToISODayOfYear(temporalDate.[[ISOYear]], temporalDate.[[ISOMonth]], temporalDate.[[ISODay]])). return Value(to_iso_day_of_year(temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day())); @@ -373,9 +357,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::week_of_year) VERIFY(calendar->identifier() == "iso8601"sv); // 4. Let temporalDate be ? ToTemporalDate(temporalDateLike). - auto* temporal_date = to_temporal_date(global_object, vm.argument(0)); - if (vm.exception()) - return {}; + auto* temporal_date = TRY_OR_DISCARD(to_temporal_date(global_object, vm.argument(0))); // 5. Return ๐ฝ(! ToISODayOfYear(temporalDate.[[ISOYear]], temporalDate.[[ISOMonth]], temporalDate.[[ISODay]])). return Value(to_iso_week_of_year(temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day())); @@ -395,9 +377,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::days_in_week) VERIFY(calendar->identifier() == "iso8601"sv); // 4. Let temporalDate be ? ToTemporalDate(temporalDateLike). - [[maybe_unused]] auto* temporal_date = to_temporal_date(global_object, vm.argument(0)); - if (vm.exception()) - return {}; + [[maybe_unused]] auto* temporal_date = TRY_OR_DISCARD(to_temporal_date(global_object, vm.argument(0))); // 5. Return 7๐ฝ. return Value(7); @@ -420,9 +400,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::days_in_month) // 4. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]] or [[InitializedTemporalYearMonth]] internal slots, then if (!temporal_date_like.is_object() || !(is<PlainDate>(temporal_date_like.as_object()) || is<PlainDateTime>(temporal_date_like.as_object()) || is<PlainYearMonth>(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 {}; + temporal_date_like = TRY_OR_DISCARD(to_temporal_date(global_object, temporal_date_like)); } // 5. Return ๐ฝ(! ISODaysInMonth(temporalDateLike.[[ISOYear]], temporalDateLike.[[ISOMonth]])). @@ -446,9 +424,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::days_in_year) // 4. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]] or [[InitializedTemporalYearMonth]] internal slot, then if (!temporal_date_like.is_object() || !(is<PlainDate>(temporal_date_like.as_object()) || is<PlainDateTime>(temporal_date_like.as_object()) || is<PlainYearMonth>(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 {}; + temporal_date_like = TRY_OR_DISCARD(to_temporal_date(global_object, temporal_date_like)); } // 5. Return ๐ฝ(! ISODaysInYear(temporalDateLike.[[ISOYear]])). @@ -472,9 +448,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::months_in_year) // 4. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]] or [[InitializedTemporalYearMonth]] internal slot, then if (!temporal_date_like.is_object() || !(is<PlainDate>(temporal_date_like.as_object()) || is<PlainDateTime>(temporal_date_like.as_object()) || is<PlainYearMonth>(temporal_date_like.as_object()))) { // a. Perform ? ToTemporalDate(temporalDateLike). - (void)to_temporal_date(global_object, temporal_date_like); - if (vm.exception()) - return {}; + (void)TRY_OR_DISCARD(to_temporal_date(global_object, temporal_date_like)); } // 5. Return 12๐ฝ. @@ -498,9 +472,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::in_leap_year) // 4. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]] or [[InitializedTemporalYearMonth]] internal slot, then if (!temporal_date_like.is_object() || !(is<PlainDate>(temporal_date_like.as_object()) || is<PlainDateTime>(temporal_date_like.as_object()) || is<PlainYearMonth>(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 {}; + temporal_date_like = TRY_OR_DISCARD(to_temporal_date(global_object, temporal_date_like)); } // 5. Return ! IsISOLeapYear(temporalDateLike.[[ISOYear]]). @@ -649,9 +621,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::era) // 3. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]], or [[InitializedTemporalYearMonth]] internal slot, then if (!temporal_date_like.is_object() || !(is<PlainDate>(temporal_date_like.as_object()) || is<PlainDateTime>(temporal_date_like.as_object()) || is<PlainYearMonth>(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 {}; + temporal_date_like = TRY_OR_DISCARD(to_temporal_date(global_object, temporal_date_like)); } // 4. If calendar.[[Identifier]] is "iso8601", then @@ -681,9 +651,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::era_year) // 3. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]], or [[InitializedTemporalYearMonth]] internal slot, then if (!temporal_date_like.is_object() || !(is<PlainDate>(temporal_date_like.as_object()) || is<PlainDateTime>(temporal_date_like.as_object()) || is<PlainYearMonth>(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 {}; + temporal_date_like = TRY_OR_DISCARD(to_temporal_date(global_object, temporal_date_like)); } // 4. If calendar.[[Identifier]] is "iso8601", then diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp index 67a3621837..26c6ca7847 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp @@ -115,7 +115,7 @@ JS_DEFINE_NATIVE_FUNCTION(Now::plain_date) return {}; // 2. Return ! CreateTemporalDate(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[Calendar]]). - return create_temporal_date(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar()); + return TRY_OR_DISCARD(create_temporal_date(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar())); } // 2.2.8 Temporal.Now.plainDateISO ( [ temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.plaindateiso @@ -132,7 +132,7 @@ JS_DEFINE_NATIVE_FUNCTION(Now::plain_date_iso) return {}; // 3. Return ! CreateTemporalDate(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[Calendar]]). - return create_temporal_date(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar()); + return TRY_OR_DISCARD(create_temporal_date(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar())); } // 2.2.9 Temporal.Now.plainTimeISO ( [ temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.plaintimeiso diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp index a8410b5301..493ad6c750 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp @@ -6,6 +6,7 @@ */ #include <LibJS/Runtime/AbstractOperations.h> +#include <LibJS/Runtime/Completion.h> #include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/Temporal/Calendar.h> #include <LibJS/Runtime/Temporal/Instant.h> @@ -35,7 +36,7 @@ void PlainDate::visit_edges(Visitor& visitor) } // 3.5.1 CreateTemporalDate ( isoYear, isoMonth, isoDay, calendar [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporaldate -PlainDate* create_temporal_date(GlobalObject& global_object, i32 iso_year, u8 iso_month, u8 iso_day, Object& calendar, FunctionObject const* new_target) +ThrowCompletionOr<PlainDate*> create_temporal_date(GlobalObject& global_object, i32 iso_year, u8 iso_month, u8 iso_day, Object& calendar, FunctionObject const* new_target) { auto& vm = global_object.vm(); @@ -45,16 +46,12 @@ PlainDate* create_temporal_date(GlobalObject& global_object, i32 iso_year, u8 is // 4. Assert: Type(calendar) is Object. // 5. If ! IsValidISODate(isoYear, isoMonth, isoDay) is false, throw a RangeError exception. - if (!is_valid_iso_date(iso_year, iso_month, iso_day)) { - vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate); - return {}; - } + if (!is_valid_iso_date(iso_year, iso_month, iso_day)) + return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate); // 6. If ! ISODateTimeWithinLimits(isoYear, isoMonth, isoDay, 12, 0, 0, 0, 0, 0) is false, throw a RangeError exception. - if (!iso_date_time_within_limits(global_object, iso_year, iso_month, iso_day, 12, 0, 0, 0, 0, 0)) { - vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate); - return {}; - } + if (!iso_date_time_within_limits(global_object, iso_year, iso_month, iso_day, 12, 0, 0, 0, 0, 0)) + return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate); // 7. If newTarget is not present, set it to %Temporal.PlainDate%. if (!new_target) @@ -65,13 +62,13 @@ PlainDate* create_temporal_date(GlobalObject& global_object, i32 iso_year, u8 is // 10. Set object.[[ISOMonth]] to isoMonth. // 11. Set object.[[ISODay]] to isoDay. // 12. Set object.[[Calendar]] to calendar. - auto* object = TRY_OR_DISCARD(ordinary_create_from_constructor<PlainDate>(global_object, *new_target, &GlobalObject::temporal_plain_date_prototype, iso_year, iso_month, iso_day, calendar)); + auto* object = TRY(ordinary_create_from_constructor<PlainDate>(global_object, *new_target, &GlobalObject::temporal_plain_date_prototype, iso_year, iso_month, iso_day, calendar)); return object; } // 3.5.2 ToTemporalDate ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaldate -PlainDate* to_temporal_date(GlobalObject& global_object, Value item, Object* options) +ThrowCompletionOr<PlainDate*> to_temporal_date(GlobalObject& global_object, Value item, Object* options) { auto& vm = global_object.vm(); @@ -98,7 +95,7 @@ PlainDate* to_temporal_date(GlobalObject& global_object, Value item, Object* opt auto* instant = create_temporal_instant(global_object, zoned_date_time.nanoseconds()).release_value(); // ii. Let plainDateTime be ? BuiltinTimeZoneGetPlainDateTimeFor(item.[[TimeZone]], instant, item.[[Calendar]]). - auto* plain_date_time = TRY_OR_DISCARD(builtin_time_zone_get_plain_date_time_for(global_object, &zoned_date_time.time_zone(), *instant, zoned_date_time.calendar())); + auto* plain_date_time = TRY(builtin_time_zone_get_plain_date_time_for(global_object, &zoned_date_time.time_zone(), *instant, zoned_date_time.calendar())); // iii. Return ! CreateTemporalDate(plainDateTime.[[ISOYear]], plainDateTime.[[ISOMonth]], plainDateTime.[[ISODay]], plainDateTime.[[Calendar]]). return create_temporal_date(global_object, plain_date_time->iso_year(), plain_date_time->iso_month(), plain_date_time->iso_day(), plain_date_time->calendar()); @@ -112,41 +109,41 @@ PlainDate* to_temporal_date(GlobalObject& global_object, Value item, Object* opt } // d. Let calendar be ? GetTemporalCalendarWithISODefault(item). - auto* calendar = TRY_OR_DISCARD(get_temporal_calendar_with_iso_default(global_object, item_object)); + auto* calendar = TRY(get_temporal_calendar_with_iso_default(global_object, item_object)); // e. Let fieldNames be ? CalendarFields(calendar, ยซ "day", "month", "monthCode", "year" ยป). - auto field_names = TRY_OR_DISCARD(calendar_fields(global_object, *calendar, { "day"sv, "month"sv, "monthCode"sv, "year"sv })); + auto field_names = TRY(calendar_fields(global_object, *calendar, { "day"sv, "month"sv, "monthCode"sv, "year"sv })); // f. Let fields be ? PrepareTemporalFields(item, fieldNames, ยซยป). - auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, item_object, field_names, {})); + auto* fields = TRY(prepare_temporal_fields(global_object, item_object, field_names, {})); // g. Return ? DateFromFields(calendar, fields, options). - return TRY_OR_DISCARD(date_from_fields(global_object, *calendar, *fields, *options)); + return date_from_fields(global_object, *calendar, *fields, *options); } // 4. Perform ? ToTemporalOverflow(options). - (void)TRY_OR_DISCARD(to_temporal_overflow(global_object, *options)); + (void)TRY(to_temporal_overflow(global_object, *options)); // 5. Let string be ? ToString(item). auto string = item.to_string(global_object); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // 6. Let result be ? ParseTemporalDateString(string). - auto result = TRY_OR_DISCARD(parse_temporal_date_string(global_object, string)); + auto result = TRY(parse_temporal_date_string(global_object, string)); // 7. Assert: ! IsValidISODate(result.[[Year]], result.[[Month]], result.[[Day]]) is true. VERIFY(is_valid_iso_date(result.year, result.month, result.day)); // 8. Let calendar be ? ToTemporalCalendarWithISODefault(result.[[Calendar]]). - auto* calendar = TRY_OR_DISCARD(to_temporal_calendar_with_iso_default(global_object, result.calendar.has_value() ? js_string(vm, *result.calendar) : js_undefined())); + auto* calendar = TRY(to_temporal_calendar_with_iso_default(global_object, result.calendar.has_value() ? js_string(vm, *result.calendar) : js_undefined())); // 9. Return ? CreateTemporalDate(result.[[Year]], result.[[Month]], result.[[Day]], calendar). return create_temporal_date(global_object, result.year, result.month, result.day, *calendar); } // 3.5.4 RegulateISODate ( year, month, day, overflow ), https://tc39.es/proposal-temporal/#sec-temporal-regulateisodate -Optional<ISODate> regulate_iso_date(GlobalObject& global_object, double year, double month, double day, StringView overflow) +ThrowCompletionOr<ISODate> regulate_iso_date(GlobalObject& global_object, double year, double month, double day, StringView overflow) { auto& vm = global_object.vm(); // 1. Assert: year, month, and day are integers. @@ -159,18 +156,16 @@ Optional<ISODate> regulate_iso_date(GlobalObject& global_object, double year, do // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat these doubles as normal integers from this point onwards. // This does not change the exposed behavior as the call to IsValidISODate will immediately check that these values are valid ISO // values (for years: -273975 - 273975, for months: 1 - 12, for days: 1 - 31) all of which are subsets of this check. - if (!AK::is_within_range<i32>(year) || !AK::is_within_range<u8>(month) || !AK::is_within_range<u8>(day)) { - vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate); - return {}; - } + if (!AK::is_within_range<i32>(year) || !AK::is_within_range<u8>(month) || !AK::is_within_range<u8>(day)) + return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate); + auto y = static_cast<i32>(year); auto m = static_cast<u8>(month); auto d = static_cast<u8>(day); // a. If ! IsValidISODate(year, month, day) is false, throw a RangeError exception. - if (!is_valid_iso_date(y, m, d)) { - vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate); - return {}; - } + if (!is_valid_iso_date(y, m, d)) + return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate); + // b. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day }. return ISODate { .year = y, .month = m, .day = d }; } @@ -179,10 +174,9 @@ Optional<ISODate> regulate_iso_date(GlobalObject& global_object, double year, do // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat this double as normal integer from this point onwards. This // does not change the exposed behavior as the parent's call to CreateTemporalDate will immediately check that this value is a valid // ISO value for years: -273975 - 273975, which is a subset of this check. - if (!AK::is_within_range<i32>(year)) { - vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate); - return {}; - } + if (!AK::is_within_range<i32>(year)) + return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate); + auto y = static_cast<i32>(year); // a. Set month to ! ConstrainToRange(month, 1, 12). @@ -335,7 +329,7 @@ String pad_iso_year(i32 y) } // 3.5.8 TemporalDateToString ( temporalDate, showCalendar ), https://tc39.es/proposal-temporal/#sec-temporal-temporaldatetostring -Optional<String> temporal_date_to_string(GlobalObject& global_object, PlainDate& temporal_date, StringView show_calendar) +ThrowCompletionOr<String> temporal_date_to_string(GlobalObject& global_object, PlainDate& temporal_date, StringView show_calendar) { auto& vm = global_object.vm(); @@ -353,8 +347,8 @@ Optional<String> temporal_date_to_string(GlobalObject& global_object, PlainDate& // 6. Let calendarID be ? ToString(temporalDate.[[Calendar]]). auto calendar_id = Value(&temporal_date.calendar()).to_string(global_object); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // 7. Let calendar be ! FormatCalendarAnnotation(calendarID, showCalendar). auto calendar = format_calendar_annotation(calendar_id, show_calendar); @@ -364,10 +358,8 @@ Optional<String> temporal_date_to_string(GlobalObject& global_object, PlainDate& } // 3.5.9 AddISODate ( year, month, day, years, months, weeks, days, overflow ), https://tc39.es/proposal-temporal/#sec-temporal-addisodate -Optional<ISODate> add_iso_date(GlobalObject& global_object, i32 year, u8 month, u8 day, double years, double months, double weeks, double days, StringView overflow) +ThrowCompletionOr<ISODate> add_iso_date(GlobalObject& global_object, i32 year, u8 month, u8 day, double years, double months, double weeks, double days, StringView overflow) { - auto& vm = global_object.vm(); - // 1. Assert: year, month, day, years, months, weeks, and days are integers. VERIFY(years == trunc(years) && months == trunc(months) && weeks == trunc(weeks) && days == trunc(days)); @@ -378,18 +370,16 @@ Optional<ISODate> add_iso_date(GlobalObject& global_object, i32 year, u8 month, auto intermediate_year_month = balance_iso_year_month(year + years, month + months); // 4. Let intermediate be ? RegulateISODate(intermediate.[[Year]], intermediate.[[Month]], day, overflow). - auto intermediate_date = regulate_iso_date(global_object, intermediate_year_month.year, intermediate_year_month.month, day, overflow); - if (vm.exception()) - return {}; + auto intermediate_date = TRY(regulate_iso_date(global_object, intermediate_year_month.year, intermediate_year_month.month, day, overflow)); // 5. Set days to days + 7 ร weeks. days += 7 * weeks; // 6. Let d be intermediate.[[Day]] + days. - auto d = intermediate_date->day + days; + auto d = intermediate_date.day + days; // 7. Let intermediate be ! BalanceISODate(intermediate.[[Year]], intermediate.[[Month]], d). - auto intermediate = balance_iso_date(intermediate_date->year, intermediate_date->month, d); + auto intermediate = balance_iso_date(intermediate_date.year, intermediate_date.month, d); // 8. Return ? RegulateISODate(intermediate.[[Year]], intermediate.[[Month]], intermediate.[[Day]], overflow). return regulate_iso_date(global_object, intermediate.year, intermediate.month, intermediate.day, overflow); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.h index c8a5dfdfd8..4d89285e8b 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.h @@ -7,7 +7,8 @@ #pragma once -#include <LibJS/Runtime/Value.h> +#include <LibJS/Runtime/Completion.h> +#include <LibJS/Runtime/Object.h> namespace JS::Temporal { @@ -40,14 +41,14 @@ struct ISODate { u8 day; }; -PlainDate* create_temporal_date(GlobalObject&, i32 iso_year, u8 iso_month, u8 iso_day, Object& calendar, FunctionObject const* new_target = nullptr); -PlainDate* to_temporal_date(GlobalObject&, Value item, Object* options = nullptr); -Optional<ISODate> regulate_iso_date(GlobalObject&, double year, double month, double day, StringView overflow); +ThrowCompletionOr<PlainDate*> create_temporal_date(GlobalObject&, i32 iso_year, u8 iso_month, u8 iso_day, Object& calendar, FunctionObject const* new_target = nullptr); +ThrowCompletionOr<PlainDate*> to_temporal_date(GlobalObject&, Value item, Object* options = nullptr); +ThrowCompletionOr<ISODate> regulate_iso_date(GlobalObject&, double year, double month, double day, StringView overflow); bool is_valid_iso_date(i32 year, u8 month, u8 day); ISODate balance_iso_date(double year, double month, double day); String pad_iso_year(i32 y); -Optional<String> temporal_date_to_string(GlobalObject&, PlainDate&, StringView show_calendar); -Optional<ISODate> add_iso_date(GlobalObject&, i32 year, u8 month, u8 day, double years, double months, double weeks, double days, StringView overflow); +ThrowCompletionOr<String> temporal_date_to_string(GlobalObject&, PlainDate&, StringView show_calendar); +ThrowCompletionOr<ISODate> add_iso_date(GlobalObject&, i32 year, u8 month, u8 day, double years, double months, double weeks, double days, StringView overflow); i8 compare_iso_date(i32 year1, u8 month1, u8 day1, i32 year2, u8 month2, u8 day2); } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateConstructor.cpp index 8cc22592d9..6c0c9910ff 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateConstructor.cpp @@ -73,7 +73,7 @@ Value PlainDateConstructor::construct(FunctionObject& new_target) } // 6. Return ? CreateTemporalDate(y, m, d, calendar, NewTarget). - return create_temporal_date(global_object, y, m, d, *calendar, &new_target); + return TRY_OR_DISCARD(create_temporal_date(global_object, y, m, d, *calendar, &new_target)); } // 3.2.2 Temporal.PlainDate.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.from @@ -90,24 +90,22 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateConstructor::from) (void)TRY_OR_DISCARD(to_temporal_overflow(global_object, *options)); // b. Return ? CreateTemporalDate(item.[[ISOYear]], item.[[ISOMonth]], item.[[ISODay]], item.[[Calendar]]). - return 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_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())); } // 3. Return ? ToTemporalDate(item, options). - return to_temporal_date(global_object, item, options); + return TRY_OR_DISCARD(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_NATIVE_FUNCTION(PlainDateConstructor::compare) { // 1. Set one to ? ToTemporalDate(one). - auto* one = to_temporal_date(global_object, vm.argument(0)); - if (vm.exception()) - return {}; + auto* one = TRY_OR_DISCARD(to_temporal_date(global_object, vm.argument(0))); + // 2. Set two to ? ToTemporalDate(two). - auto* two = to_temporal_date(global_object, vm.argument(1)); - if (vm.exception()) - return {}; + auto* two = TRY_OR_DISCARD(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())); } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp index f3f941d919..86bb43beef 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp @@ -384,7 +384,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::with_calendar) auto* calendar = TRY_OR_DISCARD(to_temporal_calendar(global_object, vm.argument(0))); // 4. Return ? CreateTemporalDate(temporalDate.[[ISOYear]], temporalDate.[[ISOMonth]], temporalDate.[[ISODay]], calendar). - return create_temporal_date(global_object, temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day(), *calendar); + return TRY_OR_DISCARD(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 @@ -397,9 +397,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::equals) return {}; // 3. Set other to ? ToTemporalDate(other). - auto* other = to_temporal_date(global_object, vm.argument(0)); - if (vm.exception()) - return {}; + auto* other = TRY_OR_DISCARD(to_temporal_date(global_object, vm.argument(0))); // 4. If temporalDate.[[ISOYear]] โ other.[[ISOYear]], return false. if (temporal_date->iso_year() != other->iso_year()) @@ -452,11 +450,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_string) auto show_calendar = TRY_OR_DISCARD(to_show_calendar_option(global_object, *options)); // 5. Return ? TemporalDateToString(temporalDate, showCalendar). - auto string = temporal_date_to_string(global_object, *temporal_date, show_calendar); - if (vm.exception()) - return {}; - - return js_string(vm, *string); + return js_string(vm, TRY_OR_DISCARD(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 @@ -470,11 +464,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_locale_string) return {}; // 3. Return ? TemporalDateToString(temporalDate, "auto"). - auto string = temporal_date_to_string(global_object, *temporal_date, "auto"sv); - if (vm.exception()) - return {}; - - return js_string(vm, *string); + return js_string(vm, TRY_OR_DISCARD(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 @@ -487,11 +477,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_json) return {}; // 3. Return ? TemporalDateToString(temporalDate, "auto"). - auto string = temporal_date_to_string(global_object, *temporal_date, "auto"sv); - if (vm.exception()) - return {}; - - return js_string(vm, *string); + return js_string(vm, TRY_OR_DISCARD(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 diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp index 99a248582a..59e33cc38c 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp @@ -412,9 +412,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::with_plain_date) return {}; // 3. Let plainDate be ? ToTemporalDate(plainDateLike). - auto* plain_date = to_temporal_date(global_object, vm.argument(0)); - if (vm.exception()) - return {}; + auto* plain_date = TRY_OR_DISCARD(to_temporal_date(global_object, vm.argument(0))); // 4. Let calendar be ? ConsolidateCalendars(dateTime.[[Calendar]], plainDate.[[Calendar]]). auto* calendar = TRY_OR_DISCARD(consolidate_calendars(global_object, date_time->calendar(), plain_date->calendar())); @@ -484,7 +482,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_plain_date) return {}; // 3. Return ? CreateTemporalDate(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[Calendar]]). - return create_temporal_date(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar()); + return TRY_OR_DISCARD(create_temporal_date(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar())); } // 5.3.38 Temporal.PlainDateTime.prototype.toPlainYearMonth ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.toplainyearmonth diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp index 3650d534c3..96722426c4 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp @@ -288,9 +288,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::to_plain_date_time) return {}; // 3. Set temporalDate to ? ToTemporalDate(temporalDate). - auto* temporal_date = to_temporal_date(global_object, vm.argument(0)); - if (vm.exception()) - return {}; + auto* temporal_date = TRY_OR_DISCARD(to_temporal_date(global_object, vm.argument(0))); // 4. Return ? CreateTemporalDateTime(temporalDate.[[ISOYear]], temporalDate.[[ISOMonth]], temporalDate.[[ISODay]], temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]], temporalDate.[[Calendar]]). return 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()); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp index 0b1c4f2ec1..cbc3f7418b 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp @@ -748,7 +748,7 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::to_plain_date) auto* temporal_date_time = TRY_OR_DISCARD(builtin_time_zone_get_plain_date_time_for(global_object, &time_zone, *instant, calendar)); // 7. Return ? CreateTemporalDate(temporalDateTime.[[ISOYear]], temporalDateTime.[[ISOMonth]], temporalDateTime.[[ISODay]], calendar). - return create_temporal_date(global_object, temporal_date_time->iso_year(), temporal_date_time->iso_month(), temporal_date_time->iso_day(), calendar); + return TRY_OR_DISCARD(create_temporal_date(global_object, temporal_date_time->iso_year(), temporal_date_time->iso_month(), temporal_date_time->iso_day(), calendar)); } // 6.3.48 Temporal.ZonedDateTime.prototype.toPlainTime ( ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.toplaintime |