mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-23 17:33:12 +00:00
Some checks are pending
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
This shaves its size down from 104 bytes to 48 bytes, cutting GC pressure caused by this type in more than half.
86 lines
2.9 KiB
C++
86 lines
2.9 KiB
C++
/*
|
|
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
|
|
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
|
|
* Copyright (c) 2023-2024, Tim Flynn <trflynn89@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Function.h>
|
|
#include <AK/Optional.h>
|
|
#include <LibJS/Runtime/Completion.h>
|
|
#include <LibJS/Runtime/Object.h>
|
|
#include <LibJS/Runtime/Value.h>
|
|
|
|
namespace JS {
|
|
|
|
// 7.4.1 Iterator Records, https://tc39.es/ecma262/#sec-iterator-records
|
|
class IteratorRecord final : public Cell {
|
|
GC_CELL(IteratorRecord, Cell);
|
|
GC_DECLARE_ALLOCATOR(IteratorRecord);
|
|
|
|
public:
|
|
IteratorRecord(GC::Ptr<Object> iterator, Value next_method, bool done)
|
|
: iterator(iterator)
|
|
, next_method(next_method)
|
|
, done(done)
|
|
{
|
|
}
|
|
|
|
GC::Ptr<Object> iterator; // [[Iterator]]
|
|
Value next_method; // [[NextMethod]]
|
|
bool done { false }; // [[Done]]
|
|
|
|
private:
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
};
|
|
|
|
class Iterator : public Object {
|
|
JS_OBJECT(Iterator, Object);
|
|
GC_DECLARE_ALLOCATOR(Iterator);
|
|
|
|
public:
|
|
static GC::Ref<Iterator> create(Realm&, Object& prototype, GC::Ref<IteratorRecord> iterated);
|
|
|
|
IteratorRecord const& iterated() const { return m_iterated; }
|
|
|
|
private:
|
|
Iterator(Object& prototype, GC::Ref<IteratorRecord> iterated);
|
|
explicit Iterator(Object& prototype);
|
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
GC::Ref<IteratorRecord> m_iterated; // [[Iterated]]
|
|
};
|
|
|
|
enum class IteratorHint {
|
|
Sync,
|
|
Async,
|
|
};
|
|
|
|
enum class PrimitiveHandling {
|
|
IterateStringPrimitives,
|
|
RejectPrimitives,
|
|
};
|
|
|
|
ThrowCompletionOr<GC::Ref<IteratorRecord>> get_iterator_direct(VM&, Object&);
|
|
ThrowCompletionOr<GC::Ref<IteratorRecord>> get_iterator_from_method(VM&, Value, GC::Ref<FunctionObject>);
|
|
ThrowCompletionOr<GC::Ref<IteratorRecord>> get_iterator(VM&, Value, IteratorHint);
|
|
ThrowCompletionOr<GC::Ref<IteratorRecord>> get_iterator_flattenable(VM&, Value, PrimitiveHandling);
|
|
ThrowCompletionOr<GC::Ref<Object>> iterator_next(VM&, IteratorRecord&, Optional<Value> = {});
|
|
ThrowCompletionOr<bool> iterator_complete(VM&, Object& iterator_result);
|
|
ThrowCompletionOr<Value> iterator_value(VM&, Object& iterator_result);
|
|
ThrowCompletionOr<GC::Ptr<Object>> iterator_step(VM&, IteratorRecord&);
|
|
ThrowCompletionOr<Optional<Value>> iterator_step_value(VM&, IteratorRecord&);
|
|
Completion iterator_close(VM&, IteratorRecord const&, Completion);
|
|
Completion async_iterator_close(VM&, IteratorRecord const&, Completion);
|
|
GC::Ref<Object> create_iterator_result_object(VM&, Value, bool done);
|
|
ThrowCompletionOr<GC::RootVector<Value>> iterator_to_list(VM&, IteratorRecord&);
|
|
ThrowCompletionOr<void> setter_that_ignores_prototype_properties(VM&, Value this_, Object const& home, PropertyKey const& property, Value value);
|
|
|
|
using IteratorValueCallback = Function<Optional<Completion>(Value)>;
|
|
Completion get_iterator_values(VM&, Value iterable, IteratorValueCallback callback);
|
|
|
|
}
|