From 8be1caa05d57c2571e9215875b0e5e6dd7b7845a Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 20 Oct 2021 08:56:01 -0400 Subject: [PATCH] LibJS: Convert IteratorStep AO to ThrowCompletionOr --- Userland/Libraries/LibJS/Runtime/ArrayConstructor.cpp | 5 +---- Userland/Libraries/LibJS/Runtime/Intl/ListFormat.cpp | 4 +--- Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp | 8 ++++---- Userland/Libraries/LibJS/Runtime/IteratorOperations.h | 2 +- Userland/Libraries/LibJS/Runtime/PromiseConstructor.cpp | 5 +++-- .../LibJS/Runtime/Temporal/AbstractOperations.cpp | 4 +--- .../LibJS/Runtime/Temporal/CalendarPrototype.cpp | 4 +--- 7 files changed, 12 insertions(+), 20 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/ArrayConstructor.cpp b/Userland/Libraries/LibJS/Runtime/ArrayConstructor.cpp index 8cc1b3e7a8..5958903c7f 100644 --- a/Userland/Libraries/LibJS/Runtime/ArrayConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/ArrayConstructor.cpp @@ -128,10 +128,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(ArrayConstructor::from) return {}; } - auto next = iterator_step(global_object, *iterator); - if (vm.exception()) - return {}; - + auto* next = TRY_OR_DISCARD(iterator_step(global_object, *iterator)); if (!next) { TRY_OR_DISCARD(array_object.set(vm.names.length, Value(k), Object::ShouldThrowExceptions::Yes)); return array; diff --git a/Userland/Libraries/LibJS/Runtime/Intl/ListFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/ListFormat.cpp index d695056bb4..a450f58c2a 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/ListFormat.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/ListFormat.cpp @@ -287,9 +287,7 @@ ThrowCompletionOr> string_list_from_iterable(GlobalObject& global // 5. Repeat, while next is not false, do { // a. Set next to ? IteratorStep(iteratorRecord). - next = iterator_step(global_object, *iterator_record); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + next = TRY(iterator_step(global_object, *iterator_record)); // b. If next is not false, then if (next != nullptr) { diff --git a/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp b/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp index 2dfdeac91e..3373639505 100644 --- a/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp @@ -75,15 +75,15 @@ Value iterator_value(GlobalObject& global_object, Object& iterator_result) } // 7.4.5 IteratorStep ( iteratorRecord ), https://tc39.es/ecma262/#sec-iteratorstep -Object* iterator_step(GlobalObject& global_object, Object& iterator) +ThrowCompletionOr iterator_step(GlobalObject& global_object, Object& iterator) { auto& vm = global_object.vm(); - auto result = TRY_OR_DISCARD(iterator_next(iterator)); + auto* result = TRY(iterator_next(iterator)); auto done = iterator_complete(global_object, *result); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); if (done) return nullptr; diff --git a/Userland/Libraries/LibJS/Runtime/IteratorOperations.h b/Userland/Libraries/LibJS/Runtime/IteratorOperations.h index d53ef65339..17fe2a202d 100644 --- a/Userland/Libraries/LibJS/Runtime/IteratorOperations.h +++ b/Userland/Libraries/LibJS/Runtime/IteratorOperations.h @@ -21,7 +21,7 @@ enum class IteratorHint { ThrowCompletionOr get_iterator(GlobalObject&, Value value, IteratorHint hint = IteratorHint::Sync, Value method = {}); ThrowCompletionOr iterator_next(Object& iterator, Value value = {}); -Object* iterator_step(GlobalObject&, Object& iterator); +ThrowCompletionOr iterator_step(GlobalObject&, Object& iterator); bool iterator_complete(GlobalObject&, Object& iterator_result); Value iterator_value(GlobalObject&, Object& iterator_result); void iterator_close(Object& iterator); diff --git a/Userland/Libraries/LibJS/Runtime/PromiseConstructor.cpp b/Userland/Libraries/LibJS/Runtime/PromiseConstructor.cpp index e8b8da0669..e31f398469 100644 --- a/Userland/Libraries/LibJS/Runtime/PromiseConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/PromiseConstructor.cpp @@ -100,11 +100,12 @@ static Value perform_promise_common(GlobalObject& global_object, Object& iterato size_t index = 0; while (true) { - auto* next = iterator_step(global_object, iterator_record); - if (vm.exception()) { + auto next_or_error = iterator_step(global_object, iterator_record); + if (next_or_error.is_throw_completion()) { set_iterator_record_complete(global_object, iterator_record); return {}; } + auto* next = next_or_error.release_value(); if (!next) { set_iterator_record_complete(global_object, iterator_record); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp index 66eca3d7cf..49df0f360d 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp @@ -51,9 +51,7 @@ ThrowCompletionOr iterable_to_list_of_type(GlobalObject& global // 4. Repeat, while next is not false, while (next) { // a. Set next to ? IteratorStep(iteratorRecord). - auto* iterator_result = iterator_step(global_object, *iterator_record); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + auto* iterator_result = TRY(iterator_step(global_object, *iterator_record)); next = iterator_result; // b. If next is not false, then diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp index f6382492e8..be82579e52 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp @@ -501,9 +501,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(CalendarPrototype::fields) // 7. Repeat, while next is not false, while (true) { // a. Set next to ? IteratorStep(iteratorRecord). - auto* next = iterator_step(global_object, *iterator_record); - if (vm.exception()) - return {}; + auto* next = TRY_OR_DISCARD(iterator_step(global_object, *iterator_record)); // b. If next is not false, then if (!next)