Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sts=4 et sw=4 tw=99:
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 : #ifndef jsiter_h
8 : #define jsiter_h
9 :
10 : /*
11 : * JavaScript iterators.
12 : */
13 :
14 : #include "mozilla/MemoryReporting.h"
15 :
16 : #include "jscntxt.h"
17 :
18 : #include "gc/Barrier.h"
19 : #include "vm/ReceiverGuard.h"
20 : #include "vm/Stack.h"
21 :
22 : /*
23 : * For cacheable native iterators, whether the iterator is currently active.
24 : * Not serialized by XDR.
25 : */
26 : #define JSITER_ACTIVE 0x1000
27 : #define JSITER_UNREUSABLE 0x2000
28 :
29 : namespace js {
30 :
31 : class PropertyIteratorObject;
32 :
33 : struct NativeIterator
34 : {
35 : GCPtrObject obj; // Object being iterated.
36 : JSObject* iterObj_; // Internal iterator object.
37 : GCPtrFlatString* props_array;
38 : GCPtrFlatString* props_cursor;
39 : GCPtrFlatString* props_end;
40 : HeapReceiverGuard* guard_array;
41 : uint32_t guard_length;
42 : uint32_t guard_key;
43 : uint32_t flags;
44 :
45 : private:
46 : /* While in compartment->enumerators, these form a doubly linked list. */
47 : NativeIterator* next_;
48 : NativeIterator* prev_;
49 :
50 : public:
51 160 : bool isKeyIter() const {
52 160 : return (flags & JSITER_FOREACH) == 0;
53 : }
54 :
55 63 : inline GCPtrFlatString* begin() const {
56 63 : return props_array;
57 : }
58 :
59 65 : inline GCPtrFlatString* end() const {
60 65 : return props_end;
61 : }
62 :
63 0 : size_t numKeys() const {
64 0 : return end() - begin();
65 : }
66 :
67 0 : JSObject* iterObj() const {
68 0 : return iterObj_;
69 : }
70 160 : GCPtrFlatString* current() const {
71 160 : MOZ_ASSERT(props_cursor < props_end);
72 160 : return props_cursor;
73 : }
74 :
75 913 : NativeIterator* next() {
76 913 : return next_;
77 : }
78 :
79 0 : static inline size_t offsetOfNext() {
80 0 : return offsetof(NativeIterator, next_);
81 : }
82 0 : static inline size_t offsetOfPrev() {
83 0 : return offsetof(NativeIterator, prev_);
84 : }
85 :
86 158 : void incCursor() {
87 158 : props_cursor = props_cursor + 1;
88 158 : }
89 93 : void link(NativeIterator* other) {
90 : /* A NativeIterator cannot appear in the enumerator list twice. */
91 93 : MOZ_ASSERT(!next_ && !prev_);
92 93 : MOZ_ASSERT(flags & JSITER_ENUMERATE);
93 :
94 93 : this->next_ = other;
95 93 : this->prev_ = other->prev_;
96 93 : other->prev_->next_ = this;
97 93 : other->prev_ = this;
98 93 : }
99 93 : void unlink() {
100 93 : MOZ_ASSERT(flags & JSITER_ENUMERATE);
101 :
102 93 : next_->prev_ = prev_;
103 93 : prev_->next_ = next_;
104 93 : next_ = nullptr;
105 93 : prev_ = nullptr;
106 93 : }
107 :
108 : static NativeIterator* allocateSentinel(JSContext* maybecx);
109 : static NativeIterator* allocateIterator(JSContext* cx, uint32_t slength, uint32_t plength);
110 : void init(JSObject* obj, JSObject* iterObj, unsigned flags, uint32_t slength, uint32_t key);
111 : bool initProperties(JSContext* cx, Handle<PropertyIteratorObject*> obj,
112 : const js::AutoIdVector& props);
113 :
114 : void trace(JSTracer* trc);
115 :
116 : static void destroy(NativeIterator* iter) {
117 : js_free(iter);
118 : }
119 : };
120 :
121 : class PropertyIteratorObject : public NativeObject
122 : {
123 : static const ClassOps classOps_;
124 :
125 : public:
126 : static const Class class_;
127 :
128 589 : NativeIterator* getNativeIterator() const {
129 589 : return static_cast<js::NativeIterator*>(getPrivate());
130 : }
131 91 : void setNativeIterator(js::NativeIterator* ni) {
132 91 : setPrivate(ni);
133 91 : }
134 :
135 : size_t sizeOfMisc(mozilla::MallocSizeOf mallocSizeOf) const;
136 :
137 : private:
138 : static void trace(JSTracer* trc, JSObject* obj);
139 : static void finalize(FreeOp* fop, JSObject* obj);
140 : };
141 :
142 : class ArrayIteratorObject : public JSObject
143 : {
144 : public:
145 : static const Class class_;
146 : };
147 :
148 : ArrayIteratorObject*
149 : NewArrayIteratorObject(JSContext* cx, NewObjectKind newKind = GenericObject);
150 :
151 : class StringIteratorObject : public JSObject
152 : {
153 : public:
154 : static const Class class_;
155 : };
156 :
157 : StringIteratorObject*
158 : NewStringIteratorObject(JSContext* cx, NewObjectKind newKind = GenericObject);
159 :
160 : JSObject*
161 : GetIterator(JSContext* cx, HandleObject obj, unsigned flags);
162 :
163 : /*
164 : * Creates either a key or value iterator, depending on flags. For a value
165 : * iterator, performs value-lookup to convert the given list of jsids.
166 : */
167 : JSObject*
168 : EnumeratedIdVectorToIterator(JSContext* cx, HandleObject obj, unsigned flags, AutoIdVector& props);
169 :
170 : JSObject*
171 : NewEmptyPropertyIterator(JSContext* cx, unsigned flags);
172 :
173 : /*
174 : * Convert the value stored in *vp to its iteration object. The flags should
175 : * contain JSITER_ENUMERATE if js::ValueToIterator is called when enumerating
176 : * for-in semantics are required, and when the caller can guarantee that the
177 : * iterator will never be exposed to scripts.
178 : */
179 : JSObject*
180 : ValueToIterator(JSContext* cx, unsigned flags, HandleValue vp);
181 :
182 : bool
183 : CloseIterator(JSContext* cx, HandleObject iterObj);
184 :
185 : bool
186 : UnwindIteratorForException(JSContext* cx, HandleObject obj);
187 :
188 : bool
189 : IteratorCloseForException(JSContext* cx, HandleObject obj);
190 :
191 : void
192 : UnwindIteratorForUncatchableException(JSContext* cx, JSObject* obj);
193 :
194 : bool
195 : IteratorConstructor(JSContext* cx, unsigned argc, Value* vp);
196 :
197 : extern bool
198 : SuppressDeletedProperty(JSContext* cx, HandleObject obj, jsid id);
199 :
200 : extern bool
201 : SuppressDeletedElement(JSContext* cx, HandleObject obj, uint32_t index);
202 :
203 : /*
204 : * IteratorMore() returns the next iteration value. If no value is available,
205 : * MagicValue(JS_NO_ITER_VALUE) is returned.
206 : */
207 : extern bool
208 : IteratorMore(JSContext* cx, HandleObject iterobj, MutableHandleValue rval);
209 :
210 : extern bool
211 : ThrowStopIteration(JSContext* cx);
212 :
213 : /*
214 : * Create an object of the form { value: VALUE, done: DONE }.
215 : * ES 2017 draft 7.4.7.
216 : */
217 : extern JSObject*
218 : CreateIterResultObject(JSContext* cx, HandleValue value, bool done);
219 :
220 : extern JSObject*
221 : InitLegacyIteratorClass(JSContext* cx, HandleObject obj);
222 :
223 : extern JSObject*
224 : InitStopIterationClass(JSContext* cx, HandleObject obj);
225 :
226 : enum class IteratorKind { Sync, Async };
227 :
228 : } /* namespace js */
229 :
230 : #endif /* jsiter_h */
|