Debian Codegraph @

js/src/vm/ForOfIterator.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "js/ForOfIterator.h"
8 #include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_*
9 #include "vm/Interpreter.h"
10 #include "vm/Iteration.h"
11 #include "vm/JSContext.h"
12 #include "vm/JSObject.h"
13
14 #include "vm/JSContext-inl.h"
15 #include "vm/JSObject-inl.h"
16
17 using namespace js;
18 using JS::ForOfIterator;
19
20 bool ForOfIterator::init(HandleValue iterable,
21 NonIterableBehavior nonIterableBehavior) {
22 JSContext* cx = cx_;
23 RootedObject iterableObj(cx, ToObject(cx, iterable));
24 if (!iterableObj) {
25 return false;
26 }
27
28 MOZ_ASSERT(index == NOT_ARRAY);
29
30 if (IsArrayWithDefaultIterator<MustBePacked::No>(iterableObj, cx)) {
31 // Array is optimizable.
32 index = 0;
33 iterator = iterableObj;
34 nextMethod.setUndefined();
35 return true;
36 }
37
38 MOZ_ASSERT(index == NOT_ARRAY);
39
40 RootedValue callee(cx);
41 RootedId iteratorId(cx, PropertyKey::Symbol(cx->wellKnownSymbols().iterator));
42 if (!GetProperty(cx, iterableObj, iterable, iteratorId, &callee)) {
43 return false;
44 }
45
46 // If obj[@@iterator] is undefined and we were asked to allow non-iterables,
47 // bail out now without setting iterator. This will make valueIsIterable(),
48 // which our caller should check, return false.
49 if (nonIterableBehavior == AllowNonIterable && callee.isUndefined()) {
50 return true;
51 }
52
53 // Throw if obj[@@iterator] isn't callable.
54 // js::Invoke is about to check for this kind of error anyway, but it would
55 // throw an inscrutable error message about |method| rather than this nice
56 // one about |obj|.
57 if (!callee.isObject() || !callee.toObject().isCallable()) {
58 UniqueChars bytes =
59 DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, iterable, nullptr);
60 if (!bytes) {
61 return false;
62 }
63 JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_NOT_ITERABLE,
64 bytes.get());
65 return false;
66 }
67
68 RootedValue res(cx);
69 if (!js::Call(cx, callee, iterable, &res)) {
70 return false;
71 }
72
73 if (!res.isObject()) {
74 return ThrowCheckIsObject(cx, CheckIsObjectKind::GetIterator);
75 }
76
77 RootedObject iteratorObj(cx, &res.toObject());
78 if (!GetProperty(cx, iteratorObj, iteratorObj, cx->names().next, &res)) {
79 return false;
80 }
81
82 iterator = iteratorObj;
83 nextMethod = res;
84 return true;
85 }
86
87 inline bool ForOfIterator::nextFromOptimizedArray(MutableHandleValue vp,
88 bool* done) {
89 MOZ_ASSERT(index != NOT_ARRAY);
90
91 if (!CheckForInterrupt(cx_)) {
92 return false;
93 }
94
95 ArrayObject* arr = &iterator->as<ArrayObject>();
96
97 if (index >= arr->length()) {
98 vp.setUndefined();
99 *done = true;
100 return true;
101 }
102 *done = false;
103
104 // Try to get array element via direct access.
105 if (index < arr->getDenseInitializedLength()) {
106 vp.set(arr->getDenseElement(index));
107 if (!vp.isMagic(JS_ELEMENTS_HOLE)) {
108 ++index;
109 return true;
110 }
111 }
112
113 return GetElement(cx_, iterator, iterator, index++, vp);
114 }
115
116 bool ForOfIterator::next(MutableHandleValue vp, bool* done) {
117 MOZ_ASSERT(iterator);
118 if (index != NOT_ARRAY) {
119 return nextFromOptimizedArray(vp, done);
120 }
121
122 RootedValue v(cx_);
123 if (!js::Call(cx_, nextMethod, iterator, &v)) {
124 return false;
125 }
126
127 if (!v.isObject()) {
128 return ThrowCheckIsObject(cx_, CheckIsObjectKind::IteratorNext);
129 }
130
131 RootedObject resultObj(cx_, &v.toObject());
132 if (!GetProperty(cx_, resultObj, resultObj, cx_->names().done, &v)) {
133 return false;
134 }
135
136 *done = ToBoolean(v);
137 if (*done) {
138 vp.setUndefined();
139 return true;
140 }
141
142 return GetProperty(cx_, resultObj, resultObj, cx_->names().value, vp);
143 }
144
145 // ES 2017 draft 0f10dba4ad18de92d47d421f378233a2eae8f077 7.4.6.
146 // When completion.[[Type]] is throw.
147 void ForOfIterator::closeThrow() {
148 MOZ_ASSERT(iterator);
149
150 RootedValue completionException(cx_);
151 Rooted<SavedFrame*> completionExceptionStack(cx_);
152 if (cx_->isExceptionPending()) {
153 if (!GetAndClearExceptionAndStack(cx_, &completionException,
154 &completionExceptionStack)) {
155 completionException.setUndefined();
156 completionExceptionStack = nullptr;
157 }
158 }
159
160 // Steps 1-2 (implicit)
161
162 // Step 3 (partial).
163 RootedValue returnVal(cx_);
164 if (!GetProperty(cx_, iterator, iterator, cx_->names().return_, &returnVal)) {
165 return;
166 }
167
168 // Step 4.
169 if (returnVal.isUndefined()) {
170 cx_->setPendingException(completionException, completionExceptionStack);
171 return;
172 }
173
174 // Step 3 (remaining part)
175 if (!returnVal.isObject()) {
176 JS_ReportErrorNumberASCII(cx_, GetErrorMessage, nullptr,
177 JSMSG_RETURN_NOT_CALLABLE);
178 return;
179 }
180 RootedObject returnObj(cx_, &returnVal.toObject());
181 if (!returnObj->isCallable()) {
182 JS_ReportErrorNumberASCII(cx_, GetErrorMessage, nullptr,
183 JSMSG_RETURN_NOT_CALLABLE);
184 return;
185 }
186
187 // Step 5.
188 RootedValue innerResultValue(cx_);
189 if (!js::Call(cx_, returnVal, iterator, &innerResultValue)) {
190 if (cx_->isExceptionPending()) {
191 cx_->clearPendingException();
192 }
193 }
194
195 // Step 6.
196 cx_->setPendingException(completionException, completionExceptionStack);
197 }