1// Copyright 2017 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_BUILTINS_BUILTINS_ITERATOR_GEN_H_
6#define V8_BUILTINS_BUILTINS_ITERATOR_GEN_H_
7
8#include "src/codegen/code-stub-assembler.h"
9
10namespace v8 {
11namespace internal {
12
13class GrowableFixedArray;
14
15class IteratorBuiltinsAssembler : public CodeStubAssembler {
16 public:
17  explicit IteratorBuiltinsAssembler(compiler::CodeAssemblerState* state)
18      : CodeStubAssembler(state) {}
19
20  using IteratorRecord = TorqueStructIteratorRecord;
21
22  // Returns object[Symbol.iterator].
23  TNode<Object> GetIteratorMethod(TNode<Context> context, TNode<Object>);
24
25  // https://tc39.github.io/ecma262/#sec-getiterator --- never used for
26  // @@asyncIterator.
27  IteratorRecord GetIterator(TNode<Context> context, TNode<Object> object);
28  IteratorRecord GetIterator(TNode<Context> context, TNode<Object> object,
29                             TNode<Object> method);
30
31  // https://tc39.github.io/ecma262/#sec-iteratorstep
32  // If the iterator is done, goto {if_done}, otherwise returns an iterator
33  // result.
34  // `fast_iterator_result_map` refers to the map for the JSIteratorResult
35  // object, loaded from the native context.
36  TNode<JSReceiver> IteratorStep(
37      TNode<Context> context, const IteratorRecord& iterator, Label* if_done,
38      base::Optional<TNode<Map>> fast_iterator_result_map = base::nullopt);
39  TNode<JSReceiver> IteratorStep(
40      TNode<Context> context, const IteratorRecord& iterator,
41      base::Optional<TNode<Map>> fast_iterator_result_map, Label* if_done) {
42    return IteratorStep(context, iterator, if_done, fast_iterator_result_map);
43  }
44
45  // https://tc39.github.io/ecma262/#sec-iteratorvalue
46  // Return the `value` field from an iterator.
47  // `fast_iterator_result_map` refers to the map for the JSIteratorResult
48  // object, loaded from the native context.
49  TNode<Object> IteratorValue(
50      TNode<Context> context, TNode<JSReceiver> result,
51      base::Optional<TNode<Map>> fast_iterator_result_map = base::nullopt);
52
53  // #sec-iterabletolist
54  // Build a JSArray by iterating over {iterable} using {iterator_fn},
55  // following the ECMAscript operation with the same name.
56  TNode<JSArray> IterableToList(TNode<Context> context, TNode<Object> iterable,
57                                TNode<Object> iterator_fn);
58
59  TNode<FixedArray> IterableToFixedArray(TNode<Context> context,
60                                         TNode<Object> iterable,
61                                         TNode<Object> iterator_fn);
62
63  void FillFixedArrayFromIterable(TNode<Context> context,
64                                  TNode<Object> iterable,
65                                  TNode<Object> iterator_fn,
66                                  GrowableFixedArray* values);
67
68  // Currently at https://tc39.github.io/proposal-intl-list-format/
69  // #sec-createstringlistfromiterable
70  TNode<FixedArray> StringListFromIterable(TNode<Context> context,
71                                           TNode<Object> iterable);
72
73  void FastIterableToList(TNode<Context> context, TNode<Object> iterable,
74                          TVariable<JSArray>* var_result, Label* slow);
75  TNode<JSArray> FastIterableToList(TNode<Context> context,
76                                    TNode<Object> iterable, Label* slow);
77};
78
79}  // namespace internal
80}  // namespace v8
81
82#endif  // V8_BUILTINS_BUILTINS_ITERATOR_GEN_H_
83