Parcourir la source

LibJS: Handle undefined options in MergeLargestUnitOption

This is an editorial change in the Temporal spec.

See: https://github.com/tc39/proposal-temporal/commit/5e161a2
Linus Groh il y a 3 ans
Parent
commit
2844a2c448

+ 12 - 8
Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp

@@ -834,31 +834,35 @@ StringView larger_of_two_temporal_units(StringView unit1, StringView unit2)
 }
 
 // 13.23 MergeLargestUnitOption ( options, largestUnit ), https://tc39.es/proposal-temporal/#sec-temporal-mergelargestunitoption
-ThrowCompletionOr<Object*> merge_largest_unit_option(GlobalObject& global_object, Object& options, String largest_unit)
+ThrowCompletionOr<Object*> merge_largest_unit_option(GlobalObject& global_object, Object* options, String largest_unit)
 {
     auto& vm = global_object.vm();
 
-    // 1. Let merged be OrdinaryObjectCreate(%Object.prototype%).
+    // 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%).
     auto* merged = Object::create(global_object, global_object.object_prototype());
 
-    // 2. Let keys be ? EnumerableOwnPropertyNames(options, key).
-    auto keys = TRY(options.enumerable_own_property_names(Object::PropertyKind::Key));
+    // 3. Let keys be ? EnumerableOwnPropertyNames(options, key).
+    auto keys = TRY(options->enumerable_own_property_names(Object::PropertyKind::Key));
 
-    // 3. For each element nextKey of keys, do
+    // 4. 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));
     }
 
-    // 4. Perform ! CreateDataPropertyOrThrow(merged, "largestUnit", largestUnit).
+    // 5. Perform ! CreateDataPropertyOrThrow(merged, "largestUnit", largestUnit).
     MUST(merged->create_data_property_or_throw(vm.names.largestUnit, js_string(vm, move(largest_unit))));
 
-    // 5. Return merged.
+    // 6. Return merged.
     return merged;
 }
 

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

@@ -119,7 +119,7 @@ ThrowCompletionOr<String> to_temporal_duration_total_unit(GlobalObject& global_o
 ThrowCompletionOr<Value> to_relative_temporal_object(GlobalObject&, Object const& options);
 ThrowCompletionOr<void> validate_temporal_unit_range(GlobalObject&, StringView largest_unit, StringView smallest_unit);
 StringView larger_of_two_temporal_units(StringView, StringView);
-ThrowCompletionOr<Object*> merge_largest_unit_option(GlobalObject&, Object& options, String largest_unit);
+ThrowCompletionOr<Object*> merge_largest_unit_option(GlobalObject&, Object* 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);

+ 2 - 2
Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp

@@ -477,7 +477,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::until)
     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, move(*largest_unit)));
+    auto* until_options = TRY(merge_largest_unit_option(global_object, options, move(*largest_unit)));
 
     // 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));
@@ -536,7 +536,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::since)
     auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, {}, false));
 
     // 14. Let untilOptions be ? MergeLargestUnitOption(options, largestUnit).
-    auto* until_options = TRY(merge_largest_unit_option(global_object, *options, move(*largest_unit)));
+    auto* until_options = TRY(merge_largest_unit_option(global_object, options, move(*largest_unit)));
 
     // 15. 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 - 5
Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp

@@ -355,10 +355,6 @@ ThrowCompletionOr<DurationRecord> difference_iso_date_time(GlobalObject& global_
     // 1. If options is not present, set options to undefined.
     // 2. Assert: Type(options) is Object or Undefined.
 
-    // FIXME: `options` is being passed to MergeLargestUnitOption unconditionally, which expects it to not be undefined (spec issue: https://github.com/tc39/proposal-temporal/issues/2132).
-    if (!options)
-        options = Object::create(global_object, nullptr);
-
     // 3. Let timeDifference be ! DifferenceTime(h1, min1, s1, ms1, mus1, ns1, h2, min2, s2, ms2, mus2, ns2).
     auto time_difference = difference_time(hour1, minute1, second1, millisecond1, microsecond1, nanosecond1, hour2, minute2, second2, millisecond2, microsecond2, nanosecond2);
 
@@ -390,7 +386,7 @@ ThrowCompletionOr<DurationRecord> difference_iso_date_time(GlobalObject& global_
     auto date_largest_unit = larger_of_two_temporal_units("day"sv, largest_unit);
 
     // 11. 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));
 
     // 12. Let dateDifference be ? CalendarDateUntil(calendar, date1, date2, untilOptions).
     auto* date_difference = TRY(calendar_date_until(global_object, calendar, date1, date2, *until_options));

+ 2 - 2
Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp

@@ -461,7 +461,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::until)
     auto* this_date = TRY(date_from_fields(global_object, calendar, *this_fields, options));
 
     // 20. 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));
 
     // 21. Let result be ? CalendarDateUntil(calendar, thisDate, otherDate, untilOptions).
     auto* result = TRY(calendar_date_until(global_object, calendar, this_date, other_date, *until_options));
@@ -544,7 +544,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::since)
     auto* this_date = TRY(date_from_fields(global_object, calendar, *this_fields, options));
 
     // 21. 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));
 
     // 22. Let result be ? CalendarDateUntil(calendar, thisDate, otherDate, untilOptions).
     auto* result = TRY(calendar_date_until(global_object, calendar, this_date, other_date, *until_options));

+ 2 - 2
Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp

@@ -1003,7 +1003,7 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::until)
     }
 
     // 15. 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));
 
     // 16. 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));
@@ -1080,7 +1080,7 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::since)
     }
 
     // 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));