OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 } | 141 } |
142 } | 142 } |
143 | 143 |
144 | 144 |
145 // Simple chaining. | 145 // Simple chaining. |
146 | 146 |
147 function PromiseIdResolveHandler(x) { return x } | 147 function PromiseIdResolveHandler(x) { return x } |
148 function PromiseIdRejectHandler(r) { throw r } | 148 function PromiseIdRejectHandler(r) { throw r } |
149 | 149 |
150 function PromiseChain(onResolve, onReject) { // a.k.a. flatMap | 150 function PromiseChain(onResolve, onReject) { // a.k.a. flatMap |
151 onResolve = IS_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve; | 151 onResolve = |
152 onReject = IS_UNDEFINED(onReject) ? PromiseIdRejectHandler : onReject; | 152 typeof onResolve === 'function' ? onResolve : PromiseIdResolveHandler; |
rossberg
2014/04/28 08:32:52
We shouldn't change .chain, only .then
yhirano
2014/04/28 12:35:40
Done.
| |
153 onReject = typeof onReject === 'function' ? onReject : PromiseIdRejectHandler; | |
153 var deferred = %_CallFunction(this.constructor, PromiseDeferred); | 154 var deferred = %_CallFunction(this.constructor, PromiseDeferred); |
154 switch (GET_PRIVATE(this, promiseStatus)) { | 155 switch (GET_PRIVATE(this, promiseStatus)) { |
155 case UNDEFINED: | 156 case UNDEFINED: |
156 throw MakeTypeError('not_a_promise', [this]); | 157 throw MakeTypeError('not_a_promise', [this]); |
157 case 0: // Pending | 158 case 0: // Pending |
158 GET_PRIVATE(this, promiseOnResolve).push(onResolve, deferred); | 159 GET_PRIVATE(this, promiseOnResolve).push(onResolve, deferred); |
159 GET_PRIVATE(this, promiseOnReject).push(onReject, deferred); | 160 GET_PRIVATE(this, promiseOnReject).push(onReject, deferred); |
160 break; | 161 break; |
161 case +1: // Resolved | 162 case +1: // Resolved |
162 PromiseEnqueue(GET_PRIVATE(this, promiseValue), [onResolve, deferred]); | 163 PromiseEnqueue(GET_PRIVATE(this, promiseValue), [onResolve, deferred]); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 } catch(e) { | 195 } catch(e) { |
195 // TODO(rossberg): perhaps log uncaught exceptions below. | 196 // TODO(rossberg): perhaps log uncaught exceptions below. |
196 try { deferred.reject(e) } catch(e) {} | 197 try { deferred.reject(e) } catch(e) {} |
197 } | 198 } |
198 } | 199 } |
199 | 200 |
200 | 201 |
201 // Multi-unwrapped chaining with thenable coercion. | 202 // Multi-unwrapped chaining with thenable coercion. |
202 | 203 |
203 function PromiseThen(onResolve, onReject) { | 204 function PromiseThen(onResolve, onReject) { |
204 onResolve = IS_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve; | 205 onResolve = |
206 typeof onResolve === 'function' ? onResolve : PromiseIdResolveHandler; | |
rossberg
2014/04/28 08:32:52
Please use the IS_SPEC_FUNCTION macro instead.
yhirano
2014/04/28 12:35:40
Done.
| |
205 var that = this; | 207 var that = this; |
206 var constructor = this.constructor; | 208 var constructor = this.constructor; |
207 return this.chain( | 209 return this.chain( |
208 function(x) { | 210 function(x) { |
209 x = PromiseCoerce(constructor, x); | 211 x = PromiseCoerce(constructor, x); |
210 return x === that ? onReject(MakeTypeError('promise_cyclic', [x])) : | 212 return x === that ? onReject(MakeTypeError('promise_cyclic', [x])) : |
211 IsPromise(x) ? x.then(onResolve, onReject) : onResolve(x); | 213 IsPromise(x) ? x.then(onResolve, onReject) : onResolve(x); |
212 }, | 214 }, |
213 onReject | 215 onReject |
214 ); | 216 ); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
299 "cast", PromiseCast | 301 "cast", PromiseCast |
300 ]); | 302 ]); |
301 InstallFunctions($Promise.prototype, DONT_ENUM, [ | 303 InstallFunctions($Promise.prototype, DONT_ENUM, [ |
302 "chain", PromiseChain, | 304 "chain", PromiseChain, |
303 "then", PromiseThen, | 305 "then", PromiseThen, |
304 "catch", PromiseCatch | 306 "catch", PromiseCatch |
305 ]); | 307 ]); |
306 } | 308 } |
307 | 309 |
308 SetUpPromise(); | 310 SetUpPromise(); |
OLD | NEW |