Browse Source

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
Linus Groh 3 years ago
parent
commit
52a4ee563d

+ 8 - 12
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<Object*> merge_largest_unit_option(GlobalObject& global_object, Object const* options, String largest_unit)
+ThrowCompletionOr<Object*> 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;
 }
 

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h

@@ -144,7 +144,7 @@ ThrowCompletionOr<SecondsStringPrecision> to_seconds_string_precision(GlobalObje
 ThrowCompletionOr<Optional<String>> get_temporal_unit(GlobalObject&, Object const& normalized_options, PropertyKey const&, UnitGroup, Variant<TemporalUnitRequired, Optional<StringView>> const& default_, Vector<StringView> const& extra_values = {});
 ThrowCompletionOr<Value> to_relative_temporal_object(GlobalObject&, Object const& options);
 StringView larger_of_two_temporal_units(StringView, StringView);
-ThrowCompletionOr<Object*> merge_largest_unit_option(GlobalObject&, Object const* options, String largest_unit);
+ThrowCompletionOr<Object*> merge_largest_unit_option(GlobalObject&, Object const& options, String largest_unit);
 Optional<u16> maximum_temporal_duration_rounding_increment(StringView unit);
 ThrowCompletionOr<void> reject_object_with_calendar_or_time_zone(GlobalObject&, Object&);
 String format_seconds_string_part(u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, Variant<StringView, u8> const& precision);

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp

@@ -540,7 +540,7 @@ ThrowCompletionOr<Duration*> 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));

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp

@@ -376,7 +376,7 @@ ThrowCompletionOr<DurationRecord> 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));

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp

@@ -323,7 +323,7 @@ ThrowCompletionOr<Duration*> 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));

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp

@@ -613,7 +613,7 @@ ThrowCompletionOr<Duration*> 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));