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 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 break; | 166 break; |
167 } | 167 } |
168 return deferred.promise; | 168 return deferred.promise; |
169 } | 169 } |
170 | 170 |
171 function PromiseCatch(onReject) { | 171 function PromiseCatch(onReject) { |
172 return this.chain(UNDEFINED, onReject); | 172 return this.chain(UNDEFINED, onReject); |
173 } | 173 } |
174 | 174 |
175 function PromiseEnqueue(value, tasks) { | 175 function PromiseEnqueue(value, tasks) { |
176 RunMicrotasks.queue.push(function() { | 176 promiseEvents.push(value, tasks); |
177 for (var i = 0; i < tasks.length; i += 2) { | |
178 PromiseHandle(value, tasks[i], tasks[i + 1]) | |
179 } | |
180 }); | |
181 | |
182 %SetMicrotaskPending(true); | 177 %SetMicrotaskPending(true); |
183 } | 178 } |
184 | 179 |
185 function PromiseHandle(value, handler, deferred) { | 180 function PromiseMicrotaskRunner() { |
186 try { | 181 var events = promiseEvents; |
187 var result = handler(value); | 182 if (events.length > 0) { |
188 if (result === deferred.promise) | 183 promiseEvents = new InternalArray; |
189 throw MakeTypeError('promise_cyclic', [result]); | 184 for (var i = 0; i < events.length; i += 2) { |
190 else if (IsPromise(result)) | 185 var value = events[i]; |
191 result.chain(deferred.resolve, deferred.reject); | 186 var tasks = events[i + 1]; |
192 else | 187 for (var j = 0; j < tasks.length; j += 2) { |
193 deferred.resolve(result); | 188 var handler = tasks[j]; |
194 } catch(e) { | 189 var deferred = tasks[j + 1]; |
195 // TODO(rossberg): perhaps log uncaught exceptions below. | 190 try { |
196 try { deferred.reject(e) } catch(e) {} | 191 var result = handler(value); |
| 192 if (result === deferred.promise) |
| 193 throw MakeTypeError('promise_cyclic', [result]); |
| 194 else if (IsPromise(result)) |
| 195 result.chain(deferred.resolve, deferred.reject); |
| 196 else |
| 197 deferred.resolve(result); |
| 198 } catch(e) { |
| 199 // TODO(rossberg): perhaps log uncaught exceptions below. |
| 200 try { deferred.reject(e) } catch(e) {} |
| 201 } |
| 202 } |
| 203 } |
197 } | 204 } |
198 } | 205 } |
| 206 RunMicrotasks.runners.push(PromiseMicrotaskRunner); |
199 | 207 |
200 | 208 |
201 // Multi-unwrapped chaining with thenable coercion. | 209 // Multi-unwrapped chaining with thenable coercion. |
202 | 210 |
203 function PromiseThen(onResolve, onReject) { | 211 function PromiseThen(onResolve, onReject) { |
204 onResolve = IS_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve; | 212 onResolve = IS_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve; |
205 var that = this; | 213 var that = this; |
206 var constructor = this.constructor; | 214 var constructor = this.constructor; |
207 return this.chain( | 215 return this.chain( |
208 function(x) { | 216 function(x) { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 "cast", PromiseCast | 307 "cast", PromiseCast |
300 ]); | 308 ]); |
301 InstallFunctions($Promise.prototype, DONT_ENUM, [ | 309 InstallFunctions($Promise.prototype, DONT_ENUM, [ |
302 "chain", PromiseChain, | 310 "chain", PromiseChain, |
303 "then", PromiseThen, | 311 "then", PromiseThen, |
304 "catch", PromiseCatch | 312 "catch", PromiseCatch |
305 ]); | 313 ]); |
306 } | 314 } |
307 | 315 |
308 SetUpPromise(); | 316 SetUpPromise(); |
OLD | NEW |