| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 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 | |
| 13 * distribution. | |
| 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. | |
| 17 * | |
| 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. | |
| 29 */ | |
| 30 | |
| 31 #include "sky/engine/config.h" | |
| 32 #include "sky/engine/bindings/core/v8/ScriptPromise.h" | |
| 33 | |
| 34 #include "sky/engine/bindings/core/v8/ExceptionMessages.h" | |
| 35 #include "sky/engine/bindings/core/v8/ExceptionState.h" | |
| 36 #include "sky/engine/bindings/core/v8/V8Binding.h" | |
| 37 #include "sky/engine/bindings/core/v8/V8ThrowException.h" | |
| 38 #include "sky/engine/core/dom/DOMException.h" | |
| 39 #include "v8/include/v8.h" | |
| 40 | |
| 41 namespace blink { | |
| 42 | |
| 43 namespace { | |
| 44 | |
| 45 struct WithScriptState { | |
| 46 // Used by ToV8Value<WithScriptState, ScriptState*>. | |
| 47 static v8::Handle<v8::Object> getCreationContext(ScriptState* scriptState) | |
| 48 { | |
| 49 return scriptState->context()->Global(); | |
| 50 } | |
| 51 }; | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 ScriptPromise::InternalResolver::InternalResolver(ScriptState* scriptState) | |
| 56 : m_resolver(scriptState, v8::Promise::Resolver::New(scriptState->isolate())
) { } | |
| 57 | |
| 58 v8::Local<v8::Promise> ScriptPromise::InternalResolver::v8Promise() const | |
| 59 { | |
| 60 if (m_resolver.isEmpty()) | |
| 61 return v8::Local<v8::Promise>(); | |
| 62 return m_resolver.v8Value().As<v8::Promise::Resolver>()->GetPromise(); | |
| 63 } | |
| 64 | |
| 65 ScriptPromise ScriptPromise::InternalResolver::promise() const | |
| 66 { | |
| 67 if (m_resolver.isEmpty()) | |
| 68 return ScriptPromise(); | |
| 69 return ScriptPromise(m_resolver.scriptState(), v8Promise()); | |
| 70 } | |
| 71 | |
| 72 void ScriptPromise::InternalResolver::resolve(v8::Local<v8::Value> value) | |
| 73 { | |
| 74 if (m_resolver.isEmpty()) | |
| 75 return; | |
| 76 m_resolver.v8Value().As<v8::Promise::Resolver>()->Resolve(value); | |
| 77 clear(); | |
| 78 } | |
| 79 | |
| 80 void ScriptPromise::InternalResolver::reject(v8::Local<v8::Value> value) | |
| 81 { | |
| 82 if (m_resolver.isEmpty()) | |
| 83 return; | |
| 84 m_resolver.v8Value().As<v8::Promise::Resolver>()->Reject(value); | |
| 85 clear(); | |
| 86 } | |
| 87 | |
| 88 ScriptPromise::ScriptPromise(ScriptState* scriptState, v8::Handle<v8::Value> val
ue) | |
| 89 : m_scriptState(scriptState) | |
| 90 { | |
| 91 if (value.IsEmpty()) | |
| 92 return; | |
| 93 | |
| 94 if (!value->IsPromise()) { | |
| 95 m_promise = ScriptValue(scriptState, v8::Handle<v8::Value>()); | |
| 96 V8ThrowException::throwTypeError("the given value is not a Promise", scr
iptState->isolate()); | |
| 97 return; | |
| 98 } | |
| 99 m_promise = ScriptValue(scriptState, value); | |
| 100 } | |
| 101 | |
| 102 ScriptPromise ScriptPromise::then(v8::Handle<v8::Function> onFulfilled, v8::Hand
le<v8::Function> onRejected) | |
| 103 { | |
| 104 if (m_promise.isEmpty()) | |
| 105 return ScriptPromise(); | |
| 106 | |
| 107 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>(); | |
| 108 | |
| 109 ASSERT(promise->IsPromise()); | |
| 110 // Return this Promise if no handlers are given. | |
| 111 // In fact it is not the exact bahavior of Promise.prototype.then | |
| 112 // but that is not a problem in this case. | |
| 113 v8::Local<v8::Promise> resultPromise = promise.As<v8::Promise>(); | |
| 114 if (!onFulfilled.IsEmpty()) { | |
| 115 resultPromise = resultPromise->Then(onFulfilled); | |
| 116 if (resultPromise.IsEmpty()) { | |
| 117 // v8::Promise::Then may return an empty value, for example when | |
| 118 // the stack is exhausted. | |
| 119 return ScriptPromise(); | |
| 120 } | |
| 121 } | |
| 122 if (!onRejected.IsEmpty()) | |
| 123 resultPromise = resultPromise->Catch(onRejected); | |
| 124 | |
| 125 return ScriptPromise(m_scriptState.get(), resultPromise); | |
| 126 } | |
| 127 | |
| 128 ScriptPromise ScriptPromise::cast(ScriptState* scriptState, const ScriptValue& v
alue) | |
| 129 { | |
| 130 return ScriptPromise::cast(scriptState, value.v8Value()); | |
| 131 } | |
| 132 | |
| 133 ScriptPromise ScriptPromise::cast(ScriptState* scriptState, v8::Handle<v8::Value
> value) | |
| 134 { | |
| 135 if (value.IsEmpty()) | |
| 136 return ScriptPromise(); | |
| 137 if (value->IsPromise()) { | |
| 138 return ScriptPromise(scriptState, value); | |
| 139 } | |
| 140 InternalResolver resolver(scriptState); | |
| 141 ScriptPromise promise = resolver.promise(); | |
| 142 resolver.resolve(value); | |
| 143 return promise; | |
| 144 } | |
| 145 | |
| 146 ScriptPromise ScriptPromise::reject(ScriptState* scriptState, const ScriptValue&
value) | |
| 147 { | |
| 148 return ScriptPromise::reject(scriptState, value.v8Value()); | |
| 149 } | |
| 150 | |
| 151 ScriptPromise ScriptPromise::reject(ScriptState* scriptState, v8::Handle<v8::Val
ue> value) | |
| 152 { | |
| 153 if (value.IsEmpty()) | |
| 154 return ScriptPromise(); | |
| 155 InternalResolver resolver(scriptState); | |
| 156 ScriptPromise promise = resolver.promise(); | |
| 157 resolver.reject(value); | |
| 158 return promise; | |
| 159 } | |
| 160 | |
| 161 ScriptPromise ScriptPromise::rejectWithDOMException(ScriptState* scriptState, Pa
ssRefPtr<DOMException> exception) | |
| 162 { | |
| 163 ASSERT(scriptState->isolate()->InContext()); | |
| 164 return reject(scriptState, V8ValueTraits<PassRefPtr<DOMException> >::toV8Val
ue(exception, scriptState->context()->Global(), scriptState->isolate())); | |
| 165 } | |
| 166 | |
| 167 v8::Local<v8::Promise> ScriptPromise::rejectRaw(v8::Isolate* isolate, v8::Handle
<v8::Value> value) | |
| 168 { | |
| 169 if (value.IsEmpty()) | |
| 170 return v8::Local<v8::Promise>(); | |
| 171 v8::Local<v8::Promise::Resolver> resolver = v8::Promise::Resolver::New(isola
te); | |
| 172 v8::Local<v8::Promise> promise = resolver->GetPromise(); | |
| 173 resolver->Reject(value); | |
| 174 return promise; | |
| 175 } | |
| 176 | |
| 177 } // namespace blink | |
| OLD | NEW |