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

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: extend testing 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
48 v8::Context::Scope contextScope(scriptState->context());
49 v8::HandleScope handleScope(isolate);
yhirano 2015/01/23 11:49:03 Can you tell me why v8::Context::Scope is necessar
Jens Widell 2015/01/23 11:53:55 I don't know that it is, to be honest. I put it he
haraken 2015/01/23 11:55:39 Given that forEach is always called from JavaScrip
Jens Widell 2015/01/23 11:58:14 Yeah, that makes sense.
50 v8::TryCatch tryCatch(isolate);
51
52 v8::Local<v8::Object> creationContext(scriptState->context()->Global());
53 v8::Local<v8::Function> v8Callback(callback.v8Value().As<v8::Function>() );
54 v8::Local<v8::Value> v8ThisArg(thisArg.v8Value());
55 v8::Local<v8::Value> args[3];
56
57 args[2] = thisValue.v8Value();
58
59 while (true) {
60 KeyType key;
61 ValueType value;
62
63 if (!source->next(scriptState, key, value, exceptionState))
64 return;
65
66 ASSERT(!exceptionState.hadException());
67
68 args[0] = toV8(value, creationContext, isolate);
69 args[1] = toV8(key, creationContext, isolate);
70
71 V8ScriptRunner::callFunction(v8Callback, scriptState->executionConte xt(), v8ThisArg, 3, args, isolate);
72
73 if (tryCatch.HasCaught()) {
74 exceptionState.rethrowV8Exception(tryCatch.Exception());
75 return;
76 }
77 }
78 }
79
41 class IterationSource : public GarbageCollectedFinalized<IterationSource> { 80 class IterationSource : public GarbageCollectedFinalized<IterationSource> {
42 public: 81 public:
43 virtual ~IterationSource() { } 82 virtual ~IterationSource() { }
44 83
45 // If end of iteration has been reached or an exception thrown: return f alse. 84 // If end of iteration has been reached or an exception thrown: return f alse.
46 // Otherwise: set |key| and |value| and return true. 85 // Otherwise: set |key| and |value| and return true.
47 virtual bool next(ScriptState*, KeyType&, ValueType&, ExceptionState&) = 0; 86 virtual bool next(ScriptState*, KeyType&, ValueType&, ExceptionState&) = 0;
48 87
49 virtual void trace(Visitor*) { } 88 virtual void trace(Visitor*) { }
50 }; 89 };
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 public: 195 public:
157 Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState) 196 Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState)
158 { 197 {
159 return this->entries(scriptState, exceptionState); 198 return this->entries(scriptState, exceptionState);
160 } 199 }
161 }; 200 };
162 201
163 } // namespace blink 202 } // namespace blink
164 203
165 #endif // Iterable_h 204 #endif // Iterable_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698