1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 13:47:35 +00:00

LibJS: Convert ListFormat AOs to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-09-18 19:45:32 +03:00
parent e65aeee67d
commit e00ca10283
3 changed files with 13 additions and 17 deletions

View file

@ -265,20 +265,20 @@ Array* format_list_to_parts(GlobalObject& global_object, ListFormat const& list_
} }
// 13.1.5 StringListFromIterable ( iterable ), https://tc39.es/ecma402/#sec-createstringlistfromiterable // 13.1.5 StringListFromIterable ( iterable ), https://tc39.es/ecma402/#sec-createstringlistfromiterable
Vector<String> string_list_from_iterable(GlobalObject& global_object, Value iterable) ThrowCompletionOr<Vector<String>> string_list_from_iterable(GlobalObject& global_object, Value iterable)
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
// 1. If iterable is undefined, then // 1. If iterable is undefined, then
if (iterable.is_undefined()) { if (iterable.is_undefined()) {
// a. Return a new empty List. // a. Return a new empty List.
return {}; return Vector<String> {};
} }
// 2. Let iteratorRecord be ? GetIterator(iterable). // 2. Let iteratorRecord be ? GetIterator(iterable).
auto* iterator_record = get_iterator(global_object, iterable); auto* iterator_record = get_iterator(global_object, iterable);
if (vm.exception()) if (auto* exception = vm.exception())
return {}; return throw_completion(exception->value());
// 3. Let list be a new empty List. // 3. Let list be a new empty List.
Vector<String> list; Vector<String> list;
@ -290,24 +290,24 @@ Vector<String> string_list_from_iterable(GlobalObject& global_object, Value iter
do { do {
// a. Set next to ? IteratorStep(iteratorRecord). // a. Set next to ? IteratorStep(iteratorRecord).
next = iterator_step(global_object, *iterator_record); next = iterator_step(global_object, *iterator_record);
if (vm.exception()) if (auto* exception = vm.exception())
return {}; return throw_completion(exception->value());
// b. If next is not false, then // b. If next is not false, then
if (next != nullptr) { if (next != nullptr) {
// i. Let nextValue be ? IteratorValue(next). // i. Let nextValue be ? IteratorValue(next).
auto next_value = iterator_value(global_object, *next); auto next_value = iterator_value(global_object, *next);
if (vm.exception()) if (auto* exception = vm.exception())
return {}; return throw_completion(exception->value());
// ii. If Type(nextValue) is not String, then // ii. If Type(nextValue) is not String, then
if (!next_value.is_string()) { if (!next_value.is_string()) {
// 1. Let error be ThrowCompletion(a newly created TypeError object). // 1. Let error be ThrowCompletion(a newly created TypeError object).
vm.throw_exception<TypeError>(global_object, ErrorType::NotAString, next_value); auto completion = vm.throw_completion<TypeError>(global_object, ErrorType::NotAString, next_value);
// 2. Return ? IteratorClose(iteratorRecord, error). // 2. Return ? IteratorClose(iteratorRecord, error).
iterator_close(*iterator_record); iterator_close(*iterator_record);
return {}; return completion;
} }
// iii. Append nextValue to the end of the List list. // iii. Append nextValue to the end of the List list.

View file

@ -60,6 +60,6 @@ Vector<PatternPartition> deconstruct_pattern(StringView pattern, Placeables plac
Vector<PatternPartition> create_parts_from_list(ListFormat const& list_format, Vector<String> const& list); Vector<PatternPartition> create_parts_from_list(ListFormat const& list_format, Vector<String> const& list);
String format_list(ListFormat const& list_format, Vector<String> const& list); String format_list(ListFormat const& list_format, Vector<String> const& list);
Array* format_list_to_parts(GlobalObject& global_object, ListFormat const& list_format, Vector<String> const& list); Array* format_list_to_parts(GlobalObject& global_object, ListFormat const& list_format, Vector<String> const& list);
Vector<String> string_list_from_iterable(GlobalObject& global_object, Value iterable); ThrowCompletionOr<Vector<String>> string_list_from_iterable(GlobalObject& global_object, Value iterable);
} }

View file

@ -45,9 +45,7 @@ JS_DEFINE_NATIVE_FUNCTION(ListFormatPrototype::format)
return {}; return {};
// 3. Let stringList be ? StringListFromIterable(list). // 3. Let stringList be ? StringListFromIterable(list).
auto string_list = string_list_from_iterable(global_object, list); auto string_list = TRY_OR_DISCARD(string_list_from_iterable(global_object, list));
if (vm.exception())
return {};
// 4. Return FormatList(lf, stringList). // 4. Return FormatList(lf, stringList).
auto formatted = format_list(*list_format, string_list); auto formatted = format_list(*list_format, string_list);
@ -66,9 +64,7 @@ JS_DEFINE_NATIVE_FUNCTION(ListFormatPrototype::format_to_parts)
return {}; return {};
// 3. Let stringList be ? StringListFromIterable(list). // 3. Let stringList be ? StringListFromIterable(list).
auto string_list = string_list_from_iterable(global_object, list); auto string_list = TRY_OR_DISCARD(string_list_from_iterable(global_object, list));
if (vm.exception())
return {};
// 4. Return FormatListToParts(lf, stringList). // 4. Return FormatListToParts(lf, stringList).
return format_list_to_parts(global_object, *list_format, string_list); return format_list_to_parts(global_object, *list_format, string_list);