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 21 matching lines...) Expand all Loading... | |
32 var lastMicrotaskId = 0; | 32 var lastMicrotaskId = 0; |
33 | 33 |
34 (function() { | 34 (function() { |
35 | 35 |
36 var $Promise = function Promise(resolver) { | 36 var $Promise = function Promise(resolver) { |
37 if (resolver === promiseRaw) return; | 37 if (resolver === promiseRaw) return; |
38 if (!%_IsConstructCall()) throw MakeTypeError('not_a_promise', [this]); | 38 if (!%_IsConstructCall()) throw MakeTypeError('not_a_promise', [this]); |
39 if (!IS_SPEC_FUNCTION(resolver)) | 39 if (!IS_SPEC_FUNCTION(resolver)) |
40 throw MakeTypeError('resolver_not_a_function', [resolver]); | 40 throw MakeTypeError('resolver_not_a_function', [resolver]); |
41 var promise = PromiseInit(this); | 41 var promise = PromiseInit(this); |
42 if (DEBUG_IS_ACTIVE) { | |
43 %DebugPromiseEvent({ type : "new", | |
44 promise: this, | |
45 resolver: resolver }); | |
46 } | |
47 try { | 42 try { |
48 %DebugPromiseHandlePrologue(function() { return promise }); | 43 %DebugPromiseHandlePrologue(function() { return promise }); |
49 resolver(function(x) { PromiseResolve(promise, x) }, | 44 resolver(function(x) { PromiseResolve(promise, x) }, |
50 function(r) { PromiseReject(promise, r) }); | 45 function(r) { PromiseReject(promise, r) }); |
51 } catch (e) { | 46 } catch (e) { |
52 PromiseReject(promise, e); | 47 PromiseReject(promise, e); |
53 } finally { | 48 } finally { |
54 %DebugPromiseHandleEpilogue(); | 49 %DebugPromiseHandleEpilogue(); |
55 } | 50 } |
56 } | 51 } |
57 | 52 |
58 // Core functionality. | 53 // Core functionality. |
59 | 54 |
60 function PromiseSet(promise, status, value, onResolve, onReject) { | 55 function PromiseSet(promise, status, value, onResolve, onReject) { |
61 SET_PRIVATE(promise, promiseStatus, status); | 56 SET_PRIVATE(promise, promiseStatus, status); |
62 SET_PRIVATE(promise, promiseValue, value); | 57 SET_PRIVATE(promise, promiseValue, value); |
63 SET_PRIVATE(promise, promiseOnResolve, onResolve); | 58 SET_PRIVATE(promise, promiseOnResolve, onResolve); |
64 SET_PRIVATE(promise, promiseOnReject, onReject); | 59 SET_PRIVATE(promise, promiseOnReject, onReject); |
65 if (DEBUG_IS_ACTIVE && status !== 0) { | 60 if (DEBUG_IS_ACTIVE) { |
66 %DebugPromiseEvent({ type: "update", | 61 %DebugPromiseEvent({ promise: promise, |
aandrey
2014/08/01 13:22:21
nit: can it be a one liner now?
Alexandra Mikhaylova
2014/08/01 13:44:53
Thanks! Fixed it.
| |
67 promise: promise, | |
68 status: status, | 62 status: status, |
69 value: value }); | 63 value: value }); |
70 } | 64 } |
71 return promise; | 65 return promise; |
72 } | 66 } |
73 | 67 |
74 function PromiseInit(promise) { | 68 function PromiseInit(promise) { |
75 return PromiseSet( | 69 return PromiseSet( |
76 promise, 0, UNDEFINED, new InternalArray, new InternalArray) | 70 promise, 0, UNDEFINED, new InternalArray, new InternalArray) |
77 } | 71 } |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
234 [onResolve, deferred], | 228 [onResolve, deferred], |
235 +1); | 229 +1); |
236 break; | 230 break; |
237 case -1: // Rejected | 231 case -1: // Rejected |
238 PromiseEnqueue(GET_PRIVATE(this, promiseValue), | 232 PromiseEnqueue(GET_PRIVATE(this, promiseValue), |
239 [onReject, deferred], | 233 [onReject, deferred], |
240 -1); | 234 -1); |
241 break; | 235 break; |
242 } | 236 } |
243 if (DEBUG_IS_ACTIVE) { | 237 if (DEBUG_IS_ACTIVE) { |
244 %DebugPromiseEvent({ type: "chain", | 238 %DebugPromiseEvent({ promise: deferred.promise, |
aandrey
2014/08/01 13:22:21
ditto
Alexandra Mikhaylova
2014/08/01 13:44:52
Done.
| |
245 promise: deferred.promise, | |
246 parentPromise: this }); | 239 parentPromise: this }); |
247 } | 240 } |
248 return deferred.promise; | 241 return deferred.promise; |
249 } | 242 } |
250 | 243 |
251 PromiseCatch = function PromiseCatch(onReject) { | 244 PromiseCatch = function PromiseCatch(onReject) { |
252 return this.then(UNDEFINED, onReject); | 245 return this.then(UNDEFINED, onReject); |
253 } | 246 } |
254 | 247 |
255 // Multi-unwrapped chaining with thenable coercion. | 248 // Multi-unwrapped chaining with thenable coercion. |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
345 "race", PromiseOne, | 338 "race", PromiseOne, |
346 "resolve", PromiseCast | 339 "resolve", PromiseCast |
347 ]); | 340 ]); |
348 InstallFunctions($Promise.prototype, DONT_ENUM, [ | 341 InstallFunctions($Promise.prototype, DONT_ENUM, [ |
349 "chain", PromiseChain, | 342 "chain", PromiseChain, |
350 "then", PromiseThen, | 343 "then", PromiseThen, |
351 "catch", PromiseCatch | 344 "catch", PromiseCatch |
352 ]); | 345 ]); |
353 | 346 |
354 })(); | 347 })(); |
OLD | NEW |