From 52a4ee563de19392e90e102375d414a1b070c6ab Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 15 Jun 2022 00:47:40 +0100 Subject: [PATCH] LibJS: Assume options is an object in the MergeLargestUnitOption AO This is an editorial change in the Temporal spec. See: https://github.com/tc39/proposal-temporal/commit/20a04ac --- .../Runtime/Temporal/AbstractOperations.cpp | 20 ++++++++----------- .../Runtime/Temporal/AbstractOperations.h | 2 +- .../LibJS/Runtime/Temporal/PlainDate.cpp | 2 +- .../LibJS/Runtime/Temporal/PlainDateTime.cpp | 2 +- .../LibJS/Runtime/Temporal/PlainYearMonth.cpp | 2 +- .../LibJS/Runtime/Temporal/ZonedDateTime.cpp | 2 +- 6 files changed, 13 insertions(+), 17 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp index 248998b5fa..0e5548d90f 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp @@ -738,35 +738,31 @@ StringView larger_of_two_temporal_units(StringView unit1, StringView unit2) } // 13.18 MergeLargestUnitOption ( options, largestUnit ), https://tc39.es/proposal-temporal/#sec-temporal-mergelargestunitoption -ThrowCompletionOr merge_largest_unit_option(GlobalObject& global_object, Object const* options, String largest_unit) +ThrowCompletionOr merge_largest_unit_option(GlobalObject& global_object, Object const& options, String largest_unit) { auto& vm = global_object.vm(); - // 1. If options is undefined, set options to OrdinaryObjectCreate(null). - if (options == nullptr) - options = Object::create(global_object, nullptr); - - // 2. Let merged be OrdinaryObjectCreate(%Object.prototype%). + // 1. Let merged be OrdinaryObjectCreate(%Object.prototype%). auto* merged = Object::create(global_object, global_object.object_prototype()); - // 3. Let keys be ? EnumerableOwnPropertyNames(options, key). - auto keys = TRY(options->enumerable_own_property_names(Object::PropertyKind::Key)); + // 2. Let keys be ? EnumerableOwnPropertyNames(options, key). + auto keys = TRY(options.enumerable_own_property_names(Object::PropertyKind::Key)); - // 4. For each element nextKey of keys, do + // 3. For each element nextKey of keys, do for (auto& key : keys) { auto next_key = MUST(PropertyKey::from_value(global_object, key)); // a. Let propValue be ? Get(options, nextKey). - auto prop_value = TRY(options->get(next_key)); + auto prop_value = TRY(options.get(next_key)); // b. Perform ! CreateDataPropertyOrThrow(merged, nextKey, propValue). MUST(merged->create_data_property_or_throw(next_key, prop_value)); } - // 5. Perform ! CreateDataPropertyOrThrow(merged, "largestUnit", largestUnit). + // 4. Perform ! CreateDataPropertyOrThrow(merged, "largestUnit", largestUnit). MUST(merged->create_data_property_or_throw(vm.names.largestUnit, js_string(vm, move(largest_unit)))); - // 6. Return merged. + // 5. Return merged. return merged; } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h index 866ab53d8e..68184441f3 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h @@ -144,7 +144,7 @@ ThrowCompletionOr to_seconds_string_precision(GlobalObje ThrowCompletionOr> get_temporal_unit(GlobalObject&, Object const& normalized_options, PropertyKey const&, UnitGroup, Variant> const& default_, Vector const& extra_values = {}); ThrowCompletionOr to_relative_temporal_object(GlobalObject&, Object const& options); StringView larger_of_two_temporal_units(StringView, StringView); -ThrowCompletionOr merge_largest_unit_option(GlobalObject&, Object const* options, String largest_unit); +ThrowCompletionOr merge_largest_unit_option(GlobalObject&, Object const& options, String largest_unit); Optional maximum_temporal_duration_rounding_increment(StringView unit); ThrowCompletionOr reject_object_with_calendar_or_time_zone(GlobalObject&, Object&); String format_seconds_string_part(u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, Variant const& precision); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp index 809cecd83d..8dc4227987 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp @@ -540,7 +540,7 @@ ThrowCompletionOr difference_temporal_plain_date(GlobalObject& global auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, {}, false)); // 13. Let untilOptions be ? MergeLargestUnitOption(options, largestUnit). - auto* until_options = TRY(merge_largest_unit_option(global_object, options, largest_unit.release_value())); + auto* until_options = TRY(merge_largest_unit_option(global_object, *options, largest_unit.release_value())); // 14. Let result be ? CalendarDateUntil(temporalDate.[[Calendar]], temporalDate, other, untilOptions). auto* duration = TRY(calendar_date_until(global_object, temporal_date.calendar(), &temporal_date, other, *until_options)); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp index c5a7fa15a7..e9650b344e 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp @@ -376,7 +376,7 @@ ThrowCompletionOr difference_iso_date_time(GlobalObject& global_ auto date_largest_unit = larger_of_two_temporal_units("day"sv, largest_unit); // 9. Let untilOptions be ? MergeLargestUnitOption(options, dateLargestUnit). - auto* until_options = TRY(merge_largest_unit_option(global_object, &options, date_largest_unit)); + auto* until_options = TRY(merge_largest_unit_option(global_object, options, date_largest_unit)); // 10. Let dateDifference be ? CalendarDateUntil(calendar, date1, date2, untilOptions). auto* date_difference = TRY(calendar_date_until(global_object, calendar, date1, date2, *until_options)); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp index 1817554c6c..d6b17d1197 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp @@ -323,7 +323,7 @@ ThrowCompletionOr difference_temporal_plain_year_month(GlobalObject& auto* this_date = TRY(calendar_date_from_fields(global_object, calendar, *this_fields)); // 22. Let untilOptions be ? MergeLargestUnitOption(options, largestUnit). - auto* until_options = TRY(merge_largest_unit_option(global_object, options, *largest_unit)); + auto* until_options = TRY(merge_largest_unit_option(global_object, *options, *largest_unit)); // 23. Let result be ? CalendarDateUntil(calendar, thisDate, otherDate, untilOptions). auto* duration = TRY(calendar_date_until(global_object, calendar, this_date, other_date, *until_options)); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp index 17f95155a7..c34db4782f 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp @@ -613,7 +613,7 @@ ThrowCompletionOr difference_temporal_zoned_date_time(GlobalObject& g } // 16. Let untilOptions be ? MergeLargestUnitOption(options, largestUnit). - auto* until_options = TRY(merge_largest_unit_option(global_object, options, *largest_unit)); + auto* until_options = TRY(merge_largest_unit_option(global_object, *options, *largest_unit)); // 17. Let difference be ? DifferenceZonedDateTime(zonedDateTime.[[Nanoseconds]], other.[[Nanoseconds]], zonedDateTime.[[TimeZone]], zonedDateTime.[[Calendar]], largestUnit, untilOptions). auto difference = TRY(difference_zoned_date_time(global_object, zoned_date_time.nanoseconds(), other->nanoseconds(), zoned_date_time.time_zone(), zoned_date_time.calendar(), *largest_unit, *until_options));