Chromium Code Reviews| 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 22 matching lines...) Expand all Loading... | |
| 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) { | 42 if (DEBUG_IS_ACTIVE) { |
| 43 %DebugPromiseEvent({ type : "new Promise", | 43 %DebugPromiseEvent({ type : "new", |
| 44 promise: this, | 44 promise: this, |
| 45 resolver: resolver }); | 45 resolver: resolver }); |
| 46 } | 46 } |
| 47 try { | 47 try { |
| 48 %DebugPromiseHandlePrologue(function() { return promise }); | 48 %DebugPromiseHandlePrologue(function() { return promise }); |
| 49 resolver(function(x) { PromiseResolve(promise, x) }, | 49 resolver(function(x) { PromiseResolve(promise, x) }, |
| 50 function(r) { PromiseReject(promise, r) }); | 50 function(r) { PromiseReject(promise, r) }); |
| 51 } catch (e) { | 51 } catch (e) { |
| 52 PromiseReject(promise, e); | 52 PromiseReject(promise, e); |
| 53 } finally { | 53 } finally { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 67 | 67 |
| 68 function PromiseInit(promise) { | 68 function PromiseInit(promise) { |
| 69 return PromiseSet( | 69 return PromiseSet( |
| 70 promise, 0, UNDEFINED, new InternalArray, new InternalArray) | 70 promise, 0, UNDEFINED, new InternalArray, new InternalArray) |
| 71 } | 71 } |
| 72 | 72 |
| 73 function PromiseDone(promise, status, value, promiseQueue) { | 73 function PromiseDone(promise, status, value, promiseQueue) { |
| 74 if (GET_PRIVATE(promise, promiseStatus) === 0) { | 74 if (GET_PRIVATE(promise, promiseStatus) === 0) { |
| 75 PromiseEnqueue(value, GET_PRIVATE(promise, promiseQueue), status); | 75 PromiseEnqueue(value, GET_PRIVATE(promise, promiseQueue), status); |
| 76 PromiseSet(promise, status, value); | 76 PromiseSet(promise, status, value); |
| 77 if (DEBUG_IS_ACTIVE) { | |
| 78 %DebugPromiseEvent({ type: "update", | |
| 79 promise: promise, | |
| 80 status: status, | |
| 81 value: value }); | |
| 82 } | |
| 77 } | 83 } |
| 78 } | 84 } |
| 79 | 85 |
| 80 function PromiseCoerce(constructor, x) { | 86 function PromiseCoerce(constructor, x) { |
| 81 if (!IsPromise(x) && IS_SPEC_OBJECT(x)) { | 87 if (!IsPromise(x) && IS_SPEC_OBJECT(x)) { |
| 82 var then; | 88 var then; |
| 83 try { | 89 try { |
| 84 then = x.then; | 90 then = x.then; |
| 85 } catch(r) { | 91 } catch(r) { |
| 86 return %_CallFunction(constructor, r, PromiseRejected); | 92 return %_CallFunction(constructor, r, PromiseRejected); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 185 var result = {}; | 191 var result = {}; |
| 186 result.promise = new this(function(resolve, reject) { | 192 result.promise = new this(function(resolve, reject) { |
| 187 result.resolve = resolve; | 193 result.resolve = resolve; |
| 188 result.reject = reject; | 194 result.reject = reject; |
| 189 }) | 195 }) |
| 190 return result; | 196 return result; |
| 191 } | 197 } |
| 192 } | 198 } |
| 193 | 199 |
| 194 function PromiseResolved(x) { | 200 function PromiseResolved(x) { |
| 201 var promise; | |
| 195 if (this === $Promise) { | 202 if (this === $Promise) { |
| 196 // Optimized case, avoid extra closure. | 203 // Optimized case, avoid extra closure. |
| 197 return PromiseSet(new $Promise(promiseRaw), +1, x); | 204 promise = PromiseSet(new $Promise(promiseRaw), +1, x); |
| 198 } else { | 205 } else { |
| 199 return new this(function(resolve, reject) { resolve(x) }); | 206 promise = new this(function(resolve, reject) { resolve(x) }); |
| 200 } | 207 } |
| 208 if (DEBUG_IS_ACTIVE) { | |
| 209 %DebugPromiseEvent({ type: "update", | |
| 210 promise: promise, | |
| 211 status: 1, | |
| 212 value: x }); | |
| 213 } | |
| 214 return promise; | |
| 201 } | 215 } |
| 202 | 216 |
| 203 function PromiseRejected(r) { | 217 function PromiseRejected(r) { |
| 218 var promise; | |
| 204 if (this === $Promise) { | 219 if (this === $Promise) { |
| 205 // Optimized case, avoid extra closure. | 220 // Optimized case, avoid extra closure. |
| 206 return PromiseSet(new $Promise(promiseRaw), -1, r); | 221 promise = PromiseSet(new $Promise(promiseRaw), -1, r); |
| 207 } else { | 222 } else { |
| 208 return new this(function(resolve, reject) { reject(r) }); | 223 promise = new this(function(resolve, reject) { reject(r) }); |
| 209 } | 224 } |
| 225 if (DEBUG_IS_ACTIVE) { | |
| 226 %DebugPromiseEvent({ type: "update", | |
|
aandrey
2014/07/22 11:58:49
now 3 update event calls instead of one doesn't lo
Alexandra Mikhaylova
2014/07/22 12:47:22
Done.
| |
| 227 promise: promise, | |
| 228 status: -1, | |
| 229 value: r }); | |
| 230 } | |
| 231 return promise; | |
| 210 } | 232 } |
| 211 | 233 |
| 212 // Simple chaining. | 234 // Simple chaining. |
| 213 | 235 |
| 214 PromiseChain = function PromiseChain(onResolve, onReject) { // a.k.a. | 236 PromiseChain = function PromiseChain(onResolve, onReject) { // a.k.a. |
| 215 // flatMap | 237 // flatMap |
| 216 onResolve = IS_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve; | 238 onResolve = IS_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve; |
| 217 onReject = IS_UNDEFINED(onReject) ? PromiseIdRejectHandler : onReject; | 239 onReject = IS_UNDEFINED(onReject) ? PromiseIdRejectHandler : onReject; |
| 218 var deferred = %_CallFunction(this.constructor, PromiseDeferred); | 240 var deferred = %_CallFunction(this.constructor, PromiseDeferred); |
| 219 switch (GET_PRIVATE(this, promiseStatus)) { | 241 switch (GET_PRIVATE(this, promiseStatus)) { |
| 220 case UNDEFINED: | 242 case UNDEFINED: |
| 221 throw MakeTypeError('not_a_promise', [this]); | 243 throw MakeTypeError('not_a_promise', [this]); |
| 222 case 0: // Pending | 244 case 0: // Pending |
| 223 GET_PRIVATE(this, promiseOnResolve).push(onResolve, deferred); | 245 GET_PRIVATE(this, promiseOnResolve).push(onResolve, deferred); |
| 224 GET_PRIVATE(this, promiseOnReject).push(onReject, deferred); | 246 GET_PRIVATE(this, promiseOnReject).push(onReject, deferred); |
| 225 break; | 247 break; |
| 226 case +1: // Resolved | 248 case +1: // Resolved |
| 227 PromiseEnqueue(GET_PRIVATE(this, promiseValue), | 249 PromiseEnqueue(GET_PRIVATE(this, promiseValue), |
| 228 [onResolve, deferred], | 250 [onResolve, deferred], |
| 229 +1); | 251 +1); |
| 230 break; | 252 break; |
| 231 case -1: // Rejected | 253 case -1: // Rejected |
| 232 PromiseEnqueue(GET_PRIVATE(this, promiseValue), | 254 PromiseEnqueue(GET_PRIVATE(this, promiseValue), |
| 233 [onReject, deferred], | 255 [onReject, deferred], |
| 234 -1); | 256 -1); |
| 235 break; | 257 break; |
| 236 } | 258 } |
| 259 if (DEBUG_IS_ACTIVE) { | |
| 260 %DebugPromiseEvent({ type: "chain", | |
| 261 promise: deferred.promise, | |
| 262 parentPromise: this }); | |
| 263 } | |
| 237 return deferred.promise; | 264 return deferred.promise; |
| 238 } | 265 } |
| 239 | 266 |
| 240 PromiseCatch = function PromiseCatch(onReject) { | 267 PromiseCatch = function PromiseCatch(onReject) { |
| 241 return this.then(UNDEFINED, onReject); | 268 return this.then(UNDEFINED, onReject); |
| 242 } | 269 } |
| 243 | 270 |
| 244 // Multi-unwrapped chaining with thenable coercion. | 271 // Multi-unwrapped chaining with thenable coercion. |
| 245 | 272 |
| 246 PromiseThen = function PromiseThen(onResolve, onReject) { | 273 PromiseThen = function PromiseThen(onResolve, onReject) { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 334 "race", PromiseOne, | 361 "race", PromiseOne, |
| 335 "resolve", PromiseCast | 362 "resolve", PromiseCast |
| 336 ]); | 363 ]); |
| 337 InstallFunctions($Promise.prototype, DONT_ENUM, [ | 364 InstallFunctions($Promise.prototype, DONT_ENUM, [ |
| 338 "chain", PromiseChain, | 365 "chain", PromiseChain, |
| 339 "then", PromiseThen, | 366 "then", PromiseThen, |
| 340 "catch", PromiseCatch | 367 "catch", PromiseCatch |
| 341 ]); | 368 ]); |
| 342 | 369 |
| 343 })(); | 370 })(); |
| OLD | NEW |