LibJS: Allow advancing built-in iterators without result object creation

Expose a method on built-in iterators that allows retrieving the next
iteration result without allocating a JS::Object. This change is a
preparation for optimizing for..of and for..in loops.
This commit is contained in:
Aliaksandr Kalenik 2025-04-30 16:29:41 +03:00 committed by Andreas Kling
parent c72d5943e6
commit ab52d86a69
Notes: github-actions[bot] 2025-04-30 18:52:44 +00:00
15 changed files with 234 additions and 169 deletions

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/SetIterator.h>
namespace JS {
@ -30,4 +30,31 @@ void SetIterator::visit_edges(Cell::Visitor& visitor)
visitor.visit(m_set);
}
ThrowCompletionOr<void> SetIterator::next(VM& vm, bool& done, Value& value)
{
if (m_done) {
done = true;
value = js_undefined();
return {};
}
if (m_iterator == m_set->end()) {
m_done = true;
done = true;
value = js_undefined();
return {};
}
VERIFY(m_iteration_kind != Object::PropertyKind::Key);
value = (*m_iterator).key;
++m_iterator;
if (m_iteration_kind == Object::PropertyKind::Value) {
return {};
}
value = Array::create_from(*vm.current_realm(), { value, value });
return {};
}
}