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 "core/dom/Iterator.h" | 10 #include "core/dom/Iterator.h" |
10 | 11 |
11 namespace blink { | 12 namespace blink { |
12 | 13 |
13 // Typically, use one of ValueIterable<> and PairIterable<> (below) instead! | 14 // Typically, use one of ValueIterable<> and PairIterable<> (below) instead! |
14 template <typename KeyType, typename ValueType> | 15 template <typename KeyType, typename ValueType> |
15 class Iterable { | 16 class Iterable { |
16 public: | 17 public: |
17 Iterator* keys(ScriptState* scriptState, ExceptionState& exceptionState) | 18 Iterator* keys(ScriptState* scriptState, ExceptionState& exceptionState) |
18 { | 19 { |
(...skipping 12 matching lines...) Expand all Loading... |
31 } | 32 } |
32 | 33 |
33 Iterator* entries(ScriptState* scriptState, ExceptionState& exceptionState) | 34 Iterator* entries(ScriptState* scriptState, ExceptionState& exceptionState) |
34 { | 35 { |
35 IterationSource* source = this->startIteration(scriptState, exceptionSta
te); | 36 IterationSource* source = this->startIteration(scriptState, exceptionSta
te); |
36 if (!source) | 37 if (!source) |
37 return nullptr; | 38 return nullptr; |
38 return new IterableIterator<EntrySelector>(source); | 39 return new IterableIterator<EntrySelector>(source); |
39 } | 40 } |
40 | 41 |
| 42 void forEach(ScriptState* scriptState, const ScriptValue& thisValue, const S
criptValue& callback, const ScriptValue& thisArg, ExceptionState& exceptionState
) |
| 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 |
41 class IterationSource : public GarbageCollectedFinalized<IterationSource> { | 77 class IterationSource : public GarbageCollectedFinalized<IterationSource> { |
42 public: | 78 public: |
43 virtual ~IterationSource() { } | 79 virtual ~IterationSource() { } |
44 | 80 |
45 // If end of iteration has been reached or an exception thrown: return f
alse. | 81 // If end of iteration has been reached or an exception thrown: return f
alse. |
46 // Otherwise: set |key| and |value| and return true. | 82 // Otherwise: set |key| and |value| and return true. |
47 virtual bool next(ScriptState*, KeyType&, ValueType&, ExceptionState&) =
0; | 83 virtual bool next(ScriptState*, KeyType&, ValueType&, ExceptionState&) =
0; |
48 | 84 |
49 virtual void trace(Visitor*) { } | 85 virtual void trace(Visitor*) { } |
50 }; | 86 }; |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 public: | 192 public: |
157 Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState) | 193 Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState) |
158 { | 194 { |
159 return this->entries(scriptState, exceptionState); | 195 return this->entries(scriptState, exceptionState); |
160 } | 196 } |
161 }; | 197 }; |
162 | 198 |
163 } // namespace blink | 199 } // namespace blink |
164 | 200 |
165 #endif // Iterable_h | 201 #endif // Iterable_h |
OLD | NEW |