1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:17:45 +00:00

LibJS: Convert to_temporal_overflow() to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-09-15 23:39:42 +01:00
parent b1e7e62657
commit 9f03647f1f
13 changed files with 26 additions and 53 deletions

View file

@ -202,12 +202,12 @@ ThrowCompletionOr<Variant<String, NumberType>> get_string_or_number_option(Globa
} }
// 13.6 ToTemporalOverflow ( normalizedOptions ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaloverflow // 13.6 ToTemporalOverflow ( normalizedOptions ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaloverflow
Optional<String> to_temporal_overflow(GlobalObject& global_object, Object const& normalized_options) ThrowCompletionOr<String> to_temporal_overflow(GlobalObject& global_object, Object const& normalized_options)
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
// 1. Return ? GetOption(normalizedOptions, "overflow", « String », « "constrain", "reject" », "constrain"). // 1. Return ? GetOption(normalizedOptions, "overflow", « String », « "constrain", "reject" », "constrain").
auto option = TRY_OR_DISCARD(get_option(global_object, normalized_options, vm.names.overflow, { OptionType::String }, { "constrain"sv, "reject"sv }, js_string(vm, "constrain"))); auto option = TRY(get_option(global_object, normalized_options, vm.names.overflow, { OptionType::String }, { "constrain"sv, "reject"sv }, js_string(vm, "constrain")));
VERIFY(option.is_string()); VERIFY(option.is_string());
return option.as_string().string(); return option.as_string().string();

View file

@ -89,7 +89,7 @@ ThrowCompletionOr<Object*> get_options_object(GlobalObject&, Value options);
ThrowCompletionOr<Value> get_option(GlobalObject&, Object const& options, PropertyName const& property, Vector<OptionType> const& types, Vector<StringView> const& values, Value fallback); ThrowCompletionOr<Value> get_option(GlobalObject&, Object const& options, PropertyName const& property, Vector<OptionType> const& types, Vector<StringView> const& values, Value fallback);
template<typename NumberType> template<typename NumberType>
ThrowCompletionOr<Variant<String, NumberType>> get_string_or_number_option(GlobalObject&, Object const& options, PropertyName const& property, Vector<StringView> const& string_values, NumberType minimum, NumberType maximum, Value fallback); ThrowCompletionOr<Variant<String, NumberType>> get_string_or_number_option(GlobalObject&, Object const& options, PropertyName const& property, Vector<StringView> const& string_values, NumberType minimum, NumberType maximum, Value fallback);
Optional<String> to_temporal_overflow(GlobalObject&, Object const& normalized_options); ThrowCompletionOr<String> to_temporal_overflow(GlobalObject&, Object const& normalized_options);
Optional<String> to_temporal_rounding_mode(GlobalObject&, Object const& normalized_options, String const& fallback); Optional<String> to_temporal_rounding_mode(GlobalObject&, Object const& normalized_options, String const& fallback);
Optional<String> to_show_calendar_option(GlobalObject&, Object const& normalized_options); Optional<String> to_show_calendar_option(GlobalObject&, Object const& normalized_options);
u64 to_temporal_rounding_increment(GlobalObject&, Object const& normalized_options, Optional<double> dividend, bool inclusive); u64 to_temporal_rounding_increment(GlobalObject&, Object const& normalized_options, Optional<double> dividend, bool inclusive);

View file

@ -791,9 +791,7 @@ Optional<ISODate> iso_date_from_fields(GlobalObject& global_object, Object const
// 1. Assert: Type(fields) is Object. // 1. Assert: Type(fields) is Object.
// 2. Let overflow be ? ToTemporalOverflow(options). // 2. Let overflow be ? ToTemporalOverflow(options).
auto overflow = to_temporal_overflow(global_object, options); auto overflow = TRY_OR_DISCARD(to_temporal_overflow(global_object, options));
if (vm.exception())
return {};
// 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 = prepare_temporal_fields(global_object, fields, { "day", "month", "monthCode", "year" }, {});
@ -828,7 +826,7 @@ Optional<ISODate> iso_date_from_fields(GlobalObject& global_object, Object const
} }
// 9. Return ? RegulateISODate(year, month, day, overflow). // 9. Return ? RegulateISODate(year, month, day, overflow).
return regulate_iso_date(global_object, year.as_double(), month, day.as_double(), *overflow); return regulate_iso_date(global_object, year.as_double(), month, day.as_double(), overflow);
} }
// 12.1.39 ISOYearMonthFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-isoyearmonthfromfields // 12.1.39 ISOYearMonthFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-isoyearmonthfromfields
@ -839,9 +837,7 @@ Optional<ISOYearMonth> iso_year_month_from_fields(GlobalObject& global_object, O
// 1. Assert: Type(fields) is Object. // 1. Assert: Type(fields) is Object.
// 2. Let overflow be ? ToTemporalOverflow(options). // 2. Let overflow be ? ToTemporalOverflow(options).
auto overflow = to_temporal_overflow(global_object, options); auto overflow = TRY_OR_DISCARD(to_temporal_overflow(global_object, options));
if (vm.exception())
return {};
// 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 = prepare_temporal_fields(global_object, fields, { "month"sv, "monthCode"sv, "year"sv }, {});
@ -865,7 +861,7 @@ Optional<ISOYearMonth> iso_year_month_from_fields(GlobalObject& global_object, O
return {}; return {};
// 7. Let result be ? RegulateISOYearMonth(year, month, overflow). // 7. Let result be ? RegulateISOYearMonth(year, month, overflow).
auto result = TRY_OR_DISCARD(regulate_iso_year_month(global_object, year.as_double(), month, *overflow)); auto result = TRY_OR_DISCARD(regulate_iso_year_month(global_object, year.as_double(), month, overflow));
// 8. Return the Record { [[Year]]: result.[[Year]], [[Month]]: result.[[Month]], [[ReferenceISODay]]: 1 }. // 8. Return the Record { [[Year]]: result.[[Year]], [[Month]]: result.[[Month]], [[ReferenceISODay]]: 1 }.
return ISOYearMonth { .year = result.year, .month = result.month, .reference_iso_day = 1 }; return ISOYearMonth { .year = result.year, .month = result.month, .reference_iso_day = 1 };
@ -879,9 +875,7 @@ Optional<ISOMonthDay> iso_month_day_from_fields(GlobalObject& global_object, Obj
// 1. Assert: Type(fields) is Object. // 1. Assert: Type(fields) is Object.
// 2. Let overflow be ? ToTemporalOverflow(options). // 2. Let overflow be ? ToTemporalOverflow(options).
auto overflow = to_temporal_overflow(global_object, options); auto overflow = TRY_OR_DISCARD(to_temporal_overflow(global_object, options));
if (vm.exception())
return {};
// 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 = prepare_temporal_fields(global_object, fields, { "day"sv, "month"sv, "monthCode"sv, "year"sv }, {});
@ -934,12 +928,12 @@ Optional<ISOMonthDay> iso_month_day_from_fields(GlobalObject& global_object, Obj
// 12. If monthCode is undefined, then // 12. If monthCode is undefined, then
if (month_code.is_undefined()) { if (month_code.is_undefined()) {
// a. Let result be ? RegulateISODate(year, month, day, overflow). // a. Let result be ? RegulateISODate(year, month, day, overflow).
result = regulate_iso_date(global_object, year.as_double(), month, day.as_double(), *overflow); result = regulate_iso_date(global_object, year.as_double(), month, day.as_double(), overflow);
} }
// 13. Else, // 13. Else,
else { else {
// a. Let result be ? RegulateISODate(referenceISOYear, month, day, overflow). // a. Let result be ? RegulateISODate(referenceISOYear, month, day, overflow).
result = regulate_iso_date(global_object, reference_iso_year, month, day.as_double(), *overflow); result = regulate_iso_date(global_object, reference_iso_year, month, day.as_double(), overflow);
} }
if (vm.exception()) if (vm.exception())
return {}; return {};

View file

@ -194,9 +194,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::date_add)
auto* options = TRY_OR_DISCARD(get_options_object(global_object, vm.argument(2))); auto* options = TRY_OR_DISCARD(get_options_object(global_object, vm.argument(2)));
// 7. Let overflow be ? ToTemporalOverflow(options). // 7. Let overflow be ? ToTemporalOverflow(options).
auto overflow = to_temporal_overflow(global_object, *options); auto overflow = TRY_OR_DISCARD(to_temporal_overflow(global_object, *options));
if (vm.exception())
return {};
// FIXME: Narrowing conversion from 'double' to 'i64' // FIXME: Narrowing conversion from 'double' to 'i64'
auto* nanoseconds = js_bigint(vm, Crypto::SignedBigInteger::create_from(duration->nanoseconds())); auto* nanoseconds = js_bigint(vm, Crypto::SignedBigInteger::create_from(duration->nanoseconds()));
@ -205,7 +203,7 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::date_add)
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);
// 9. Let result be ? AddISODate(date.[[ISOYear]], date.[[ISOMonth]], date.[[ISODay]], duration.[[Years]], duration.[[Months]], duration.[[Weeks]], balanceResult.[[Days]], overflow). // 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()) if (vm.exception())
return {}; return {};

View file

@ -131,9 +131,7 @@ PlainDate* to_temporal_date(GlobalObject& global_object, Value item, Object* opt
} }
// 4. Perform ? ToTemporalOverflow(options). // 4. Perform ? ToTemporalOverflow(options).
(void)to_temporal_overflow(global_object, *options); (void)TRY_OR_DISCARD(to_temporal_overflow(global_object, *options));
if (vm.exception())
return {};
// 5. Let string be ? ToString(item). // 5. Let string be ? ToString(item).
auto string = item.to_string(global_object); auto string = item.to_string(global_object);

View file

@ -89,9 +89,8 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateConstructor::from)
if (item.is_object() && is<PlainDate>(item.as_object())) { if (item.is_object() && is<PlainDate>(item.as_object())) {
auto& plain_date_item = static_cast<PlainDate&>(item.as_object()); auto& plain_date_item = static_cast<PlainDate&>(item.as_object());
// a. Perform ? ToTemporalOverflow(options). // a. Perform ? ToTemporalOverflow(options).
(void)to_temporal_overflow(global_object, *options); (void)TRY_OR_DISCARD(to_temporal_overflow(global_object, *options));
if (vm.exception())
return {};
// b. Return ? CreateTemporalDate(item.[[ISOYear]], item.[[ISOMonth]], item.[[ISODay]], item.[[Calendar]]). // b. Return ? CreateTemporalDate(item.[[ISOYear]], item.[[ISOMonth]], item.[[ISODay]], item.[[Calendar]]).
return create_temporal_date(global_object, plain_date_item.iso_year(), plain_date_item.iso_month(), plain_date_item.iso_day(), plain_date_item.calendar()); return create_temporal_date(global_object, plain_date_item.iso_year(), plain_date_item.iso_month(), plain_date_item.iso_day(), plain_date_item.calendar());
} }

View file

@ -112,12 +112,10 @@ Optional<ISODateTime> interpret_temporal_date_time_fields(GlobalObject& global_o
return {}; return {};
// 3. Let overflow be ? ToTemporalOverflow(options). // 3. Let overflow be ? ToTemporalOverflow(options).
auto overflow = to_temporal_overflow(global_object, options); auto overflow = TRY_OR_DISCARD(to_temporal_overflow(global_object, options));
if (vm.exception())
return {};
// 4. Let timeResult be ? RegulateTime(timeResult.[[Hour]], timeResult.[[Minute]], timeResult.[[Second]], timeResult.[[Millisecond]], timeResult.[[Microsecond]], timeResult.[[Nanosecond]], overflow). // 4. Let timeResult be ? RegulateTime(timeResult.[[Hour]], timeResult.[[Minute]], timeResult.[[Second]], timeResult.[[Millisecond]], timeResult.[[Microsecond]], timeResult.[[Nanosecond]], overflow).
auto time_result = TRY_OR_DISCARD(regulate_time(global_object, unregulated_time_result.hour, unregulated_time_result.minute, unregulated_time_result.second, unregulated_time_result.millisecond, unregulated_time_result.microsecond, unregulated_time_result.nanosecond, *overflow)); auto time_result = TRY_OR_DISCARD(regulate_time(global_object, unregulated_time_result.hour, unregulated_time_result.minute, unregulated_time_result.second, unregulated_time_result.millisecond, unregulated_time_result.microsecond, unregulated_time_result.nanosecond, overflow));
// 5. Return the Record { [[Year]]: temporalDate.[[ISOYear]], [[Month]]: temporalDate.[[ISOMonth]], [[Day]]: temporalDate.[[ISODay]], [[Hour]]: timeResult.[[Hour]], [[Minute]]: timeResult.[[Minute]], [[Second]]: timeResult.[[Second]], [[Millisecond]]: timeResult.[[Millisecond]], [[Microsecond]]: timeResult.[[Microsecond]], [[Nanosecond]]: timeResult.[[Nanosecond]] }. // 5. Return the Record { [[Year]]: temporalDate.[[ISOYear]], [[Month]]: temporalDate.[[ISOMonth]], [[Day]]: temporalDate.[[ISODay]], [[Hour]]: timeResult.[[Hour]], [[Minute]]: timeResult.[[Minute]], [[Second]]: timeResult.[[Second]], [[Millisecond]]: timeResult.[[Millisecond]], [[Microsecond]]: timeResult.[[Microsecond]], [[Nanosecond]]: timeResult.[[Nanosecond]] }.
return ISODateTime { return ISODateTime {
@ -198,9 +196,7 @@ PlainDateTime* to_temporal_date_time(GlobalObject& global_object, Value item, Ob
// 3. Else, // 3. Else,
else { else {
// a. Perform ? ToTemporalOverflow(options). // a. Perform ? ToTemporalOverflow(options).
(void)to_temporal_overflow(global_object, *options); (void)TRY_OR_DISCARD(to_temporal_overflow(global_object, *options));
if (vm.exception())
return {};
// b. Let string be ? ToString(item). // b. Let string be ? ToString(item).
auto string = item.to_string(global_object); auto string = item.to_string(global_object);

View file

@ -110,9 +110,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimeConstructor::from)
auto& plain_date_time = static_cast<PlainDateTime&>(item.as_object()); auto& plain_date_time = static_cast<PlainDateTime&>(item.as_object());
// a. Perform ? ToTemporalOverflow(options). // a. Perform ? ToTemporalOverflow(options).
(void)to_temporal_overflow(global_object, *options); (void)TRY_OR_DISCARD(to_temporal_overflow(global_object, *options));
if (vm.exception())
return {};
// b. Return ? CreateTemporalDateTime(item.[[ISOYear]], item.[[ISOMonth]], item.[[ISODay]], item.[[ISOHour]], item.[[ISOMinute]], item.[[ISOSecond]], item.[[ISOMillisecond]], item.[[ISOMicrosecond]], item.[[ISONanosecond]], item.[[Calendar]]). // b. Return ? CreateTemporalDateTime(item.[[ISOYear]], item.[[ISOMonth]], item.[[ISODay]], item.[[ISOHour]], item.[[ISOMinute]], item.[[ISOSecond]], item.[[ISOMillisecond]], item.[[ISOMicrosecond]], item.[[ISONanosecond]], item.[[Calendar]]).
return create_temporal_date_time(global_object, plain_date_time.iso_year(), plain_date_time.iso_month(), plain_date_time.iso_day(), plain_date_time.iso_hour(), plain_date_time.iso_minute(), plain_date_time.iso_second(), plain_date_time.iso_millisecond(), plain_date_time.iso_microsecond(), plain_date_time.iso_nanosecond(), plain_date_time.calendar()); return create_temporal_date_time(global_object, plain_date_time.iso_year(), plain_date_time.iso_month(), plain_date_time.iso_day(), plain_date_time.iso_hour(), plain_date_time.iso_minute(), plain_date_time.iso_second(), plain_date_time.iso_millisecond(), plain_date_time.iso_microsecond(), plain_date_time.iso_nanosecond(), plain_date_time.calendar());

View file

@ -98,9 +98,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayConstructor::from)
// 2. If Type(item) is Object and item has an [[InitializedTemporalMonthDay]] internal slot, then // 2. If Type(item) is Object and item has an [[InitializedTemporalMonthDay]] internal slot, then
if (item.is_object() && is<PlainMonthDay>(item.as_object())) { if (item.is_object() && is<PlainMonthDay>(item.as_object())) {
// a. Perform ? ToTemporalOverflow(options). // a. Perform ? ToTemporalOverflow(options).
(void)to_temporal_overflow(global_object, *options); (void)TRY_OR_DISCARD(to_temporal_overflow(global_object, *options));
if (vm.exception())
return {};
auto& plain_month_day_object = static_cast<PlainMonthDay&>(item.as_object()); auto& plain_month_day_object = static_cast<PlainMonthDay&>(item.as_object());

View file

@ -88,9 +88,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimeConstructor::from)
auto* options = TRY_OR_DISCARD(get_options_object(global_object, vm.argument(1))); auto* options = TRY_OR_DISCARD(get_options_object(global_object, vm.argument(1)));
// 2. Let overflow be ? ToTemporalOverflow(options). // 2. Let overflow be ? ToTemporalOverflow(options).
auto overflow = to_temporal_overflow(global_object, *options); auto overflow = TRY_OR_DISCARD(to_temporal_overflow(global_object, *options));
if (vm.exception())
return {};
auto item = vm.argument(0); auto item = vm.argument(0);
@ -102,7 +100,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimeConstructor::from)
} }
// 4. Return ? ToTemporalTime(item, overflow). // 4. Return ? ToTemporalTime(item, overflow).
return TRY_OR_DISCARD(to_temporal_time(global_object, item, *overflow)); return TRY_OR_DISCARD(to_temporal_time(global_object, item, overflow));
} }
// 4.2.3 Temporal.PlainTime.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.compare // 4.2.3 Temporal.PlainTime.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.compare

View file

@ -195,9 +195,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::with)
auto* options = TRY_OR_DISCARD(get_options_object(global_object, vm.argument(1))); auto* options = TRY_OR_DISCARD(get_options_object(global_object, vm.argument(1)));
// 11. Let overflow be ? ToTemporalOverflow(options). // 11. Let overflow be ? ToTemporalOverflow(options).
auto overflow = to_temporal_overflow(global_object, *options); auto overflow = TRY_OR_DISCARD(to_temporal_overflow(global_object, *options));
if (vm.exception())
return {};
// 12. If partialTime.[[Hour]] is not undefined, then // 12. If partialTime.[[Hour]] is not undefined, then
// a. Let hour be partialTime.[[Hour]]. // a. Let hour be partialTime.[[Hour]].
@ -236,7 +234,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::with)
auto nanosecond = partial_time.nanosecond.value_or(temporal_time->iso_nanosecond()); auto nanosecond = partial_time.nanosecond.value_or(temporal_time->iso_nanosecond());
// 24. Let result be ? RegulateTime(hour, minute, second, millisecond, microsecond, nanosecond, overflow). // 24. Let result be ? RegulateTime(hour, minute, second, millisecond, microsecond, nanosecond, overflow).
auto result = TRY_OR_DISCARD(regulate_time(global_object, hour, minute, second, millisecond, microsecond, nanosecond, *overflow)); auto result = TRY_OR_DISCARD(regulate_time(global_object, hour, minute, second, millisecond, microsecond, nanosecond, overflow));
// 25. Return ? CreateTemporalTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]). // 25. Return ? CreateTemporalTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]).
return TRY_OR_DISCARD(create_temporal_time(global_object, result.hour, result.minute, result.second, result.millisecond, result.microsecond, result.nanosecond)); return TRY_OR_DISCARD(create_temporal_time(global_object, result.hour, result.minute, result.second, result.millisecond, result.microsecond, result.nanosecond));

View file

@ -71,9 +71,7 @@ ThrowCompletionOr<PlainYearMonth*> to_temporal_year_month(GlobalObject& global_o
} }
// 4. Perform ? ToTemporalOverflow(options). // 4. Perform ? ToTemporalOverflow(options).
(void)to_temporal_overflow(global_object, *options); (void)TRY(to_temporal_overflow(global_object, *options));
if (auto* exception = vm.exception())
return throw_completion(exception->value());
// 5. Let string be ? ToString(item). // 5. Let string be ? ToString(item).
auto string = item.to_string(global_object); auto string = item.to_string(global_object);

View file

@ -100,9 +100,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthConstructor::from)
// 2. If Type(item) is Object and item has an [[InitializedTemporalYearMonth]] internal slot, then // 2. If Type(item) is Object and item has an [[InitializedTemporalYearMonth]] internal slot, then
if (item.is_object() && is<PlainYearMonth>(item.as_object())) { if (item.is_object() && is<PlainYearMonth>(item.as_object())) {
// a. Perform ? ToTemporalOverflow(options). // a. Perform ? ToTemporalOverflow(options).
(void)to_temporal_overflow(global_object, *options); (void)TRY_OR_DISCARD(to_temporal_overflow(global_object, *options));
if (vm.exception())
return {};
auto& plain_year_month_object = static_cast<PlainYearMonth&>(item.as_object()); auto& plain_year_month_object = static_cast<PlainYearMonth&>(item.as_object());