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) { | 5 (function(global, utils) { |
6 | 6 |
7 "use strict"; | 7 "use strict"; |
8 | 8 |
9 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 var promiseValueSymbol = utils.GetPrivateSymbol("promise_value_symbol"); | 22 var promiseValueSymbol = utils.GetPrivateSymbol("promise_value_symbol"); |
23 | 23 |
24 // ------------------------------------------------------------------- | 24 // ------------------------------------------------------------------- |
25 | 25 |
26 // Status values: 0 = pending, +1 = resolved, -1 = rejected | 26 // Status values: 0 = pending, +1 = resolved, -1 = rejected |
27 var lastMicrotaskId = 0; | 27 var lastMicrotaskId = 0; |
28 | 28 |
29 var GlobalPromise = function Promise(resolver) { | 29 var GlobalPromise = function Promise(resolver) { |
30 if (resolver === promiseRawSymbol) return; | 30 if (resolver === promiseRawSymbol) return; |
31 if (!%_IsConstructCall()) throw MakeTypeError(kNotAPromise, this); | 31 if (!%_IsConstructCall()) throw MakeTypeError(kNotAPromise, this); |
32 if (!IS_SPEC_FUNCTION(resolver)) | 32 if (!IS_CALLABLE(resolver)) |
33 throw MakeTypeError(kResolverNotAFunction, resolver); | 33 throw MakeTypeError(kResolverNotAFunction, resolver); |
34 var promise = PromiseInit(this); | 34 var promise = PromiseInit(this); |
35 try { | 35 try { |
36 %DebugPushPromise(promise, Promise); | 36 %DebugPushPromise(promise, Promise); |
37 resolver(function(x) { PromiseResolve(promise, x) }, | 37 resolver(function(x) { PromiseResolve(promise, x) }, |
38 function(r) { PromiseReject(promise, r) }); | 38 function(r) { PromiseReject(promise, r) }); |
39 } catch (e) { | 39 } catch (e) { |
40 PromiseReject(promise, e); | 40 PromiseReject(promise, e); |
41 } finally { | 41 } finally { |
42 %DebugPopPromise(); | 42 %DebugPopPromise(); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 } | 77 } |
78 | 78 |
79 function PromiseCoerce(constructor, x) { | 79 function PromiseCoerce(constructor, x) { |
80 if (!IsPromise(x) && IS_SPEC_OBJECT(x)) { | 80 if (!IsPromise(x) && IS_SPEC_OBJECT(x)) { |
81 var then; | 81 var then; |
82 try { | 82 try { |
83 then = x.then; | 83 then = x.then; |
84 } catch(r) { | 84 } catch(r) { |
85 return %_CallFunction(constructor, r, PromiseRejected); | 85 return %_CallFunction(constructor, r, PromiseRejected); |
86 } | 86 } |
87 if (IS_SPEC_FUNCTION(then)) { | 87 if (IS_CALLABLE(then)) { |
88 var deferred = %_CallFunction(constructor, PromiseDeferred); | 88 var deferred = %_CallFunction(constructor, PromiseDeferred); |
89 try { | 89 try { |
90 %_CallFunction(x, deferred.resolve, deferred.reject, then); | 90 %_CallFunction(x, deferred.resolve, deferred.reject, then); |
91 } catch(r) { | 91 } catch(r) { |
92 deferred.reject(r); | 92 deferred.reject(r); |
93 } | 93 } |
94 return deferred.promise; | 94 return deferred.promise; |
95 } | 95 } |
96 } | 96 } |
97 return x; | 97 return x; |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 return deferred.promise; | 251 return deferred.promise; |
252 } | 252 } |
253 | 253 |
254 function PromiseCatch(onReject) { | 254 function PromiseCatch(onReject) { |
255 return this.then(UNDEFINED, onReject); | 255 return this.then(UNDEFINED, onReject); |
256 } | 256 } |
257 | 257 |
258 // Multi-unwrapped chaining with thenable coercion. | 258 // Multi-unwrapped chaining with thenable coercion. |
259 | 259 |
260 function PromiseThen(onResolve, onReject) { | 260 function PromiseThen(onResolve, onReject) { |
261 onResolve = IS_SPEC_FUNCTION(onResolve) ? onResolve | 261 onResolve = IS_CALLABLE(onResolve) ? onResolve : PromiseIdResolveHandler; |
262 : PromiseIdResolveHandler; | 262 onReject = IS_CALLABLE(onReject) ? onReject : PromiseIdRejectHandler; |
263 onReject = IS_SPEC_FUNCTION(onReject) ? onReject | |
264 : PromiseIdRejectHandler; | |
265 var that = this; | 263 var that = this; |
266 var constructor = this.constructor; | 264 var constructor = this.constructor; |
267 return %_CallFunction( | 265 return %_CallFunction( |
268 this, | 266 this, |
269 function(x) { | 267 function(x) { |
270 x = PromiseCoerce(constructor, x); | 268 x = PromiseCoerce(constructor, x); |
271 if (x === that) { | 269 if (x === that) { |
272 DEBUG_PREPARE_STEP_IN_IF_STEPPING(onReject); | 270 DEBUG_PREPARE_STEP_IN_IF_STEPPING(onReject); |
273 return onReject(MakeTypeError(kPromiseCyclic, x)); | 271 return onReject(MakeTypeError(kPromiseCyclic, x)); |
274 } else if (IsPromise(x)) { | 272 } else if (IsPromise(x)) { |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 "promise_catch", PromiseCatch, | 381 "promise_catch", PromiseCatch, |
384 "promise_chain", PromiseChain, | 382 "promise_chain", PromiseChain, |
385 "promise_create", PromiseCreate, | 383 "promise_create", PromiseCreate, |
386 "promise_has_user_defined_reject_handler", PromiseHasUserDefinedRejectHandler, | 384 "promise_has_user_defined_reject_handler", PromiseHasUserDefinedRejectHandler, |
387 "promise_reject", PromiseReject, | 385 "promise_reject", PromiseReject, |
388 "promise_resolve", PromiseResolve, | 386 "promise_resolve", PromiseResolve, |
389 "promise_then", PromiseThen, | 387 "promise_then", PromiseThen, |
390 ]); | 388 ]); |
391 | 389 |
392 }) | 390 }) |
OLD | NEW |