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 26 matching lines...) Expand all Loading... |
37 #include "bindings/core/v8/V8ThrowException.h" | 37 #include "bindings/core/v8/V8ThrowException.h" |
38 #include "core/dom/DOMException.h" | 38 #include "core/dom/DOMException.h" |
39 #include <v8.h> | 39 #include <v8.h> |
40 | 40 |
41 namespace blink { | 41 namespace blink { |
42 | 42 |
43 namespace { | 43 namespace { |
44 | 44 |
45 struct WithScriptState { | 45 struct WithScriptState { |
46 // Used by ToV8Value<WithScriptState, ScriptState*>. | 46 // Used by ToV8Value<WithScriptState, ScriptState*>. |
47 static v8::Handle<v8::Object> getCreationContext(ScriptState* scriptState) | 47 static v8::Local<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->context())
) { } | 56 : m_resolver(scriptState, v8::Promise::Resolver::New(scriptState->context())
) { } |
57 | 57 |
(...skipping 20 matching lines...) Expand all Loading... |
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(m_resolver.context(
), 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::Local<v8::Value> valu
e) |
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()) { |
95 m_promise = ScriptValue(scriptState, v8::Handle<v8::Value>()); | 95 m_promise = ScriptValue(scriptState, v8::Local<v8::Value>()); |
96 V8ThrowException::throwTypeError(scriptState->isolate(), "the given valu
e is not a Promise"); | 96 V8ThrowException::throwTypeError(scriptState->isolate(), "the given valu
e is not a Promise"); |
97 return; | 97 return; |
98 } | 98 } |
99 m_promise = ScriptValue(scriptState, value); | 99 m_promise = ScriptValue(scriptState, value); |
100 } | 100 } |
101 | 101 |
102 ScriptPromise ScriptPromise::then(v8::Handle<v8::Function> onFulfilled, v8::Hand
le<v8::Function> onRejected) | 102 ScriptPromise ScriptPromise::then(v8::Local<v8::Function> onFulfilled, v8::Local
<v8::Function> onRejected) |
103 { | 103 { |
104 if (m_promise.isEmpty()) | 104 if (m_promise.isEmpty()) |
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 if (!resultPromise->Then(m_scriptState->context(), onFulfilled).ToLocal(
&resultPromise)) | 115 if (!resultPromise->Then(m_scriptState->context(), onFulfilled).ToLocal(
&resultPromise)) |
116 return ScriptPromise(); | 116 return ScriptPromise(); |
117 } | 117 } |
118 if (!onRejected.IsEmpty()) { | 118 if (!onRejected.IsEmpty()) { |
119 if (!resultPromise->Catch(m_scriptState->context(), onRejected).ToLocal(
&resultPromise)) | 119 if (!resultPromise->Catch(m_scriptState->context(), onRejected).ToLocal(
&resultPromise)) |
120 return ScriptPromise(); | 120 return ScriptPromise(); |
121 } | 121 } |
122 | 122 |
123 return ScriptPromise(m_scriptState.get(), resultPromise); | 123 return ScriptPromise(m_scriptState.get(), resultPromise); |
124 } | 124 } |
125 | 125 |
126 ScriptPromise ScriptPromise::cast(ScriptState* scriptState, const ScriptValue& v
alue) | 126 ScriptPromise ScriptPromise::cast(ScriptState* scriptState, const ScriptValue& v
alue) |
127 { | 127 { |
128 return ScriptPromise::cast(scriptState, value.v8Value()); | 128 return ScriptPromise::cast(scriptState, value.v8Value()); |
129 } | 129 } |
130 | 130 |
131 ScriptPromise ScriptPromise::cast(ScriptState* scriptState, v8::Handle<v8::Value
> value) | 131 ScriptPromise ScriptPromise::cast(ScriptState* scriptState, v8::Local<v8::Value>
value) |
132 { | 132 { |
133 if (value.IsEmpty()) | 133 if (value.IsEmpty()) |
134 return ScriptPromise(); | 134 return ScriptPromise(); |
135 if (value->IsPromise()) { | 135 if (value->IsPromise()) { |
136 return ScriptPromise(scriptState, value); | 136 return ScriptPromise(scriptState, value); |
137 } | 137 } |
138 InternalResolver resolver(scriptState); | 138 InternalResolver resolver(scriptState); |
139 ScriptPromise promise = resolver.promise(); | 139 ScriptPromise promise = resolver.promise(); |
140 resolver.resolve(value); | 140 resolver.resolve(value); |
141 return promise; | 141 return promise; |
142 } | 142 } |
143 | 143 |
144 ScriptPromise ScriptPromise::reject(ScriptState* scriptState, const ScriptValue&
value) | 144 ScriptPromise ScriptPromise::reject(ScriptState* scriptState, const ScriptValue&
value) |
145 { | 145 { |
146 return ScriptPromise::reject(scriptState, value.v8Value()); | 146 return ScriptPromise::reject(scriptState, value.v8Value()); |
147 } | 147 } |
148 | 148 |
149 ScriptPromise ScriptPromise::reject(ScriptState* scriptState, v8::Handle<v8::Val
ue> value) | 149 ScriptPromise ScriptPromise::reject(ScriptState* scriptState, v8::Local<v8::Valu
e> value) |
150 { | 150 { |
151 if (value.IsEmpty()) | 151 if (value.IsEmpty()) |
152 return ScriptPromise(); | 152 return ScriptPromise(); |
153 InternalResolver resolver(scriptState); | 153 InternalResolver resolver(scriptState); |
154 ScriptPromise promise = resolver.promise(); | 154 ScriptPromise promise = resolver.promise(); |
155 resolver.reject(value); | 155 resolver.reject(value); |
156 return promise; | 156 return promise; |
157 } | 157 } |
158 | 158 |
159 ScriptPromise ScriptPromise::rejectWithDOMException(ScriptState* scriptState, DO
MException* exception) | 159 ScriptPromise ScriptPromise::rejectWithDOMException(ScriptState* scriptState, DO
MException* exception) |
160 { | 160 { |
161 ASSERT(scriptState->isolate()->InContext()); | 161 ASSERT(scriptState->isolate()->InContext()); |
162 return reject(scriptState, toV8(exception, scriptState->context()->Global(),
scriptState->isolate())); | 162 return reject(scriptState, toV8(exception, scriptState->context()->Global(),
scriptState->isolate())); |
163 } | 163 } |
164 | 164 |
165 v8::Local<v8::Promise> ScriptPromise::rejectRaw(ScriptState* scriptState, v8::Ha
ndle<v8::Value> value) | 165 v8::Local<v8::Promise> ScriptPromise::rejectRaw(ScriptState* scriptState, v8::Lo
cal<v8::Value> value) |
166 { | 166 { |
167 if (value.IsEmpty()) | 167 if (value.IsEmpty()) |
168 return v8::Local<v8::Promise>(); | 168 return v8::Local<v8::Promise>(); |
169 v8::Local<v8::Promise::Resolver> resolver; | 169 v8::Local<v8::Promise::Resolver> resolver; |
170 if (!v8::Promise::Resolver::New(scriptState->context()).ToLocal(&resolver)) | 170 if (!v8::Promise::Resolver::New(scriptState->context()).ToLocal(&resolver)) |
171 return v8::Local<v8::Promise>(); | 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(scriptState->context(), 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 |