1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 10:27:34 +00:00

LibJS: Convert iterable_to_list_of_type() to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-09-15 23:11:02 +01:00
parent 683e31e1ff
commit f2b5ddd167
3 changed files with 13 additions and 14 deletions

View file

@ -9,6 +9,7 @@
#include <AK/DateTimeLexer.h> #include <AK/DateTimeLexer.h>
#include <AK/TypeCasts.h> #include <AK/TypeCasts.h>
#include <AK/Variant.h> #include <AK/Variant.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/IteratorOperations.h> #include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/PropertyName.h> #include <LibJS/Runtime/PropertyName.h>
#include <LibJS/Runtime/Temporal/AbstractOperations.h> #include <LibJS/Runtime/Temporal/AbstractOperations.h>
@ -34,15 +35,15 @@ static Optional<OptionType> to_option_type(Value value)
} }
// 13.1 IterableToListOfType ( items, elementTypes ), https://tc39.es/proposal-temporal/#sec-iterabletolistoftype // 13.1 IterableToListOfType ( items, elementTypes ), https://tc39.es/proposal-temporal/#sec-iterabletolistoftype
MarkedValueList iterable_to_list_of_type(GlobalObject& global_object, Value items, Vector<OptionType> const& element_types) ThrowCompletionOr<MarkedValueList> iterable_to_list_of_type(GlobalObject& global_object, Value items, Vector<OptionType> const& element_types)
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
auto& heap = global_object.heap(); auto& heap = global_object.heap();
// 1. Let iteratorRecord be ? GetIterator(items, sync). // 1. Let iteratorRecord be ? GetIterator(items, sync).
auto iterator_record = get_iterator(global_object, items, IteratorHint::Sync); auto iterator_record = get_iterator(global_object, items, IteratorHint::Sync);
if (vm.exception()) if (auto* exception = vm.exception())
return MarkedValueList { heap }; return throw_completion(exception->value());
// 2. Let values be a new empty List. // 2. Let values be a new empty List.
MarkedValueList values(heap); MarkedValueList values(heap);
@ -53,23 +54,23 @@ MarkedValueList iterable_to_list_of_type(GlobalObject& global_object, Value item
while (next) { while (next) {
// a. Set next to ? IteratorStep(iteratorRecord). // a. Set next to ? IteratorStep(iteratorRecord).
auto* iterator_result = iterator_step(global_object, *iterator_record); auto* iterator_result = iterator_step(global_object, *iterator_record);
if (vm.exception()) if (auto* exception = vm.exception())
return MarkedValueList { heap }; return throw_completion(exception->value());
next = iterator_result; next = iterator_result;
// b. If next is not false, then // b. If next is not false, then
if (next) { if (next) {
// i. Let nextValue be ? IteratorValue(next). // i. Let nextValue be ? IteratorValue(next).
auto next_value = iterator_value(global_object, *iterator_result); auto next_value = iterator_value(global_object, *iterator_result);
if (vm.exception()) if (auto* exception = vm.exception())
return MarkedValueList { heap }; return throw_completion(exception->value());
// ii. If Type(nextValue) is not an element of elementTypes, then // 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)) { 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). // 1. Let completion be ThrowCompletion(a newly created TypeError object).
vm.throw_exception<TypeError>(global_object, ErrorType::FixmeAddAnErrorString); auto completion = vm.throw_completion<TypeError>(global_object, ErrorType::FixmeAddAnErrorString);
// 2. Return ? IteratorClose(iteratorRecord, completion). // 2. Return ? IteratorClose(iteratorRecord, completion).
iterator_close(*iterator_record); iterator_close(*iterator_record);
return MarkedValueList { heap }; return completion;
} }
// iii. Append nextValue to the end of the List values. // iii. Append nextValue to the end of the List values.
values.append(next_value); values.append(next_value);
@ -77,7 +78,7 @@ MarkedValueList iterable_to_list_of_type(GlobalObject& global_object, Value item
} }
// 5. Return values. // 5. Return values.
return values; return { move(values) };
} }
// 13.2 GetOptionsObject ( options ), https://tc39.es/proposal-temporal/#sec-getoptionsobject // 13.2 GetOptionsObject ( options ), https://tc39.es/proposal-temporal/#sec-getoptionsobject

View file

@ -84,7 +84,7 @@ struct SecondsStringPrecision {
u32 increment; u32 increment;
}; };
MarkedValueList iterable_to_list_of_type(GlobalObject&, Value items, Vector<OptionType> const& element_types); ThrowCompletionOr<MarkedValueList> iterable_to_list_of_type(GlobalObject&, Value items, Vector<OptionType> const& element_types);
Object* get_options_object(GlobalObject&, Value options); Object* get_options_object(GlobalObject&, Value options);
Value get_option(GlobalObject&, Object const& options, PropertyName const& property, Vector<OptionType> const& types, Vector<StringView> const& values, Value fallback); Value get_option(GlobalObject&, Object const& options, PropertyName const& property, Vector<OptionType> const& types, Vector<StringView> const& values, Value fallback);
template<typename NumberType> template<typename NumberType>

View file

@ -110,9 +110,7 @@ Vector<String> calendar_fields(GlobalObject& global_object, Object& calendar, Ve
} }
// 4. Return ? IterableToListOfType(fieldsArray, « String »). // 4. Return ? IterableToListOfType(fieldsArray, « String »).
auto list = iterable_to_list_of_type(global_object, fields_array, { OptionType::String }); auto list = TRY_OR_DISCARD(iterable_to_list_of_type(global_object, fields_array, { OptionType::String }));
if (vm.exception())
return {};
Vector<String> result; Vector<String> result;
for (auto& value : list) for (auto& value : list)