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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "js/ForOfIterator.h"
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_*
#include "vm/Interpreter.h"
#include "vm/JSContext.h"
#include "vm/JSObject.h"
#include "vm/PIC.h"
#include "vm/Realm.h"
#include "vm/JSObject-inl.h"
using namespace js;
using JS::ForOfIterator;
bool ForOfIterator::init(HandleValue iterable,
NonIterableBehavior nonIterableBehavior) {
JSContext* cx = cx_;
RootedObject iterableObj(cx, ToObject(cx, iterable));
if (!iterableObj) {
return false;
}
MOZ_ASSERT(index == NOT_ARRAY);
// Check the PIC first for a match.
if (iterableObj->is<ArrayObject>()) {
ForOfPIC::Chain* stubChain = ForOfPIC::getOrCreate(cx);
if (!stubChain) {
return false;
}
bool optimized;
if (!stubChain->tryOptimizeArray(cx, iterableObj.as<ArrayObject>(),
&optimized)) {
return false;
}
if (optimized) {
// Got optimized stub. Array is optimizable.
index = 0;
iterator = iterableObj;
nextMethod.setUndefined();
return true;
}
}
MOZ_ASSERT(index == NOT_ARRAY);
RootedValue callee(cx);
RootedId iteratorId(cx, SYMBOL_TO_JSID(cx->wellKnownSymbols().iterator));
if (!GetProperty(cx, iterableObj, iterable, iteratorId, &callee)) {
return false;
}
// If obj[@@iterator] is undefined and we were asked to allow non-iterables,
// bail out now without setting iterator. This will make valueIsIterable(),
// which our caller should check, return false.
if (nonIterableBehavior == AllowNonIterable && callee.isUndefined()) {
return true;
}
// Throw if obj[@@iterator] isn't callable.
// js::Invoke is about to check for this kind of error anyway, but it would
// throw an inscrutable error message about |method| rather than this nice
// one about |obj|.
if (!callee.isObject() || !callee.toObject().isCallable()) {
UniqueChars bytes =
DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, iterable, nullptr);
if (!bytes) {
return false;
}
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_NOT_ITERABLE,
bytes.get());
return false;
}
RootedValue res(cx);
if (!js::Call(cx, callee, iterable, &res)) {
return false;
}
if (!res.isObject()) {
return ThrowCheckIsObject(cx, CheckIsObjectKind::GetIterator);
}
RootedObject iteratorObj(cx, &res.toObject());
if (!GetProperty(cx, iteratorObj, iteratorObj, cx->names().next, &res)) {
return false;
}
iterator = iteratorObj;
nextMethod = res;
return true;
}
inline bool ForOfIterator::nextFromOptimizedArray(MutableHandleValue vp,
bool* done) {
MOZ_ASSERT(index != NOT_ARRAY);
if (!CheckForInterrupt(cx_)) {
return false;
}
ArrayObject* arr = &iterator->as<ArrayObject>();
if (index >= arr->length()) {
vp.setUndefined();
*done = true;
return true;
}
*done = false;
// Try to get array element via direct access.
if (index < arr->getDenseInitializedLength()) {
vp.set(arr->getDenseElement(index));
if (!vp.isMagic(JS_ELEMENTS_HOLE)) {
++index;
return true;
}
}
return GetElement(cx_, iterator, iterator, index++, vp);
}
bool ForOfIterator::next(MutableHandleValue vp, bool* done) {
MOZ_ASSERT(iterator);
if (index != NOT_ARRAY) {
return nextFromOptimizedArray(vp, done);
}
RootedValue v(cx_);
if (!js::Call(cx_, nextMethod, iterator, &v)) {
return false;
}
if (!v.isObject()) {
return ThrowCheckIsObject(cx_, CheckIsObjectKind::IteratorNext);
}
RootedObject resultObj(cx_, &v.toObject());
if (!GetProperty(cx_, resultObj, resultObj, cx_->names().done, &v)) {
return false;
}
*done = ToBoolean(v);
if (*done) {
vp.setUndefined();
return true;
}
return GetProperty(cx_, resultObj, resultObj, cx_->names().value, vp);
}
// ES 2017 draft 0f10dba4ad18de92d47d421f378233a2eae8f077 7.4.6.
// When completion.[[Type]] is throw.
void ForOfIterator::closeThrow() {
MOZ_ASSERT(iterator);
RootedValue completionException(cx_);
RootedSavedFrame completionExceptionStack(cx_);
if (cx_->isExceptionPending()) {
if (!GetAndClearExceptionAndStack(cx_, &completionException,
&completionExceptionStack)) {
completionException.setUndefined();
completionExceptionStack = nullptr;
}
}
// Steps 1-2 (implicit)
// Step 3 (partial).
RootedValue returnVal(cx_);
if (!GetProperty(cx_, iterator, iterator, cx_->names().return_, &returnVal)) {
return;
}
// Step 4.
if (returnVal.isUndefined()) {
cx_->setPendingException(completionException, completionExceptionStack);
return;
}
// Step 3 (remaining part)
if (!returnVal.isObject()) {
JS_ReportErrorNumberASCII(cx_, GetErrorMessage, nullptr,
JSMSG_RETURN_NOT_CALLABLE);
return;
}
RootedObject returnObj(cx_, &returnVal.toObject());
if (!returnObj->isCallable()) {
JS_ReportErrorNumberASCII(cx_, GetErrorMessage, nullptr,
JSMSG_RETURN_NOT_CALLABLE);
return;
}
// Step 5.
RootedValue innerResultValue(cx_);
if (!js::Call(cx_, returnVal, iterator, &innerResultValue)) {
if (cx_->isExceptionPending()) {
cx_->clearPendingException();
}
}
// Step 6.
cx_->setPendingException(completionException, completionExceptionStack);
}
|