Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(380)

Side by Side Diff: Source/core/dom/Iterable.h

Issue 807263007: IDL: Add forEach() method to iterable<>/maplike<>/setlike<> interfaces (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: remove FIXMEs about missing forEach() Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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);
haraken 2015/01/27 01:56:30 I guess you don't need this TryCatch (see the belo
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);
haraken 2015/01/27 01:56:30 Nit: It is interesting that the order is value =>
Jens Widell 2015/01/27 07:19:23 Yeah, in this context it seems a bit backwards. Th
67
68 V8ScriptRunner::callFunction(v8Callback, scriptState->executionConte xt(), v8ThisArg, 3, args, isolate);
69
70 if (tryCatch.HasCaught()) {
haraken 2015/01/27 01:56:30 Can we use exceptionState.hadException()? We don't
Jens Widell 2015/01/27 07:19:23 Since I'm not passing 'exceptionState' to V8Script
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698