2022-01-09 18:12:24 +00:00
|
|
|
/*
|
2023-07-19 10:54:48 +00:00
|
|
|
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
|
|
|
|
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
|
2024-10-23 18:34:35 +00:00
|
|
|
* Copyright (c) 2023-2024, Tim Flynn <trflynn89@ladybird.org>
|
2022-01-09 18:12:24 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-07-19 10:54:48 +00:00
|
|
|
#include <AK/Function.h>
|
|
|
|
#include <AK/Optional.h>
|
2023-06-24 14:01:04 +00:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2022-01-09 18:12:24 +00:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
|
|
|
#include <LibJS/Runtime/Value.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2022-02-18 13:46:32 +00:00
|
|
|
// 7.4.1 Iterator Records, https://tc39.es/ecma262/#sec-iterator-records
|
2023-12-07 09:44:41 +00:00
|
|
|
class IteratorRecord final : public Object {
|
|
|
|
JS_OBJECT(IteratorRecord, Object);
|
2024-11-14 15:01:23 +00:00
|
|
|
GC_DECLARE_ALLOCATOR(IteratorRecord);
|
2023-12-07 09:44:41 +00:00
|
|
|
|
|
|
|
public:
|
2024-11-14 15:01:23 +00:00
|
|
|
IteratorRecord(Realm& realm, GC::Ptr<Object> iterator, Value next_method, bool done)
|
2023-12-07 09:44:41 +00:00
|
|
|
: Object(ConstructWithoutPrototypeTag::Tag, realm)
|
|
|
|
, iterator(iterator)
|
|
|
|
, next_method(next_method)
|
|
|
|
, done(done)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ptr<Object> iterator; // [[Iterator]]
|
|
|
|
Value next_method; // [[NextMethod]]
|
|
|
|
bool done { false }; // [[Done]]
|
2023-12-07 09:44:41 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
virtual bool is_iterator_record() const override { return true; }
|
2022-01-09 18:12:24 +00:00
|
|
|
};
|
|
|
|
|
2023-12-07 09:44:41 +00:00
|
|
|
template<>
|
|
|
|
inline bool Object::fast_is<IteratorRecord>() const { return is_iterator_record(); }
|
|
|
|
|
2023-06-24 14:01:04 +00:00
|
|
|
class Iterator : public Object {
|
|
|
|
JS_OBJECT(Iterator, Object);
|
2024-11-14 15:01:23 +00:00
|
|
|
GC_DECLARE_ALLOCATOR(Iterator);
|
2023-06-24 14:01:04 +00:00
|
|
|
|
|
|
|
public:
|
2024-11-14 15:01:23 +00:00
|
|
|
static GC::Ref<Iterator> create(Realm&, Object& prototype, GC::Ref<IteratorRecord> iterated);
|
2023-06-24 14:01:04 +00:00
|
|
|
|
|
|
|
IteratorRecord const& iterated() const { return m_iterated; }
|
|
|
|
|
|
|
|
private:
|
2024-11-14 15:01:23 +00:00
|
|
|
Iterator(Object& prototype, GC::Ref<IteratorRecord> iterated);
|
2023-06-24 14:01:04 +00:00
|
|
|
explicit Iterator(Object& prototype);
|
|
|
|
|
2023-12-07 09:44:41 +00:00
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ref<IteratorRecord> m_iterated; // [[Iterated]]
|
2023-06-24 14:01:04 +00:00
|
|
|
};
|
|
|
|
|
2023-07-19 10:54:48 +00:00
|
|
|
enum class IteratorHint {
|
|
|
|
Sync,
|
|
|
|
Async,
|
|
|
|
};
|
|
|
|
|
2024-10-23 18:34:35 +00:00
|
|
|
enum class PrimitiveHandling {
|
|
|
|
IterateStringPrimitives,
|
|
|
|
RejectPrimitives,
|
2023-07-12 19:45:05 +00:00
|
|
|
};
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
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> = {});
|
2023-07-19 10:54:48 +00:00
|
|
|
ThrowCompletionOr<bool> iterator_complete(VM&, Object& iterator_result);
|
|
|
|
ThrowCompletionOr<Value> iterator_value(VM&, Object& iterator_result);
|
2024-11-14 15:01:23 +00:00
|
|
|
ThrowCompletionOr<GC::Ptr<Object>> iterator_step(VM&, IteratorRecord&);
|
2024-10-23 18:34:35 +00:00
|
|
|
ThrowCompletionOr<Optional<Value>> iterator_step_value(VM&, IteratorRecord&);
|
2023-07-19 10:54:48 +00:00
|
|
|
Completion iterator_close(VM&, IteratorRecord const&, Completion);
|
|
|
|
Completion async_iterator_close(VM&, IteratorRecord const&, Completion);
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ref<Object> create_iterator_result_object(VM&, Value, bool done);
|
|
|
|
ThrowCompletionOr<GC::MarkedVector<Value>> iterator_to_list(VM&, IteratorRecord&);
|
2024-07-16 00:38:28 +00:00
|
|
|
ThrowCompletionOr<void> setter_that_ignores_prototype_properties(VM&, Value this_, Object const& home, PropertyKey const& property, Value value);
|
2023-07-19 10:54:48 +00:00
|
|
|
|
|
|
|
using IteratorValueCallback = Function<Optional<Completion>(Value)>;
|
|
|
|
Completion get_iterator_values(VM&, Value iterable, IteratorValueCallback callback);
|
2023-06-24 15:27:30 +00:00
|
|
|
|
2022-01-09 18:12:24 +00:00
|
|
|
}
|