Chromium Code Reviews| 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 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 function PromiseEnqueue(value, tasks) { | 175 function PromiseEnqueue(value, tasks) { |
| 176 GetMicrotaskQueue().push(function() { | 176 GetMicrotaskQueue().push(function() { |
| 177 for (var i = 0; i < tasks.length; i += 2) { | 177 for (var i = 0; i < tasks.length; i += 2) { |
| 178 PromiseHandle(value, tasks[i], tasks[i + 1]) | 178 PromiseHandle(value, tasks[i], tasks[i + 1]) |
| 179 } | 179 } |
| 180 }); | 180 }); |
| 181 | 181 |
| 182 %SetMicrotaskPending(true); | 182 %SetMicrotaskPending(true); |
| 183 } | 183 } |
| 184 | 184 |
| 185 | |
|
rossberg
2014/04/24 14:33:47
nit: spurious new line
Yang
2014/04/24 14:44:53
Done.
| |
| 185 function PromiseHandle(value, handler, deferred) { | 186 function PromiseHandle(value, handler, deferred) { |
| 186 try { | 187 try { |
| 187 var result = handler(value); | 188 var result = handler(value); |
| 188 if (result === deferred.promise) | 189 if (result === deferred.promise) |
| 189 throw MakeTypeError('promise_cyclic', [result]); | 190 throw MakeTypeError('promise_cyclic', [result]); |
| 190 else if (IsPromise(result)) | 191 else if (IsPromise(result)) |
| 191 %_CallFunction(result, deferred.resolve, deferred.reject, PromiseChain); | 192 %_CallFunction(result, deferred.resolve, deferred.reject, PromiseChain); |
| 192 else | 193 else |
| 193 deferred.resolve(result); | 194 deferred.resolve(result); |
| 194 } catch(e) { | 195 } catch (exception) { |
| 195 // TODO(rossberg): perhaps log uncaught exceptions below. | 196 var uncaught = false; |
| 196 try { deferred.reject(e) } catch(e) {} | 197 if (!IS_SPEC_FUNCTION(deferred.reject)) { |
|
rossberg
2014/04/24 14:33:47
Why do you have to test this case? It will throw i
Yang
2014/04/24 14:44:53
Yeah, it will throw, with a TypeError 'undefined i
rossberg
2014/04/24 14:56:51
That may be so, but that's equally true for other
| |
| 198 // There is no reject handler defined in the custom deferred promise. | |
| 199 // Therefore the exception cannot be caught. | |
| 200 uncaught = true; | |
| 201 } else { | |
| 202 var reject_queue = GET_PRIVATE(deferred.promise, promiseOnReject); | |
| 203 if (reject_queue && reject_queue.length == 0) { | |
| 204 // The deferred promise may get a reject handler attached later. | |
| 205 // For now, we consider the exception to be (for the moment) uncaught. | |
| 206 uncaught = true; | |
| 207 } | |
| 208 try { | |
| 209 deferred.reject(exception); | |
| 210 } catch (inner_exception) { | |
| 211 // The reject handler can only throw for a custom deferred promise. | |
| 212 // The resulting exception is considered to be uncaught. | |
| 213 uncaught = true; | |
| 214 exception = inner_exception; | |
| 215 } | |
| 216 } | |
| 217 if (uncaught) %DebugPendingExceptionInPromise(exception, deferred.promise); | |
| 197 } | 218 } |
| 198 } | 219 } |
| 199 | 220 |
| 200 | 221 |
| 201 // Multi-unwrapped chaining with thenable coercion. | 222 // Multi-unwrapped chaining with thenable coercion. |
| 202 | 223 |
| 203 function PromiseThen(onResolve, onReject) { | 224 function PromiseThen(onResolve, onReject) { |
| 204 onResolve = | 225 onResolve = |
| 205 IS_NULL_OR_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve; | 226 IS_NULL_OR_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve; |
| 206 onReject = | 227 onReject = |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 "resolve", PromiseCast | 339 "resolve", PromiseCast |
| 319 ]); | 340 ]); |
| 320 InstallFunctions($Promise.prototype, DONT_ENUM, [ | 341 InstallFunctions($Promise.prototype, DONT_ENUM, [ |
| 321 "chain", PromiseChain, | 342 "chain", PromiseChain, |
| 322 "then", PromiseThen, | 343 "then", PromiseThen, |
| 323 "catch", PromiseCatch | 344 "catch", PromiseCatch |
| 324 ]); | 345 ]); |
| 325 } | 346 } |
| 326 | 347 |
| 327 SetUpPromise(); | 348 SetUpPromise(); |
| OLD | NEW |