OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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 (function(global, utils, extrasUtils) { | 5 (function(global, utils, extrasUtils) { |
6 | 6 |
7 "use strict"; | 7 "use strict"; |
8 | 8 |
9 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
10 | 10 |
11 // ------------------------------------------------------------------- | 11 // ------------------------------------------------------------------- |
12 // Imports | 12 // Imports |
13 | 13 |
14 var AsyncFunctionNext; | 14 var AsyncFunctionNext; |
15 var AsyncFunctionThrow; | 15 var AsyncFunctionThrow; |
16 var PromiseReject; | 16 var IsPromise; |
17 var PromiseResolve; | 17 var GlobalPromise; |
18 var PromiseThen; | 18 var NewPromiseCapability; |
| 19 var PerformPromiseThen; |
19 | 20 |
20 utils.Import(function(from) { | 21 utils.Import(function(from) { |
21 AsyncFunctionNext = from.AsyncFunctionNext; | 22 AsyncFunctionNext = from.AsyncFunctionNext; |
22 AsyncFunctionThrow = from.AsyncFunctionThrow; | 23 AsyncFunctionThrow = from.AsyncFunctionThrow; |
23 PromiseReject = from.PromiseCreateRejected; | 24 IsPromise = from.IsPromise; |
24 PromiseResolve = from.PromiseCreateResolved; | 25 GlobalPromise = from.GlobalPromise; |
25 PromiseThen = from.PromiseThen; | 26 NewPromiseCapability = from.NewPromiseCapability; |
| 27 PerformPromiseThen = from.PerformPromiseThen; |
26 }); | 28 }); |
27 | 29 |
28 // ------------------------------------------------------------------- | 30 // ------------------------------------------------------------------- |
29 | 31 |
30 function AsyncFunctionAwait(generator, value) { | 32 function AsyncFunctionAwait(generator, value) { |
31 return %_Call( | 33 // Promise.resolve(value).then( |
32 PromiseThen, PromiseResolve(value), | 34 // value => AsyncFunctionNext(value), |
33 function(sentValue) { | 35 // error => AsyncFunctionThrow(error) |
34 return %_Call(AsyncFunctionNext, generator, sentValue); | 36 // ); |
35 }, | 37 var promise; |
36 function(sentError) { | 38 if (IsPromise(value)) { |
37 return %_Call(AsyncFunctionThrow, generator, sentError); | 39 promise = value; |
38 }); | 40 } else { |
| 41 var promiseCapability = NewPromiseCapability(GlobalPromise); |
| 42 %_Call(promiseCapability.resolve, UNDEFINED, value); |
| 43 promise = promiseCapability.promise; |
| 44 } |
| 45 |
| 46 var onFulfilled = |
| 47 (sentValue) => %_Call(AsyncFunctionNext, generator, sentValue); |
| 48 var onRejected = |
| 49 (sentError) => %_Call(AsyncFunctionThrow, generator, sentError); |
| 50 |
| 51 var throwawayCapability = NewPromiseCapability(GlobalPromise); |
| 52 return PerformPromiseThen(promise, onFulfilled, onRejected, |
| 53 throwawayCapability); |
39 } | 54 } |
40 | 55 |
41 %InstallToContext([ "async_function_await", AsyncFunctionAwait ]); | 56 %InstallToContext([ "async_function_await", AsyncFunctionAwait ]); |
42 | 57 |
43 }) | 58 }) |
OLD | NEW |