| 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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 PromiseEnqueue(GET_PRIVATE(this, promiseValue), [onResolve, deferred]); | 163 PromiseEnqueue(GET_PRIVATE(this, promiseValue), [onResolve, deferred]); |
| 164 break; | 164 break; |
| 165 case -1: // Rejected | 165 case -1: // Rejected |
| 166 PromiseEnqueue(GET_PRIVATE(this, promiseValue), [onReject, deferred]); | 166 PromiseEnqueue(GET_PRIVATE(this, promiseValue), [onReject, deferred]); |
| 167 break; | 167 break; |
| 168 } | 168 } |
| 169 return deferred.promise; | 169 return deferred.promise; |
| 170 } | 170 } |
| 171 | 171 |
| 172 function PromiseCatch(onReject) { | 172 function PromiseCatch(onReject) { |
| 173 return this.chain(UNDEFINED, onReject); | 173 return this.then(UNDEFINED, onReject); |
| 174 } | 174 } |
| 175 | 175 |
| 176 function PromiseEnqueue(value, tasks) { | 176 function PromiseEnqueue(value, tasks) { |
| 177 GetMicrotaskQueue().push(function() { | 177 GetMicrotaskQueue().push(function() { |
| 178 for (var i = 0; i < tasks.length; i += 2) { | 178 for (var i = 0; i < tasks.length; i += 2) { |
| 179 PromiseHandle(value, tasks[i], tasks[i + 1]) | 179 PromiseHandle(value, tasks[i], tasks[i + 1]) |
| 180 } | 180 } |
| 181 }); | 181 }); |
| 182 | 182 |
| 183 %SetMicrotaskPending(true); | 183 %SetMicrotaskPending(true); |
| 184 } | 184 } |
| 185 | 185 |
| 186 function PromiseHandle(value, handler, deferred) { | 186 function PromiseHandle(value, handler, deferred) { |
| 187 try { | 187 try { |
| 188 var result = handler(value); | 188 var result = handler(value); |
| 189 if (result === deferred.promise) | 189 if (result === deferred.promise) |
| 190 throw MakeTypeError('promise_cyclic', [result]); | 190 throw MakeTypeError('promise_cyclic', [result]); |
| 191 else if (IsPromise(result)) | 191 else if (IsPromise(result)) |
| 192 result.chain(deferred.resolve, deferred.reject); | 192 %_CallFunction(result, deferred.resolve, deferred.reject, PromiseChain); |
| 193 else | 193 else |
| 194 deferred.resolve(result); | 194 deferred.resolve(result); |
| 195 } catch(e) { | 195 } catch(e) { |
| 196 // TODO(rossberg): perhaps log uncaught exceptions below. | 196 // TODO(rossberg): perhaps log uncaught exceptions below. |
| 197 try { deferred.reject(e) } catch(e) {} | 197 try { deferred.reject(e) } catch(e) {} |
| 198 } | 198 } |
| 199 } | 199 } |
| 200 | 200 |
| 201 | 201 |
| 202 // Multi-unwrapped chaining with thenable coercion. | 202 // Multi-unwrapped chaining with thenable coercion. |
| 203 | 203 |
| 204 function PromiseThen(onResolve, onReject) { | 204 function PromiseThen(onResolve, onReject) { |
| 205 onResolve = | 205 onResolve = |
| 206 IS_NULL_OR_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve; | 206 IS_NULL_OR_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve; |
| 207 onReject = | 207 onReject = |
| 208 IS_NULL_OR_UNDEFINED(onReject) ? PromiseIdRejectHandler : onReject; | 208 IS_NULL_OR_UNDEFINED(onReject) ? PromiseIdRejectHandler : onReject; |
| 209 var that = this; | 209 var that = this; |
| 210 var constructor = this.constructor; | 210 var constructor = this.constructor; |
| 211 return this.chain( | 211 return %_CallFunction( |
| 212 this, |
| 212 function(x) { | 213 function(x) { |
| 213 x = PromiseCoerce(constructor, x); | 214 x = PromiseCoerce(constructor, x); |
| 214 return x === that ? onReject(MakeTypeError('promise_cyclic', [x])) : | 215 return x === that ? onReject(MakeTypeError('promise_cyclic', [x])) : |
| 215 IsPromise(x) ? x.then(onResolve, onReject) : onResolve(x); | 216 IsPromise(x) ? x.then(onResolve, onReject) : onResolve(x); |
| 216 }, | 217 }, |
| 217 onReject | 218 onReject, |
| 219 PromiseChain |
| 218 ); | 220 ); |
| 219 } | 221 } |
| 220 | 222 |
| 221 PromiseCoerce.table = new $WeakMap; | 223 PromiseCoerce.table = new $WeakMap; |
| 222 | 224 |
| 223 function PromiseCoerce(constructor, x) { | 225 function PromiseCoerce(constructor, x) { |
| 224 if (!IsPromise(x) && IS_SPEC_OBJECT(x)) { | 226 if (!IsPromise(x) && IS_SPEC_OBJECT(x)) { |
| 225 var then; | 227 var then; |
| 226 try { | 228 try { |
| 227 then = x.then; | 229 then = x.then; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 "resolve", PromiseCast | 319 "resolve", PromiseCast |
| 318 ]); | 320 ]); |
| 319 InstallFunctions($Promise.prototype, DONT_ENUM, [ | 321 InstallFunctions($Promise.prototype, DONT_ENUM, [ |
| 320 "chain", PromiseChain, | 322 "chain", PromiseChain, |
| 321 "then", PromiseThen, | 323 "then", PromiseThen, |
| 322 "catch", PromiseCatch | 324 "catch", PromiseCatch |
| 323 ]); | 325 ]); |
| 324 } | 326 } |
| 325 | 327 |
| 326 SetUpPromise(); | 328 SetUpPromise(); |
| OLD | NEW |