diff options
author | Linus Groh <mail@linusgroh.de> | 2021-09-16 22:07:14 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-09-17 08:25:12 +0100 |
commit | ea7cc70118e1f16d2897d2851198345a0a93627d (patch) | |
tree | 0725e23f50c63ec2473c09ec8b0aff08b7ede450 /Userland/Libraries/LibJS | |
parent | f8d92232c88a659a871e7cb498270c13c6a1524f (diff) | |
download | serenity-ea7cc70118e1f16d2897d2851198345a0a93627d.zip |
LibJS: Convert Duration AOs to ThrowCompletionOr
Diffstat (limited to 'Userland/Libraries/LibJS')
6 files changed, 68 insertions, 87 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp index bc9ba72a15..a7b4f7df68 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp @@ -180,9 +180,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::date_add) return {}; // 5. Set duration to ? ToTemporalDuration(duration). - auto* duration = to_temporal_duration(global_object, vm.argument(1)); - if (vm.exception()) - return {}; + auto* duration = TRY_OR_DISCARD(to_temporal_duration(global_object, vm.argument(1))); // 6. Set options to ? GetOptionsObject(options). auto* options = TRY_OR_DISCARD(get_options_object(global_object, vm.argument(2))); @@ -194,10 +192,10 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::date_add) auto* nanoseconds = js_bigint(vm, Crypto::SignedBigInteger::create_from(duration->nanoseconds())); // 8. Let balanceResult be ! BalanceDuration(duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]], "day"). - auto balance_result = balance_duration(global_object, duration->days(), duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), *nanoseconds, "day"sv); + 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); + 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 {}; diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp index 05158486a0..b6b155b480 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp @@ -5,6 +5,7 @@ */ #include <LibJS/Runtime/AbstractOperations.h> +#include <LibJS/Runtime/Completion.h> #include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/Object.h> #include <LibJS/Runtime/Temporal/AbstractOperations.h> @@ -31,11 +32,11 @@ Duration::Duration(double years, double months, double weeks, double days, doubl } // 7.5.1 ToTemporalDuration ( item ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalduration -Duration* to_temporal_duration(GlobalObject& global_object, Value item) +ThrowCompletionOr<Duration*> to_temporal_duration(GlobalObject& global_object, Value item) { auto& vm = global_object.vm(); - Optional<TemporalDuration> result; + TemporalDuration result; // 1. If Type(item) is Object, then if (item.is_object()) { @@ -45,27 +46,25 @@ Duration* to_temporal_duration(GlobalObject& global_object, Value item) return &static_cast<Duration&>(item.as_object()); } // b. Let result be ? ToTemporalDurationRecord(item). - result = to_temporal_duration_record(global_object, item.as_object()); - if (vm.exception()) - return {}; + result = TRY(to_temporal_duration_record(global_object, item.as_object())); } // 2. Else, else { // a. 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()); // b. Let result be ? ParseTemporalDurationString(string). - result = TRY_OR_DISCARD(parse_temporal_duration_string(global_object, string)); + result = TRY(parse_temporal_duration_string(global_object, string)); } // 3. Return ? CreateTemporalDuration(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]). - return create_temporal_duration(global_object, result->years, result->months, result->weeks, result->days, result->hours, result->minutes, result->seconds, result->milliseconds, result->microseconds, result->nanoseconds); + return create_temporal_duration(global_object, result.years, result.months, result.weeks, result.days, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds); } // 7.5.2 ToTemporalDurationRecord ( temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaldurationrecord -TemporalDuration to_temporal_duration_record(GlobalObject& global_object, Object const& temporal_duration_like) +ThrowCompletionOr<TemporalDuration> to_temporal_duration_record(GlobalObject& global_object, Object const& temporal_duration_like) { auto& vm = global_object.vm(); @@ -91,8 +90,8 @@ TemporalDuration to_temporal_duration_record(GlobalObject& global_object, Object // b. Let val be ? Get(temporalDurationLike, prop). auto value = temporal_duration_like.get(property); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // c. If val is undefined, then if (value.is_undefined()) { @@ -106,14 +105,13 @@ TemporalDuration to_temporal_duration_record(GlobalObject& global_object, Object // ii. Let val be ? ToNumber(val). value = value.to_number(global_object); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // iii. If ! IsIntegralNumber(val) is false, then if (!value.is_integral_number()) { // 1. Throw a RangeError exception. - vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidDurationPropertyValueNonIntegral, property.as_string(), value.to_string_without_side_effects()); - return {}; + return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidDurationPropertyValueNonIntegral, property.as_string(), value.to_string_without_side_effects()); } // iv. Set result's internal slot whose name is the Internal Slot value of the current row to val. @@ -124,8 +122,7 @@ TemporalDuration to_temporal_duration_record(GlobalObject& global_object, Object // 6. If any is false, then if (!any) { // a. Throw a TypeError exception. - vm.throw_exception<TypeError>(global_object, ErrorType::TemporalInvalidDurationLikeObject); - return {}; + return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalInvalidDurationLikeObject); } // 7. Return result. @@ -176,14 +173,14 @@ bool is_valid_duration(double years, double months, double weeks, double days, d } // 7.5.6 ToPartialDuration ( temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal-topartialduration -PartialDuration to_partial_duration(GlobalObject& global_object, Value temporal_duration_like) +ThrowCompletionOr<PartialDuration> to_partial_duration(GlobalObject& global_object, Value temporal_duration_like) { auto& vm = global_object.vm(); // 1. If Type(temporalDurationLike) is not Object, then if (!temporal_duration_like.is_object()) { - vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, temporal_duration_like.to_string_without_side_effects()); - return {}; + // a. Throw a TypeError exception. + return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, temporal_duration_like.to_string_without_side_effects()); } // 2. Let result be the Record { [[Years]]: undefined, [[Months]]: undefined, [[Weeks]]: undefined, [[Days]]: undefined, [[Hours]]: undefined, [[Minutes]]: undefined, [[Seconds]]: undefined, [[Milliseconds]]: undefined, [[Microseconds]]: undefined, [[Nanoseconds]]: undefined }. @@ -198,8 +195,8 @@ PartialDuration to_partial_duration(GlobalObject& global_object, Value temporal_ // b. Let value be ? Get(temporalDurationLike, property). auto value = temporal_duration_like.as_object().get(property); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // c. If value is not undefined, then if (!value.is_undefined()) { @@ -208,14 +205,13 @@ PartialDuration to_partial_duration(GlobalObject& global_object, Value temporal_ // ii. Set value to ? ToNumber(value). value = value.to_number(global_object); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // iii. If ! IsIntegralNumber(value) is false, then if (!value.is_integral_number()) { // 1. Throw a RangeError exception. - vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidDurationPropertyValueNonIntegral, property.as_string(), value.to_string_without_side_effects()); - return {}; + return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidDurationPropertyValueNonIntegral, property.as_string(), value.to_string_without_side_effects()); } // iv. Set result's internal slot whose name is the Internal Slot value of the current row to value. @@ -226,8 +222,7 @@ PartialDuration to_partial_duration(GlobalObject& global_object, Value temporal_ // 5. If any is false, then if (!any) { // a. Throw a TypeError exception. - vm.throw_exception<TypeError>(global_object, ErrorType::TemporalInvalidDurationLikeObject); - return {}; + return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalInvalidDurationLikeObject); } // 6. Return result. @@ -235,15 +230,13 @@ PartialDuration to_partial_duration(GlobalObject& global_object, Value temporal_ } // 7.5.7 CreateTemporalDuration ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalduration -Duration* create_temporal_duration(GlobalObject& global_object, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, FunctionObject const* new_target) +ThrowCompletionOr<Duration*> create_temporal_duration(GlobalObject& global_object, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, FunctionObject const* new_target) { auto& vm = global_object.vm(); // 1. If ! IsValidDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds) is false, throw a RangeError exception. - if (!is_valid_duration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds)) { - vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidDuration); - return {}; - } + if (!is_valid_duration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds)) + return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidDuration); // 2. If newTarget is not present, set it to %Temporal.Duration%. if (!new_target) @@ -260,7 +253,7 @@ Duration* create_temporal_duration(GlobalObject& global_object, double years, do // 11. Set object.[[Milliseconds]] to milliseconds. // 12. Set object.[[Microseconds]] to microseconds. // 13. Set object.[[Nanoseconds]] to nanoseconds. - auto* object = TRY_OR_DISCARD(ordinary_create_from_constructor<Duration>(global_object, *new_target, &GlobalObject::temporal_duration_prototype, years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds)); + auto* object = TRY(ordinary_create_from_constructor<Duration>(global_object, *new_target, &GlobalObject::temporal_duration_prototype, years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds)); // 14. Return object. return object; @@ -273,7 +266,7 @@ Duration* create_negated_temporal_duration(GlobalObject& global_object, Duration // 2. Assert: duration has an [[InitializedTemporalDuration]] internal slot. // 3. Return ! CreateTemporalDuration(−duration.[[Years]], −duration.[[Months]], −duration.[[Weeks]], −duration.[[Days]], −duration.[[Hours]], −duration.[[Minutes]], −duration.[[Seconds]], −duration.[[Milliseconds]], −duration.[[Microseconds]], −duration.[[Nanoseconds]]). - return create_temporal_duration(global_object, -duration.years(), -duration.months(), -duration.weeks(), -duration.days(), -duration.hours(), -duration.minutes(), -duration.seconds(), -duration.milliseconds(), -duration.microseconds(), -duration.nanoseconds()); + return create_temporal_duration(global_object, -duration.years(), -duration.months(), -duration.weeks(), -duration.days(), -duration.hours(), -duration.minutes(), -duration.seconds(), -duration.milliseconds(), -duration.microseconds(), -duration.nanoseconds()).release_value(); } // 7.5.10 TotalDurationNanoseconds ( days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, offsetShift ), https://tc39.es/proposal-temporal/#sec-temporal-totaldurationnanoseconds @@ -310,7 +303,7 @@ BigInt* total_duration_nanoseconds(GlobalObject& global_object, double days, dou } // 7.5.11 BalanceDuration ( days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, largestUnit [ , relativeTo ] ), https://tc39.es/proposal-temporal/#sec-temporal-balanceduration -Optional<BalancedDuration> balance_duration(GlobalObject& global_object, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, BigInt const& nanoseconds, String const& largest_unit, Object* relative_to) +ThrowCompletionOr<BalancedDuration> balance_duration(GlobalObject& global_object, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, BigInt const& nanoseconds, String const& largest_unit, Object* relative_to) { auto& vm = global_object.vm(); // 1. If relativeTo is not present, set relativeTo to undefined. @@ -320,8 +313,8 @@ Optional<BalancedDuration> balance_duration(GlobalObject& global_object, double if (relative_to && is<ZonedDateTime>(*relative_to)) { // a. Let endNs be ? AddZonedDateTime(relativeTo.[[Nanoseconds]], relativeTo.[[TimeZone]], relativeTo.[[Calendar]], 0, 0, 0, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds). TODO(); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // b. Set nanoseconds to endNs − relativeTo.[[Nanoseconds]]. } // 3. Else, @@ -334,8 +327,8 @@ Optional<BalancedDuration> balance_duration(GlobalObject& global_object, double if (largest_unit.is_one_of("year"sv, "month"sv, "week"sv, "day"sv)) { // a. Let result be ? NanosecondsToDays(nanoseconds, relativeTo). TODO(); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // b. Set days to result.[[Days]]. @@ -453,48 +446,43 @@ Optional<BalancedDuration> balance_duration(GlobalObject& global_object, double } // 7.5.20 ToLimitedTemporalDuration ( temporalDurationLike, disallowedFields ),https://tc39.es/proposal-temporal/#sec-temporal-tolimitedtemporalduration -Optional<TemporalDuration> to_limited_temporal_duration(GlobalObject& global_object, Value temporal_duration_like, Vector<StringView> const& disallowed_fields) +ThrowCompletionOr<TemporalDuration> to_limited_temporal_duration(GlobalObject& global_object, Value temporal_duration_like, Vector<StringView> const& disallowed_fields) { auto& vm = global_object.vm(); - Optional<TemporalDuration> duration; + TemporalDuration duration; // 1. If Type(temporalDurationLike) is not Object, then if (!temporal_duration_like.is_object()) { // a. Let str be ? ToString(temporalDurationLike). auto str = temporal_duration_like.to_string(global_object); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // b. Let duration be ? ParseTemporalDurationString(str). - duration = TRY_OR_DISCARD(parse_temporal_duration_string(global_object, str)); + duration = TRY(parse_temporal_duration_string(global_object, str)); } // 2. Else, else { // a. Let duration be ? ToTemporalDurationRecord(temporalDurationLike). - duration = to_temporal_duration_record(global_object, temporal_duration_like.as_object()); - if (vm.exception()) - return {}; + duration = TRY(to_temporal_duration_record(global_object, temporal_duration_like.as_object())); } // 3. If ! IsValidDuration(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]) is false, throw a RangeError exception. - if (!is_valid_duration(duration->years, duration->months, duration->weeks, duration->days, duration->hours, duration->minutes, duration->seconds, duration->milliseconds, duration->microseconds, duration->nanoseconds)) { - vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidDuration); - return {}; - } + if (!is_valid_duration(duration.years, duration.months, duration.weeks, duration.days, duration.hours, duration.minutes, duration.seconds, duration.milliseconds, duration.microseconds, duration.nanoseconds)) + return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidDuration); // 4. For each row of Table 7, except the header row, in table order, do for (auto& [internal_slot, property] : temporal_duration_like_properties<TemporalDuration, double>(vm)) { // a. Let prop be the Property value of the current row. // b. Let value be duration's internal slot whose name is the Internal Slot value of the current row. - auto value = (*duration).*internal_slot; + auto value = duration.*internal_slot; // If value is not 0 and disallowedFields contains prop, then if (value != 0 && disallowed_fields.contains_slow(property.as_string())) { // i. Throw a RangeError exception. - vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidDurationPropertyValueNonZero, property.as_string(), value); - return {}; + return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidDurationPropertyValueNonZero, property.as_string(), value); } } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Duration.h b/Userland/Libraries/LibJS/Runtime/Temporal/Duration.h index d7101569ec..771984f65d 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Duration.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Duration.h @@ -7,6 +7,7 @@ #pragma once #include <AK/Optional.h> +#include <LibJS/Runtime/Completion.h> #include <LibJS/Runtime/Object.h> namespace JS::Temporal { @@ -107,15 +108,15 @@ auto temporal_duration_like_properties = [](VM& vm) { }; }; -Duration* to_temporal_duration(GlobalObject&, Value item); -TemporalDuration to_temporal_duration_record(GlobalObject&, Object const& temporal_duration_like); +ThrowCompletionOr<Duration*> to_temporal_duration(GlobalObject&, Value item); +ThrowCompletionOr<TemporalDuration> to_temporal_duration_record(GlobalObject&, Object const& temporal_duration_like); i8 duration_sign(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds); bool is_valid_duration(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds); -PartialDuration to_partial_duration(GlobalObject&, Value temporal_duration_like); -Duration* create_temporal_duration(GlobalObject&, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, FunctionObject const* new_target = nullptr); +ThrowCompletionOr<PartialDuration> to_partial_duration(GlobalObject&, Value temporal_duration_like); +ThrowCompletionOr<Duration*> create_temporal_duration(GlobalObject&, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, FunctionObject const* new_target = nullptr); Duration* create_negated_temporal_duration(GlobalObject& global_object, Duration const& duration); BigInt* total_duration_nanoseconds(GlobalObject&, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, BigInt const& nanoseconds, double offset_shift); -Optional<BalancedDuration> balance_duration(GlobalObject&, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, BigInt const& nanoseconds, String const& largest_unit, Object* relative_to = nullptr); -Optional<TemporalDuration> to_limited_temporal_duration(GlobalObject&, Value temporal_duration_like, Vector<StringView> const& disallowed_fields); +ThrowCompletionOr<BalancedDuration> balance_duration(GlobalObject&, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, BigInt const& nanoseconds, String const& largest_unit, Object* relative_to = nullptr); +ThrowCompletionOr<TemporalDuration> to_limited_temporal_duration(GlobalObject&, Value temporal_duration_like, Vector<StringView> const& disallowed_fields); } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/DurationConstructor.cpp index 83cf7a9364..2a69dada37 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/DurationConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/DurationConstructor.cpp @@ -81,7 +81,7 @@ Value DurationConstructor::construct(FunctionObject& new_target) auto ns = TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, vm.argument(9), ErrorType::TemporalInvalidDuration)); // 12. Return ? CreateTemporalDuration(y, mo, w, d, h, m, s, ms, mis, ns, NewTarget). - return create_temporal_duration(global_object, y, mo, w, d, h, m, s, ms, mis, ns, &new_target); + return TRY_OR_DISCARD(create_temporal_duration(global_object, y, mo, w, d, h, m, s, ms, mis, ns, &new_target)); } // 7.2.2 Temporal.Duration.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.duration.from @@ -94,11 +94,11 @@ JS_DEFINE_NATIVE_FUNCTION(DurationConstructor::from) auto& duration = static_cast<Duration&>(item.as_object()); // a. Return ? CreateTemporalDuration(item.[[Years]], item.[[Months]], item.[[Weeks]], item.[[Days]], item.[[Hours]], item.[[Minutes]], item.[[Seconds]], item.[[Milliseconds]], item.[[Microseconds]], item.[[Nanoseconds]]). - return create_temporal_duration(global_object, duration.years(), duration.months(), duration.weeks(), duration.days(), duration.hours(), duration.minutes(), duration.seconds(), duration.milliseconds(), duration.microseconds(), duration.nanoseconds()); + return TRY_OR_DISCARD(create_temporal_duration(global_object, duration.years(), duration.months(), duration.weeks(), duration.days(), duration.hours(), duration.minutes(), duration.seconds(), duration.milliseconds(), duration.microseconds(), duration.nanoseconds())); } // 2. Return ? ToTemporalDuration(item). - return to_temporal_duration(global_object, item); + return TRY_OR_DISCARD(to_temporal_duration(global_object, item)); } } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp index 0622572cae..9568c0f73b 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp @@ -220,9 +220,7 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::with) return {}; // 3. Let temporalDurationLike be ? ToPartialDuration(temporalDurationLike). - auto temporal_duration_like = to_partial_duration(global_object, vm.argument(0)); - if (vm.exception()) - return {}; + auto temporal_duration_like = TRY_OR_DISCARD(to_partial_duration(global_object, vm.argument(0))); // 4. If temporalDurationLike.[[Years]] is not undefined, then // a. Let years be temporalDurationLike.[[Years]]. @@ -285,7 +283,7 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::with) auto nanoseconds = temporal_duration_like.nanoseconds.value_or(duration->nanoseconds()); // 24. Return ? CreateTemporalDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds). - return create_temporal_duration(global_object, years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds); + return TRY_OR_DISCARD(create_temporal_duration(global_object, years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds)); } // 7.3.16 Temporal.Duration.prototype.negated ( ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.negated @@ -311,7 +309,7 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::abs) return {}; // 3. Return ! CreateTemporalDuration(abs(duration.[[Years]]), abs(duration.[[Months]]), abs(duration.[[Weeks]]), abs(duration.[[Days]]), abs(duration.[[Hours]]), abs(duration.[[Minutes]]), abs(duration.[[Seconds]]), abs(duration.[[Milliseconds]]), abs(duration.[[Microseconds]]), abs(duration.[[Nanoseconds]])). - return create_temporal_duration(global_object, fabs(duration->years()), fabs(duration->months()), fabs(duration->weeks()), fabs(duration->days()), fabs(duration->hours()), fabs(duration->minutes()), fabs(duration->seconds()), fabs(duration->milliseconds()), fabs(duration->microseconds()), fabs(duration->nanoseconds())); + return TRY_OR_DISCARD(create_temporal_duration(global_object, fabs(duration->years()), fabs(duration->months()), fabs(duration->weeks()), fabs(duration->days()), fabs(duration->hours()), fabs(duration->minutes()), fabs(duration->seconds()), fabs(duration->milliseconds()), fabs(duration->microseconds()), fabs(duration->nanoseconds()))); } // 7.3.25 Temporal.Duration.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.valueof diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp index 044d187b55..5227a33c06 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp @@ -137,12 +137,10 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::add) return {}; // 3. Let duration be ? ToLimitedTemporalDuration(temporalDurationLike, « "years", "months", "weeks", "days" »). - auto duration = to_limited_temporal_duration(global_object, temporal_duration_like, { "years"sv, "months"sv, "weeks"sv, "days"sv }); - if (vm.exception()) - return {}; + auto duration = TRY_OR_DISCARD(to_limited_temporal_duration(global_object, temporal_duration_like, { "years"sv, "months"sv, "weeks"sv, "days"sv })); // 4. Let ns be ? AddInstant(instant.[[Nanoseconds]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]). - auto* ns = add_instant(global_object, instant->nanoseconds(), duration->hours, duration->minutes, duration->seconds, duration->milliseconds, duration->microseconds, duration->nanoseconds); + auto* ns = add_instant(global_object, instant->nanoseconds(), duration.hours, duration.minutes, duration.seconds, duration.milliseconds, duration.microseconds, duration.nanoseconds); if (vm.exception()) return {}; @@ -162,12 +160,10 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::subtract) return {}; // 3. Let duration be ? ToLimitedTemporalDuration(temporalDurationLike, « "years", "months", "weeks", "days" »). - auto duration = to_limited_temporal_duration(global_object, temporal_duration_like, { "years"sv, "months"sv, "weeks"sv, "days"sv }); - if (vm.exception()) - return {}; + auto duration = TRY_OR_DISCARD(to_limited_temporal_duration(global_object, temporal_duration_like, { "years"sv, "months"sv, "weeks"sv, "days"sv })); // 4. Let ns be ? AddInstant(instant.[[Nanoseconds]], −duration.[[Hours]], −duration.[[Minutes]], −duration.[[Seconds]], −duration.[[Milliseconds]], −duration.[[Microseconds]], −duration.[[Nanoseconds]]). - auto* ns = add_instant(global_object, instant->nanoseconds(), -duration->hours, -duration->minutes, -duration->seconds, -duration->milliseconds, -duration->microseconds, -duration->nanoseconds); + auto* ns = add_instant(global_object, instant->nanoseconds(), -duration.hours, -duration.minutes, -duration.seconds, -duration.milliseconds, -duration.microseconds, -duration.nanoseconds); if (vm.exception()) return {}; @@ -217,10 +213,10 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::until) auto rounded_ns = difference_instant(global_object, instant->nanoseconds(), other->nanoseconds(), rounding_increment, *smallest_unit, rounding_mode); // 13. Let result be ! BalanceDuration(0, 0, 0, 0, 0, 0, roundedNs, largestUnit). - auto result = balance_duration(global_object, 0, 0, 0, 0, 0, 0, *rounded_ns, largest_unit); + auto result = balance_duration(global_object, 0, 0, 0, 0, 0, 0, *rounded_ns, largest_unit).release_value(); // 14. Return ? CreateTemporalDuration(0, 0, 0, 0, result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]). - return create_temporal_duration(global_object, 0, 0, 0, 0, result->hours, result->minutes, result->seconds, result->milliseconds, result->microseconds, result->nanoseconds); + return TRY_OR_DISCARD(create_temporal_duration(global_object, 0, 0, 0, 0, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds)); } // 8.3.10 Temporal.Instant.prototype.since ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.since @@ -265,10 +261,10 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::since) auto rounded_ns = difference_instant(global_object, other->nanoseconds(), instant->nanoseconds(), rounding_increment, *smallest_unit, rounding_mode); // 13. Let result be ! BalanceDuration(0, 0, 0, 0, 0, 0, roundedNs, largestUnit). - auto result = balance_duration(global_object, 0, 0, 0, 0, 0, 0, *rounded_ns, largest_unit); + auto result = balance_duration(global_object, 0, 0, 0, 0, 0, 0, *rounded_ns, largest_unit).release_value(); // 14. Return ? CreateTemporalDuration(0, 0, 0, 0, result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]). - return create_temporal_duration(global_object, 0, 0, 0, 0, result->hours, result->minutes, result->seconds, result->milliseconds, result->microseconds, result->nanoseconds); + return TRY_OR_DISCARD(create_temporal_duration(global_object, 0, 0, 0, 0, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds)); } // 8.3.11 Temporal.Instant.prototype.round ( options ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.round |