| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef Iterable_h | |
| 6 #define Iterable_h | |
| 7 | |
| 8 #include "bindings/core/v8/V8IteratorResultValue.h" | |
| 9 #include "bindings/core/v8/V8ScriptRunner.h" | |
| 10 #include "core/dom/Iterator.h" | |
| 11 | |
| 12 namespace blink { | |
| 13 | |
| 14 // Typically, use one of ValueIterable<> and PairIterable<> (below) instead! | |
| 15 template <typename KeyType, typename ValueType> | |
| 16 class Iterable { | |
| 17 public: | |
| 18 Iterator* keysForBinding(ScriptState* scriptState, ExceptionState& exception
State) | |
| 19 { | |
| 20 IterationSource* source = this->startIteration(scriptState, exceptionSta
te); | |
| 21 if (!source) | |
| 22 return nullptr; | |
| 23 return new IterableIterator<KeySelector>(source); | |
| 24 } | |
| 25 | |
| 26 Iterator* valuesForBinding(ScriptState* scriptState, ExceptionState& excepti
onState) | |
| 27 { | |
| 28 IterationSource* source = this->startIteration(scriptState, exceptionSta
te); | |
| 29 if (!source) | |
| 30 return nullptr; | |
| 31 return new IterableIterator<ValueSelector>(source); | |
| 32 } | |
| 33 | |
| 34 Iterator* entriesForBinding(ScriptState* scriptState, ExceptionState& except
ionState) | |
| 35 { | |
| 36 IterationSource* source = this->startIteration(scriptState, exceptionSta
te); | |
| 37 if (!source) | |
| 38 return nullptr; | |
| 39 return new IterableIterator<EntrySelector>(source); | |
| 40 } | |
| 41 | |
| 42 void forEachForBinding(ScriptState* scriptState, const ScriptValue& thisValu
e, const ScriptValue& callback, const ScriptValue& thisArg, ExceptionState& exce
ptionState) | |
| 43 { | |
| 44 IterationSource* source = this->startIteration(scriptState, exceptionSta
te); | |
| 45 | |
| 46 v8::Isolate* isolate = scriptState->isolate(); | |
| 47 v8::TryCatch tryCatch(isolate); | |
| 48 | |
| 49 v8::Local<v8::Object> creationContext(scriptState->context()->Global()); | |
| 50 v8::Local<v8::Function> v8Callback(callback.v8Value().As<v8::Function>()
); | |
| 51 v8::Local<v8::Value> v8ThisArg(thisArg.v8Value()); | |
| 52 v8::Local<v8::Value> args[3]; | |
| 53 | |
| 54 args[2] = thisValue.v8Value(); | |
| 55 | |
| 56 while (true) { | |
| 57 KeyType key; | |
| 58 ValueType value; | |
| 59 | |
| 60 if (!source->next(scriptState, key, value, exceptionState)) | |
| 61 return; | |
| 62 | |
| 63 ASSERT(!exceptionState.hadException()); | |
| 64 | |
| 65 args[0] = toV8(value, creationContext, isolate); | |
| 66 args[1] = toV8(key, creationContext, isolate); | |
| 67 | |
| 68 V8ScriptRunner::callFunction(v8Callback, scriptState->executionConte
xt(), v8ThisArg, 3, args, isolate); | |
| 69 | |
| 70 if (tryCatch.HasCaught()) { | |
| 71 exceptionState.rethrowV8Exception(tryCatch.Exception()); | |
| 72 return; | |
| 73 } | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 class IterationSource : public GarbageCollectedFinalized<IterationSource> { | |
| 78 public: | |
| 79 virtual ~IterationSource() { } | |
| 80 | |
| 81 // If end of iteration has been reached or an exception thrown: return f
alse. | |
| 82 // Otherwise: set |key| and |value| and return true. | |
| 83 virtual bool next(ScriptState*, KeyType&, ValueType&, ExceptionState&) =
0; | |
| 84 | |
| 85 virtual void trace(Visitor*) { } | |
| 86 }; | |
| 87 | |
| 88 private: | |
| 89 virtual IterationSource* startIteration(ScriptState*, ExceptionState&) = 0; | |
| 90 | |
| 91 struct KeySelector { | |
| 92 static const KeyType& select(ScriptState*, const KeyType& key, const Val
ueType& value) | |
| 93 { | |
| 94 return key; | |
| 95 } | |
| 96 }; | |
| 97 struct ValueSelector { | |
| 98 static const ValueType& select(ScriptState*, const KeyType& key, const V
alueType& value) | |
| 99 { | |
| 100 return value; | |
| 101 } | |
| 102 }; | |
| 103 struct EntrySelector { | |
| 104 static Vector<ScriptValue, 2> select(ScriptState* scriptState, const Key
Type& key, const ValueType& value) | |
| 105 { | |
| 106 v8::Local<v8::Object> creationContext = scriptState->context()->Glob
al(); | |
| 107 v8::Isolate* isolate = scriptState->isolate(); | |
| 108 | |
| 109 Vector<ScriptValue, 2> entry; | |
| 110 entry.append(ScriptValue(scriptState, toV8(key, creationContext, iso
late))); | |
| 111 entry.append(ScriptValue(scriptState, toV8(value, creationContext, i
solate))); | |
| 112 return entry; | |
| 113 } | |
| 114 }; | |
| 115 | |
| 116 template <typename Selector> | |
| 117 class IterableIterator final : public Iterator { | |
| 118 public: | |
| 119 explicit IterableIterator(IterationSource* source) | |
| 120 : m_source(source) | |
| 121 { | |
| 122 } | |
| 123 | |
| 124 ScriptValue next(ScriptState* scriptState, ExceptionState& exceptionStat
e) override | |
| 125 { | |
| 126 KeyType key; | |
| 127 ValueType value; | |
| 128 | |
| 129 if (!m_source->next(scriptState, key, value, exceptionState)) | |
| 130 return v8IteratorResultDone(scriptState); | |
| 131 | |
| 132 return v8IteratorResult(scriptState, Selector::select(scriptState, k
ey, value)); | |
| 133 } | |
| 134 | |
| 135 ScriptValue next(ScriptState* scriptState, ScriptValue, ExceptionState&
exceptionState) override | |
| 136 { | |
| 137 return next(scriptState, exceptionState); | |
| 138 } | |
| 139 | |
| 140 void trace(Visitor* visitor) override | |
| 141 { | |
| 142 visitor->trace(m_source); | |
| 143 Iterator::trace(visitor); | |
| 144 } | |
| 145 | |
| 146 private: | |
| 147 Member<IterationSource> m_source; | |
| 148 }; | |
| 149 }; | |
| 150 | |
| 151 // Utiltity mixin base-class for classes implementing IDL interfaces with "itera
ble<T>". | |
| 152 template <typename ValueType> | |
| 153 class ValueIterable : public Iterable<unsigned, ValueType> { | |
| 154 public: | |
| 155 Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState) | |
| 156 { | |
| 157 return this->valuesForBinding(scriptState, exceptionState); | |
| 158 } | |
| 159 | |
| 160 class IterationSource : public Iterable<unsigned, ValueType>::IterationSourc
e { | |
| 161 public: | |
| 162 IterationSource() | |
| 163 : m_index(0) | |
| 164 { | |
| 165 } | |
| 166 | |
| 167 ~IterationSource() override { } | |
| 168 | |
| 169 // If end of iteration has been reached or an exception thrown: return f
alse. | |
| 170 // Otherwise: set |value| and return true. | |
| 171 // Note: |this->m_index| is the index being accessed. | |
| 172 virtual bool next(ScriptState*, ValueType&, ExceptionState&) = 0; | |
| 173 | |
| 174 protected: | |
| 175 unsigned m_index; | |
| 176 | |
| 177 private: | |
| 178 bool next(ScriptState* scriptState, unsigned& key, ValueType& value, Exc
eptionState& exceptionState) final | |
| 179 { | |
| 180 if (!next(scriptState, value, exceptionState)) | |
| 181 return false; | |
| 182 key = m_index; | |
| 183 ++m_index; | |
| 184 return true; | |
| 185 } | |
| 186 }; | |
| 187 }; | |
| 188 | |
| 189 // Utiltity mixin base-class for classes implementing IDL interfaces with "itera
ble<T1, T2>". | |
| 190 template <typename KeyType, typename ValueType> | |
| 191 class PairIterable : public Iterable<KeyType, ValueType> { | |
| 192 public: | |
| 193 Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState) | |
| 194 { | |
| 195 return this->entriesForBinding(scriptState, exceptionState); | |
| 196 } | |
| 197 }; | |
| 198 | |
| 199 } // namespace blink | |
| 200 | |
| 201 #endif // Iterable_h | |
| OLD | NEW |