summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-09-16 18:09:52 +0100
committerLinus Groh <mail@linusgroh.de>2021-09-16 22:34:24 +0100
commit6b4777c5584295cc1ca7a4bb9a7c316fd9e987c4 (patch)
treed1253eb6671bb36f5531e3c00ce9d7a73bf2acb4 /Userland/Libraries/LibJS
parent9ac426c906a75fa80f1dea612fcff44f4481cb56 (diff)
downloadserenity-6b4777c5584295cc1ca7a4bb9a7c316fd9e987c4.zip
LibJS: Convert prepare_temporal_fields() to ThrowCompletionOr
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp17
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h2
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp12
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp4
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp8
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp4
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp8
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp4
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp4
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp8
10 files changed, 22 insertions, 49 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp
index 2b4f3c32a4..237add8c2b 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp
@@ -1038,7 +1038,7 @@ ThrowCompletionOr<double> to_positive_integer(GlobalObject& global_object, Value
}
// 13.48 PrepareTemporalFields ( fields, fieldNames, requiredFields ), https://tc39.es/proposal-temporal/#sec-temporal-preparetemporalfields
-Object* prepare_temporal_fields(GlobalObject& global_object, Object const& fields, Vector<String> const& field_names, Vector<StringView> const& required_fields)
+ThrowCompletionOr<Object*> prepare_temporal_fields(GlobalObject& global_object, Object const& fields, Vector<String> const& field_names, Vector<StringView> const& required_fields)
{
auto& vm = global_object.vm();
@@ -1052,16 +1052,15 @@ Object* prepare_temporal_fields(GlobalObject& global_object, Object const& field
for (auto& property : field_names) {
// a. Let value be ? Get(fields, property).
auto value = fields.get(property);
- if (vm.exception())
- return {};
+ if (auto* exception = vm.exception())
+ return throw_completion(exception->value());
// b. If value is undefined, then
if (value.is_undefined()) {
// i. If requiredFields contains property, then
if (required_fields.contains_slow(property)) {
// 1. Throw a TypeError exception.
- vm.throw_exception<TypeError>(global_object, ErrorType::TemporalMissingRequiredProperty, property);
- return {};
+ return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalMissingRequiredProperty, property);
}
// ii. If property is in the Property column of Table 13, then
// NOTE: The other properties in the table are automatically handled as their default value is undefined
@@ -1076,13 +1075,13 @@ Object* prepare_temporal_fields(GlobalObject& global_object, Object const& field
// 1. Let Conversion represent the abstract operation named by the Conversion value of the same row.
// 2. Set value to ? Conversion(value).
if (property.is_one_of("year", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond", "eraYear")) {
- value = Value(TRY_OR_DISCARD(to_integer_throw_on_infinity(global_object, value, ErrorType::TemporalPropertyMustBeFinite)));
+ value = Value(TRY(to_integer_throw_on_infinity(global_object, value, ErrorType::TemporalPropertyMustBeFinite)));
} else if (property.is_one_of("month", "day")) {
- value = Value(TRY_OR_DISCARD(to_positive_integer(global_object, value)));
+ value = Value(TRY(to_positive_integer(global_object, value)));
} else if (property.is_one_of("monthCode", "offset", "era")) {
value = value.to_primitive_string(global_object);
- if (vm.exception())
- return {};
+ if (auto* exception = vm.exception())
+ return throw_completion(exception->value());
}
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h
index 130c6c1ad9..d59eb448e4 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h
@@ -113,7 +113,7 @@ ThrowCompletionOr<TemporalTime> parse_temporal_time_string(GlobalObject&, String
ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject&, String const& iso_string);
ThrowCompletionOr<TemporalYearMonth> parse_temporal_year_month_string(GlobalObject&, String const& iso_string);
ThrowCompletionOr<double> to_positive_integer(GlobalObject&, Value argument);
-Object* prepare_temporal_fields(GlobalObject&, Object const& fields, Vector<String> const& field_names, Vector<StringView> const& required_fields);
+ThrowCompletionOr<Object*> prepare_temporal_fields(GlobalObject&, Object const& fields, Vector<String> const& field_names, Vector<StringView> const& required_fields);
// 13.46 ToIntegerThrowOnInfinity ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-tointegerthrowoninfinity
template<typename... Args>
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp
index e225e41dfd..4e2536eb6e 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp
@@ -791,9 +791,7 @@ Optional<ISODate> iso_date_from_fields(GlobalObject& global_object, Object const
auto overflow = TRY_OR_DISCARD(to_temporal_overflow(global_object, options));
// 3. Set fields to ? PrepareTemporalFields(fields, « "day", "month", "monthCode", "year" », «»).
- auto* prepared_fields = prepare_temporal_fields(global_object, fields, { "day", "month", "monthCode", "year" }, {});
- if (vm.exception())
- return {};
+ auto* prepared_fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, fields, { "day", "month", "monthCode", "year" }, {}));
// 4. Let year be ? Get(fields, "year").
auto year = prepared_fields->get(vm.names.year);
@@ -837,9 +835,7 @@ Optional<ISOYearMonth> iso_year_month_from_fields(GlobalObject& global_object, O
auto overflow = TRY_OR_DISCARD(to_temporal_overflow(global_object, options));
// 3. Set fields to ? PrepareTemporalFields(fields, « "month", "monthCode", "year" », «»).
- auto* prepared_fields = prepare_temporal_fields(global_object, fields, { "month"sv, "monthCode"sv, "year"sv }, {});
- if (vm.exception())
- return {};
+ auto* prepared_fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, fields, { "month"sv, "monthCode"sv, "year"sv }, {}));
// 4. Let year be ? Get(fields, "year").
auto year = prepared_fields->get(vm.names.year);
@@ -875,9 +871,7 @@ Optional<ISOMonthDay> iso_month_day_from_fields(GlobalObject& global_object, Obj
auto overflow = TRY_OR_DISCARD(to_temporal_overflow(global_object, options));
// 3. Set fields to ? PrepareTemporalFields(fields, « "day", "month", "monthCode", "year" », «»).
- auto* prepared_fields = prepare_temporal_fields(global_object, fields, { "day"sv, "month"sv, "monthCode"sv, "year"sv }, {});
- if (vm.exception())
- return {};
+ auto* prepared_fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, fields, { "day"sv, "month"sv, "monthCode"sv, "year"sv }, {}));
// 4. Let month be ? Get(fields, "month").
auto month_value = prepared_fields->get(vm.names.month);
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp
index 5729cf914a..59af7b0b42 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp
@@ -122,9 +122,7 @@ PlainDate* to_temporal_date(GlobalObject& global_object, Value item, Object* opt
return {};
// f. Let fields be ? PrepareTemporalFields(item, fieldNames, «»).
- auto* fields = prepare_temporal_fields(global_object, item_object, field_names, {});
- if (vm.exception())
- return {};
+ auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, item_object, field_names, {}));
// g. Return ? DateFromFields(calendar, fields, options).
return date_from_fields(global_object, *calendar, *fields, *options);
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp
index a80d101706..a870fdab2f 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp
@@ -317,9 +317,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_plain_year_month)
return {};
// 5. Let fields be ? PrepareTemporalFields(temporalDate, fieldNames, «»).
- auto* fields = prepare_temporal_fields(global_object, *temporal_date, field_names, {});
- if (vm.exception())
- return {};
+ auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *temporal_date, field_names, {}));
// 6. Return ? YearMonthFromFields(calendar, fields).
return year_month_from_fields(global_object, calendar, *fields);
@@ -343,9 +341,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_plain_month_day)
return {};
// 5. Let fields be ? PrepareTemporalFields(temporalDate, fieldNames, «»).
- auto* fields = prepare_temporal_fields(global_object, *temporal_date, field_names, {});
- if (vm.exception())
- return {};
+ auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *temporal_date, field_names, {}));
// 6. Return ? MonthDayFromFields(calendar, fields).
return month_day_from_fields(global_object, calendar, *fields);
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp
index 25c3c3beea..2e32e26d5c 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp
@@ -183,9 +183,7 @@ PlainDateTime* to_temporal_date_time(GlobalObject& global_object, Value item, Ob
return {};
// f. Let fields be ? PrepareTemporalFields(item, fieldNames, «»).
- auto* fields = prepare_temporal_fields(global_object, item_object, field_names, {});
- if (vm.exception())
- return {};
+ auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, item_object, field_names, {}));
// g. Let result be ? InterpretTemporalDateTimeFields(calendar, fields, options).
auto maybe_result = interpret_temporal_date_time_fields(global_object, *calendar, *fields, *options);
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp
index f3de919178..d233b7f20a 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp
@@ -507,9 +507,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_plain_year_month)
return {};
// 5. Let fields be ? PrepareTemporalFields(dateTime, fieldNames, «»).
- auto* fields = prepare_temporal_fields(global_object, *date_time, field_names, {});
- if (vm.exception())
- return {};
+ auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *date_time, field_names, {}));
// 6. Return ? YearMonthFromFields(calendar, fields).
return year_month_from_fields(global_object, calendar, *fields);
@@ -533,9 +531,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_plain_month_day)
return {};
// 5. Let fields be ? PrepareTemporalFields(dateTime, fieldNames, «»).
- auto* fields = prepare_temporal_fields(global_object, *date_time, field_names, {});
- if (vm.exception())
- return {};
+ auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *date_time, field_names, {}));
// 6. Return ? MonthDayFromFields(calendar, fields).
return month_day_from_fields(global_object, calendar, *fields);
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp
index a5a0ded83b..44670069e8 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp
@@ -103,9 +103,7 @@ ThrowCompletionOr<PlainMonthDay*> to_temporal_month_day(GlobalObject& global_obj
return throw_completion(exception->value());
// e. Let fields be ? PrepareTemporalFields(item, fieldNames, «»).
- auto* fields = prepare_temporal_fields(global_object, item_object, field_names, {});
- if (auto* exception = vm.exception())
- return throw_completion(exception->value());
+ auto* fields = TRY(prepare_temporal_fields(global_object, item_object, field_names, {}));
// f. Let month be ? Get(fields, "month").
auto month = fields->get(vm.names.month);
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp
index 39f1a7f8bd..19425cadb8 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp
@@ -62,9 +62,7 @@ ThrowCompletionOr<PlainYearMonth*> to_temporal_year_month(GlobalObject& global_o
return throw_completion(exception->value());
// d. Let fields be ? PrepareTemporalFields(item, fieldNames, «»).
- auto* fields = prepare_temporal_fields(global_object, item_object, field_names, {});
- if (auto* exception = vm.exception())
- return throw_completion(exception->value());
+ auto* fields = TRY(prepare_temporal_fields(global_object, item_object, field_names, {}));
// e. Return ? YearMonthFromFields(calendar, fields, options).
return year_month_from_fields(global_object, *calendar, *fields, options);
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
index f0d206a545..0296675687 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
@@ -822,9 +822,7 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::to_plain_year_month)
return {};
// 8. Let fields be ? PrepareTemporalFields(temporalDateTime, fieldNames, «»).
- auto* fields = prepare_temporal_fields(global_object, *temporal_date_time, field_names, {});
- if (vm.exception())
- return {};
+ auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *temporal_date_time, field_names, {}));
// 9. Return ? YearMonthFromFields(calendar, fields).
return year_month_from_fields(global_object, calendar, *fields);
@@ -857,9 +855,7 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::to_plain_month_day)
return {};
// 8. Let fields be ? PrepareTemporalFields(temporalDateTime, fieldNames, «»).
- auto* fields = prepare_temporal_fields(global_object, *temporal_date_time, field_names, {});
- if (vm.exception())
- return {};
+ auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *temporal_date_time, field_names, {}));
// 9. Return ? MonthDayFromFields(calendar, fields).
return month_day_from_fields(global_object, calendar, *fields);