summaryrefslogtreecommitdiff
path: root/deps/v8/src/builtins/iterator.tq
blob: b169da57a4bf1d649bd3e883f680d5dc48fc2bbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include 'src/builtins/builtins-iterator-gen.h'

namespace iterator {
// Returned from IteratorBuiltinsAssembler::GetIterator().
@export
struct IteratorRecord {
  // iteratorRecord.[[Iterator]]
  object: JSReceiver;

  // iteratorRecord.[[NextMethod]]
  next: JSAny;
}

extern macro IteratorBuiltinsAssembler::FastIterableToList(
    implicit context: Context)(JSAny): JSArray labels Slow;

extern macro IteratorBuiltinsAssembler::GetIteratorMethod(
    implicit context: Context)(JSAny): JSAny;
extern macro IteratorBuiltinsAssembler::GetIterator(implicit context: Context)(
    JSAny): IteratorRecord;
extern macro IteratorBuiltinsAssembler::GetIterator(implicit context: Context)(
    JSAny, JSAny): IteratorRecord;

extern macro IteratorBuiltinsAssembler::IteratorStep(implicit context: Context)(
    IteratorRecord): JSReceiver
    labels Done;
extern macro IteratorBuiltinsAssembler::IteratorStep(implicit context: Context)(
    IteratorRecord, Map): JSReceiver
    labels Done;

extern macro IteratorBuiltinsAssembler::IteratorValue(
    implicit context: Context)(JSReceiver): JSAny;
extern macro IteratorBuiltinsAssembler::IteratorValue(
    implicit context: Context)(JSReceiver, Map): JSAny;

extern macro IteratorBuiltinsAssembler::IterableToList(
    implicit context: Context)(JSAny, JSAny): JSArray;

extern macro IteratorBuiltinsAssembler::StringListFromIterable(
    implicit context: Context)(JSAny): JSArray;

extern builtin IterableToListWithSymbolLookup(implicit context: Context)(JSAny):
    JSArray;
extern builtin IterableToFixedArrayWithSymbolLookupSlow(
    implicit context: Context)(JSAny): FixedArray;

transitioning builtin GetIteratorWithFeedback(
    context: Context, receiver: JSAny, loadSlot: TaggedIndex,
    callSlot: TaggedIndex,
    maybeFeedbackVector: Undefined|FeedbackVector): JSAny {
  // TODO(v8:9891): Remove this dcheck once all callers are ported to Torque.
  // This dcheck ensures correctness of maybeFeedbackVector's type which can
  // be easily broken for calls from CSA.
  dcheck(
      IsUndefined(maybeFeedbackVector) ||
      Is<FeedbackVector>(maybeFeedbackVector));
  let iteratorMethod: JSAny;
  typeswitch (maybeFeedbackVector) {
    case (Undefined): {
      iteratorMethod = GetProperty(receiver, IteratorSymbolConstant());
    }
    case (feedback: FeedbackVector): {
      iteratorMethod = LoadIC(
          context, receiver, IteratorSymbolConstant(), loadSlot, feedback);
    }
  }
  // TODO(v8:10047): Use TaggedIndex here once TurboFan supports it.
  const callSlotSmi: Smi = TaggedIndexToSmi(callSlot);
  return CallIteratorWithFeedback(
      context, receiver, iteratorMethod, callSlotSmi, maybeFeedbackVector);
}

extern macro LoadContextFromBaseline(): Context;
extern macro LoadFeedbackVectorFromBaseline(): FeedbackVector;

transitioning builtin GetIteratorBaseline(
    receiver: JSAny, loadSlot: TaggedIndex, callSlot: TaggedIndex): JSAny {
  const context: Context = LoadContextFromBaseline();
  const feedback: FeedbackVector = LoadFeedbackVectorFromBaseline();
  const iteratorMethod: JSAny =
      LoadIC(context, receiver, IteratorSymbolConstant(), loadSlot, feedback);
  // TODO(v8:10047): Use TaggedIndex here once TurboFan supports it.
  const callSlotSmi: Smi = TaggedIndexToSmi(callSlot);
  return CallIteratorWithFeedback(
      context, receiver, iteratorMethod, callSlotSmi, feedback);
}

extern macro CreateAsyncFromSyncIterator(Context, JSAny): JSAny;

transitioning builtin CreateAsyncFromSyncIteratorBaseline(syncIterator: JSAny):
    JSAny {
  const context: Context = LoadContextFromBaseline();
  return CreateAsyncFromSyncIterator(context, syncIterator);
}

macro GetLazyReceiver(receiver: JSAny): JSAny {
  return receiver;
}

transitioning builtin CallIteratorWithFeedback(
    context: Context, receiver: JSAny, iteratorMethod: JSAny, callSlot: Smi,
    feedback: Undefined|FeedbackVector): JSAny {
  // TODO(v8:10047): Use TaggedIndex here once TurboFan supports it.
  const callSlotUnTagged: uintptr = Unsigned(SmiUntag(callSlot));
  ic::CollectCallFeedback(
      iteratorMethod, %MakeLazy<JSAny, JSAny>('GetLazyReceiver', receiver),
      context, feedback, callSlotUnTagged);
  const iteratorCallable: Callable = Cast<Callable>(iteratorMethod)
      otherwise ThrowIteratorError(receiver);
  const iterator = Call(context, iteratorCallable, receiver);
  ThrowIfNotJSReceiver(iterator, MessageTemplate::kSymbolIteratorInvalid, '');
  return iterator;
}

// https://tc39.es/ecma262/#sec-iteratorclose
@export
transitioning macro IteratorCloseOnException(implicit context: Context)(
    iterator: IteratorRecord): void {
  try {
    // 4. Let innerResult be GetMethod(iterator, "return").
    const method = GetProperty(iterator.object, kReturnString);

    // 5. If innerResult.[[Type]] is normal, then
    //   a. Let return be innerResult.[[Value]].
    //   b. If return is undefined, return Completion(completion).
    if (method == Undefined || method == Null) return;

    //   c. Set innerResult to Call(return, iterator).
    // If an exception occurs, the original exception remains bound
    Call(context, method, iterator.object);
  } catch (_e, _message) {
    // Swallow the exception.
  }

  // (If completion.[[Type]] is throw) return Completion(completion).
}
}