| 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 22 matching lines...) Expand all Loading... |
| 33 | 33 |
| 34 #include "RuntimeEnabledFeatures.h" | 34 #include "RuntimeEnabledFeatures.h" |
| 35 #include "bindings/v8/V8Binding.h" | 35 #include "bindings/v8/V8Binding.h" |
| 36 #include "bindings/v8/V8DOMWrapper.h" | 36 #include "bindings/v8/V8DOMWrapper.h" |
| 37 #include "bindings/v8/custom/V8PromiseCustom.h" | 37 #include "bindings/v8/custom/V8PromiseCustom.h" |
| 38 | 38 |
| 39 #include <v8.h> | 39 #include <v8.h> |
| 40 | 40 |
| 41 namespace WebCore { | 41 namespace WebCore { |
| 42 | 42 |
| 43 ScriptPromise::ScriptPromise(v8::Handle<v8::Value> value, v8::Isolate* isolate) | 43 ScriptPromise::ScriptPromise(ScriptState* scriptState, v8::Handle<v8::Value> val
ue) |
| 44 : m_scriptState(scriptState) |
| 44 { | 45 { |
| 45 if (value.IsEmpty()) | 46 if (value.IsEmpty()) |
| 46 return; | 47 return; |
| 47 | 48 |
| 48 if (!V8PromiseCustom::isPromise(value, isolate) && !value->IsPromise()) { | 49 if (!V8PromiseCustom::isPromise(value, scriptState->isolate()) && !value->Is
Promise()) { |
| 49 m_promise = ScriptValue(v8::Handle<v8::Value>(), isolate); | 50 m_promise = ScriptValue(scriptState, v8::Handle<v8::Value>()); |
| 50 V8ThrowException::throwTypeError("the given value is not a Promise", iso
late); | 51 V8ThrowException::throwTypeError("the given value is not a Promise", scr
iptState->isolate()); |
| 51 return; | 52 return; |
| 52 } | 53 } |
| 53 m_promise = ScriptValue(value, isolate); | 54 m_promise = ScriptValue(scriptState, value); |
| 54 } | 55 } |
| 55 | 56 |
| 56 ScriptPromise ScriptPromise::then(PassOwnPtr<ScriptFunction> onFulfilled, PassOw
nPtr<ScriptFunction> onRejected) | 57 ScriptPromise ScriptPromise::then(PassOwnPtr<ScriptFunction> onFulfilled, PassOw
nPtr<ScriptFunction> onRejected) |
| 57 { | 58 { |
| 58 if (m_promise.isEmpty()) | 59 if (m_promise.isEmpty()) |
| 59 return ScriptPromise(); | 60 return ScriptPromise(); |
| 60 | 61 |
| 61 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>(); | 62 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>(); |
| 62 v8::Local<v8::Function> v8OnFulfilled = ScriptFunction::adoptByGarbageCollec
tor(onFulfilled); | 63 v8::Local<v8::Function> v8OnFulfilled = ScriptFunction::adoptByGarbageCollec
tor(onFulfilled); |
| 63 v8::Local<v8::Function> v8OnRejected = ScriptFunction::adoptByGarbageCollect
or(onRejected); | 64 v8::Local<v8::Function> v8OnRejected = ScriptFunction::adoptByGarbageCollect
or(onRejected); |
| 64 | 65 |
| 65 if (V8PromiseCustom::isPromise(promise, isolate())) | 66 if (V8PromiseCustom::isPromise(promise, m_scriptState->isolate())) |
| 66 return ScriptPromise(V8PromiseCustom::then(promise, v8OnFulfilled, v8OnR
ejected, isolate()), isolate()); | 67 return ScriptPromise(m_scriptState.get(), V8PromiseCustom::then(promise,
v8OnFulfilled, v8OnRejected, m_scriptState->isolate())); |
| 67 | 68 |
| 68 ASSERT(promise->IsPromise()); | 69 ASSERT(promise->IsPromise()); |
| 69 // Return this Promise if no handlers are given. | 70 // Return this Promise if no handlers are given. |
| 70 // In fact it is not the exact bahavior of Promise.prototype.then | 71 // In fact it is not the exact bahavior of Promise.prototype.then |
| 71 // but that is not a problem in this case. | 72 // but that is not a problem in this case. |
| 72 v8::Local<v8::Promise> resultPromise = promise.As<v8::Promise>(); | 73 v8::Local<v8::Promise> resultPromise = promise.As<v8::Promise>(); |
| 73 // FIXME: Use Then once it is introduced. | 74 // FIXME: Use Then once it is introduced. |
| 74 if (!v8OnFulfilled.IsEmpty()) { | 75 if (!v8OnFulfilled.IsEmpty()) { |
| 75 resultPromise = resultPromise->Chain(v8OnFulfilled); | 76 resultPromise = resultPromise->Chain(v8OnFulfilled); |
| 76 if (resultPromise.IsEmpty()) { | 77 if (resultPromise.IsEmpty()) { |
| 77 // v8::Promise::Chain may return an empty value, for example when | 78 // v8::Promise::Chain may return an empty value, for example when |
| 78 // the stack is exhausted. | 79 // the stack is exhausted. |
| 79 return ScriptPromise(); | 80 return ScriptPromise(); |
| 80 } | 81 } |
| 81 } | 82 } |
| 82 if (!v8OnRejected.IsEmpty()) | 83 if (!v8OnRejected.IsEmpty()) |
| 83 resultPromise = resultPromise->Catch(v8OnRejected); | 84 resultPromise = resultPromise->Catch(v8OnRejected); |
| 84 | 85 |
| 85 return ScriptPromise(resultPromise, isolate()); | 86 return ScriptPromise(m_scriptState.get(), resultPromise); |
| 86 } | 87 } |
| 87 | 88 |
| 88 ScriptPromise ScriptPromise::cast(const ScriptValue& value) | 89 ScriptPromise ScriptPromise::cast(const ScriptValue& value) |
| 89 { | 90 { |
| 90 if (value.isEmpty()) | 91 if (value.isEmpty()) |
| 91 return ScriptPromise(); | 92 return ScriptPromise(); |
| 92 v8::Local<v8::Value> v8Value(value.v8Value()); | 93 v8::Local<v8::Value> v8Value(value.v8Value()); |
| 93 v8::Isolate* isolate = value.isolate(); | 94 v8::Isolate* isolate = value.isolate(); |
| 94 if (V8PromiseCustom::isPromise(v8Value, isolate) || v8Value->IsPromise()) { | 95 if (V8PromiseCustom::isPromise(v8Value, isolate) || v8Value->IsPromise()) { |
| 95 return ScriptPromise(v8Value, isolate); | 96 return ScriptPromise(value.scriptState(), v8Value); |
| 96 } | 97 } |
| 97 if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled()) { | 98 if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled()) { |
| 98 v8::Local<v8::Promise::Resolver> resolver = v8::Promise::Resolver::New(i
solate); | 99 v8::Local<v8::Promise::Resolver> resolver = v8::Promise::Resolver::New(i
solate); |
| 99 if (resolver.IsEmpty()) { | 100 if (resolver.IsEmpty()) { |
| 100 // The Promise constructor may return an empty value, for example | 101 // The Promise constructor may return an empty value, for example |
| 101 // when the stack is exhausted. | 102 // when the stack is exhausted. |
| 102 return ScriptPromise(); | 103 return ScriptPromise(); |
| 103 } | 104 } |
| 104 resolver->Resolve(v8Value); | 105 resolver->Resolve(v8Value); |
| 105 return ScriptPromise(resolver->GetPromise(), isolate); | 106 return ScriptPromise(value.scriptState(), resolver->GetPromise()); |
| 106 } | 107 } |
| 107 return ScriptPromise(V8PromiseCustom::toPromise(v8Value, isolate), isolate); | 108 return ScriptPromise(value.scriptState(), V8PromiseCustom::toPromise(v8Value
, isolate)); |
| 108 } | 109 } |
| 109 | 110 |
| 110 } // namespace WebCore | 111 } // namespace WebCore |
| OLD | NEW |