LibJS: Add the IteratorStep abstract iterator operation

As well as add 2 missing exception checks in the IteratorComplete and
IteratorValue abstract iterator operations.
This commit is contained in:
Idan Horowitz 2021-07-04 01:33:37 +03:00 committed by Linus Groh
parent ba4d367dea
commit 5ee1ae37b2
Notes: sideshowbarker 2024-07-18 10:31:29 +09:00
2 changed files with 30 additions and 2 deletions

View file

@ -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

View file

@ -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);