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

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

Issue 197213007: ScriptPromise implementation for V8 Promises (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 9 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 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 13 matching lines...) Expand all
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "config.h" 31 #include "config.h"
32 #include "bindings/v8/ScriptPromiseResolver.h" 32 #include "bindings/v8/ScriptPromiseResolver.h"
33 33
34 #include "RuntimeEnabledFeatures.h"
34 #include "bindings/v8/ScriptState.h" 35 #include "bindings/v8/ScriptState.h"
35 #include "bindings/v8/ScriptValue.h" 36 #include "bindings/v8/ScriptValue.h"
36 #include "bindings/v8/V8Binding.h" 37 #include "bindings/v8/V8Binding.h"
37 #include "bindings/v8/V8DOMWrapper.h" 38 #include "bindings/v8/V8DOMWrapper.h"
38 #include "bindings/v8/custom/V8PromiseCustom.h" 39 #include "bindings/v8/custom/V8PromiseCustom.h"
39 40
40 #include <v8.h> 41 #include <v8.h>
41 42
42 namespace WebCore { 43 namespace WebCore {
43 44
44 ScriptPromiseResolver::ScriptPromiseResolver(ExecutionContext* context) 45 ScriptPromiseResolver::ScriptPromiseResolver(ExecutionContext* context)
45 : m_isolate(toIsolate(context)) 46 : m_isolate(toIsolate(context))
46 , m_promise(ScriptPromise::createPending(context))
47 { 47 {
48 ASSERT(context);
49 v8::Isolate* isolate = toIsolate(context);
50 ASSERT(isolate->InContext());
51 if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled()) {
52 m_resolver = ScriptValue(v8::Promise::Resolver::New(isolate), isolate);
53 } else {
54 v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld ::current(isolate));
55 v8::Handle<v8::Object> creationContext = v8Context.IsEmpty() ? v8::Objec t::New(isolate) : v8Context->Global();
56 m_promise = ScriptPromise(V8PromiseCustom::createPromise(creationContext , isolate), isolate);
57 }
48 } 58 }
49 59
50 ScriptPromiseResolver::ScriptPromiseResolver(v8::Isolate* isolate) 60 ScriptPromiseResolver::ScriptPromiseResolver(v8::Isolate* isolate)
51 : m_isolate(isolate) 61 : m_isolate(isolate)
52 , m_promise(ScriptPromise::createPending(isolate))
53 { 62 {
63 ASSERT(isolate->InContext());
64 if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled()) {
65 m_resolver = ScriptValue(v8::Promise::Resolver::New(isolate), isolate);
66 } else {
67 m_promise = ScriptPromise(V8PromiseCustom::createPromise(v8::Object::New (isolate), isolate), isolate);
68 }
54 } 69 }
55 70
56 ScriptPromiseResolver::~ScriptPromiseResolver() 71 ScriptPromiseResolver::~ScriptPromiseResolver()
57 { 72 {
58 // We don't call "reject" here because it requires a caller 73 // We don't call "reject" here because it requires a caller
59 // to be in a v8 context. 74 // to be in a v8 context.
60 75
61 m_promise.clear(); 76 m_promise.clear();
77 m_resolver.clear();
78 }
79
80 ScriptPromise ScriptPromiseResolver::promise()
81 {
82 ASSERT(m_isolate->InContext());
83 if (!m_resolver.hasNoValue()) {
84 v8::Local<v8::Promise::Resolver> v8Resolver = m_resolver.v8Value().As<v8 ::Promise::Resolver>();
85 return ScriptPromise(v8Resolver->GetPromise(), m_isolate);
86 }
87 return m_promise;
62 } 88 }
63 89
64 PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create(ExecutionContext * context) 90 PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create(ExecutionContext * context)
65 { 91 {
66 ASSERT(context); 92 ASSERT(context);
67 ASSERT(toIsolate(context)->InContext()); 93 ASSERT(toIsolate(context)->InContext());
68 return adoptRef(new ScriptPromiseResolver(context)); 94 return adoptRef(new ScriptPromiseResolver(context));
69 } 95 }
70 96
71 PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create(v8::Isolate* iso late) 97 PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create(v8::Isolate* iso late)
72 { 98 {
73 ASSERT(isolate->InContext()); 99 ASSERT(isolate->InContext());
74 return adoptRef(new ScriptPromiseResolver(isolate)); 100 return adoptRef(new ScriptPromiseResolver(isolate));
75 } 101 }
76 102
77 bool ScriptPromiseResolver::isPending() const
78 {
79 ASSERT(m_isolate->InContext());
80 if (m_promise.hasNoValue())
81 return false;
82 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>();
83 v8::Local<v8::Object> internal = V8PromiseCustom::getInternal(promise);
84 V8PromiseCustom::PromiseState state = V8PromiseCustom::getState(internal);
85 return state == V8PromiseCustom::Pending;
86 }
87
88 void ScriptPromiseResolver::resolve(v8::Handle<v8::Value> value) 103 void ScriptPromiseResolver::resolve(v8::Handle<v8::Value> value)
89 { 104 {
90 ASSERT(m_isolate->InContext()); 105 ASSERT(m_isolate->InContext());
91 if (!isPending()) 106 if (!m_resolver.hasNoValue()) {
92 return; 107 m_resolver.v8Value().As<v8::Promise::Resolver>()->Resolve(value);
93 V8PromiseCustom::resolve(m_promise.v8Value().As<v8::Object>(), value, m_isol ate); 108 } else if (!m_promise.hasNoValue()) {
109 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>();
110 ASSERT(V8PromiseCustom::isPromise(promise, m_isolate));
111 V8PromiseCustom::resolve(promise, value, m_isolate);
112 }
94 m_promise.clear(); 113 m_promise.clear();
114 m_resolver.clear();
95 } 115 }
96 116
97 void ScriptPromiseResolver::reject(v8::Handle<v8::Value> value) 117 void ScriptPromiseResolver::reject(v8::Handle<v8::Value> value)
98 { 118 {
99 ASSERT(m_isolate->InContext()); 119 ASSERT(m_isolate->InContext());
100 if (!isPending()) 120 if (!m_resolver.hasNoValue()) {
101 return; 121 m_resolver.v8Value().As<v8::Promise::Resolver>()->Reject(value);
102 V8PromiseCustom::reject(m_promise.v8Value().As<v8::Object>(), value, m_isola te); 122 } else if (!m_promise.hasNoValue()) {
123 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>();
124 ASSERT(V8PromiseCustom::isPromise(promise, m_isolate));
125 V8PromiseCustom::reject(promise, value, m_isolate);
126 }
103 m_promise.clear(); 127 m_promise.clear();
128 m_resolver.clear();
104 } 129 }
105 130
106 void ScriptPromiseResolver::resolve(ScriptValue value) 131 void ScriptPromiseResolver::resolve(ScriptValue value)
107 { 132 {
108 ASSERT(m_isolate->InContext()); 133 ASSERT(m_isolate->InContext());
109 resolve(value.v8Value()); 134 resolve(value.v8Value());
110 } 135 }
111 136
112 void ScriptPromiseResolver::reject(ScriptValue value) 137 void ScriptPromiseResolver::reject(ScriptValue value)
113 { 138 {
114 ASSERT(m_isolate->InContext()); 139 ASSERT(m_isolate->InContext());
115 reject(value.v8Value()); 140 reject(value.v8Value());
116 } 141 }
117 142
118 } // namespace WebCore 143 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/bindings/v8/ScriptPromiseResolver.h ('k') | Source/bindings/v8/ScriptPromiseResolverTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698