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

@ -8,11 +8,13 @@
#include <AK/String.h>
#include <AK/Utf8View.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/Object.h>
namespace JS {
class StringIterator final : public Object {
class StringIterator final : public Object
, public BuiltinIterator {
JS_OBJECT(StringIterator, Object);
GC_DECLARE_ALLOCATOR(StringIterator);
@ -21,8 +23,8 @@ public:
virtual ~StringIterator() override = default;
Utf8CodePointIterator& iterator() { return m_iterator; }
bool done() const { return m_done; }
BuiltinIterator* as_builtin_iterator() override { return this; }
ThrowCompletionOr<void> next(VM&, bool& done, Value& value) override;
private:
explicit StringIterator(String string, Object& prototype);