mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 23:17:45 +00:00
LibJS: Convert ListFormat AOs to ThrowCompletionOr
This commit is contained in:
parent
e65aeee67d
commit
e00ca10283
3 changed files with 13 additions and 17 deletions
|
@ -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
|
||||
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();
|
||||
|
||||
// 1. If iterable is undefined, then
|
||||
if (iterable.is_undefined()) {
|
||||
// a. Return a new empty List.
|
||||
return {};
|
||||
return Vector<String> {};
|
||||
}
|
||||
|
||||
// 2. Let iteratorRecord be ? GetIterator(iterable).
|
||||
auto* iterator_record = get_iterator(global_object, iterable);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
|
||||
// 3. Let list be a new empty List.
|
||||
Vector<String> list;
|
||||
|
@ -290,24 +290,24 @@ Vector<String> string_list_from_iterable(GlobalObject& global_object, Value iter
|
|||
do {
|
||||
// a. Set next to ? IteratorStep(iteratorRecord).
|
||||
next = iterator_step(global_object, *iterator_record);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
|
||||
// b. If next is not false, then
|
||||
if (next != nullptr) {
|
||||
// i. Let nextValue be ? IteratorValue(next).
|
||||
auto next_value = iterator_value(global_object, *next);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
|
||||
// ii. If Type(nextValue) is not String, then
|
||||
if (!next_value.is_string()) {
|
||||
// 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).
|
||||
iterator_close(*iterator_record);
|
||||
return {};
|
||||
return completion;
|
||||
}
|
||||
|
||||
// iii. Append nextValue to the end of the List list.
|
||||
|
|
|
@ -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);
|
||||
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);
|
||||
Vector<String> string_list_from_iterable(GlobalObject& global_object, Value iterable);
|
||||
ThrowCompletionOr<Vector<String>> string_list_from_iterable(GlobalObject& global_object, Value iterable);
|
||||
|
||||
}
|
||||
|
|
|
@ -45,9 +45,7 @@ JS_DEFINE_NATIVE_FUNCTION(ListFormatPrototype::format)
|
|||
return {};
|
||||
|
||||
// 3. Let stringList be ? StringListFromIterable(list).
|
||||
auto string_list = string_list_from_iterable(global_object, list);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto string_list = TRY_OR_DISCARD(string_list_from_iterable(global_object, list));
|
||||
|
||||
// 4. Return FormatList(lf, stringList).
|
||||
auto formatted = format_list(*list_format, string_list);
|
||||
|
@ -66,9 +64,7 @@ JS_DEFINE_NATIVE_FUNCTION(ListFormatPrototype::format_to_parts)
|
|||
return {};
|
||||
|
||||
// 3. Let stringList be ? StringListFromIterable(list).
|
||||
auto string_list = string_list_from_iterable(global_object, list);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto string_list = TRY_OR_DISCARD(string_list_from_iterable(global_object, list));
|
||||
|
||||
// 4. Return FormatListToParts(lf, stringList).
|
||||
return format_list_to_parts(global_object, *list_format, string_list);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue