From 5ea1810ada65dc8c734ab12f1f9735055bb69b93 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Thu, 16 Sep 2021 02:11:23 +0300 Subject: [PATCH] LibJS: Convert PlainMonthDay AOs to ThrowCompletionOr --- .../Runtime/Temporal/CalendarPrototype.cpp | 2 +- .../LibJS/Runtime/Temporal/PlainMonthDay.cpp | 46 +++++++++---------- .../LibJS/Runtime/Temporal/PlainMonthDay.h | 6 +-- .../Temporal/PlainMonthDayConstructor.cpp | 6 +-- .../Temporal/PlainMonthDayPrototype.cpp | 22 ++------- 5 files changed, 33 insertions(+), 49 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp index 4a380d09e5..a9fe5f745a 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp @@ -170,7 +170,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::month_day_from_fields) return {}; // 7. Return ? CreateTemporalMonthDay(result.[[Month]], result.[[Day]], calendar, result.[[ReferenceISOYear]]). - return create_temporal_month_day(global_object, result->month, result->day, *calendar, result->reference_iso_year); + return TRY_OR_DISCARD(create_temporal_month_day(global_object, result->month, result->day, *calendar, result->reference_iso_year)); } // 12.4.7 Temporal.Calendar.prototype.dateAdd ( date, duration [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.dateadd diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp index 4c4c754ec3..a5a0ded83b 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp @@ -34,7 +34,7 @@ void PlainMonthDay::visit_edges(Visitor& visitor) } // 10.5.1 ToTemporalMonthDay ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalmonthday -PlainMonthDay* to_temporal_month_day(GlobalObject& global_object, Value item, Object const* options) +ThrowCompletionOr to_temporal_month_day(GlobalObject& global_object, Value item, Object const* options) { auto& vm = global_object.vm(); @@ -82,8 +82,8 @@ PlainMonthDay* to_temporal_month_day(GlobalObject& global_object, Value item, Ob } else { // i. Let calendar be ? Get(item, "calendar"). auto calendar_value = item_object.get(vm.names.calendar); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // ii. If calendar is undefined, then // 1. Let calendarAbsent be true. @@ -93,34 +93,34 @@ PlainMonthDay* to_temporal_month_day(GlobalObject& global_object, Value item, Ob // iv. Set calendar to ? ToTemporalCalendarWithISODefault(calendar). calendar = to_temporal_calendar_with_iso_default(global_object, calendar_value); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); } // d. Let fieldNames be ? CalendarFields(calendar, « "day", "month", "monthCode", "year" »). auto field_names = calendar_fields(global_object, *calendar, { "day"sv, "month"sv, "monthCode"sv, "year"sv }); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // e. Let fields be ? PrepareTemporalFields(item, fieldNames, «»). auto* fields = prepare_temporal_fields(global_object, item_object, field_names, {}); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // f. Let month be ? Get(fields, "month"). auto month = fields->get(vm.names.month); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // g. Let monthCode be ? Get(fields, "monthCode"). auto month_code = fields->get(vm.names.monthCode); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // h. Let year be ? Get(fields, "year"). auto year = fields->get(vm.names.year); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // i. If calendarAbsent is true, and month is not undefined, and monthCode is undefined and year is undefined, then if (calendar_absent && !month.is_undefined() && month_code.is_undefined() && year.is_undefined()) { @@ -139,7 +139,7 @@ PlainMonthDay* to_temporal_month_day(GlobalObject& global_object, Value item, Ob } // 10.5.2 CreateTemporalMonthDay ( isoMonth, isoDay, calendar, referenceISOYear [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalmonthday -PlainMonthDay* create_temporal_month_day(GlobalObject& global_object, u8 iso_month, u8 iso_day, Object& calendar, i32 reference_iso_year, FunctionObject const* new_target) +ThrowCompletionOr create_temporal_month_day(GlobalObject& global_object, u8 iso_month, u8 iso_day, Object& calendar, i32 reference_iso_year, FunctionObject const* new_target) { auto& vm = global_object.vm(); @@ -147,10 +147,8 @@ PlainMonthDay* create_temporal_month_day(GlobalObject& global_object, u8 iso_mon // 2. Assert: Type(calendar) is Object. // 3. If ! IsValidISODate(referenceISOYear, isoMonth, isoDay) is false, throw a RangeError exception. - if (!is_valid_iso_date(reference_iso_year, iso_month, iso_day)) { - vm.throw_exception(global_object, ErrorType::TemporalInvalidPlainMonthDay); - return {}; - } + if (!is_valid_iso_date(reference_iso_year, iso_month, iso_day)) + return vm.throw_completion(global_object, ErrorType::TemporalInvalidPlainMonthDay); // 4. If newTarget is not present, set it to %Temporal.PlainMonthDay%. if (!new_target) @@ -161,14 +159,14 @@ PlainMonthDay* create_temporal_month_day(GlobalObject& global_object, u8 iso_mon // 7. Set object.[[ISODay]] to isoDay. // 8. Set object.[[Calendar]] to calendar. // 9. Set object.[[ISOYear]] to referenceISOYear. - auto* object = TRY_OR_DISCARD(ordinary_create_from_constructor(global_object, *new_target, &GlobalObject::temporal_plain_month_day_prototype, iso_month, iso_day, reference_iso_year, calendar)); + auto* object = TRY(ordinary_create_from_constructor(global_object, *new_target, &GlobalObject::temporal_plain_month_day_prototype, iso_month, iso_day, reference_iso_year, calendar)); // 10. Return object. return object; } // 10.5.3 TemporalMonthDayToString ( monthDay, showCalendar ), https://tc39.es/proposal-temporal/#sec-temporal-temporalmonthdaytostring -Optional temporal_month_day_to_string(GlobalObject& global_object, PlainMonthDay& month_day, StringView show_calendar) +ThrowCompletionOr temporal_month_day_to_string(GlobalObject& global_object, PlainMonthDay& month_day, StringView show_calendar) { auto& vm = global_object.vm(); @@ -182,8 +180,8 @@ Optional temporal_month_day_to_string(GlobalObject& global_object, Plain // 6. Let calendarID be ? ToString(monthDay.[[Calendar]]). auto calendar_id = Value(&month_day.calendar()).to_string(global_object); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // 7. If calendarID is not "iso8601", then if (calendar_id != "iso8601"sv) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.h index 0137fa0faa..b3fa0401bf 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.h @@ -39,8 +39,8 @@ struct ISOMonthDay { i32 reference_iso_year; }; -PlainMonthDay* to_temporal_month_day(GlobalObject&, Value item, Object const* options = nullptr); -PlainMonthDay* create_temporal_month_day(GlobalObject&, u8 iso_month, u8 iso_day, Object& calendar, i32 reference_iso_year, FunctionObject const* new_target = nullptr); -Optional temporal_month_day_to_string(GlobalObject&, PlainMonthDay&, StringView show_calendar); +ThrowCompletionOr to_temporal_month_day(GlobalObject&, Value item, Object const* options = nullptr); +ThrowCompletionOr create_temporal_month_day(GlobalObject&, u8 iso_month, u8 iso_day, Object& calendar, i32 reference_iso_year, FunctionObject const* new_target = nullptr); +ThrowCompletionOr temporal_month_day_to_string(GlobalObject&, PlainMonthDay&, StringView show_calendar); } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayConstructor.cpp index 639d713783..61023460c7 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayConstructor.cpp @@ -90,7 +90,7 @@ Value PlainMonthDayConstructor::construct(FunctionObject& new_target) } // 7. Return ? CreateTemporalMonthDay(m, d, calendar, ref, NewTarget). - return create_temporal_month_day(global_object, m, d, *calendar, ref, &new_target); + return TRY_OR_DISCARD(create_temporal_month_day(global_object, m, d, *calendar, ref, &new_target)); } // 10.2.2 Temporal.PlainMonthDay.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.from @@ -113,11 +113,11 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayConstructor::from) auto& plain_month_day_object = static_cast(item.as_object()); // b. Return ? CreateTemporalMonthDay(item.[[ISOMonth]], item.[[ISODay]], item.[[Calendar]], item.[[ISOYear]]). - return create_temporal_month_day(global_object, plain_month_day_object.iso_month(), plain_month_day_object.iso_day(), plain_month_day_object.calendar(), plain_month_day_object.iso_year()); + return TRY_OR_DISCARD(create_temporal_month_day(global_object, plain_month_day_object.iso_month(), plain_month_day_object.iso_day(), plain_month_day_object.calendar(), plain_month_day_object.iso_year())); } // 3. Return ? ToTemporalMonthDay(item, options). - return to_temporal_month_day(global_object, item, options); + return TRY_OR_DISCARD(to_temporal_month_day(global_object, item, options)); } } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp index 83436c278e..5957df4119 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp @@ -96,9 +96,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::equals) return {}; // 3. Set other to ? ToTemporalMonthDay(other). - auto* other = to_temporal_month_day(global_object, vm.argument(0)); - if (vm.exception()) - return {}; + auto* other = TRY_OR_DISCARD(to_temporal_month_day(global_object, vm.argument(0))); // 4. If monthDay.[[ISOMonth]] ≠ other.[[ISOMonth]], return false. if (month_day->iso_month() != other->iso_month()) @@ -136,11 +134,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_string) return {}; // 5. Return ? TemporalMonthDayToString(monthDay, showCalendar). - auto string = temporal_month_day_to_string(global_object, *month_day, *show_calendar); - if (vm.exception()) - return {}; - - return js_string(vm, *string); + return js_string(vm, TRY_OR_DISCARD(temporal_month_day_to_string(global_object, *month_day, *show_calendar))); } // 10.3.9 Temporal.PlainMonthDay.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.tolocalestring @@ -154,11 +148,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_locale_string) return {}; // 3. Return ? TemporalMonthDayToString(monthDay, "auto"). - auto string = temporal_month_day_to_string(global_object, *month_day, "auto"sv); - if (vm.exception()) - return {}; - - return js_string(vm, *string); + return js_string(vm, TRY_OR_DISCARD(temporal_month_day_to_string(global_object, *month_day, "auto"sv))); } // 10.3.10 Temporal.PlainMonthDay.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.tojson @@ -171,11 +161,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_json) return {}; // 3. Return ? TemporalMonthDayToString(monthDay, "auto"). - auto string = temporal_month_day_to_string(global_object, *month_day, "auto"sv); - if (vm.exception()) - return {}; - - return js_string(vm, *string); + return js_string(vm, TRY_OR_DISCARD(temporal_month_day_to_string(global_object, *month_day, "auto"sv))); } // 10.3.11 Temporal.PlainMonthDay.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.valueof