| 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, status: status, value: value }); |
| 67 promise: promise, | |
| 68 status: status, | |
| 69 value: value }); | |
| 70 } | 62 } |
| 71 return promise; | 63 return promise; |
| 72 } | 64 } |
| 73 | 65 |
| 74 function PromiseInit(promise) { | 66 function PromiseInit(promise) { |
| 75 return PromiseSet( | 67 return PromiseSet( |
| 76 promise, 0, UNDEFINED, new InternalArray, new InternalArray) | 68 promise, 0, UNDEFINED, new InternalArray, new InternalArray) |
| 77 } | 69 } |
| 78 | 70 |
| 79 function PromiseDone(promise, status, value, promiseQueue) { | 71 function PromiseDone(promise, status, value, promiseQueue) { |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 [onResolve, deferred], | 226 [onResolve, deferred], |
| 235 +1); | 227 +1); |
| 236 break; | 228 break; |
| 237 case -1: // Rejected | 229 case -1: // Rejected |
| 238 PromiseEnqueue(GET_PRIVATE(this, promiseValue), | 230 PromiseEnqueue(GET_PRIVATE(this, promiseValue), |
| 239 [onReject, deferred], | 231 [onReject, deferred], |
| 240 -1); | 232 -1); |
| 241 break; | 233 break; |
| 242 } | 234 } |
| 243 if (DEBUG_IS_ACTIVE) { | 235 if (DEBUG_IS_ACTIVE) { |
| 244 %DebugPromiseEvent({ type: "chain", | 236 %DebugPromiseEvent({ promise: deferred.promise, parentPromise: this }); |
| 245 promise: deferred.promise, | |
| 246 parentPromise: this }); | |
| 247 } | 237 } |
| 248 return deferred.promise; | 238 return deferred.promise; |
| 249 } | 239 } |
| 250 | 240 |
| 251 PromiseCatch = function PromiseCatch(onReject) { | 241 PromiseCatch = function PromiseCatch(onReject) { |
| 252 return this.then(UNDEFINED, onReject); | 242 return this.then(UNDEFINED, onReject); |
| 253 } | 243 } |
| 254 | 244 |
| 255 // Multi-unwrapped chaining with thenable coercion. | 245 // Multi-unwrapped chaining with thenable coercion. |
| 256 | 246 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 "race", PromiseOne, | 335 "race", PromiseOne, |
| 346 "resolve", PromiseCast | 336 "resolve", PromiseCast |
| 347 ]); | 337 ]); |
| 348 InstallFunctions($Promise.prototype, DONT_ENUM, [ | 338 InstallFunctions($Promise.prototype, DONT_ENUM, [ |
| 349 "chain", PromiseChain, | 339 "chain", PromiseChain, |
| 350 "then", PromiseThen, | 340 "then", PromiseThen, |
| 351 "catch", PromiseCatch | 341 "catch", PromiseCatch |
| 352 ]); | 342 ]); |
| 353 | 343 |
| 354 })(); | 344 })(); |
| OLD | NEW |