From 3025f77991d0e6f84ce59b2d099bd0df24dd7518 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 15 Jun 2022 00:05:05 +0100 Subject: [PATCH] LibJS: Add an explicit operation for merging calendar field names This is an editorial change in the Temporal spec. See: https://github.com/tc39/proposal-temporal/commit/2bd7977 --- .../LibJS/Runtime/Temporal/Calendar.cpp | 28 +++++++++++++++++++ .../LibJS/Runtime/Temporal/Calendar.h | 1 + .../Temporal/PlainMonthDayPrototype.cpp | 12 ++------ .../Temporal/PlainYearMonthPrototype.cpp | 12 ++------ 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp index bce28d0f95..d0c5738569 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp @@ -1004,4 +1004,32 @@ ThrowCompletionOr default_merge_calendar_fields(GlobalObject& global_ob return merged; } +// 12.2.41 CalendarMergeFieldNames ( receiverFieldNames, inputFieldNames ), https://tc39.es/proposal-temporal/#sec-temporal-calendarmergefieldnames +Vector calendar_merge_field_names(Vector const& receiver_field_names, Vector const& input_field_names) +{ + // 1. Let merged be a new empty List. + Vector merged; + + // 2. For each element name of receiverFieldNames, do + for (auto const& field_name : receiver_field_names) { + // a. If merged does not contain name, then + if (!merged.contains_slow(field_name)) { + // i. Append name to merged. + merged.append(field_name); + } + } + + // 3. For each element name of inputFieldNames, do + for (auto const& field_name : input_field_names) { + // a. If merged does not contain name, then + if (!merged.contains_slow(field_name)) { + // i. Append name to merged. + merged.append(field_name); + } + } + + // 4. Return merged. + return merged; +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h index bada3ded72..ae19d9e0e5 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h @@ -74,5 +74,6 @@ u8 iso_month(Object& temporal_object); String iso_month_code(Object& temporal_object); u8 iso_day(Object& temporal_object); ThrowCompletionOr default_merge_calendar_fields(GlobalObject&, Object const& fields, Object const& additional_fields); +Vector calendar_merge_field_names(Vector const& receiver_field_names, Vector const& input_field_names); } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp index fc4a0a518e..5a7cbca33c 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp @@ -231,16 +231,8 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_plain_date) // 9. Let mergedFields be ? CalendarMergeFields(calendar, fields, inputFields). auto* merged_fields = TRY(calendar_merge_fields(global_object, calendar, *fields, *input_fields)); - // 10. Let mergedFieldNames be the List containing all the elements of receiverFieldNames followed by all the elements of inputFieldNames, with duplicate elements removed. - Vector merged_field_names; - for (auto& field_name : receiver_field_names) { - if (!merged_field_names.contains_slow(field_name)) - merged_field_names.append(move(field_name)); - } - for (auto& field_name : input_field_names) { - if (!merged_field_names.contains_slow(field_name)) - merged_field_names.append(move(field_name)); - } + // 10. Let mergedFieldNames be CalendarMergeFieldNames(receiverFieldNames, inputFieldNames). + auto merged_field_names = calendar_merge_field_names(receiver_field_names, input_field_names); // 11. Set mergedFields to ? PrepareTemporalFields(mergedFields, mergedFieldNames, «»). merged_fields = TRY(prepare_temporal_fields(global_object, *merged_fields, merged_field_names, Vector {})); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp index 6f440ecc10..0dfadf15f3 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp @@ -398,16 +398,8 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_plain_date) // 9. Let mergedFields be ? CalendarMergeFields(calendar, fields, inputFields). auto* merged_fields = TRY(calendar_merge_fields(global_object, calendar, *fields, *input_fields)); - // 10. Let mergedFieldNames be the List containing all the elements of receiverFieldNames followed by all the elements of inputFieldNames, with duplicate elements removed. - Vector merged_field_names; - for (auto& field_name : receiver_field_names) { - if (!merged_field_names.contains_slow(field_name)) - merged_field_names.append(move(field_name)); - } - for (auto& field_name : input_field_names) { - if (!merged_field_names.contains_slow(field_name)) - merged_field_names.append(move(field_name)); - } + // 10. Let mergedFieldNames be CalendarMergeFieldNames(receiverFieldNames, inputFieldNames). + auto merged_field_names = calendar_merge_field_names(receiver_field_names, input_field_names); // 11. Set mergedFields to ? PrepareTemporalFields(mergedFields, mergedFieldNames, «»). merged_fields = TRY(prepare_temporal_fields(global_object, *merged_fields, merged_field_names, Vector {}));