mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-23 08:00:20 +00:00
LibJS/Bytecode: Move IteratorToArray impl to CommonImplementations
This commit is contained in:
parent
c697ff61f6
commit
5179ff5fc9
Notes:
sideshowbarker
2024-07-16 23:59:28 +09:00
Author: https://github.com/skyrising Commit: https://github.com/SerenityOS/serenity/commit/5179ff5fc9 Pull-request: https://github.com/SerenityOS/serenity/pull/21677
3 changed files with 25 additions and 22 deletions
|
@ -544,4 +544,27 @@ IteratorRecord object_to_iterator(VM& vm, Object& object)
|
|||
};
|
||||
}
|
||||
|
||||
ThrowCompletionOr<NonnullGCPtr<Array>> 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++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -37,5 +37,6 @@ ThrowCompletionOr<ECMAScriptFunctionObject*> new_class(VM&, ClassExpression cons
|
|||
ThrowCompletionOr<NonnullGCPtr<Object>> 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<NonnullGCPtr<Array>> iterator_to_array(VM&, Value iterator);
|
||||
|
||||
}
|
||||
|
|
|
@ -647,28 +647,7 @@ ThrowCompletionOr<void> ImportCall::execute_impl(Bytecode::Interpreter& interpre
|
|||
|
||||
ThrowCompletionOr<void> 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 {};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue