diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-10-20 08:24:54 -0400 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-10-21 00:26:45 +0100 |
commit | 860a37640b24e425da34c086d57fa938595d97f7 (patch) | |
tree | 55a4128a92fb93cfcdb55f7a4042a95411fe3def /Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp | |
parent | a3b3800cd45410e65f148b4e0877503bf653f8dd (diff) | |
download | serenity-860a37640b24e425da34c086d57fa938595d97f7.zip |
LibJS: Convert GetIterator AO to ThrowCompletionOr
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp b/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp index 5b823dcd68..513a0121e0 100644 --- a/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp @@ -12,24 +12,23 @@ namespace JS { // 7.4.1 GetIterator ( obj [ , hint [ , method ] ] ), https://tc39.es/ecma262/#sec-getiterator -Object* get_iterator(GlobalObject& global_object, Value value, IteratorHint hint, Value method) +ThrowCompletionOr<Object*> get_iterator(GlobalObject& global_object, Value value, IteratorHint hint, Value method) { auto& vm = global_object.vm(); if (method.is_empty()) { if (hint == IteratorHint::Async) TODO(); - auto object = TRY_OR_DISCARD(value.to_object(global_object)); - method = TRY_OR_DISCARD(object->get(*vm.well_known_symbol_iterator())); - } - if (!method.is_function()) { - vm.throw_exception<TypeError>(global_object, ErrorType::NotIterable, value.to_string_without_side_effects()); - return nullptr; - } - auto iterator = TRY_OR_DISCARD(vm.call(method.as_function(), value)); - if (!iterator.is_object()) { - vm.throw_exception<TypeError>(global_object, ErrorType::NotIterable, value.to_string_without_side_effects()); - return nullptr; + auto object = TRY(value.to_object(global_object)); + method = TRY(object->get(*vm.well_known_symbol_iterator())); } + + if (!method.is_function()) + return vm.throw_completion<TypeError>(global_object, ErrorType::NotIterable, value.to_string_without_side_effects()); + + auto iterator = TRY(vm.call(method.as_function(), value)); + if (!iterator.is_object()) + return vm.throw_completion<TypeError>(global_object, ErrorType::NotIterable, value.to_string_without_side_effects()); + return &iterator.as_object(); } @@ -170,9 +169,10 @@ void get_iterator_values(GlobalObject& global_object, Value value, Function<Iter { auto& vm = global_object.vm(); - auto iterator = get_iterator(global_object, value, IteratorHint::Sync, method); - if (!iterator) + auto iterator_or_error = get_iterator(global_object, value, IteratorHint::Sync, method); + if (iterator_or_error.is_error()) return; + auto* iterator = iterator_or_error.release_value(); while (true) { auto next_object = iterator_next(*iterator); |