Bumping manifests a=b2g-bump
[gecko.git] / js / src / jsiter.h
blob0f8d167e254cb416ef2ad0ceb3b1307d85cdefbb
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/. */
7 #ifndef jsiter_h
8 #define jsiter_h
11 * JavaScript iterators.
14 #include "mozilla/MemoryReporting.h"
16 #include "jscntxt.h"
18 #include "gc/Barrier.h"
19 #include "vm/Stack.h"
22 * For cacheable native iterators, whether the iterator is currently active.
23 * Not serialized by XDR.
25 #define JSITER_ACTIVE 0x1000
26 #define JSITER_UNREUSABLE 0x2000
28 namespace js {
30 struct NativeIterator
32 HeapPtrObject obj; // Object being iterated.
33 JSObject* iterObj_; // Internal iterator object.
34 HeapPtrFlatString* props_array;
35 HeapPtrFlatString* props_cursor;
36 HeapPtrFlatString* props_end;
37 Shape** shapes_array;
38 uint32_t shapes_length;
39 uint32_t shapes_key;
40 uint32_t flags;
42 private:
43 /* While in compartment->enumerators, these form a doubly linked list. */
44 NativeIterator* next_;
45 NativeIterator* prev_;
47 public:
48 bool isKeyIter() const {
49 return (flags & JSITER_FOREACH) == 0;
52 inline HeapPtrFlatString* begin() const {
53 return props_array;
56 inline HeapPtrFlatString* end() const {
57 return props_end;
60 size_t numKeys() const {
61 return end() - begin();
64 JSObject* iterObj() const {
65 return iterObj_;
67 HeapPtrFlatString* current() const {
68 MOZ_ASSERT(props_cursor < props_end);
69 return props_cursor;
72 NativeIterator* next() {
73 return next_;
76 static inline size_t offsetOfNext() {
77 return offsetof(NativeIterator, next_);
79 static inline size_t offsetOfPrev() {
80 return offsetof(NativeIterator, prev_);
83 void incCursor() {
84 props_cursor = props_cursor + 1;
86 void link(NativeIterator* other) {
87 /* A NativeIterator cannot appear in the enumerator list twice. */
88 MOZ_ASSERT(!next_ && !prev_);
89 MOZ_ASSERT(flags & JSITER_ENUMERATE);
91 this->next_ = other;
92 this->prev_ = other->prev_;
93 other->prev_->next_ = this;
94 other->prev_ = this;
96 void unlink() {
97 MOZ_ASSERT(flags & JSITER_ENUMERATE);
99 next_->prev_ = prev_;
100 prev_->next_ = next_;
101 next_ = nullptr;
102 prev_ = nullptr;
105 static NativeIterator* allocateSentinel(JSContext* cx);
106 static NativeIterator* allocateIterator(JSContext* cx, uint32_t slength,
107 const js::AutoIdVector& props);
108 void init(JSObject* obj, JSObject* iterObj, unsigned flags, uint32_t slength, uint32_t key);
110 void mark(JSTracer* trc);
112 static void destroy(NativeIterator* iter) {
113 js_free(iter);
117 class PropertyIteratorObject : public NativeObject
119 public:
120 static const Class class_;
122 NativeIterator* getNativeIterator() const {
123 return static_cast<js::NativeIterator*>(getPrivate());
125 void setNativeIterator(js::NativeIterator* ni) {
126 setPrivate(ni);
129 size_t sizeOfMisc(mozilla::MallocSizeOf mallocSizeOf) const;
131 private:
132 static void trace(JSTracer* trc, JSObject* obj);
133 static void finalize(FreeOp* fop, JSObject* obj);
136 class ArrayIteratorObject : public JSObject
138 public:
139 static const Class class_;
142 class StringIteratorObject : public JSObject
144 public:
145 static const Class class_;
148 bool
149 VectorToIdArray(JSContext* cx, AutoIdVector& props, JSIdArray** idap);
151 bool
152 GetIterator(JSContext* cx, HandleObject obj, unsigned flags, MutableHandleObject objp);
154 JSObject*
155 GetIteratorObject(JSContext* cx, HandleObject obj, unsigned flags);
158 * Creates either a key or value iterator, depending on flags. For a value
159 * iterator, performs value-lookup to convert the given list of jsids.
161 bool
162 EnumeratedIdVectorToIterator(JSContext* cx, HandleObject obj, unsigned flags, AutoIdVector& props,
163 MutableHandleObject objp);
165 bool
166 NewEmptyPropertyIterator(JSContext* cx, unsigned flags, MutableHandleObject objp);
169 * Convert the value stored in *vp to its iteration object. The flags should
170 * contain JSITER_ENUMERATE if js::ValueToIterator is called when enumerating
171 * for-in semantics are required, and when the caller can guarantee that the
172 * iterator will never be exposed to scripts.
174 bool
175 ValueToIterator(JSContext* cx, unsigned flags, MutableHandleValue vp);
177 bool
178 CloseIterator(JSContext* cx, HandleObject iterObj);
180 bool
181 UnwindIteratorForException(JSContext* cx, HandleObject obj);
183 void
184 UnwindIteratorForUncatchableException(JSContext* cx, JSObject* obj);
186 bool
187 IteratorConstructor(JSContext* cx, unsigned argc, Value* vp);
189 extern bool
190 SuppressDeletedProperty(JSContext* cx, HandleObject obj, jsid id);
192 extern bool
193 SuppressDeletedElement(JSContext* cx, HandleObject obj, uint32_t index);
195 extern bool
196 SuppressDeletedElements(JSContext* cx, HandleObject obj, uint32_t begin, uint32_t end);
199 * IteratorMore() returns the next iteration value. If no value is available,
200 * MagicValue(JS_NO_ITER_VALUE) is returned.
202 extern bool
203 IteratorMore(JSContext* cx, HandleObject iterobj, MutableHandleValue rval);
205 extern bool
206 ThrowStopIteration(JSContext* cx);
209 * Create an object of the form { value: VALUE, done: DONE }.
210 * ES6 draft from 2013-09-05, section 25.4.3.4.
212 extern JSObject*
213 CreateItrResultObject(JSContext* cx, HandleValue value, bool done);
215 } /* namespace js */
217 extern JSObject*
218 js_InitIteratorClasses(JSContext* cx, js::HandleObject obj);
220 #endif /* jsiter_h */