瀏覽代碼

LibJS: Convert IteratorStep AO to ThrowCompletionOr

Timothy Flynn 3 年之前
父節點
當前提交
8be1caa05d

+ 1 - 4
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;

+ 1 - 3
Userland/Libraries/LibJS/Runtime/Intl/ListFormat.cpp

@@ -287,9 +287,7 @@ ThrowCompletionOr<Vector<String>> 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) {

+ 4 - 4
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<Object*> 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;

+ 1 - 1
Userland/Libraries/LibJS/Runtime/IteratorOperations.h

@@ -21,7 +21,7 @@ enum class IteratorHint {
 
 ThrowCompletionOr<Object*> get_iterator(GlobalObject&, Value value, IteratorHint hint = IteratorHint::Sync, Value method = {});
 ThrowCompletionOr<Object*> iterator_next(Object& iterator, Value value = {});
-Object* iterator_step(GlobalObject&, Object& iterator);
+ThrowCompletionOr<Object*> iterator_step(GlobalObject&, Object& iterator);
 bool iterator_complete(GlobalObject&, Object& iterator_result);
 Value iterator_value(GlobalObject&, Object& iterator_result);
 void iterator_close(Object& iterator);

+ 3 - 2
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);

+ 1 - 3
Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp

@@ -51,9 +51,7 @@ ThrowCompletionOr<MarkedValueList> 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

+ 1 - 3
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)