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 |
11 | 11 |
12 // For bootstrapper. | 12 // For bootstrapper. |
13 | 13 |
14 var IsPromise; | 14 var IsPromise; |
15 var PromiseCreate; | 15 var PromiseCreate; |
16 var PromiseResolve; | 16 var PromiseResolve; |
17 var PromiseReject; | 17 var PromiseReject; |
18 var PromiseChain; | 18 var PromiseChain; |
19 var PromiseCatch; | 19 var PromiseCatch; |
20 var PromiseThen; | 20 var PromiseThen; |
21 var PromiseHasRejectHandler; | 21 var PromiseHasRejectHandler; |
| 22 var PromiseHasUserDefinedRejectHandler; |
22 | 23 |
23 // mirror-debugger.js currently uses builtins.promiseStatus. It would be nice | 24 // mirror-debugger.js currently uses builtins.promiseStatus. It would be nice |
24 // if we could move these property names into the closure below. | 25 // if we could move these property names into the closure below. |
25 // TODO(jkummerow/rossberg/yangguo): Find a better solution. | 26 // TODO(jkummerow/rossberg/yangguo): Find a better solution. |
26 | 27 |
27 // Status values: 0 = pending, +1 = resolved, -1 = rejected | 28 // Status values: 0 = pending, +1 = resolved, -1 = rejected |
28 var promiseStatus = GLOBAL_PRIVATE("Promise#status"); | 29 var promiseStatus = GLOBAL_PRIVATE("Promise#status"); |
29 var promiseValue = GLOBAL_PRIVATE("Promise#value"); | 30 var promiseValue = GLOBAL_PRIVATE("Promise#value"); |
30 var promiseOnResolve = GLOBAL_PRIVATE("Promise#onResolve"); | 31 var promiseOnResolve = GLOBAL_PRIVATE("Promise#onResolve"); |
31 var promiseOnReject = GLOBAL_PRIVATE("Promise#onReject"); | 32 var promiseOnReject = GLOBAL_PRIVATE("Promise#onReject"); |
32 var promiseRaw = GLOBAL_PRIVATE("Promise#raw"); | 33 var promiseRaw = GLOBAL_PRIVATE("Promise#raw"); |
33 var promiseDebug = GLOBAL_PRIVATE("Promise#debug"); | 34 var promiseHasHandler = %PromiseHasHandlerSymbol(); |
34 var lastMicrotaskId = 0; | 35 var lastMicrotaskId = 0; |
35 | 36 |
| 37 |
36 (function() { | 38 (function() { |
37 | 39 |
38 var $Promise = function Promise(resolver) { | 40 var $Promise = function Promise(resolver) { |
39 if (resolver === promiseRaw) return; | 41 if (resolver === promiseRaw) return; |
40 if (!%_IsConstructCall()) throw MakeTypeError('not_a_promise', [this]); | 42 if (!%_IsConstructCall()) throw MakeTypeError('not_a_promise', [this]); |
41 if (!IS_SPEC_FUNCTION(resolver)) | 43 if (!IS_SPEC_FUNCTION(resolver)) |
42 throw MakeTypeError('resolver_not_a_function', [resolver]); | 44 throw MakeTypeError('resolver_not_a_function', [resolver]); |
43 var promise = PromiseInit(this); | 45 var promise = PromiseInit(this); |
44 try { | 46 try { |
45 %DebugPushPromise(promise); | 47 %DebugPushPromise(promise); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 PromiseCreate = function PromiseCreate() { | 154 PromiseCreate = function PromiseCreate() { |
153 return new $Promise(PromiseNopResolver) | 155 return new $Promise(PromiseNopResolver) |
154 } | 156 } |
155 | 157 |
156 PromiseResolve = function PromiseResolve(promise, x) { | 158 PromiseResolve = function PromiseResolve(promise, x) { |
157 PromiseDone(promise, +1, x, promiseOnResolve) | 159 PromiseDone(promise, +1, x, promiseOnResolve) |
158 } | 160 } |
159 | 161 |
160 PromiseReject = function PromiseReject(promise, r) { | 162 PromiseReject = function PromiseReject(promise, r) { |
161 // Check promise status to confirm that this reject has an effect. | 163 // Check promise status to confirm that this reject has an effect. |
162 // Check promiseDebug property to avoid duplicate event. | 164 // Call runtime for callbacks to the debugger or for unhandled reject. |
163 if (DEBUG_IS_ACTIVE && | 165 if (GET_PRIVATE(promise, promiseStatus) == 0) { |
164 GET_PRIVATE(promise, promiseStatus) == 0 && | 166 var debug_is_active = DEBUG_IS_ACTIVE; |
165 !HAS_DEFINED_PRIVATE(promise, promiseDebug)) { | 167 if (debug_is_active || !HAS_DEFINED_PRIVATE(promise, promiseHasHandler)) { |
166 %DebugPromiseRejectEvent(promise, r); | 168 %PromiseRejectEvent(promise, r, debug_is_active); |
| 169 } |
167 } | 170 } |
168 PromiseDone(promise, -1, r, promiseOnReject) | 171 PromiseDone(promise, -1, r, promiseOnReject) |
169 } | 172 } |
170 | 173 |
171 // Convenience. | 174 // Convenience. |
172 | 175 |
173 function PromiseDeferred() { | 176 function PromiseDeferred() { |
174 if (this === $Promise) { | 177 if (this === $Promise) { |
175 // Optimized case, avoid extra closure. | 178 // Optimized case, avoid extra closure. |
176 var promise = PromiseInit(new $Promise(promiseRaw)); | 179 var promise = PromiseInit(new $Promise(promiseRaw)); |
(...skipping 15 matching lines...) Expand all Loading... |
192 function PromiseResolved(x) { | 195 function PromiseResolved(x) { |
193 if (this === $Promise) { | 196 if (this === $Promise) { |
194 // Optimized case, avoid extra closure. | 197 // Optimized case, avoid extra closure. |
195 return PromiseSet(new $Promise(promiseRaw), +1, x); | 198 return PromiseSet(new $Promise(promiseRaw), +1, x); |
196 } else { | 199 } else { |
197 return new this(function(resolve, reject) { resolve(x) }); | 200 return new this(function(resolve, reject) { resolve(x) }); |
198 } | 201 } |
199 } | 202 } |
200 | 203 |
201 function PromiseRejected(r) { | 204 function PromiseRejected(r) { |
| 205 var promise; |
202 if (this === $Promise) { | 206 if (this === $Promise) { |
203 // Optimized case, avoid extra closure. | 207 // Optimized case, avoid extra closure. |
204 return PromiseSet(new $Promise(promiseRaw), -1, r); | 208 promise = PromiseSet(new $Promise(promiseRaw), -1, r); |
| 209 // The debug event for this would always be an uncaught promise reject, |
| 210 // which is usually simply noise. Do not trigger that debug event. |
| 211 %PromiseRejectEvent(promise, r, false); |
205 } else { | 212 } else { |
206 return new this(function(resolve, reject) { reject(r) }); | 213 promise = new this(function(resolve, reject) { reject(r) }); |
207 } | 214 } |
| 215 return promise; |
208 } | 216 } |
209 | 217 |
210 // Simple chaining. | 218 // Simple chaining. |
211 | 219 |
212 PromiseChain = function PromiseChain(onResolve, onReject) { // a.k.a. | 220 PromiseChain = function PromiseChain(onResolve, onReject) { // a.k.a. |
213 // flatMap | 221 // flatMap |
214 onResolve = IS_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve; | 222 onResolve = IS_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve; |
215 onReject = IS_UNDEFINED(onReject) ? PromiseIdRejectHandler : onReject; | 223 onReject = IS_UNDEFINED(onReject) ? PromiseIdRejectHandler : onReject; |
216 var deferred = %_CallFunction(this.constructor, PromiseDeferred); | 224 var deferred = %_CallFunction(this.constructor, PromiseDeferred); |
217 switch (GET_PRIVATE(this, promiseStatus)) { | 225 switch (GET_PRIVATE(this, promiseStatus)) { |
218 case UNDEFINED: | 226 case UNDEFINED: |
219 throw MakeTypeError('not_a_promise', [this]); | 227 throw MakeTypeError('not_a_promise', [this]); |
220 case 0: // Pending | 228 case 0: // Pending |
221 GET_PRIVATE(this, promiseOnResolve).push(onResolve, deferred); | 229 GET_PRIVATE(this, promiseOnResolve).push(onResolve, deferred); |
222 GET_PRIVATE(this, promiseOnReject).push(onReject, deferred); | 230 GET_PRIVATE(this, promiseOnReject).push(onReject, deferred); |
223 break; | 231 break; |
224 case +1: // Resolved | 232 case +1: // Resolved |
225 PromiseEnqueue(GET_PRIVATE(this, promiseValue), | 233 PromiseEnqueue(GET_PRIVATE(this, promiseValue), |
226 [onResolve, deferred], | 234 [onResolve, deferred], |
227 +1); | 235 +1); |
228 break; | 236 break; |
229 case -1: // Rejected | 237 case -1: // Rejected |
| 238 if (!HAS_DEFINED_PRIVATE(this, promiseHasHandler)) { |
| 239 // Promise has already been rejected, but had no handler. |
| 240 // Revoke previously triggered reject event. |
| 241 %PromiseRevokeReject(this); |
| 242 } |
230 PromiseEnqueue(GET_PRIVATE(this, promiseValue), | 243 PromiseEnqueue(GET_PRIVATE(this, promiseValue), |
231 [onReject, deferred], | 244 [onReject, deferred], |
232 -1); | 245 -1); |
233 break; | 246 break; |
234 } | 247 } |
| 248 // Mark this promise as having handler. |
| 249 SET_PRIVATE(this, promiseHasHandler, true); |
235 if (DEBUG_IS_ACTIVE) { | 250 if (DEBUG_IS_ACTIVE) { |
236 %DebugPromiseEvent({ promise: deferred.promise, parentPromise: this }); | 251 %DebugPromiseEvent({ promise: deferred.promise, parentPromise: this }); |
237 } | 252 } |
238 return deferred.promise; | 253 return deferred.promise; |
239 } | 254 } |
240 | 255 |
241 PromiseCatch = function PromiseCatch(onReject) { | 256 PromiseCatch = function PromiseCatch(onReject) { |
242 return this.then(UNDEFINED, onReject); | 257 return this.then(UNDEFINED, onReject); |
243 } | 258 } |
244 | 259 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 } | 333 } |
319 } catch (e) { | 334 } catch (e) { |
320 deferred.reject(e) | 335 deferred.reject(e) |
321 } | 336 } |
322 return deferred.promise; | 337 return deferred.promise; |
323 } | 338 } |
324 | 339 |
325 | 340 |
326 // Utility for debugger | 341 // Utility for debugger |
327 | 342 |
328 function PromiseHasRejectHandlerRecursive(promise) { | 343 function PromiseHasUserDefinedRejectHandlerRecursive(promise) { |
329 var queue = GET_PRIVATE(promise, promiseOnReject); | 344 var queue = GET_PRIVATE(promise, promiseOnReject); |
330 if (IS_UNDEFINED(queue)) return false; | 345 if (IS_UNDEFINED(queue)) return false; |
331 // Do a depth first search for a reject handler that's not | |
332 // the default PromiseIdRejectHandler. | |
333 for (var i = 0; i < queue.length; i += 2) { | 346 for (var i = 0; i < queue.length; i += 2) { |
334 if (queue[i] != PromiseIdRejectHandler) return true; | 347 if (queue[i] != PromiseIdRejectHandler) return true; |
335 if (PromiseHasRejectHandlerRecursive(queue[i + 1].promise)) return true; | 348 if (PromiseHasUserDefinedRejectHandlerRecursive(queue[i + 1].promise)) { |
| 349 return true; |
| 350 } |
336 } | 351 } |
337 return false; | 352 return false; |
338 } | 353 } |
339 | 354 |
340 PromiseHasRejectHandler = function PromiseHasRejectHandler() { | 355 // Return whether the promise will be handled by a user-defined reject |
341 // Mark promise as already having triggered a reject event. | 356 // handler somewhere down the promise chain. For this, we do a depth-first |
342 SET_PRIVATE(this, promiseDebug, true); | 357 // search for a reject handler that's not the default PromiseIdRejectHandler. |
343 return PromiseHasRejectHandlerRecursive(this); | 358 PromiseHasUserDefinedRejectHandler = |
| 359 function PromiseHasUserDefinedRejectHandler() { |
| 360 return PromiseHasUserDefinedRejectHandlerRecursive(this); |
344 }; | 361 }; |
345 | 362 |
346 // ------------------------------------------------------------------- | 363 // ------------------------------------------------------------------- |
347 // Install exported functions. | 364 // Install exported functions. |
348 | 365 |
349 %CheckIsBootstrapping(); | 366 %CheckIsBootstrapping(); |
350 %AddNamedProperty(global, 'Promise', $Promise, DONT_ENUM); | 367 %AddNamedProperty(global, 'Promise', $Promise, DONT_ENUM); |
351 InstallFunctions($Promise, DONT_ENUM, [ | 368 InstallFunctions($Promise, DONT_ENUM, [ |
352 "defer", PromiseDeferred, | 369 "defer", PromiseDeferred, |
353 "accept", PromiseResolved, | 370 "accept", PromiseResolved, |
354 "reject", PromiseRejected, | 371 "reject", PromiseRejected, |
355 "all", PromiseAll, | 372 "all", PromiseAll, |
356 "race", PromiseOne, | 373 "race", PromiseOne, |
357 "resolve", PromiseCast | 374 "resolve", PromiseCast |
358 ]); | 375 ]); |
359 InstallFunctions($Promise.prototype, DONT_ENUM, [ | 376 InstallFunctions($Promise.prototype, DONT_ENUM, [ |
360 "chain", PromiseChain, | 377 "chain", PromiseChain, |
361 "then", PromiseThen, | 378 "then", PromiseThen, |
362 "catch", PromiseCatch | 379 "catch", PromiseCatch |
363 ]); | 380 ]); |
364 | 381 |
365 })(); | 382 })(); |
OLD | NEW |