From f2b5ddd167d891abacb2fe1b871fc98c9eea4f7b Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 15 Sep 2021 23:11:02 +0100 Subject: LibJS: Convert iterable_to_list_of_type() to ThrowCompletionOr --- .../LibJS/Runtime/Temporal/AbstractOperations.cpp | 21 +++++++++++---------- .../LibJS/Runtime/Temporal/AbstractOperations.h | 2 +- .../Libraries/LibJS/Runtime/Temporal/Calendar.cpp | 4 +--- 3 files changed, 13 insertions(+), 14 deletions(-) (limited to 'Userland/Libraries/LibJS') diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp index bba238bc0d..95017e567a 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -34,15 +35,15 @@ static Optional to_option_type(Value value) } // 13.1 IterableToListOfType ( items, elementTypes ), https://tc39.es/proposal-temporal/#sec-iterabletolistoftype -MarkedValueList iterable_to_list_of_type(GlobalObject& global_object, Value items, Vector const& element_types) +ThrowCompletionOr iterable_to_list_of_type(GlobalObject& global_object, Value items, Vector const& element_types) { auto& vm = global_object.vm(); auto& heap = global_object.heap(); // 1. Let iteratorRecord be ? GetIterator(items, sync). auto iterator_record = get_iterator(global_object, items, IteratorHint::Sync); - if (vm.exception()) - return MarkedValueList { heap }; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // 2. Let values be a new empty List. MarkedValueList values(heap); @@ -53,23 +54,23 @@ MarkedValueList iterable_to_list_of_type(GlobalObject& global_object, Value item while (next) { // a. Set next to ? IteratorStep(iteratorRecord). auto* iterator_result = iterator_step(global_object, *iterator_record); - if (vm.exception()) - return MarkedValueList { heap }; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); next = iterator_result; // b. If next is not false, then if (next) { // i. Let nextValue be ? IteratorValue(next). auto next_value = iterator_value(global_object, *iterator_result); - if (vm.exception()) - return MarkedValueList { heap }; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // ii. If Type(nextValue) is not an element of elementTypes, then if (auto type = to_option_type(next_value); !type.has_value() || !element_types.contains_slow(*type)) { // 1. Let completion be ThrowCompletion(a newly created TypeError object). - vm.throw_exception(global_object, ErrorType::FixmeAddAnErrorString); + auto completion = vm.throw_completion(global_object, ErrorType::FixmeAddAnErrorString); // 2. Return ? IteratorClose(iteratorRecord, completion). iterator_close(*iterator_record); - return MarkedValueList { heap }; + return completion; } // iii. Append nextValue to the end of the List values. values.append(next_value); @@ -77,7 +78,7 @@ MarkedValueList iterable_to_list_of_type(GlobalObject& global_object, Value item } // 5. Return values. - return values; + return { move(values) }; } // 13.2 GetOptionsObject ( options ), https://tc39.es/proposal-temporal/#sec-getoptionsobject diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h index 3e65418028..62fb5880a6 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h @@ -84,7 +84,7 @@ struct SecondsStringPrecision { u32 increment; }; -MarkedValueList iterable_to_list_of_type(GlobalObject&, Value items, Vector const& element_types); +ThrowCompletionOr iterable_to_list_of_type(GlobalObject&, Value items, Vector const& element_types); Object* get_options_object(GlobalObject&, Value options); Value get_option(GlobalObject&, Object const& options, PropertyName const& property, Vector const& types, Vector const& values, Value fallback); template diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp index b0ff02bac3..1a343543c9 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp @@ -110,9 +110,7 @@ Vector calendar_fields(GlobalObject& global_object, Object& calendar, Ve } // 4. Return ? IterableToListOfType(fieldsArray, « String »). - auto list = iterable_to_list_of_type(global_object, fields_array, { OptionType::String }); - if (vm.exception()) - return {}; + auto list = TRY_OR_DISCARD(iterable_to_list_of_type(global_object, fields_array, { OptionType::String })); Vector result; for (auto& value : list) -- cgit v1.2.3