1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:57:35 +00:00

LibJS: Convert IteratorNext AO to ThrowCompletionOr

This commit is contained in:
Timothy Flynn 2021-10-20 08:44:30 -04:00 committed by Linus Groh
parent f4c8f2102f
commit c981d7b9bd
4 changed files with 31 additions and 30 deletions

View file

@ -144,9 +144,10 @@ void IteratorToArray::execute_impl(Bytecode::Interpreter& interpreter) const
size_t index = 0; size_t index = 0;
while (true) { while (true) {
auto iterator_result = iterator_next(*iterator); auto iterator_result_or_error = iterator_next(*iterator);
if (!iterator_result) if (iterator_result_or_error.is_error())
return; return;
auto* iterator_result = iterator_result_or_error.release_value();
auto complete = iterator_complete(global_object, *iterator_result); auto complete = iterator_complete(global_object, *iterator_result);
if (vm.exception()) if (vm.exception())
@ -490,7 +491,13 @@ void IteratorNext::execute_impl(Bytecode::Interpreter& interpreter) const
if (object_or_error.is_error()) if (object_or_error.is_error())
return; return;
auto* object = object_or_error.release_value(); auto* object = object_or_error.release_value();
interpreter.accumulator() = iterator_next(*object);
auto iterator_result_or_error = iterator_next(*object);
if (iterator_result_or_error.is_error())
return;
auto* iterator_result = iterator_result_or_error.release_value();
interpreter.accumulator() = iterator_result;
} }
void IteratorResultDone::execute_impl(Bytecode::Interpreter& interpreter) const void IteratorResultDone::execute_impl(Bytecode::Interpreter& interpreter) const

View file

@ -34,29 +34,24 @@ ThrowCompletionOr<Object*> get_iterator(GlobalObject& global_object, Value value
} }
// 7.4.2 IteratorNext ( iteratorRecord [ , value ] ), https://tc39.es/ecma262/#sec-iteratornext // 7.4.2 IteratorNext ( iteratorRecord [ , value ] ), https://tc39.es/ecma262/#sec-iteratornext
Object* iterator_next(Object& iterator, Value value) ThrowCompletionOr<Object*> iterator_next(Object& iterator, Value value)
{ {
// FIXME: Implement using iterator records, not ordinary objects // FIXME: Implement using iterator records, not ordinary objects
auto& vm = iterator.vm(); auto& vm = iterator.vm();
auto& global_object = iterator.global_object(); auto& global_object = iterator.global_object();
auto next_method = TRY_OR_DISCARD(iterator.get(vm.names.next)); auto next_method = TRY(iterator.get(vm.names.next));
if (!next_method.is_function())
if (!next_method.is_function()) { return vm.throw_completion<TypeError>(global_object, ErrorType::IterableNextNotAFunction);
vm.throw_exception<TypeError>(global_object, ErrorType::IterableNextNotAFunction);
return nullptr;
}
Value result; Value result;
if (value.is_empty()) if (value.is_empty())
result = TRY_OR_DISCARD(vm.call(next_method.as_function(), &iterator)); result = TRY(vm.call(next_method.as_function(), &iterator));
else else
result = TRY_OR_DISCARD(vm.call(next_method.as_function(), &iterator, value)); result = TRY(vm.call(next_method.as_function(), &iterator, value));
if (!result.is_object()) { if (!result.is_object())
vm.throw_exception<TypeError>(global_object, ErrorType::IterableNextBadReturn); return vm.throw_completion<TypeError>(global_object, ErrorType::IterableNextBadReturn);
return nullptr;
}
return &result.as_object(); return &result.as_object();
} }
@ -84,9 +79,7 @@ Object* iterator_step(GlobalObject& global_object, Object& iterator)
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
auto result = iterator_next(iterator); auto result = TRY_OR_DISCARD(iterator_next(iterator));
if (vm.exception())
return {};
auto done = iterator_complete(global_object, *result); auto done = iterator_complete(global_object, *result);
if (vm.exception()) if (vm.exception())
@ -176,9 +169,10 @@ void get_iterator_values(GlobalObject& global_object, Value value, Function<Iter
auto* iterator = iterator_or_error.release_value(); auto* iterator = iterator_or_error.release_value();
while (true) { while (true) {
auto next_object = iterator_next(*iterator); auto next_object_or_error = iterator_next(*iterator);
if (!next_object) if (next_object_or_error.is_error())
return; return;
auto* next_object = next_object_or_error.release_value();
auto done_property_or_error = next_object->get(vm.names.done); auto done_property_or_error = next_object->get(vm.names.done);
if (done_property_or_error.is_error()) if (done_property_or_error.is_error())

View file

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

View file

@ -359,12 +359,12 @@ ThrowCompletionOr<void> VM::iterator_binding_initialization(BindingPattern const
auto* array = Array::create(global_object, 0); auto* array = Array::create(global_object, 0);
while (!iterator_done) { while (!iterator_done) {
auto next_object = iterator_next(*iterator); auto next_object_or_error = iterator_next(*iterator);
if (!next_object) { if (next_object_or_error.is_throw_completion()) {
iterator_done = true; iterator_done = true;
VERIFY(exception()); return JS::throw_completion(next_object_or_error.release_error().value());
return JS::throw_completion(exception()->value());
} }
auto* next_object = next_object_or_error.release_value();
auto done_property = TRY(next_object->get(names.done)); auto done_property = TRY(next_object->get(names.done));
if (done_property.to_boolean()) { if (done_property.to_boolean()) {
@ -378,12 +378,12 @@ ThrowCompletionOr<void> VM::iterator_binding_initialization(BindingPattern const
value = array; value = array;
} else if (!iterator_done) { } else if (!iterator_done) {
auto next_object = iterator_next(*iterator); auto next_object_or_error = iterator_next(*iterator);
if (!next_object) { if (next_object_or_error.is_throw_completion()) {
iterator_done = true; iterator_done = true;
VERIFY(exception()); return JS::throw_completion(next_object_or_error.release_error().value());
return JS::throw_completion(exception()->value());
} }
auto* next_object = next_object_or_error.release_value();
auto done_property = TRY(next_object->get(names.done)); auto done_property = TRY(next_object->get(names.done));
if (done_property.to_boolean()) { if (done_property.to_boolean()) {