| OLD | NEW |
| 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 // Used by ToV8Value<WithScriptState, ScriptState*>. | 46 // Used by ToV8Value<WithScriptState, ScriptState*>. |
| 47 static v8::Handle<v8::Object> getCreationContext(ScriptState* scriptState) | 47 static v8::Handle<v8::Object> getCreationContext(ScriptState* scriptState) |
| 48 { | 48 { |
| 49 return scriptState->context()->Global(); | 49 return scriptState->context()->Global(); |
| 50 } | 50 } |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 } // namespace | 53 } // namespace |
| 54 | 54 |
| 55 ScriptPromise::InternalResolver::InternalResolver(ScriptState* scriptState) | 55 ScriptPromise::InternalResolver::InternalResolver(ScriptState* scriptState) |
| 56 : m_resolver(scriptState, v8::Promise::Resolver::New(scriptState->isolate())
) { } | 56 : m_resolver(scriptState, v8::Promise::Resolver::New(scriptState->context())
) { } |
| 57 | 57 |
| 58 v8::Local<v8::Promise> ScriptPromise::InternalResolver::v8Promise() const | 58 v8::Local<v8::Promise> ScriptPromise::InternalResolver::v8Promise() const |
| 59 { | 59 { |
| 60 if (m_resolver.isEmpty()) | 60 if (m_resolver.isEmpty()) |
| 61 return v8::Local<v8::Promise>(); | 61 return v8::Local<v8::Promise>(); |
| 62 return m_resolver.v8Value().As<v8::Promise::Resolver>()->GetPromise(); | 62 return m_resolver.v8Value().As<v8::Promise::Resolver>()->GetPromise(); |
| 63 } | 63 } |
| 64 | 64 |
| 65 ScriptPromise ScriptPromise::InternalResolver::promise() const | 65 ScriptPromise ScriptPromise::InternalResolver::promise() const |
| 66 { | 66 { |
| 67 if (m_resolver.isEmpty()) | 67 if (m_resolver.isEmpty()) |
| 68 return ScriptPromise(); | 68 return ScriptPromise(); |
| 69 return ScriptPromise(m_resolver.scriptState(), v8Promise()); | 69 return ScriptPromise(m_resolver.scriptState(), v8Promise()); |
| 70 } | 70 } |
| 71 | 71 |
| 72 void ScriptPromise::InternalResolver::resolve(v8::Local<v8::Value> value) | 72 void ScriptPromise::InternalResolver::resolve(v8::Local<v8::Value> value) |
| 73 { | 73 { |
| 74 if (m_resolver.isEmpty()) | 74 if (m_resolver.isEmpty()) |
| 75 return; | 75 return; |
| 76 m_resolver.v8Value().As<v8::Promise::Resolver>()->Resolve(value); | 76 m_resolver.v8Value().As<v8::Promise::Resolver>()->Resolve(m_resolver.context
(), value); |
| 77 clear(); | 77 clear(); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void ScriptPromise::InternalResolver::reject(v8::Local<v8::Value> value) | 80 void ScriptPromise::InternalResolver::reject(v8::Local<v8::Value> value) |
| 81 { | 81 { |
| 82 if (m_resolver.isEmpty()) | 82 if (m_resolver.isEmpty()) |
| 83 return; | 83 return; |
| 84 m_resolver.v8Value().As<v8::Promise::Resolver>()->Reject(value); | 84 m_resolver.v8Value().As<v8::Promise::Resolver>()->Reject(m_resolver.context(
), value); |
| 85 clear(); | 85 clear(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 ScriptPromise::ScriptPromise(ScriptState* scriptState, v8::Handle<v8::Value> val
ue) | 88 ScriptPromise::ScriptPromise(ScriptState* scriptState, v8::Handle<v8::Value> val
ue) |
| 89 : m_scriptState(scriptState) | 89 : m_scriptState(scriptState) |
| 90 { | 90 { |
| 91 if (value.IsEmpty()) | 91 if (value.IsEmpty()) |
| 92 return; | 92 return; |
| 93 | 93 |
| 94 if (!value->IsPromise()) { | 94 if (!value->IsPromise()) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 105 return ScriptPromise(); | 105 return ScriptPromise(); |
| 106 | 106 |
| 107 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>(); | 107 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>(); |
| 108 | 108 |
| 109 ASSERT(promise->IsPromise()); | 109 ASSERT(promise->IsPromise()); |
| 110 // Return this Promise if no handlers are given. | 110 // Return this Promise if no handlers are given. |
| 111 // In fact it is not the exact bahavior of Promise.prototype.then | 111 // In fact it is not the exact bahavior of Promise.prototype.then |
| 112 // but that is not a problem in this case. | 112 // but that is not a problem in this case. |
| 113 v8::Local<v8::Promise> resultPromise = promise.As<v8::Promise>(); | 113 v8::Local<v8::Promise> resultPromise = promise.As<v8::Promise>(); |
| 114 if (!onFulfilled.IsEmpty()) { | 114 if (!onFulfilled.IsEmpty()) { |
| 115 resultPromise = resultPromise->Then(onFulfilled); | 115 if (!resultPromise->Then(m_scriptState->context(), onFulfilled).ToLocal(
&resultPromise)) |
| 116 if (resultPromise.IsEmpty()) { | |
| 117 // v8::Promise::Then may return an empty value, for example when | |
| 118 // the stack is exhausted. | |
| 119 return ScriptPromise(); | 116 return ScriptPromise(); |
| 120 } | |
| 121 } | 117 } |
| 122 if (!onRejected.IsEmpty()) | 118 if (!onRejected.IsEmpty()) { |
| 123 resultPromise = resultPromise->Catch(onRejected); | 119 if (!resultPromise->Catch(m_scriptState->context(), onRejected).ToLocal(
&resultPromise)) |
| 120 return ScriptPromise(); |
| 121 } |
| 124 | 122 |
| 125 return ScriptPromise(m_scriptState.get(), resultPromise); | 123 return ScriptPromise(m_scriptState.get(), resultPromise); |
| 126 } | 124 } |
| 127 | 125 |
| 128 ScriptPromise ScriptPromise::cast(ScriptState* scriptState, const ScriptValue& v
alue) | 126 ScriptPromise ScriptPromise::cast(ScriptState* scriptState, const ScriptValue& v
alue) |
| 129 { | 127 { |
| 130 return ScriptPromise::cast(scriptState, value.v8Value()); | 128 return ScriptPromise::cast(scriptState, value.v8Value()); |
| 131 } | 129 } |
| 132 | 130 |
| 133 ScriptPromise ScriptPromise::cast(ScriptState* scriptState, v8::Handle<v8::Value
> value) | 131 ScriptPromise ScriptPromise::cast(ScriptState* scriptState, v8::Handle<v8::Value
> value) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 157 resolver.reject(value); | 155 resolver.reject(value); |
| 158 return promise; | 156 return promise; |
| 159 } | 157 } |
| 160 | 158 |
| 161 ScriptPromise ScriptPromise::rejectWithDOMException(ScriptState* scriptState, Pa
ssRefPtrWillBeRawPtr<DOMException> exception) | 159 ScriptPromise ScriptPromise::rejectWithDOMException(ScriptState* scriptState, Pa
ssRefPtrWillBeRawPtr<DOMException> exception) |
| 162 { | 160 { |
| 163 ASSERT(scriptState->isolate()->InContext()); | 161 ASSERT(scriptState->isolate()->InContext()); |
| 164 return reject(scriptState, toV8(exception, scriptState->context()->Global(),
scriptState->isolate())); | 162 return reject(scriptState, toV8(exception, scriptState->context()->Global(),
scriptState->isolate())); |
| 165 } | 163 } |
| 166 | 164 |
| 167 v8::Local<v8::Promise> ScriptPromise::rejectRaw(v8::Isolate* isolate, v8::Handle
<v8::Value> value) | 165 v8::Local<v8::Promise> ScriptPromise::rejectRaw(ScriptState* scriptState, v8::Ha
ndle<v8::Value> value) |
| 168 { | 166 { |
| 169 if (value.IsEmpty()) | 167 if (value.IsEmpty()) |
| 170 return v8::Local<v8::Promise>(); | 168 return v8::Local<v8::Promise>(); |
| 171 v8::Local<v8::Promise::Resolver> resolver = v8::Promise::Resolver::New(isola
te); | 169 v8::Local<v8::Promise::Resolver> resolver; |
| 170 if (!v8::Promise::Resolver::New(scriptState->context()).ToLocal(&resolver)) |
| 171 return v8::Local<v8::Promise>(); |
| 172 v8::Local<v8::Promise> promise = resolver->GetPromise(); | 172 v8::Local<v8::Promise> promise = resolver->GetPromise(); |
| 173 resolver->Reject(value); | 173 resolver->Reject(scriptState->context(), value); |
| 174 return promise; | 174 return promise; |
| 175 } | 175 } |
| 176 | 176 |
| 177 } // namespace blink | 177 } // namespace blink |
| OLD | NEW |