v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
js-iterator-helpers.h
Go to the documentation of this file.
1// Copyright 2023 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_OBJECTS_JS_ITERATOR_HELPERS_H_
6#define V8_OBJECTS_JS_ITERATOR_HELPERS_H_
7
9
10// Has to be the last include (doesn't have include guards):
12
13namespace v8 {
14namespace internal {
15
16class Boolean;
17
18#include "torque-generated/src/objects/js-iterator-helpers-tq.inc"
19
20// Iterator helpers are iterators that transform an underlying iterator in some
21// way. They are specified as spec generators. That is, the spec defines the
22// body of iterator helpers using algorithm steps with yields (like JS
23// generators) packaged in an Abstract Closure, and then makes a generator
24// object internally. Generator machinery such as GeneratorResume [1] are then
25// used to specify %IteratorHelperPrototype%.{next,return}. While this aids
26// understandability of the specification, it is not conducive to ease of
27// implementation or performance in V8.
28//
29// Instead, each iterator helper is implemented as an iterator directly, with
30// JSIteratorHelper acting as a superclass to multiplex the various kinds of
31// helpers.
32//
33// Each helper has its own Torque class to hold the state it needs. (In the
34// spec, the state is captured in the Abstract Closures.) The classes are named
35// after the name of the method that produces them. E.g., the iterator helper
36// returned by Iterator.prototype.map is named JSIteratorMapHelper, and has
37// fields for the underlying iterator, the mapper function, and a counter.
38//
39// The algorithm steps in the body Abstract Closure in the specification is
40// implemented directly as next() (and return(), if necessary) builtin
41// methods. E.g., the map helper's body is implemented as
42// Builtin::kIteratorMapHelperNext.
43//
44// All iterator helper objects have %IteratorHelperPrototype% as their
45// [[Prototype]]. The implementations of %IteratorHelperPrototype%.{next,return}
46// multiplex, typeswitching over all known iterator helpers and manually calling
47// their next() (and return(), if necessary) builtins. E.g., Calling next() on
48// JSIteratorMapHelper would ultimately call Builtin::kIteratorMapHelperNext.
49//
50// [1] https://tc39.es/ecma262/#sec-generatorresume
51
52// The superclass of all iterator helpers.
54 : public TorqueGeneratedJSIteratorHelper<JSIteratorHelper, JSObject> {
55 public:
56 void JSIteratorHelperPrintHeader(std::ostream& os, const char* helper_name);
57
59};
60
61// The iterator helper returned by Iterator.prototype.map.
63 : public TorqueGeneratedJSIteratorMapHelper<JSIteratorMapHelper,
64 JSIteratorHelper> {
65 public:
68
70};
71
72// The iterator helper returned by Iterator.prototype.filter.
74 : public TorqueGeneratedJSIteratorFilterHelper<JSIteratorFilterHelper,
75 JSIteratorHelper> {
76 public:
79
81};
82
83// The iterator helper returned by Iterator.prototype.take.
85 : public TorqueGeneratedJSIteratorTakeHelper<JSIteratorTakeHelper,
86 JSIteratorHelper> {
87 public:
90
92};
93
94// The iterator helper returned by Iterator.prototype.drop.
96 : public TorqueGeneratedJSIteratorDropHelper<JSIteratorDropHelper,
97 JSIteratorHelper> {
98 public:
101
103};
104
105// The iterator helper returned by Iterator.prototype.flatMap.
107 : public TorqueGeneratedJSIteratorFlatMapHelper<JSIteratorFlatMapHelper,
108 JSIteratorHelper> {
109 public:
112
114};
115
116} // namespace internal
117} // namespace v8
118
120
121#endif // V8_OBJECTS_JS_ITERATOR_HELPERS_H_
void JSIteratorHelperPrintHeader(std::ostream &os, const char *helper_name)
#define DECL_VERIFIER(Name)
#define DECL_PRINTER(Name)
#define TQ_OBJECT_CONSTRUCTORS(Type)