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