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