summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMoustafa Raafat <MoustafaRaafat2@gmail.com>2022-10-19 22:41:41 +0100
committerLinus Groh <mail@linusgroh.de>2022-10-20 00:53:44 +0200
commit8f964604f0c773338ceb3f4013f5006e48c66e1a (patch)
treea0002bc94489d5dd24b30dc0f338b023bdc7c7d6
parentd38aeddd770dd70ca180a5aa1c395a45ee62d82c (diff)
downloadserenity-8f964604f0c773338ceb3f4013f5006e48c66e1a.zip
LibJS: Refactor CalendarFields for better linearity
This is a normative change in the Temporal spec. See: https://github.com/tc39/proposal-temporal/commit/9b139a1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp19
1 files changed, 9 insertions, 10 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp
index 0cd18e80fa..8730316d8b 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp
@@ -106,18 +106,17 @@ ThrowCompletionOr<Vector<String>> calendar_fields(VM& vm, Object& calendar, Vect
// 1. Let fields be ? GetMethod(calendar, "fields").
auto fields = TRY(Value(&calendar).get_method(vm, vm.names.fields));
- // 2. Let fieldsArray be CreateArrayFromList(fieldNames).
- auto field_names_values = MarkedVector<Value> { vm.heap() };
- for (auto& field_name : field_names)
- field_names_values.append(js_string(vm, field_name));
- Value fields_array = Array::create_from(realm, field_names_values);
-
- // 3. If fields is not undefined, then
- if (fields) {
- // a. Set fieldsArray to ? Call(fields, calendar, « fieldsArray »).
- fields_array = TRY(call(vm, *fields, &calendar, fields_array));
+ // 2. If fields is undefined, return fieldNames.
+ if (!fields) {
+ Vector<String> result;
+ for (auto& value : field_names)
+ result.append(value);
+ return result;
}
+ // 3. Let fieldsArray be ? Call(fields, calendar, « CreateArrayFromList(fieldNames) »).
+ auto fields_array = TRY(call(vm, *fields, &calendar, Array::create_from<StringView>(realm, field_names, [&](auto value) { return js_string(vm, value); })));
+
// 4. Return ? IterableToListOfType(fieldsArray, « String »).
auto list = TRY(iterable_to_list_of_type(vm, fields_array, { OptionType::String }));