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

Side by Side Diff: Source/bindings/v8/ScriptPromiseResolverWithContext.cpp

Issue 243703007: Enable V8 Promises. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 8 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #include "config.h" 5 #include "config.h"
6 #include "bindings/v8/ScriptPromiseResolverWithContext.h" 6 #include "bindings/v8/ScriptPromiseResolverWithContext.h"
7 7
8 #include "bindings/v8/V8PerIsolateData.h"
9
8 namespace WebCore { 10 namespace WebCore {
9 11
12 class ScriptPromiseResolverWithContext::RetainThis : public RefCounted<RetainThi s> {
13 public:
14 static PassRefPtr<RetainThis> create(ScriptPromiseResolverWithContext* resol ver)
15 {
16 return adoptRef(new RetainThis(resolver));
17 }
18
19 private:
20 RetainThis(ScriptPromiseResolverWithContext* resolver) : m_resolver(resolver ) { }
21 RefPtr<ScriptPromiseResolverWithContext> m_resolver;
22 };
23
10 ScriptPromiseResolverWithContext::ScriptPromiseResolverWithContext(NewScriptStat e* scriptState) 24 ScriptPromiseResolverWithContext::ScriptPromiseResolverWithContext(NewScriptStat e* scriptState)
11 : ActiveDOMObject(scriptState->executionContext()) 25 : ActiveDOMObject(scriptState->executionContext())
12 , m_state(Pending) 26 , m_state(Pending)
13 , m_scriptState(scriptState) 27 , m_scriptState(scriptState)
14 , m_timer(this, &ScriptPromiseResolverWithContext::resolveOrRejectImmediatel y) 28 , m_timer(this, &ScriptPromiseResolverWithContext::onTimerFired)
15 , m_resolver(ScriptPromiseResolver::create(m_scriptState->executionContext() )) { } 29 , m_resolver(ScriptPromiseResolver::create(m_scriptState->executionContext() )) { }
16 30
31 ScriptPromiseResolverWithContext::~ScriptPromiseResolverWithContext()
32 {
33 }
34
17 void ScriptPromiseResolverWithContext::suspend() 35 void ScriptPromiseResolverWithContext::suspend()
18 { 36 {
19 m_timer.stop(); 37 m_timer.stop();
20 } 38 }
21 39
22 void ScriptPromiseResolverWithContext::resume() 40 void ScriptPromiseResolverWithContext::resume()
23 { 41 {
24 if (m_state == Resolving || m_state == Rejecting) 42 if (m_state == Resolving || m_state == Rejecting)
25 m_timer.startOneShot(0, FROM_HERE); 43 m_timer.startOneShot(0, FROM_HERE);
26 } 44 }
27 45
28 void ScriptPromiseResolverWithContext::stop() 46 void ScriptPromiseResolverWithContext::stop()
29 { 47 {
30 m_timer.stop(); 48 m_timer.stop();
31 clear(); 49 clear();
32 } 50 }
33 51
34 void ScriptPromiseResolverWithContext::resolveOrRejectImmediately(Timer<ScriptPr omiseResolverWithContext>*) 52 void ScriptPromiseResolverWithContext::onTimerFired(Timer<ScriptPromiseResolverW ithContext>*)
53 {
54 RefPtr<ScriptPromiseResolverWithContext> protect(this);
55 NewScriptState::Scope scope(m_scriptState.get());
56 v8::Isolate* isolate = m_scriptState->isolate();
57 resolveOrRejectImmediately();
58
59 v8::V8::RunMicrotasks(isolate);
60 }
61
62 void ScriptPromiseResolverWithContext::resolveOrRejectImmediately()
35 { 63 {
36 ASSERT(!executionContext()->activeDOMObjectsAreStopped()); 64 ASSERT(!executionContext()->activeDOMObjectsAreStopped());
37 ASSERT(!executionContext()->activeDOMObjectsAreSuspended()); 65 ASSERT(!executionContext()->activeDOMObjectsAreSuspended());
38 if (m_state == Resolving) { 66 if (m_state == Resolving) {
39 NewScriptState::Scope scope(m_scriptState.get());
40 m_resolver->resolve(m_value.newLocal(m_scriptState->isolate())); 67 m_resolver->resolve(m_value.newLocal(m_scriptState->isolate()));
41 } else { 68 } else {
42 ASSERT(m_state == Rejecting); 69 ASSERT(m_state == Rejecting);
43 NewScriptState::Scope scope(m_scriptState.get());
44 m_resolver->reject(m_value.newLocal(m_scriptState->isolate())); 70 m_resolver->reject(m_value.newLocal(m_scriptState->isolate()));
45 } 71 }
46 m_state = ResolvedOrRejected;
47 clear(); 72 clear();
48 } 73 }
49 74
75 void ScriptPromiseResolverWithContext::retainThis()
76 {
77 // This creates a cyclic reference intentionally to keep this object
78 // during waiting for the timer.
79 // The reference will be cleared in clear().
80 m_retainThis = RetainThis::create(this);
81 }
82
50 void ScriptPromiseResolverWithContext::clear() 83 void ScriptPromiseResolverWithContext::clear()
51 { 84 {
85 m_state = ResolvedOrRejected;
52 m_resolver.clear(); 86 m_resolver.clear();
53 m_value.clear(); 87 m_value.clear();
88 m_retainThis.clear();
89 // |this| may be deleted here.
54 } 90 }
55 91
56 } // namespace WebCore 92 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698