mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:27:44 +00:00
LibJS: Convert prepare_temporal_fields() to ThrowCompletionOr
This commit is contained in:
parent
9ac426c906
commit
6b4777c558
10 changed files with 22 additions and 49 deletions
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue