OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 "use strict"; | 5 "use strict"; |
6 | 6 |
7 // This file relies on the fact that the following declaration has been made | 7 // This file relies on the fact that the following declaration has been made |
8 // in runtime.js: | 8 // in runtime.js: |
9 // var $Object = global.Object | 9 // var $Object = global.Object |
10 // var $WeakMap = global.WeakMap | 10 // var $WeakMap = global.WeakMap |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 function(x) { | 197 function(x) { |
198 x = PromiseCoerce(constructor, x); | 198 x = PromiseCoerce(constructor, x); |
199 return x === that ? onReject(MakeTypeError('promise_cyclic', [x])) : | 199 return x === that ? onReject(MakeTypeError('promise_cyclic', [x])) : |
200 IsPromise(x) ? x.then(onResolve, onReject) : onResolve(x); | 200 IsPromise(x) ? x.then(onResolve, onReject) : onResolve(x); |
201 }, | 201 }, |
202 onReject, | 202 onReject, |
203 PromiseChain | 203 PromiseChain |
204 ); | 204 ); |
205 } | 205 } |
206 | 206 |
207 PromiseCoerce.table = new $WeakMap; | |
208 | |
209 function PromiseCoerce(constructor, x) { | 207 function PromiseCoerce(constructor, x) { |
210 if (!IsPromise(x) && IS_SPEC_OBJECT(x)) { | 208 if (!IsPromise(x) && IS_SPEC_OBJECT(x)) { |
211 var then; | 209 var then; |
212 try { | 210 try { |
213 then = x.then; | 211 then = x.then; |
214 } catch(r) { | 212 } catch(r) { |
215 var promise = %_CallFunction(constructor, r, PromiseRejected); | 213 return %_CallFunction(constructor, r, PromiseRejected); |
216 PromiseCoerce.table.set(x, promise); | |
217 return promise; | |
218 } | 214 } |
219 if (typeof then === 'function') { | 215 if (IS_SPEC_FUNCTION(then)) { |
220 if (PromiseCoerce.table.has(x)) { | 216 var deferred = %_CallFunction(constructor, PromiseDeferred); |
221 return PromiseCoerce.table.get(x); | 217 try { |
222 } else { | 218 %_CallFunction(x, deferred.resolve, deferred.reject, then); |
223 var deferred = %_CallFunction(constructor, PromiseDeferred); | 219 } catch(r) { |
224 PromiseCoerce.table.set(x, deferred.promise); | 220 deferred.reject(r); |
225 try { | |
226 %_CallFunction(x, deferred.resolve, deferred.reject, then); | |
227 } catch(r) { | |
228 deferred.reject(r); | |
229 } | |
230 return deferred.promise; | |
231 } | 221 } |
| 222 return deferred.promise; |
232 } | 223 } |
233 } | 224 } |
234 return x; | 225 return x; |
235 } | 226 } |
236 | 227 |
237 | 228 |
238 // Combinators. | 229 // Combinators. |
239 | 230 |
240 function PromiseCast(x) { | 231 function PromiseCast(x) { |
241 // TODO(rossberg): cannot do better until we support @@create. | 232 // TODO(rossberg): cannot do better until we support @@create. |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 SetUpPromise(); | 303 SetUpPromise(); |
313 | 304 |
314 // Functions to expose promise details to the debugger. | 305 // Functions to expose promise details to the debugger. |
315 function GetPromiseStatus(promise) { | 306 function GetPromiseStatus(promise) { |
316 return GET_PRIVATE(promise, promiseStatus); | 307 return GET_PRIVATE(promise, promiseStatus); |
317 } | 308 } |
318 | 309 |
319 function GetPromiseValue(promise) { | 310 function GetPromiseValue(promise) { |
320 return GET_PRIVATE(promise, promiseValue); | 311 return GET_PRIVATE(promise, promiseValue); |
321 } | 312 } |
OLD | NEW |