OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef Iterable_h | 5 #ifndef Iterable_h |
6 #define Iterable_h | 6 #define Iterable_h |
7 | 7 |
8 #include "bindings/core/v8/V8IteratorResultValue.h" | 8 #include "bindings/core/v8/V8IteratorResultValue.h" |
9 #include "bindings/core/v8/V8ScriptRunner.h" | 9 #include "bindings/core/v8/V8ScriptRunner.h" |
10 #include "core/dom/Iterator.h" | 10 #include "core/dom/Iterator.h" |
11 | 11 |
12 namespace blink { | 12 namespace blink { |
13 | 13 |
14 // Typically, use one of ValueIterable<> and PairIterable<> (below) instead! | 14 // Typically, use one of ValueIterable<> and PairIterable<> (below) instead! |
15 template <typename KeyType, typename ValueType> | 15 template <typename KeyType, typename ValueType> |
16 class Iterable { | 16 class Iterable { |
17 public: | 17 public: |
18 Iterator* keysForBinding(ScriptState* scriptState, ExceptionState& exception
State) | 18 Iterator* keysForBinding(ScriptState* scriptState, ExceptionState& exception
State) |
19 { | 19 { |
20 IterationSource* source = this->startIteration(scriptState, exceptionSta
te); | 20 IterationSource* source = this->startIteration(scriptState, exceptionSta
te); |
21 if (!source) | 21 if (!source) |
22 return nullptr; | 22 return nullptr; |
23 return new IterableIterator<KeySelector>(source); | 23 return createKeyIterator(source); |
24 } | 24 } |
25 | 25 |
26 Iterator* valuesForBinding(ScriptState* scriptState, ExceptionState& excepti
onState) | 26 Iterator* valuesForBinding(ScriptState* scriptState, ExceptionState& excepti
onState) |
27 { | 27 { |
28 IterationSource* source = this->startIteration(scriptState, exceptionSta
te); | 28 IterationSource* source = this->startIteration(scriptState, exceptionSta
te); |
29 if (!source) | 29 if (!source) |
30 return nullptr; | 30 return nullptr; |
31 return new IterableIterator<ValueSelector>(source); | 31 return createValueIterator(source); |
32 } | 32 } |
33 | 33 |
34 Iterator* entriesForBinding(ScriptState* scriptState, ExceptionState& except
ionState) | 34 Iterator* entriesForBinding(ScriptState* scriptState, ExceptionState& except
ionState) |
35 { | 35 { |
36 IterationSource* source = this->startIteration(scriptState, exceptionSta
te); | 36 IterationSource* source = this->startIteration(scriptState, exceptionSta
te); |
37 if (!source) | 37 if (!source) |
38 return nullptr; | 38 return nullptr; |
39 return new IterableIterator<EntrySelector>(source); | 39 return createEntryIterator(source); |
40 } | 40 } |
41 | 41 |
42 void forEachForBinding(ScriptState* scriptState, const ScriptValue& thisValu
e, const ScriptValue& callback, const ScriptValue& thisArg, ExceptionState& exce
ptionState) | 42 void forEachForBinding(ScriptState* scriptState, const ScriptValue& thisValu
e, const ScriptValue& callback, const ScriptValue& thisArg, ExceptionState& exce
ptionState) |
43 { | 43 { |
44 IterationSource* source = this->startIteration(scriptState, exceptionSta
te); | 44 IterationSource* source = this->startIteration(scriptState, exceptionSta
te); |
45 | 45 |
46 v8::Isolate* isolate = scriptState->isolate(); | 46 v8::Isolate* isolate = scriptState->isolate(); |
47 v8::TryCatch tryCatch(isolate); | 47 v8::TryCatch tryCatch(isolate); |
48 | 48 |
49 v8::Local<v8::Object> creationContext(scriptState->context()->Global()); | 49 v8::Local<v8::Object> creationContext(scriptState->context()->Global()); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 public: | 82 public: |
83 virtual ~IterationSource() { } | 83 virtual ~IterationSource() { } |
84 | 84 |
85 // If end of iteration has been reached or an exception thrown: return f
alse. | 85 // If end of iteration has been reached or an exception thrown: return f
alse. |
86 // Otherwise: set |key| and |value| and return true. | 86 // Otherwise: set |key| and |value| and return true. |
87 virtual bool next(ScriptState*, KeyType&, ValueType&, ExceptionState&) =
0; | 87 virtual bool next(ScriptState*, KeyType&, ValueType&, ExceptionState&) =
0; |
88 | 88 |
89 DEFINE_INLINE_VIRTUAL_TRACE() { } | 89 DEFINE_INLINE_VIRTUAL_TRACE() { } |
90 }; | 90 }; |
91 | 91 |
92 private: | 92 protected: |
93 virtual IterationSource* startIteration(ScriptState*, ExceptionState&) = 0; | 93 virtual Iterator* createKeyIterator(IterationSource* source) { return new It
erableIterator<KeySelector>(source); } |
| 94 virtual Iterator* createValueIterator(IterationSource* source) { return new
IterableIterator<ValueSelector>(source); } |
| 95 virtual Iterator* createEntryIterator(IterationSource* source) { return new
IterableIterator<MapEntrySelector>(source); } |
94 | 96 |
95 struct KeySelector { | 97 struct KeySelector { |
96 STATIC_ONLY(KeySelector); | 98 STATIC_ONLY(KeySelector); |
97 static const KeyType& select(ScriptState*, const KeyType& key, const Val
ueType& value) | 99 static const KeyType& select(ScriptState*, const KeyType& key, const Val
ueType& value) |
98 { | 100 { |
99 return key; | 101 return key; |
100 } | 102 } |
101 }; | 103 }; |
102 struct ValueSelector { | 104 struct ValueSelector { |
103 STATIC_ONLY(ValueSelector); | 105 STATIC_ONLY(ValueSelector); |
104 static const ValueType& select(ScriptState*, const KeyType& key, const V
alueType& value) | 106 static const ValueType& select(ScriptState*, const KeyType& key, const V
alueType& value) |
105 { | 107 { |
106 return value; | 108 return value; |
107 } | 109 } |
108 }; | 110 }; |
109 struct EntrySelector { | 111 struct SetEntrySelector { |
110 STATIC_ONLY(EntrySelector); | 112 STATIC_ONLY(SetEntrySelector); |
111 static Vector<ScriptValue, 2> select(ScriptState* scriptState, const Key
Type& key, const ValueType& value) | 113 static Vector<ScriptValue, 2> select(ScriptState* scriptState, const Key
Type& key, const ValueType& value) |
112 { | 114 { |
113 v8::Local<v8::Object> creationContext = scriptState->context()->Glob
al(); | 115 v8::Local<v8::Object> creationContext = scriptState->context()->Glob
al(); |
| 116 v8::Isolate* isolate = scriptState->isolate(); |
| 117 |
| 118 Vector<ScriptValue, 2> entry; |
| 119 entry.append(ScriptValue(scriptState, toV8(value, creationContext, i
solate))); |
| 120 entry.append(ScriptValue(scriptState, toV8(value, creationContext, i
solate))); |
| 121 return entry; |
| 122 } |
| 123 }; |
| 124 struct MapEntrySelector { |
| 125 STATIC_ONLY(MapEntrySelector); |
| 126 static Vector<ScriptValue, 2> select(ScriptState* scriptState, const Key
Type& key, const ValueType& value) |
| 127 { |
| 128 v8::Local<v8::Object> creationContext = scriptState->context()->Glob
al(); |
114 v8::Isolate* isolate = scriptState->isolate(); | 129 v8::Isolate* isolate = scriptState->isolate(); |
115 | 130 |
116 Vector<ScriptValue, 2> entry; | 131 Vector<ScriptValue, 2> entry; |
117 entry.append(ScriptValue(scriptState, toV8(key, creationContext, iso
late))); | 132 entry.append(ScriptValue(scriptState, toV8(key, creationContext, iso
late))); |
118 entry.append(ScriptValue(scriptState, toV8(value, creationContext, i
solate))); | 133 entry.append(ScriptValue(scriptState, toV8(value, creationContext, i
solate))); |
119 return entry; | 134 return entry; |
120 } | 135 } |
121 }; | 136 }; |
122 | 137 |
123 template <typename Selector> | 138 template <typename Selector> |
(...skipping 22 matching lines...) Expand all Loading... |
146 | 161 |
147 DEFINE_INLINE_VIRTUAL_TRACE() | 162 DEFINE_INLINE_VIRTUAL_TRACE() |
148 { | 163 { |
149 visitor->trace(m_source); | 164 visitor->trace(m_source); |
150 Iterator::trace(visitor); | 165 Iterator::trace(visitor); |
151 } | 166 } |
152 | 167 |
153 private: | 168 private: |
154 Member<IterationSource> m_source; | 169 Member<IterationSource> m_source; |
155 }; | 170 }; |
| 171 |
| 172 private: |
| 173 virtual IterationSource* startIteration(ScriptState*, ExceptionState&) = 0; |
156 }; | 174 }; |
157 | 175 |
158 // Utiltity mixin base-class for classes implementing IDL interfaces with "itera
ble<T>". | 176 // Utiltity mixin base-class for classes implementing IDL interfaces with "itera
ble<T>". |
159 template <typename ValueType> | 177 template <typename ValueType> |
160 class ValueIterable : public Iterable<unsigned, ValueType> { | 178 class ValueIterable : public Iterable<unsigned, ValueType> { |
161 public: | 179 public: |
162 Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState) | 180 Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState) |
163 { | 181 { |
164 return this->valuesForBinding(scriptState, exceptionState); | 182 return this->valuesForBinding(scriptState, exceptionState); |
165 } | 183 } |
166 | 184 |
| 185 Iterator* createKeyIterator(typename Iterable<unsigned, ValueType>::Iteratio
nSource* source) override { return this->createValueIterator(source); } |
| 186 Iterator* createEntryIterator(typename Iterable<unsigned, ValueType>::Iterat
ionSource* source) override { return new typename Iterable<unsigned, ValueType>:
:template IterableIterator<typename Iterable<unsigned, ValueType>::SetEntrySelec
tor>(source); } |
| 187 |
167 class IterationSource : public Iterable<unsigned, ValueType>::IterationSourc
e { | 188 class IterationSource : public Iterable<unsigned, ValueType>::IterationSourc
e { |
168 public: | 189 public: |
169 IterationSource() | 190 IterationSource() |
170 : m_index(0) | 191 : m_index(0) |
171 { | 192 { |
172 } | 193 } |
173 | 194 |
174 ~IterationSource() override { } | 195 ~IterationSource() override { } |
175 | 196 |
176 // If end of iteration has been reached or an exception thrown: return f
alse. | 197 // If end of iteration has been reached or an exception thrown: return f
alse. |
(...skipping 22 matching lines...) Expand all Loading... |
199 public: | 220 public: |
200 Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState) | 221 Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState) |
201 { | 222 { |
202 return this->entriesForBinding(scriptState, exceptionState); | 223 return this->entriesForBinding(scriptState, exceptionState); |
203 } | 224 } |
204 }; | 225 }; |
205 | 226 |
206 } // namespace blink | 227 } // namespace blink |
207 | 228 |
208 #endif // Iterable_h | 229 #endif // Iterable_h |
OLD | NEW |