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 (function(global, utils, extrasUtils) { | 5 (function(global, utils, extrasUtils) { |
6 | 6 |
7 "use strict"; | 7 "use strict"; |
8 | 8 |
9 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
10 | 10 |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
220 function NewPromiseCapability(C) { | 220 function NewPromiseCapability(C) { |
221 if (C === GlobalPromise) { | 221 if (C === GlobalPromise) { |
222 // Optimized case, avoid extra closure. | 222 // Optimized case, avoid extra closure. |
223 var promise = PromiseInit(new GlobalPromise(promiseRawSymbol)); | 223 var promise = PromiseInit(new GlobalPromise(promiseRawSymbol)); |
224 var callbacks = CreateResolvingFunctions(promise); | 224 var callbacks = CreateResolvingFunctions(promise); |
225 return { | 225 return { |
226 promise: promise, | 226 promise: promise, |
227 resolve: callbacks.resolve, | 227 resolve: callbacks.resolve, |
228 reject: callbacks.reject | 228 reject: callbacks.reject |
229 }; | 229 }; |
230 } else { | |
231 var result = {promise: UNDEFINED, resolve: UNDEFINED, reject: UNDEFINED }; | |
232 result.promise = new C(function(resolve, reject) { | |
233 // TODO(littledan): Check for resolve and reject being not undefined | |
234 result.resolve = resolve; | |
235 result.reject = reject; | |
236 }); | |
237 return result; | |
238 } | 230 } |
231 | |
232 var result = {promise: UNDEFINED, resolve: UNDEFINED, reject: UNDEFINED }; | |
233 result.promise = new C(function(resolve, reject) { | |
234 if (!IS_UNDEFINED(result.resolve) || !IS_UNDEFINED(result.reject)) | |
235 throw MakeTypeError(kPromiseExecutorAlreadyInvoked); | |
236 result.resolve = resolve; | |
237 result.reject = reject; | |
238 }); | |
239 | |
240 if (!IS_CALLABLE(result.resolve)) | |
241 throw MakeTypeError(kCalledNonCallable, "promiseCapability.[[Resolve]]"); | |
242 if (!IS_CALLABLE(result.reject)) | |
243 throw MakeTypeError(kCalledNonCallable, "promiseCapability.[[Reject]]"); | |
244 | |
245 return result; | |
239 } | 246 } |
240 | 247 |
241 function PromiseDeferred() { | 248 function PromiseDeferred() { |
242 return NewPromiseCapability(this); | 249 return NewPromiseCapability(this); |
243 } | 250 } |
244 | 251 |
245 function PromiseResolved(x) { | 252 function PromiseResolved(x) { |
246 return %_Call(PromiseCast, this, x); | 253 return %_Call(PromiseCast, this, x); |
247 } | 254 } |
248 | 255 |
249 function PromiseRejected(r) { | 256 function PromiseRejected(r) { |
250 if (!IS_RECEIVER(this)) { | 257 if (!IS_RECEIVER(this)) { |
251 throw MakeTypeError(kCalledOnNonObject, PromiseRejected); | 258 throw MakeTypeError(kCalledOnNonObject, PromiseRejected); |
252 } | 259 } |
253 var promise; | |
254 if (this === GlobalPromise) { | 260 if (this === GlobalPromise) { |
255 // Optimized case, avoid extra closure. | 261 // Optimized case, avoid extra closure. |
256 promise = PromiseCreateAndSet(-1, r); | 262 var promise = PromiseCreateAndSet(-1, r); |
257 // The debug event for this would always be an uncaught promise reject, | 263 // The debug event for this would always be an uncaught promise reject, |
258 // which is usually simply noise. Do not trigger that debug event. | 264 // which is usually simply noise. Do not trigger that debug event. |
259 %PromiseRejectEvent(promise, r, false); | 265 %PromiseRejectEvent(promise, r, false); |
266 return promise; | |
260 } else { | 267 } else { |
261 promise = new this(function(resolve, reject) { reject(r) }); | 268 var promiseCapability = NewPromiseCapability(this); |
269 %_Call(promiseCapability.reject, UNDEFINED, r); | |
adamk
2016/01/05 20:33:09
Any reason to use %_Call here instead of just maki
caitp (gmail)
2016/01/05 20:39:54
The `promiseCapability` object is emulating a Reco
adamk
2016/01/05 20:44:41
You could do:
var reject = promiseCapability.reje
| |
270 return promiseCapability.promise; | |
262 } | 271 } |
263 return promise; | |
264 } | 272 } |
265 | 273 |
266 // Multi-unwrapped chaining with thenable coercion. | 274 // Multi-unwrapped chaining with thenable coercion. |
267 | 275 |
268 function PromiseThen(onResolve, onReject) { | 276 function PromiseThen(onResolve, onReject) { |
269 var constructor = this.constructor; | 277 var constructor = this.constructor; |
270 onResolve = IS_CALLABLE(onResolve) ? onResolve : PromiseIdResolveHandler; | 278 onResolve = IS_CALLABLE(onResolve) ? onResolve : PromiseIdResolveHandler; |
271 onReject = IS_CALLABLE(onReject) ? onReject : PromiseIdRejectHandler; | 279 onReject = IS_CALLABLE(onReject) ? onReject : PromiseIdRejectHandler; |
272 var deferred = NewPromiseCapability(constructor); | 280 var deferred = NewPromiseCapability(constructor); |
273 switch (GET_PRIVATE(this, promiseStatusSymbol)) { | 281 switch (GET_PRIVATE(this, promiseStatusSymbol)) { |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
352 deferred.resolve(resolutions); | 360 deferred.resolve(resolutions); |
353 } | 361 } |
354 | 362 |
355 } catch (e) { | 363 } catch (e) { |
356 deferred.reject(e) | 364 deferred.reject(e) |
357 } | 365 } |
358 return deferred.promise; | 366 return deferred.promise; |
359 } | 367 } |
360 | 368 |
361 function PromiseRace(iterable) { | 369 function PromiseRace(iterable) { |
370 if (!IS_RECEIVER(this)) { | |
371 throw MakeTypeError(kCalledOnNonObject, PromiseRace); | |
372 } | |
373 | |
362 var deferred = NewPromiseCapability(this); | 374 var deferred = NewPromiseCapability(this); |
363 try { | 375 try { |
364 for (var value of iterable) { | 376 for (var value of iterable) { |
365 var reject = function(r) { deferred.reject(r) }; | 377 var reject = function(r) { deferred.reject(r) }; |
366 this.resolve(value).then(function(x) { deferred.resolve(x) }, reject); | 378 this.resolve(value).then(function(x) { deferred.resolve(x) }, reject); |
367 SET_PRIVATE(reject, promiseCombinedDeferredSymbol, deferred); | 379 SET_PRIVATE(reject, promiseCombinedDeferredSymbol, deferred); |
368 } | 380 } |
369 } catch (e) { | 381 } catch (e) { |
370 deferred.reject(e) | 382 deferred.reject(e) |
371 } | 383 } |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
443 [PromiseChain, PromiseDeferred, PromiseResolved].forEach( | 455 [PromiseChain, PromiseDeferred, PromiseResolved].forEach( |
444 fn => %FunctionRemovePrototype(fn)); | 456 fn => %FunctionRemovePrototype(fn)); |
445 | 457 |
446 utils.Export(function(to) { | 458 utils.Export(function(to) { |
447 to.PromiseChain = PromiseChain; | 459 to.PromiseChain = PromiseChain; |
448 to.PromiseDeferred = PromiseDeferred; | 460 to.PromiseDeferred = PromiseDeferred; |
449 to.PromiseResolved = PromiseResolved; | 461 to.PromiseResolved = PromiseResolved; |
450 }); | 462 }); |
451 | 463 |
452 }) | 464 }) |
OLD | NEW |