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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 | 59 |
60 var GlobalPromise = function Promise(resolver) { | 60 var GlobalPromise = function Promise(resolver) { |
61 if (resolver === promiseRawSymbol) { | 61 if (resolver === promiseRawSymbol) { |
62 return %NewObject(GlobalPromise, new.target); | 62 return %NewObject(GlobalPromise, new.target); |
63 } | 63 } |
64 if (IS_UNDEFINED(new.target)) throw MakeTypeError(kNotAPromise, this); | 64 if (IS_UNDEFINED(new.target)) throw MakeTypeError(kNotAPromise, this); |
65 if (!IS_CALLABLE(resolver)) | 65 if (!IS_CALLABLE(resolver)) |
66 throw MakeTypeError(kResolverNotAFunction, resolver); | 66 throw MakeTypeError(kResolverNotAFunction, resolver); |
67 | 67 |
68 var promise = PromiseInit(%NewObject(GlobalPromise, new.target)); | 68 var promise = PromiseInit(%NewObject(GlobalPromise, new.target)); |
| 69 var callbacks = CreateResolvingFunctions(promise); |
69 | 70 |
70 try { | 71 try { |
71 %DebugPushPromise(promise, Promise); | 72 %DebugPushPromise(promise, Promise); |
72 var callbacks = CreateResolvingFunctions(promise); | |
73 resolver(callbacks.resolve, callbacks.reject); | 73 resolver(callbacks.resolve, callbacks.reject); |
74 } catch (e) { | 74 } catch (e) { |
75 PromiseReject(promise, e); | 75 %_Call(callbacks.reject, UNDEFINED, e); |
76 } finally { | 76 } finally { |
77 %DebugPopPromise(); | 77 %DebugPopPromise(); |
78 } | 78 } |
79 | 79 |
80 return promise; | 80 return promise; |
81 } | 81 } |
82 | 82 |
83 // Core functionality. | 83 // Core functionality. |
84 | 84 |
85 function PromiseSet(promise, status, value, onResolve, onReject) { | 85 function PromiseSet(promise, status, value, onResolve, onReject) { |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 } catch (e) { | 174 } catch (e) { |
175 return PromiseReject(promise, e); | 175 return PromiseReject(promise, e); |
176 } | 176 } |
177 if (IS_CALLABLE(then)) { | 177 if (IS_CALLABLE(then)) { |
178 // PromiseResolveThenableJob | 178 // PromiseResolveThenableJob |
179 var id, name, instrumenting = DEBUG_IS_ACTIVE; | 179 var id, name, instrumenting = DEBUG_IS_ACTIVE; |
180 %EnqueueMicrotask(function() { | 180 %EnqueueMicrotask(function() { |
181 if (instrumenting) { | 181 if (instrumenting) { |
182 %DebugAsyncTaskEvent({ type: "willHandle", id: id, name: name }); | 182 %DebugAsyncTaskEvent({ type: "willHandle", id: id, name: name }); |
183 } | 183 } |
| 184 var callbacks = CreateResolvingFunctions(promise); |
184 try { | 185 try { |
185 var callbacks = CreateResolvingFunctions(promise); | |
186 %_Call(then, x, callbacks.resolve, callbacks.reject); | 186 %_Call(then, x, callbacks.resolve, callbacks.reject); |
187 } catch (e) { | 187 } catch (e) { |
188 PromiseReject(promise, e); | 188 %_Call(callbacks.reject, UNDEFINED, e); |
189 } | 189 } |
190 if (instrumenting) { | 190 if (instrumenting) { |
191 %DebugAsyncTaskEvent({ type: "didHandle", id: id, name: name }); | 191 %DebugAsyncTaskEvent({ type: "didHandle", id: id, name: name }); |
192 } | 192 } |
193 }); | 193 }); |
194 if (instrumenting) { | 194 if (instrumenting) { |
195 id = ++lastMicrotaskId; | 195 id = ++lastMicrotaskId; |
196 name = "PromseResolveThenableJob"; | 196 name = "PromseResolveThenableJob"; |
197 %DebugAsyncTaskEvent({ type: "enqueue", id: id, name: name }); | 197 %DebugAsyncTaskEvent({ type: "enqueue", id: id, name: name }); |
198 } | 198 } |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 throw MakeTypeError(kCalledOnNonObject, PromiseCast); | 331 throw MakeTypeError(kCalledOnNonObject, PromiseCast); |
332 } | 332 } |
333 if (IsPromise(x) && x.constructor === this) return x; | 333 if (IsPromise(x) && x.constructor === this) return x; |
334 | 334 |
335 var promiseCapability = NewPromiseCapability(this); | 335 var promiseCapability = NewPromiseCapability(this); |
336 var resolveResult = %_Call(promiseCapability.resolve, UNDEFINED, x); | 336 var resolveResult = %_Call(promiseCapability.resolve, UNDEFINED, x); |
337 return promiseCapability.promise; | 337 return promiseCapability.promise; |
338 } | 338 } |
339 | 339 |
340 function PromiseAll(iterable) { | 340 function PromiseAll(iterable) { |
| 341 if (!IS_RECEIVER(this)) { |
| 342 throw MakeTypeError(kCalledOnNonObject, "Promise.all"); |
| 343 } |
| 344 |
341 var deferred = NewPromiseCapability(this); | 345 var deferred = NewPromiseCapability(this); |
342 var resolutions = []; | 346 var resolutions = new InternalArray(); |
| 347 var count; |
| 348 |
| 349 function CreateResolveElementFunction(index, values, promiseCapability) { |
| 350 var alreadyCalled = false; |
| 351 return function(x) { |
| 352 if (alreadyCalled === true) return; |
| 353 alreadyCalled = true; |
| 354 values[index] = x; |
| 355 if (--count === 0) { |
| 356 var valuesArray = []; |
| 357 %MoveArrayContents(values, valuesArray); |
| 358 %_Call(promiseCapability.resolve, UNDEFINED, valuesArray); |
| 359 } |
| 360 }; |
| 361 } |
| 362 |
343 try { | 363 try { |
344 var count = 0; | |
345 var i = 0; | 364 var i = 0; |
| 365 count = 1; |
346 for (var value of iterable) { | 366 for (var value of iterable) { |
347 var reject = function(r) { deferred.reject(r) }; | 367 var nextPromise = this.resolve(value); |
348 this.resolve(value).then( | 368 ++count; |
349 // Nested scope to get closure over current i. | 369 nextPromise.then( |
350 // TODO(arv): Use an inner let binding once available. | 370 CreateResolveElementFunction(i, resolutions, deferred), |
351 (function(i) { | 371 deferred.reject); |
352 return function(x) { | 372 SET_PRIVATE(deferred.reject, promiseCombinedDeferredSymbol, deferred); |
353 resolutions[i] = x; | |
354 if (--count === 0) deferred.resolve(resolutions); | |
355 } | |
356 })(i), reject); | |
357 SET_PRIVATE(reject, promiseCombinedDeferredSymbol, deferred); | |
358 ++i; | 373 ++i; |
359 ++count; | |
360 } | 374 } |
361 | 375 |
362 if (count === 0) { | 376 // 6.d |
363 deferred.resolve(resolutions); | 377 if (--count === 0) { |
| 378 var valuesArray = []; |
| 379 %MoveArrayContents(resolutions, valuesArray); |
| 380 %_Call(deferred.resolve, UNDEFINED, valuesArray); |
364 } | 381 } |
365 | 382 |
366 } catch (e) { | 383 } catch (e) { |
367 deferred.reject(e) | 384 %_Call(deferred.reject, UNDEFINED, e); |
368 } | 385 } |
369 return deferred.promise; | 386 return deferred.promise; |
370 } | 387 } |
371 | 388 |
372 function PromiseRace(iterable) { | 389 function PromiseRace(iterable) { |
373 if (!IS_RECEIVER(this)) { | 390 if (!IS_RECEIVER(this)) { |
374 throw MakeTypeError(kCalledOnNonObject, PromiseRace); | 391 throw MakeTypeError(kCalledOnNonObject, PromiseRace); |
375 } | 392 } |
376 | 393 |
377 var deferred = NewPromiseCapability(this); | 394 var deferred = NewPromiseCapability(this); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 [PromiseChain, PromiseDeferred, PromiseResolved].forEach( | 475 [PromiseChain, PromiseDeferred, PromiseResolved].forEach( |
459 fn => %FunctionRemovePrototype(fn)); | 476 fn => %FunctionRemovePrototype(fn)); |
460 | 477 |
461 utils.Export(function(to) { | 478 utils.Export(function(to) { |
462 to.PromiseChain = PromiseChain; | 479 to.PromiseChain = PromiseChain; |
463 to.PromiseDeferred = PromiseDeferred; | 480 to.PromiseDeferred = PromiseDeferred; |
464 to.PromiseResolved = PromiseResolved; | 481 to.PromiseResolved = PromiseResolved; |
465 }); | 482 }); |
466 | 483 |
467 }) | 484 }) |
OLD | NEW |