2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
32 #include "bindings/core/v8/ScriptPromise.h"
34 #include "bindings/core/v8/ExceptionMessages.h"
35 #include "bindings/core/v8/ExceptionState.h"
36 #include "bindings/core/v8/V8Binding.h"
37 #include "bindings/core/v8/V8ThrowException.h"
38 #include "core/dom/DOMException.h"
46 struct WithScriptState {
47 // Used by ToV8Value<WithScriptState, ScriptState*>.
48 static v8::Handle<v8::Object> getCreationContext(ScriptState* scriptState)
50 return scriptState->context()->Global();
56 ScriptPromise::InternalResolver::InternalResolver(ScriptState* scriptState)
57 : m_resolver(scriptState, v8::Promise::Resolver::New(scriptState->isolate())) { }
59 v8::Local<v8::Promise> ScriptPromise::InternalResolver::v8Promise() const
61 if (m_resolver.isEmpty())
62 return v8::Local<v8::Promise>();
63 return m_resolver.v8Value().As<v8::Promise::Resolver>()->GetPromise();
66 ScriptPromise ScriptPromise::InternalResolver::promise() const
68 if (m_resolver.isEmpty())
69 return ScriptPromise();
70 return ScriptPromise(m_resolver.scriptState(), v8Promise());
73 void ScriptPromise::InternalResolver::resolve(v8::Local<v8::Value> value)
75 if (m_resolver.isEmpty())
77 m_resolver.v8Value().As<v8::Promise::Resolver>()->Resolve(value);
81 void ScriptPromise::InternalResolver::reject(v8::Local<v8::Value> value)
83 if (m_resolver.isEmpty())
85 m_resolver.v8Value().As<v8::Promise::Resolver>()->Reject(value);
89 ScriptPromise::ScriptPromise(ScriptState* scriptState, v8::Handle<v8::Value> value)
90 : m_scriptState(scriptState)
95 if (!value->IsPromise()) {
96 m_promise = ScriptValue(scriptState, v8::Handle<v8::Value>());
97 V8ThrowException::throwTypeError("the given value is not a Promise", scriptState->isolate());
100 m_promise = ScriptValue(scriptState, value);
103 ScriptPromise ScriptPromise::then(v8::Handle<v8::Function> onFulfilled, v8::Handle<v8::Function> onRejected)
105 if (m_promise.isEmpty())
106 return ScriptPromise();
108 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>();
110 ASSERT(promise->IsPromise());
111 // Return this Promise if no handlers are given.
112 // In fact it is not the exact bahavior of Promise.prototype.then
113 // but that is not a problem in this case.
114 v8::Local<v8::Promise> resultPromise = promise.As<v8::Promise>();
115 if (!onFulfilled.IsEmpty()) {
116 resultPromise = resultPromise->Then(onFulfilled);
117 if (resultPromise.IsEmpty()) {
118 // v8::Promise::Then may return an empty value, for example when
119 // the stack is exhausted.
120 return ScriptPromise();
123 if (!onRejected.IsEmpty())
124 resultPromise = resultPromise->Catch(onRejected);
126 return ScriptPromise(m_scriptState.get(), resultPromise);
129 ScriptPromise ScriptPromise::cast(ScriptState* scriptState, const ScriptValue& value)
131 return ScriptPromise::cast(scriptState, value.v8Value());
134 ScriptPromise ScriptPromise::cast(ScriptState* scriptState, v8::Handle<v8::Value> value)
137 return ScriptPromise();
138 if (value->IsPromise()) {
139 return ScriptPromise(scriptState, value);
141 InternalResolver resolver(scriptState);
142 ScriptPromise promise = resolver.promise();
143 resolver.resolve(value);
147 ScriptPromise ScriptPromise::reject(ScriptState* scriptState, const ScriptValue& value)
149 return ScriptPromise::reject(scriptState, value.v8Value());
152 ScriptPromise ScriptPromise::reject(ScriptState* scriptState, v8::Handle<v8::Value> value)
155 return ScriptPromise();
156 InternalResolver resolver(scriptState);
157 ScriptPromise promise = resolver.promise();
158 resolver.reject(value);
162 ScriptPromise ScriptPromise::rejectWithDOMException(ScriptState* scriptState, PassRefPtrWillBeRawPtr<DOMException> exception)
164 ASSERT(scriptState->isolate()->InContext());
165 return reject(scriptState, V8ValueTraits<PassRefPtrWillBeRawPtr<DOMException> >::toV8Value(exception, scriptState->context()->Global(), scriptState->isolate()));
168 v8::Local<v8::Promise> ScriptPromise::rejectRaw(v8::Isolate* isolate, v8::Handle<v8::Value> value)
171 return v8::Local<v8::Promise>();
172 v8::Local<v8::Promise::Resolver> resolver = v8::Promise::Resolver::New(isolate);
173 v8::Local<v8::Promise> promise = resolver->GetPromise();
174 resolver->Reject(value);