1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:37:35 +00:00

LibJS: Convert prepare_temporal_fields() to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-09-16 18:09:52 +01:00
parent 9ac426c906
commit 6b4777c558
10 changed files with 22 additions and 49 deletions

View file

@ -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 // 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(); auto& vm = global_object.vm();
@ -1052,16 +1052,15 @@ Object* prepare_temporal_fields(GlobalObject& global_object, Object const& field
for (auto& property : field_names) { for (auto& property : field_names) {
// a. Let value be ? Get(fields, property). // a. Let value be ? Get(fields, property).
auto value = fields.get(property); auto value = fields.get(property);
if (vm.exception()) if (auto* exception = vm.exception())
return {}; return throw_completion(exception->value());
// b. If value is undefined, then // b. If value is undefined, then
if (value.is_undefined()) { if (value.is_undefined()) {
// i. If requiredFields contains property, then // i. If requiredFields contains property, then
if (required_fields.contains_slow(property)) { if (required_fields.contains_slow(property)) {
// 1. Throw a TypeError exception. // 1. Throw a TypeError exception.
vm.throw_exception<TypeError>(global_object, ErrorType::TemporalMissingRequiredProperty, property); return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalMissingRequiredProperty, property);
return {};
} }
// ii. If property is in the Property column of Table 13, then // 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 // 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. // 1. Let Conversion represent the abstract operation named by the Conversion value of the same row.
// 2. Set value to ? Conversion(value). // 2. Set value to ? Conversion(value).
if (property.is_one_of("year", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond", "eraYear")) { 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")) { } 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")) { } else if (property.is_one_of("monthCode", "offset", "era")) {
value = value.to_primitive_string(global_object); value = value.to_primitive_string(global_object);
if (vm.exception()) if (auto* exception = vm.exception())
return {}; return throw_completion(exception->value());
} }
} }

View file

@ -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<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject&, String const& iso_string);
ThrowCompletionOr<TemporalYearMonth> parse_temporal_year_month_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); 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 // 13.46 ToIntegerThrowOnInfinity ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-tointegerthrowoninfinity
template<typename... Args> template<typename... Args>

View file

@ -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)); auto overflow = TRY_OR_DISCARD(to_temporal_overflow(global_object, options));
// 3. Set fields to ? PrepareTemporalFields(fields, « "day", "month", "monthCode", "year" », «»). // 3. Set fields to ? PrepareTemporalFields(fields, « "day", "month", "monthCode", "year" », «»).
auto* prepared_fields = prepare_temporal_fields(global_object, fields, { "day", "month", "monthCode", "year" }, {}); auto* prepared_fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, fields, { "day", "month", "monthCode", "year" }, {}));
if (vm.exception())
return {};
// 4. Let year be ? Get(fields, "year"). // 4. Let year be ? Get(fields, "year").
auto year = prepared_fields->get(vm.names.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)); auto overflow = TRY_OR_DISCARD(to_temporal_overflow(global_object, options));
// 3. Set fields to ? PrepareTemporalFields(fields, « "month", "monthCode", "year" », «»). // 3. Set fields to ? PrepareTemporalFields(fields, « "month", "monthCode", "year" », «»).
auto* prepared_fields = prepare_temporal_fields(global_object, fields, { "month"sv, "monthCode"sv, "year"sv }, {}); auto* prepared_fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, fields, { "month"sv, "monthCode"sv, "year"sv }, {}));
if (vm.exception())
return {};
// 4. Let year be ? Get(fields, "year"). // 4. Let year be ? Get(fields, "year").
auto year = prepared_fields->get(vm.names.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)); auto overflow = TRY_OR_DISCARD(to_temporal_overflow(global_object, options));
// 3. Set fields to ? PrepareTemporalFields(fields, « "day", "month", "monthCode", "year" », «»). // 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 }, {}); auto* prepared_fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, fields, { "day"sv, "month"sv, "monthCode"sv, "year"sv }, {}));
if (vm.exception())
return {};
// 4. Let month be ? Get(fields, "month"). // 4. Let month be ? Get(fields, "month").
auto month_value = prepared_fields->get(vm.names.month); auto month_value = prepared_fields->get(vm.names.month);

View file

@ -122,9 +122,7 @@ PlainDate* to_temporal_date(GlobalObject& global_object, Value item, Object* opt
return {}; return {};
// f. Let fields be ? PrepareTemporalFields(item, fieldNames, «»). // f. Let fields be ? PrepareTemporalFields(item, fieldNames, «»).
auto* fields = prepare_temporal_fields(global_object, item_object, field_names, {}); auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, item_object, field_names, {}));
if (vm.exception())
return {};
// g. Return ? DateFromFields(calendar, fields, options). // g. Return ? DateFromFields(calendar, fields, options).
return date_from_fields(global_object, *calendar, *fields, *options); return date_from_fields(global_object, *calendar, *fields, *options);

View file

@ -317,9 +317,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_plain_year_month)
return {}; return {};
// 5. Let fields be ? PrepareTemporalFields(temporalDate, fieldNames, «»). // 5. Let fields be ? PrepareTemporalFields(temporalDate, fieldNames, «»).
auto* fields = prepare_temporal_fields(global_object, *temporal_date, field_names, {}); auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *temporal_date, field_names, {}));
if (vm.exception())
return {};
// 6. Return ? YearMonthFromFields(calendar, fields). // 6. Return ? YearMonthFromFields(calendar, fields).
return year_month_from_fields(global_object, 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 {}; return {};
// 5. Let fields be ? PrepareTemporalFields(temporalDate, fieldNames, «»). // 5. Let fields be ? PrepareTemporalFields(temporalDate, fieldNames, «»).
auto* fields = prepare_temporal_fields(global_object, *temporal_date, field_names, {}); auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *temporal_date, field_names, {}));
if (vm.exception())
return {};
// 6. Return ? MonthDayFromFields(calendar, fields). // 6. Return ? MonthDayFromFields(calendar, fields).
return month_day_from_fields(global_object, calendar, *fields); return month_day_from_fields(global_object, calendar, *fields);

View file

@ -183,9 +183,7 @@ PlainDateTime* to_temporal_date_time(GlobalObject& global_object, Value item, Ob
return {}; return {};
// f. Let fields be ? PrepareTemporalFields(item, fieldNames, «»). // f. Let fields be ? PrepareTemporalFields(item, fieldNames, «»).
auto* fields = prepare_temporal_fields(global_object, item_object, field_names, {}); auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, item_object, field_names, {}));
if (vm.exception())
return {};
// g. Let result be ? InterpretTemporalDateTimeFields(calendar, fields, options). // g. Let result be ? InterpretTemporalDateTimeFields(calendar, fields, options).
auto maybe_result = interpret_temporal_date_time_fields(global_object, *calendar, *fields, *options); auto maybe_result = interpret_temporal_date_time_fields(global_object, *calendar, *fields, *options);

View file

@ -507,9 +507,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_plain_year_month)
return {}; return {};
// 5. Let fields be ? PrepareTemporalFields(dateTime, fieldNames, «»). // 5. Let fields be ? PrepareTemporalFields(dateTime, fieldNames, «»).
auto* fields = prepare_temporal_fields(global_object, *date_time, field_names, {}); auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *date_time, field_names, {}));
if (vm.exception())
return {};
// 6. Return ? YearMonthFromFields(calendar, fields). // 6. Return ? YearMonthFromFields(calendar, fields).
return year_month_from_fields(global_object, 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 {}; return {};
// 5. Let fields be ? PrepareTemporalFields(dateTime, fieldNames, «»). // 5. Let fields be ? PrepareTemporalFields(dateTime, fieldNames, «»).
auto* fields = prepare_temporal_fields(global_object, *date_time, field_names, {}); auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *date_time, field_names, {}));
if (vm.exception())
return {};
// 6. Return ? MonthDayFromFields(calendar, fields). // 6. Return ? MonthDayFromFields(calendar, fields).
return month_day_from_fields(global_object, calendar, *fields); return month_day_from_fields(global_object, calendar, *fields);

View file

@ -103,9 +103,7 @@ ThrowCompletionOr<PlainMonthDay*> to_temporal_month_day(GlobalObject& global_obj
return throw_completion(exception->value()); return throw_completion(exception->value());
// e. Let fields be ? PrepareTemporalFields(item, fieldNames, «»). // e. Let fields be ? PrepareTemporalFields(item, fieldNames, «»).
auto* fields = prepare_temporal_fields(global_object, item_object, field_names, {}); auto* fields = TRY(prepare_temporal_fields(global_object, item_object, field_names, {}));
if (auto* exception = vm.exception())
return throw_completion(exception->value());
// f. Let month be ? Get(fields, "month"). // f. Let month be ? Get(fields, "month").
auto month = fields->get(vm.names.month); auto month = fields->get(vm.names.month);

View file

@ -62,9 +62,7 @@ ThrowCompletionOr<PlainYearMonth*> to_temporal_year_month(GlobalObject& global_o
return throw_completion(exception->value()); return throw_completion(exception->value());
// d. Let fields be ? PrepareTemporalFields(item, fieldNames, «»). // d. Let fields be ? PrepareTemporalFields(item, fieldNames, «»).
auto* fields = prepare_temporal_fields(global_object, item_object, field_names, {}); auto* fields = TRY(prepare_temporal_fields(global_object, item_object, field_names, {}));
if (auto* exception = vm.exception())
return throw_completion(exception->value());
// e. Return ? YearMonthFromFields(calendar, fields, options). // e. Return ? YearMonthFromFields(calendar, fields, options).
return year_month_from_fields(global_object, *calendar, *fields, options); return year_month_from_fields(global_object, *calendar, *fields, options);

View file

@ -822,9 +822,7 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::to_plain_year_month)
return {}; return {};
// 8. Let fields be ? PrepareTemporalFields(temporalDateTime, fieldNames, «»). // 8. Let fields be ? PrepareTemporalFields(temporalDateTime, fieldNames, «»).
auto* fields = prepare_temporal_fields(global_object, *temporal_date_time, field_names, {}); auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *temporal_date_time, field_names, {}));
if (vm.exception())
return {};
// 9. Return ? YearMonthFromFields(calendar, fields). // 9. Return ? YearMonthFromFields(calendar, fields).
return year_month_from_fields(global_object, 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 {}; return {};
// 8. Let fields be ? PrepareTemporalFields(temporalDateTime, fieldNames, «»). // 8. Let fields be ? PrepareTemporalFields(temporalDateTime, fieldNames, «»).
auto* fields = prepare_temporal_fields(global_object, *temporal_date_time, field_names, {}); auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *temporal_date_time, field_names, {}));
if (vm.exception())
return {};
// 9. Return ? MonthDayFromFields(calendar, fields). // 9. Return ? MonthDayFromFields(calendar, fields).
return month_day_from_fields(global_object, calendar, *fields); return month_day_from_fields(global_object, calendar, *fields);