diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-07-04 01:33:37 +0300 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-07-04 00:51:43 +0100 |
commit | 5ee1ae37b2e2eafb88114160396be7cd61e7343b (patch) | |
tree | 52777cf125799359f45ebc9638effcddf662145a /Userland | |
parent | ba4d367deae1543ffd03c538dba66bb367fdbc38 (diff) | |
download | serenity-5ee1ae37b2e2eafb88114160396be7cd61e7343b.zip |
LibJS: Add the IteratorStep abstract iterator operation
As well as add 2 missing exception checks in the IteratorComplete and
IteratorValue abstract iterator operations.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp | 31 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/IteratorOperations.h | 1 |
2 files changed, 30 insertions, 2 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp b/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp index 0e934e06c0..fbd57f2bd5 100644 --- a/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp @@ -73,13 +73,40 @@ Object* iterator_next(Object& iterator, Value value) // 7.4.3 IteratorComplete ( iterResult ), https://tc39.es/ecma262/#sec-iteratorcomplete bool iterator_complete(GlobalObject& global_object, Object& iterator_result) { - return iterator_result.get(global_object.vm().names.done).value_or(Value(false)).to_boolean(); + auto& vm = global_object.vm(); + auto done = iterator_result.get(vm.names.done).value_or(js_undefined()); + if (vm.exception()) + return {}; + return done.to_boolean(); } // 7.4.4 IteratorValue ( iterResult ), https://tc39.es/ecma262/#sec-iteratorvalue Value iterator_value(GlobalObject& global_object, Object& iterator_result) { - return iterator_result.get(global_object.vm().names.value).value_or(js_undefined()); + auto& vm = global_object.vm(); + auto value = iterator_result.get(vm.names.value).value_or(js_undefined()); + if (vm.exception()) + return {}; + return value; +} + +// 7.4.5 IteratorStep ( iteratorRecord ), https://tc39.es/ecma262/#sec-iteratorstep +Object* iterator_step(GlobalObject& global_object, Object& iterator) +{ + auto& vm = global_object.vm(); + + auto result = iterator_next(iterator); + if (vm.exception()) + return {}; + + auto done = iterator_complete(global_object, *result); + if (vm.exception()) + return {}; + + if (done) + return nullptr; + + return result; } // 7.4.6 IteratorClose ( iteratorRecord, completion ), https://tc39.es/ecma262/#sec-iteratorclose diff --git a/Userland/Libraries/LibJS/Runtime/IteratorOperations.h b/Userland/Libraries/LibJS/Runtime/IteratorOperations.h index 0fe9a8ef21..daa9e493c7 100644 --- a/Userland/Libraries/LibJS/Runtime/IteratorOperations.h +++ b/Userland/Libraries/LibJS/Runtime/IteratorOperations.h @@ -20,6 +20,7 @@ enum class IteratorHint { Object* get_iterator(GlobalObject&, Value value, IteratorHint hint = IteratorHint::Sync, Value method = {}); Object* iterator_next(Object& iterator, Value value = {}); +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); |