OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 "use strict"; | 5 "use strict"; |
6 | 6 |
7 // This file relies on the fact that the following declaration has been made | 7 // This file relies on the fact that the following declaration has been made |
8 // in runtime.js: | 8 // in runtime.js: |
9 // var $Object = global.Object | 9 // var $Object = global.Object |
10 // var $WeakMap = global.WeakMap | 10 // var $WeakMap = global.WeakMap |
(...skipping 17 matching lines...) Expand all Loading... |
28 return IS_SPEC_OBJECT(x) && %HasLocalProperty(x, promiseStatus); | 28 return IS_SPEC_OBJECT(x) && %HasLocalProperty(x, promiseStatus); |
29 } | 29 } |
30 | 30 |
31 function Promise(resolver) { | 31 function Promise(resolver) { |
32 if (resolver === promiseRaw) return; | 32 if (resolver === promiseRaw) return; |
33 if (!%_IsConstructCall()) throw MakeTypeError('not_a_promise', [this]); | 33 if (!%_IsConstructCall()) throw MakeTypeError('not_a_promise', [this]); |
34 if (typeof resolver !== 'function') | 34 if (typeof resolver !== 'function') |
35 throw MakeTypeError('resolver_not_a_function', [resolver]); | 35 throw MakeTypeError('resolver_not_a_function', [resolver]); |
36 var promise = PromiseInit(this); | 36 var promise = PromiseInit(this); |
37 try { | 37 try { |
| 38 %DebugPromiseHandlePrologue(function() { return promise }); |
38 resolver(function(x) { PromiseResolve(promise, x) }, | 39 resolver(function(x) { PromiseResolve(promise, x) }, |
39 function(r) { PromiseReject(promise, r) }); | 40 function(r) { PromiseReject(promise, r) }); |
40 } catch (e) { | 41 } catch (e) { |
41 PromiseReject(promise, e); | 42 PromiseReject(promise, e); |
| 43 } finally { |
| 44 %DebugPromiseHandleEpilogue(); |
42 } | 45 } |
43 } | 46 } |
44 | 47 |
45 function PromiseSet(promise, status, value, onResolve, onReject) { | 48 function PromiseSet(promise, status, value, onResolve, onReject) { |
46 SET_PRIVATE(promise, promiseStatus, status); | 49 SET_PRIVATE(promise, promiseStatus, status); |
47 SET_PRIVATE(promise, promiseValue, value); | 50 SET_PRIVATE(promise, promiseValue, value); |
48 SET_PRIVATE(promise, promiseOnResolve, onResolve); | 51 SET_PRIVATE(promise, promiseOnResolve, onResolve); |
49 SET_PRIVATE(promise, promiseOnReject, onReject); | 52 SET_PRIVATE(promise, promiseOnReject, onReject); |
50 return promise; | 53 return promise; |
51 } | 54 } |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 for (var i = 0; i < tasks.length; i += 2) { | 157 for (var i = 0; i < tasks.length; i += 2) { |
155 PromiseHandle(value, tasks[i], tasks[i + 1]) | 158 PromiseHandle(value, tasks[i], tasks[i + 1]) |
156 } | 159 } |
157 }); | 160 }); |
158 | 161 |
159 %SetMicrotaskPending(true); | 162 %SetMicrotaskPending(true); |
160 } | 163 } |
161 | 164 |
162 function PromiseHandle(value, handler, deferred) { | 165 function PromiseHandle(value, handler, deferred) { |
163 try { | 166 try { |
| 167 %DebugPromiseHandlePrologue( |
| 168 function() { |
| 169 var queue = GET_PRIVATE(deferred.promise, promiseOnReject); |
| 170 return (queue && queue.length == 0) ? deferred.promise : UNDEFINED; |
| 171 }); |
164 var result = handler(value); | 172 var result = handler(value); |
165 if (result === deferred.promise) | 173 if (result === deferred.promise) |
166 throw MakeTypeError('promise_cyclic', [result]); | 174 throw MakeTypeError('promise_cyclic', [result]); |
167 else if (IsPromise(result)) | 175 else if (IsPromise(result)) |
168 %_CallFunction(result, deferred.resolve, deferred.reject, PromiseChain); | 176 %_CallFunction(result, deferred.resolve, deferred.reject, PromiseChain); |
169 else | 177 else |
170 deferred.resolve(result); | 178 deferred.resolve(result); |
171 } catch (exception) { | 179 } catch (exception) { |
172 var uncaught = false; | 180 try { |
173 var reject_queue = GET_PRIVATE(deferred.promise, promiseOnReject); | 181 %DebugPromiseHandlePrologue(function() { return deferred.promise }); |
174 if (reject_queue && reject_queue.length == 0) { | 182 deferred.reject(exception); |
175 // The deferred promise may get a reject handler attached later. | 183 } catch (e) { } finally { |
176 // For now, we consider the exception to be (for the moment) uncaught. | 184 %DebugPromiseHandleEpilogue(); |
177 uncaught = true; | |
178 } | 185 } |
179 try { | 186 } finally { |
180 deferred.reject(exception); | 187 %DebugPromiseHandleEpilogue(); |
181 } catch (e) { | |
182 // The reject handler can only throw for a custom deferred promise. | |
183 // We consider the original exception to be uncaught. | |
184 uncaught = true; | |
185 } | |
186 if (uncaught) %DebugPendingExceptionInPromise(exception, deferred.promise); | |
187 } | 188 } |
188 } | 189 } |
189 | 190 |
190 | 191 |
191 // Multi-unwrapped chaining with thenable coercion. | 192 // Multi-unwrapped chaining with thenable coercion. |
192 | 193 |
193 function PromiseThen(onResolve, onReject) { | 194 function PromiseThen(onResolve, onReject) { |
194 onResolve = | 195 onResolve = |
195 IS_NULL_OR_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve; | 196 IS_NULL_OR_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve; |
196 onReject = | 197 onReject = |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 ]); | 315 ]); |
315 } | 316 } |
316 | 317 |
317 SetUpPromise(); | 318 SetUpPromise(); |
318 | 319 |
319 // Functions to expose promise details to the debugger. | 320 // Functions to expose promise details to the debugger. |
320 function GetPromiseStatus(promise) { | 321 function GetPromiseStatus(promise) { |
321 return GET_PRIVATE(promise, promiseStatus); | 322 return GET_PRIVATE(promise, promiseStatus); |
322 } | 323 } |
323 | 324 |
324 function GetPromiseOnResolve(promise) { | |
325 return GET_PRIVATE(promise, promiseOnResolve); | |
326 } | |
327 | |
328 function GetPromiseOnReject(promise) { | |
329 return GET_PRIVATE(promise, promiseOnReject); | |
330 } | |
331 | |
332 function GetPromiseValue(promise) { | 325 function GetPromiseValue(promise) { |
333 return GET_PRIVATE(promise, promiseValue); | 326 return GET_PRIVATE(promise, promiseValue); |
334 } | 327 } |
OLD | NEW |