From 5179ff5fc9cf17e22c83cefcb38c3538e78ffbbc Mon Sep 17 00:00:00 2001 From: Simon Wanner Date: Sun, 29 Oct 2023 17:07:25 +0100 Subject: [PATCH] LibJS/Bytecode: Move IteratorToArray impl to CommonImplementations --- .../LibJS/Bytecode/CommonImplementations.cpp | 23 +++++++++++++++++++ .../LibJS/Bytecode/CommonImplementations.h | 1 + .../Libraries/LibJS/Bytecode/Interpreter.cpp | 23 +------------------ 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp index ee8f9bcfa30..a3a573cb66a 100644 --- a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp +++ b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp @@ -544,4 +544,27 @@ IteratorRecord object_to_iterator(VM& vm, Object& object) }; } +ThrowCompletionOr> iterator_to_array(VM& vm, Value iterator) +{ + auto iterator_object = TRY(iterator.to_object(vm)); + auto iterator_record = object_to_iterator(vm, iterator_object); + + auto array = MUST(Array::create(*vm.current_realm(), 0)); + size_t index = 0; + + while (true) { + auto iterator_result = TRY(iterator_next(vm, iterator_record)); + + auto complete = TRY(iterator_complete(vm, iterator_result)); + + if (complete) + return array; + + auto value = TRY(iterator_value(vm, iterator_result)); + + MUST(array->create_data_property_or_throw(index, value)); + index++; + } +} + } diff --git a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h index bdef78d9bd5..623997b337c 100644 --- a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h +++ b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h @@ -37,5 +37,6 @@ ThrowCompletionOr new_class(VM&, ClassExpression cons ThrowCompletionOr> super_call_with_argument_array(VM&, Value argument_array, bool is_synthetic); Object* iterator_to_object(VM&, IteratorRecord); IteratorRecord object_to_iterator(VM&, Object&); +ThrowCompletionOr> iterator_to_array(VM&, Value iterator); } diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp index 62a5812e80b..dd3cdfba269 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -647,28 +647,7 @@ ThrowCompletionOr ImportCall::execute_impl(Bytecode::Interpreter& interpre ThrowCompletionOr IteratorToArray::execute_impl(Bytecode::Interpreter& interpreter) const { - auto& vm = interpreter.vm(); - auto iterator_object = TRY(interpreter.accumulator().to_object(vm)); - auto iterator = object_to_iterator(vm, iterator_object); - - auto array = MUST(Array::create(interpreter.realm(), 0)); - size_t index = 0; - - while (true) { - auto iterator_result = TRY(iterator_next(vm, iterator)); - - auto complete = TRY(iterator_complete(vm, iterator_result)); - - if (complete) { - interpreter.accumulator() = array; - return {}; - } - - auto value = TRY(iterator_value(vm, iterator_result)); - - MUST(array->create_data_property_or_throw(index, value)); - index++; - } + interpreter.accumulator() = TRY(iterator_to_array(interpreter.vm(), interpreter.accumulator())); return {}; }